Example_Eet_File(3)

NAME
Example_Eet_File - Example of the various ways to interface with an Eet File

1 //Compile with:
2 // gcc -o eet-file eet-file.c `pkg-config --cflags --libs eet`
3
4 #include <Eet.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 static int
9 create_eet_file(void)
10 {
11    Eet_File *ef;
12    char buf[1024], *ptr;
13    int size, len, i;
14    const char *some_strings[] = {
15       "And some more strings",
16       "spread across several",
17       "elements of an array!"
18    };
19    const char some_data[] =
20      "
21      "0
22      "f3
23      "13
24      "50e1
25      "121c
26      "09
27      "c488
28      "35dd"
29      "55490
30      "40"
31      "4154"
32      "
33      "9
34
35    ef = eet_open("/tmp/my_file.eet", EET_FILE_MODE_WRITE);
36    if (!ef) return 0;
37
38    strcpy(buf, "Here is a string of data to save!");
39    size = eet_write(ef, "/key/to_store/at", buf, sizeof(buf), 1);
40    if (!size)
41      {
42         fprintf(stderr, "Error writing data!);
43         eet_close(ef);
44         return 0;
45      }
46    len = strlen(buf);
47    printf("strlen() = %d, eet_write() = %d, len, size);
48
49    ptr = buf;
50    for (i = 0; i < 3; i++)
51      {
52         len = strlen(some_strings[i]) + 1;
53         memcpy(ptr, some_strings[i], len);
54         ptr += len;
55      }
56    eet_write(ef, "/several/strings", buf, sizeof(buf), 1);
57    eet_sync(ef);
58
59    eet_write(ef, "/some/mysterious/data", some_data, sizeof(some_data) - 1, 1);
60
61    eet_delete(ef, "/several/strings");
62
63    return eet_close(ef) == EET_ERROR_NONE;
64 }
65
66 int
67 main(void)
68 {
69    Eet_File *ef;
70    char *ret, **list;
71    int size, num, i;
72
73    eet_init();
74
75    if (!create_eet_file())
76      return -1;
77
78    ef = eet_open("/tmp/my_file.eet", EET_FILE_MODE_READ);
79    if (!ef) return -1;
80
81    list = eet_list(ef, "*", &num);
82    if (list)
83      {
84         for (i = 0; i < num; i++)
85           printf("Key stored: %s, list[i]);
86         free(list);
87      }
88
89    ret = eet_read(ef, "/key/to_store/at", &size);
90    if (ret)
91      {
92         printf("Data read (%i bytes):s, size, ret);
93         free(ret);
94      }
95
96    ret = eet_read(ef, "/several/strings", &size);
97    if (ret)
98      {
99         printf("More data read (%i bytes):s, size, ret);
100         free(ret);
101      }
102
103    ret = eet_read(ef, "/some/mysterious/data", &size);
104    if (ret)
105      {
106         Eet_File *ef2;
107
108         ef2 = eet_memopen_read(ret, size);
109
110         num = eet_num_entries(ef2);
111         printf("Mysterious data has %d entries, num);
112
113         printf("Mysterious data:s,
114                (char *)eet_read_direct(ef2, "/mysterious/data", NULL));
115
116         eet_close(ef2);
117
118         free(ret);
119      }
120
121    eet_close(ef);
122
123    eet_shutdown();
124
125    return 0;
126 }
127