diff --git a/legacy/eet/src/bin/eet_main.c b/legacy/eet/src/bin/eet_main.c index 289d2d2212..ece7120a09 100644 --- a/legacy/eet/src/bin/eet_main.c +++ b/legacy/eet/src/bin/eet_main.c @@ -73,7 +73,6 @@ static void do_eet_decode(const char *file, const char *key, const char *out) { Eet_File *ef; - void *data; int size = 0; FILE *f; @@ -83,25 +82,18 @@ do_eet_decode(const char *file, const char *key, const char *out) printf("cannot open for reading: %s\n", file); exit(-1); } - data = eet_read(ef, key, &size); - if (!data) - { - printf("cannot read key %s\n", key); - exit(-1); - } f = fopen(out, "w"); if (!f) { printf("cannot open %s\n", out); exit(-1); } - if (!eet_data_text_dump(data, size, do_eet_decode_dump, f)) + if (!eet_data_dump(ef, key, do_eet_decode_dump, f)) { printf("cannot write to %s\n", out); exit(-1); } fclose(f); - free(data); eet_close(ef); } @@ -153,7 +145,6 @@ do_eet_encode(const char *file, const char *key, const char *out, int compress) Eet_File *ef; char *text; int textlen = 0; - void *data; int size = 0; FILE *f; @@ -186,15 +177,12 @@ do_eet_encode(const char *file, const char *key, const char *out, int compress) exit(-1); } fclose(f); - data = eet_data_text_undump(text, textlen, &size); - if (!data) + if (eet_data_undump(ef, key, text, textlen, compress)) { printf("cannot parse %s\n", out); exit(-1); } - eet_write(ef, key, data, size, compress); free(text); - free(data); eet_close(ef); } diff --git a/legacy/eet/src/lib/Eet.h b/legacy/eet/src/lib/Eet.h index 39dce99c46..623219f458 100644 --- a/legacy/eet/src/lib/Eet.h +++ b/legacy/eet/src/lib/Eet.h @@ -913,6 +913,45 @@ extern "C" { */ EAPI void *eet_data_text_undump(const char *text, int textlen, int *size_ret); + /** + * Dump an eet encoded data structure from an eet file into ascii text + * @param ef A valid eet file handle. + * @param name Name of the entry. eg: "/base/file_i_want". + * @param dumpfunc The function to call passed a string when new data is converted to text + * @param dumpdata The data to pass to the @p dumpfunc callback. + * @return 1 on success, 0 on failure + * + * This function will take an open and valid eet file from eet_open() request + * the data encoded by eet_data_descriptor_encode() corresponding to the key @p name + * and convert it into human readable ascii text. It does this by calling the + * @p dumpfunc callback for all new text that is generated. This callback should + * append to any existing text buffer and will be passed the pointer @p dumpdata + * as a parameter as well as a string with new text to be appended. + * + * @since 1.0.0 + */ + EAPI int eet_data_dump(Eet_File *ef, const char *name, void (*dumpfunc) (void *data, const char *str), void *dumpdata); + + /** + * Take an ascii encoding from eet_data_dump() and re-encode in binary. + * @param ef A valid eet file handle. + * @param name Name of the entry. eg: "/base/file_i_want". + * @param text The pointer to the string data to parse and encode. + * @param textlen The size of the string in bytes (not including 0 byte terminator). + * @param compress Compression flags (1 == compress, 0 = don't compress). + * @return 1 on success, 0 on failure + * + * This function will parse the string pointed to by @p text, encode it the same + * way eet_data_descriptor_encode() takes an in-memory data struct and encodes into a + * binary blob. + * + * The data (optionally compressed) will be in ram, pending a flush to + * disk (it will stay in ram till the eet file handle is closed though). + * + * @since 1.0.0 + */ + EAPI int eet_data_undump(Eet_File *ef, const char *name, const char *text, int textlen, int compress); + /** * Decode a data structure from an arbitary location in memory. * @param edd The data descriptor to use when decoding. diff --git a/legacy/eet/src/lib/eet_data.c b/legacy/eet/src/lib/eet_data.c index 0ad2e2ba66..8cd64c210c 100644 --- a/legacy/eet/src/lib/eet_data.c +++ b/legacy/eet/src/lib/eet_data.c @@ -2493,6 +2493,39 @@ error: return NULL; } +EAPI int +eet_data_dump(Eet_File *ef, + const char *name, + void (*dumpfunc) (void *data, const char *str), + void *dumpdata) +{ + const Eet_Dictionary *ed = NULL; + const void *data; + int ret = 0; + int required_free = 0; + int size; + + ed = eet_dictionary_get(ef); + + data = eet_read_direct(ef, name, &size); + if (!data) + { + required_free = 1; + data = eet_read(ef, name, &size); + if (!data) return 0; + } + + if (_eet_data_descriptor_decode(ed, NULL, data, size, 0, + dumpfunc, dumpdata)) + ret = 1; + + if (required_free) + free((void*)data); + + return ret; +} + + EAPI int eet_data_text_dump(const void *data_in, int size_in, @@ -2513,6 +2546,27 @@ eet_data_text_undump(const char *text, return _eet_data_dump_parse(NULL, size_ret, text, textlen); } +EAPI int +eet_data_undump(Eet_File *ef, + const char *name, + const char *text, + int textlen, + int compress) +{ + Eet_Dictionary *ed; + void *data_enc; + int size; + int val; + + ed = eet_dictionary_get(ef); + + data_enc = _eet_data_dump_parse(ed, &size, text, textlen); + if (!data_enc) return 0; + val = eet_write(ef, name, data_enc, size, compress); + free(data_enc); + return val; +} + EAPI void * eet_data_descriptor_decode(Eet_Data_Descriptor *edd, const void *data_in,