Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encode() returns error code if save_file() cannot open output file. #14

Merged
merged 1 commit into from
Sep 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lodepng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5938,10 +5938,12 @@ void load_file(std::vector<unsigned char>& 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<unsigned char>& buffer, const std::string& filename)
unsigned save_file(const std::vector<unsigned char>& 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 */

Expand Down Expand Up @@ -6127,7 +6129,7 @@ unsigned encode(const std::string& filename,
{
std::vector<unsigned char> 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;
}

Expand Down
2 changes: 1 addition & 1 deletion lodepng.h
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ void load_file(std::vector<unsigned char>& 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<unsigned char>& buffer, const std::string& filename);
unsigned save_file(const std::vector<unsigned char>& buffer, const std::string& filename);
#endif /* LODEPNG_COMPILE_DISK */
#endif /* LODEPNG_COMPILE_PNG */

Expand Down