gmio_amf: auto-define ZIP entry filename from filepath in gmio_amf_write_file()

This commit is contained in:
Hugues Delorme 2017-01-26 17:24:54 +01:00
parent 6897ca12d5
commit 14a7be93a6
4 changed files with 47 additions and 19 deletions

View File

@ -32,6 +32,7 @@
#include "../gmio_core/error.h" #include "../gmio_core/error.h"
#include "../gmio_core/internal/error_check.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/float_format_utils.h"
#include "../gmio_core/internal/helper_memblock.h" #include "../gmio_core/internal/helper_memblock.h"
#include "../gmio_core/internal/helper_stream.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; const bool compress = opts != NULL ? opts->create_zip_archive : false;
FILE* file = fopen(filepath, compress ? "wb" : "w"); FILE* file = fopen(filepath, compress ? "wb" : "w");
int error = GMIO_ERROR_OK;
if (file != NULL) { 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); struct gmio_stream stream = gmio_stream_stdio(file);
const int error = gmio_amf_write(&stream, doc, opts); if (compress && opts->zip_entry_filename_len == 0) {
fclose(file); /* opts->zip_entry_filename is empty then try to take the filename
return error; * 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;
} }

View File

@ -51,11 +51,9 @@
*/ */
struct gmio_amf_write_options 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 * If null, then a temporary memblock is created with the global default
* constructor function * constructor function
*
* \sa gmio_memblock_isnull() * \sa gmio_memblock_isnull()
* \sa gmio_memblock_default() */ * \sa gmio_memblock_default() */
struct gmio_memblock stream_memblock; 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 */ /*! Optional interface by which the I/O operation can be controlled */
struct gmio_task_iface task_iface; struct gmio_task_iface task_iface;
/*! The format used when writting double values as strings /*! The format used when writting double values as strings.
* * Defaults to \c GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE when calling
* Defaulted to \c GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE when calling * gmio_amf_write() with \c options==NULL */
* gmio_amf_write() with \c options==NULL
*/
enum gmio_float_text_format float64_format; enum gmio_float_text_format float64_format;
/*! The maximum number of significant digits when writting double values /*! The maximum number of significant digits when writting \c double values.
* * Defaults to \c 16 when calling gmio_amf_write() with \c options==NULL */
* Defaulted to \c 16 when calling gmio_amf_write() with \c options==NULL
*/
uint8_t float64_prec; uint8_t float64_prec;
/* ZIP/Deflate compression */ /* ZIP/Deflate compression */
@ -91,7 +85,7 @@ struct gmio_amf_write_options
/*! Flag to disable use of the Zip64 format extensions */ /*! Flag to disable use of the Zip64 format extensions */
bool dont_use_zip64_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; struct gmio_zlib_compress_options z_compress_options;
}; };

View File

@ -52,6 +52,7 @@ const char* all_tests()
UTEST_RUN(test_amf_write_doc_1_plaintext); UTEST_RUN(test_amf_write_doc_1_plaintext);
UTEST_RUN(test_amf_write_doc_1_zip); UTEST_RUN(test_amf_write_doc_1_zip);
UTEST_RUN(test_amf_write_doc_1_zip64); UTEST_RUN(test_amf_write_doc_1_zip64);
UTEST_RUN(test_amf_write_doc_1_zip64_file);
return NULL; return NULL;
} }

View File

@ -439,3 +439,15 @@ static const char* test_amf_write_doc_1_zip64()
free(wbuff.ptr); free(wbuff.ptr);
return NULL; 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;
}