diff --git a/src/gmio_amf/amf_io.c b/src/gmio_amf/amf_io.c index 26b1a29..2051e51 100644 --- a/src/gmio_amf/amf_io.c +++ b/src/gmio_amf/amf_io.c @@ -32,6 +32,7 @@ #include "../gmio_core/error.h" #include "../gmio_core/internal/error_check.h" +#include "../gmio_core/internal/file_utils.h" #include "../gmio_core/internal/float_format_utils.h" #include "../gmio_core/internal/helper_memblock.h" #include "../gmio_core/internal/helper_stream.h" @@ -936,13 +937,33 @@ int gmio_amf_write_file( { const bool compress = opts != NULL ? opts->create_zip_archive : false; FILE* file = fopen(filepath, compress ? "wb" : "w"); + int error = GMIO_ERROR_OK; if (file != NULL) { - /* TODO: if opts->zip_entry_filename is empty then try to take the - * filename part of filepath */ struct gmio_stream stream = gmio_stream_stdio(file); - const int error = gmio_amf_write(&stream, doc, opts); - fclose(file); - return error; + if (compress && opts->zip_entry_filename_len == 0) { + /* opts->zip_entry_filename is empty then try to take the filename + * part of filepath */ + const struct gmio_const_string basefilename = + gmio_fileutils_find_basefilename(filepath); + if (!gmio_const_string_is_empty(&basefilename)) { + char filename[512] = {0}; + const struct gmio_const_string suffix = { ".amf", 4 }; + const size_t filename_len = gmio_const_string_concat( + filename, sizeof(filename), &basefilename, &suffix); + struct gmio_amf_write_options ovr_opts = *opts; + ovr_opts.zip_entry_filename = filename; + ovr_opts.zip_entry_filename_len = (uint16_t)filename_len; + error = gmio_amf_write(&stream, doc, &ovr_opts); + goto label_end; + } + } + error = gmio_amf_write(&stream, doc, opts); } - return GMIO_ERROR_STDIO; + else { + error = GMIO_ERROR_STDIO; + } + +label_end: + fclose(file); + return error; } diff --git a/src/gmio_amf/amf_io_options.h b/src/gmio_amf/amf_io_options.h index dc2c1f0..ebfcd66 100644 --- a/src/gmio_amf/amf_io_options.h +++ b/src/gmio_amf/amf_io_options.h @@ -51,11 +51,9 @@ */ struct gmio_amf_write_options { - /*! Used by the stream to bufferize I/O operations - * + /*! Used by the stream to bufferize I/O operations. * If null, then a temporary memblock is created with the global default * constructor function - * * \sa gmio_memblock_isnull() * \sa gmio_memblock_default() */ struct gmio_memblock stream_memblock; @@ -63,17 +61,13 @@ struct gmio_amf_write_options /*! Optional interface by which the I/O operation can be controlled */ struct gmio_task_iface task_iface; - /*! The format used when writting double values as strings - * - * Defaulted to \c GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE when calling - * gmio_amf_write() with \c options==NULL - */ + /*! The format used when writting double values as strings. + * Defaults to \c GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE when calling + * gmio_amf_write() with \c options==NULL */ enum gmio_float_text_format float64_format; - /*! The maximum number of significant digits when writting double values - * - * Defaulted to \c 16 when calling gmio_amf_write() with \c options==NULL - */ + /*! The maximum number of significant digits when writting \c double values. + * Defaults to \c 16 when calling gmio_amf_write() with \c options==NULL */ uint8_t float64_prec; /* ZIP/Deflate compression */ @@ -91,7 +85,7 @@ struct gmio_amf_write_options /*! Flag to disable use of the Zip64 format extensions */ bool dont_use_zip64_extensions; - /*! Options for the deflate(zlib) compression */ + /*! Options for the zlib(deflate) compression */ struct gmio_zlib_compress_options z_compress_options; }; diff --git a/tests/main_test_amf.c b/tests/main_test_amf.c index 3693789..3ffce5e 100644 --- a/tests/main_test_amf.c +++ b/tests/main_test_amf.c @@ -52,6 +52,7 @@ const char* all_tests() UTEST_RUN(test_amf_write_doc_1_plaintext); UTEST_RUN(test_amf_write_doc_1_zip); UTEST_RUN(test_amf_write_doc_1_zip64); + UTEST_RUN(test_amf_write_doc_1_zip64_file); return NULL; } diff --git a/tests/test_amf_io.c b/tests/test_amf_io.c index 02a2156..59aabc9 100644 --- a/tests/test_amf_io.c +++ b/tests/test_amf_io.c @@ -439,3 +439,15 @@ static const char* test_amf_write_doc_1_zip64() free(wbuff.ptr); return NULL; } + +static const char* test_amf_write_doc_1_zip64_file() +{ + const struct __tamf__document testdoc = __tamf__create_doc_1(); + const struct gmio_amf_document doc = __tamf_create_doc(&testdoc); + struct gmio_amf_write_options options = {0}; + options.float64_prec = 9; + options.create_zip_archive = true; + const int error = gmio_amf_write_file("output_64_file.zip", &doc, &options); + UTEST_COMPARE_INT(error, GMIO_ERROR_OK); + return NULL; +}