From 4db007c3b1b8f793efecd4b4ac046147ee0d2485 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Sat, 1 Aug 2015 18:34:54 -0400 Subject: encode() now returns error code if save_file() cannot open output file. Before this change, encode() would return 0 whether or not the output file was created. There was no way for a caller to know when the file could not be created (e.g. due to a non-existent parent directory). Changed save_file() to return error 79 if it cannot open output file. encode() checks return code from save_file() and passes any error value back to the caller. --- lodepng.cpp | 6 ++++-- lodepng.h | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lodepng.cpp b/lodepng.cpp index 326b952..f2a7b23 100644 --- a/lodepng.cpp +++ b/lodepng.cpp @@ -5938,10 +5938,12 @@ void load_file(std::vector& buffer, const std::string& filename) } /*write given buffer to the file, overwriting the file, it doesn't append to it.*/ -void save_file(const std::vector& buffer, const std::string& filename) +unsigned save_file(const std::vector& buffer, const std::string& filename) { std::ofstream file(filename.c_str(), std::ios::out|std::ios::binary); + if(!file) return 79; file.write(buffer.empty() ? 0 : (char*)&buffer[0], std::streamsize(buffer.size())); + return 0; } #endif /* LODEPNG_COMPILE_DISK */ @@ -6127,7 +6129,7 @@ unsigned encode(const std::string& filename, { std::vector buffer; unsigned error = encode(buffer, in, w, h, colortype, bitdepth); - if(!error) save_file(buffer, filename); + if(!error) error = save_file(buffer, filename); return error; } diff --git a/lodepng.h b/lodepng.h index 74e4495..e68d40b 100644 --- a/lodepng.h +++ b/lodepng.h @@ -855,7 +855,7 @@ void load_file(std::vector& buffer, const std::string& filename); Save the binary data in an std::vector to a file on disk. The file is overwritten without warning. */ -void save_file(const std::vector& buffer, const std::string& filename); +unsigned save_file(const std::vector& buffer, const std::string& filename); #endif /* LODEPNG_COMPILE_DISK */ #endif /* LODEPNG_COMPILE_PNG */ -- cgit v1.2.3