Example of the various ways to interface with an Eet File

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files
- Examples

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 "\x1e\xe7\x0f\x42\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x35"
21 "\x00\x00\x00\xa0\x00\x00\x00\xa0\x00\x00\x00\x24\x00\x00\x00\x11"
22 "\x00\x00\x00\x00\x2f\x6d\x69\x73\x74\x65\x72\x69\x6f\x75\x73\x2f"
23 "\x64\x61\x74\x61\x00\x41\x6e\x20\x45\x45\x54\x20\x69\x6e\x73\x69"
24 "\x64\x65\x20\x6f\x66\x20\x61\x6e\x20\x45\x45\x54\x21\x0a\x54\x68"
25 "\x69\x73\x20\x77\x61\x73\x6e\x27\x74\x20\x72\x65\x61\x6c\x6c\x79"
26 "\x20\x75\x73\x65\x66\x75\x6c\x20\x62\x75\x74\x20\x69\x74\x20\x68"
27 "\x65\x6c\x70\x65\x64\x20\x74\x6f\x20\x73\x68\x6f\x77\x20\x68\x6f"
28 "\x77\x0a\x74\x6f\x20\x75\x73\x65\x20\x65\x65\x74\x5f\x6d\x65\x6d"
29 "\x6f\x70\x65\x6e\x5f\x72\x65\x61\x64\x28\x29\x20\x74\x6f\x20\x6f"
30 "\x70\x65\x6e\x20\x61\x6e\x20\x65\x65\x74\x20\x66\x69\x6c\x65\x20"
31 "\x66\x72\x6f\x6d\x0a\x64\x61\x74\x61\x20\x61\x6c\x72\x65\x61\x64"
32 "\x79\x20\x6c\x6f\x61\x64\x65\x64\x20\x69\x6e\x20\x6d\x65\x6d\x6f"
33 "\x72\x79\x2e\x0a\x00";
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!\n");
43 eet_close(ef);
44 return 0;
45 }
46 len = strlen(buf);
47 printf("strlen() = %d, eet_write() = %d\n", 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\n", 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):\n%s\n", 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):\n%s\n", 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\n", num);
112
113 printf("Mysterious data:\n%s\n",
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
eet_list
EAPI char ** eet_list(Eet_File *ef, const char *glob, int *count_ret)
List all entries in eet file matching shell glob.
Definition: eet_lib.c:2555
eet_open
EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode)
Open an eet file on disk, and returns a handle to it.
Definition: eet_lib.c:1491
eet_sync
EAPI Eet_Error eet_sync(Eet_File *ef)
Sync content of an eet file handle, flushing pending writes.
Definition: eet_lib.c:664
Eet_File
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition: Eet.h:518
eet_delete
EAPI int eet_delete(Eet_File *ef, const char *name)
Delete a specified entry from an Eet file being written or re-written.
Definition: eet_lib.c:2484
EET_FILE_MODE_READ
File is read-only.
Definition: Eet.h:475
eet_shutdown
EAPI int eet_shutdown(void)
Shut down the EET library.
Definition: eet_lib.c:607
eet_read
EAPI void * eet_read(Eet_File *ef, const char *name, int *size_ret)
Read a specified entry from an eet file and return data.
Definition: eet_lib.c:1992
eet_num_entries
EAPI int eet_num_entries(Eet_File *ef)
Return the number of entries in the specified eet file.
Definition: eet_lib.c:2639
Eet.h
The file that provides the eet functions.
EET_FILE_MODE_WRITE
File is write-only.
Definition: Eet.h:476
EET_ERROR_NONE
No error, it&#39;s all fine!
Definition: Eet.h:198
eet_read_direct
EAPI const void * eet_read_direct(Eet_File *ef, const char *name, int *size_ret)
Read a specified entry from an eet file and return data.
Definition: eet_lib.c:2000
eet_close
EAPI Eet_Error eet_close(Eet_File *ef)
Close an eet file handle and flush pending writes.
Definition: eet_lib.c:1889
eet_memopen_read
EAPI Eet_File * eet_memopen_read(const void *data, size_t size)
Open an eet file directly from a memory location.
Definition: eet_lib.c:1379
eet_write
EAPI int eet_write(Eet_File *ef, const char *name, const void *data, int size, int compress)
Write a specified entry to an eet file handle.
Definition: eet_lib.c:2474
eet_init
EAPI int eet_init(void)
Initialize the EET library.
Definition: eet_lib.c:553