Adjust miniz usage to use nowide FILE streams everywhere.

This commit is contained in:
tamasmeszaros 2019-06-02 22:27:35 +02:00
parent 15620a2fc2
commit 460887cdde
4 changed files with 83 additions and 34 deletions

View file

@ -712,19 +712,37 @@ bool load_amf_file(const char *path, DynamicPrintConfig *config, Model *model)
return result;
}
namespace {
void close_archive_reader(mz_zip_archive *arch) {
if (arch) {
FILE *f = mz_zip_get_cfile(arch);
mz_zip_reader_end(arch);
if (f) fclose(f);
}
}
void close_archive_writer(mz_zip_archive *arch) {
if (arch) {
FILE *f = mz_zip_get_cfile(arch);
mz_zip_writer_end(arch);
if (f) fclose(f);
}
}
}
bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat, DynamicPrintConfig* config, Model* model, unsigned int& version)
{
if (stat.m_uncomp_size == 0)
{
printf("Found invalid size\n");
mz_zip_reader_end(&archive);
close_archive_reader(&archive);
return false;
}
XML_Parser parser = XML_ParserCreate(nullptr); // encoding
if (!parser) {
printf("Couldn't allocate memory for parser\n");
mz_zip_reader_end(&archive);
close_archive_reader(&archive);
return false;
}
@ -737,7 +755,7 @@ bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_fi
if (parser_buffer == nullptr)
{
printf("Unable to create buffer\n");
mz_zip_reader_end(&archive);
close_archive_reader(&archive);
return false;
}
@ -745,14 +763,14 @@ bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_fi
if (res == 0)
{
printf("Error while reading model data to buffer\n");
mz_zip_reader_end(&archive);
close_archive_reader(&archive);
return false;
}
if (!XML_ParseBuffer(parser, (int)stat.m_uncomp_size, 1))
{
printf("Error (%s) while parsing xml file at line %d\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser));
mz_zip_reader_end(&archive);
close_archive_reader(&archive);
return false;
}
@ -774,8 +792,10 @@ bool load_amf_archive(const char *path, DynamicPrintConfig *config, Model *model
mz_zip_archive archive;
mz_zip_zero_struct(&archive);
mz_bool res = mz_zip_reader_init_file(&archive, path, 0);
if (res == 0)
FILE * f = boost::nowide::fopen(path, "rb");
auto res = mz_bool(f == nullptr ? MZ_FALSE : MZ_TRUE);
res = res && mz_zip_reader_init_cfile(&archive, f, -1, 0);
if (res == MZ_FALSE)
{
printf("Unable to init zip reader\n");
return false;
@ -793,7 +813,7 @@ bool load_amf_archive(const char *path, DynamicPrintConfig *config, Model *model
{
if (!extract_model_from_archive(archive, stat, config, model, version))
{
mz_zip_reader_end(&archive);
close_archive_reader(&archive);
printf("Archive does not contain a valid model");
return false;
}
@ -814,7 +834,7 @@ bool load_amf_archive(const char *path, DynamicPrintConfig *config, Model *model
}
#endif // forward compatibility
mz_zip_reader_end(&archive);
close_archive_reader(&archive);
return true;
}
@ -854,7 +874,8 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config)
mz_zip_archive archive;
mz_zip_zero_struct(&archive);
mz_bool res = mz_zip_writer_init_file(&archive, export_path.c_str(), 0);
FILE *f = boost::nowide::fopen(export_path.c_str(), "wb");
auto res = mz_bool(f != nullptr && mz_zip_writer_init_cfile(&archive, f, 0));
if (res == 0)
return false;
@ -1018,19 +1039,20 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config)
if (!mz_zip_writer_add_mem(&archive, internal_amf_filename.c_str(), (const void*)out.data(), out.length(), MZ_DEFAULT_COMPRESSION))
{
mz_zip_writer_end(&archive);
close_archive_writer(&archive);
if (f) fclose(f);
boost::filesystem::remove(export_path);
return false;
}
if (!mz_zip_writer_finalize_archive(&archive))
{
mz_zip_writer_end(&archive);
close_archive_writer(&archive);
boost::filesystem::remove(export_path);
return false;
}
mz_zip_writer_end(&archive);
close_archive_writer(&archive);
return true;
}