diff options
Diffstat (limited to 'src/mbgl/util/io.cpp')
-rw-r--r-- | src/mbgl/util/io.cpp | 34 |
1 files changed, 10 insertions, 24 deletions
diff --git a/src/mbgl/util/io.cpp b/src/mbgl/util/io.cpp index 6a6ed7b250..2dc6cabf0f 100644 --- a/src/mbgl/util/io.cpp +++ b/src/mbgl/util/io.cpp @@ -1,43 +1,29 @@ #include <mbgl/util/io.hpp> -#include <cstdio> #include <cerrno> #include <iostream> -#include <sstream> #include <fstream> namespace mbgl { namespace util { -void write_file(const std::string &filename, const std::string &data) { - FILE *fd = fopen(filename.c_str(), "wb"); - if (fd) { - fwrite(data.data(), sizeof(std::string::value_type), data.size(), fd); - fclose(fd); - } else { - throw std::runtime_error(std::string("Failed to open file ") + filename); - } -} - -std::string read_file(const std::string &filename) { - std::ifstream file(filename); +void writeFile(const std::string &filename, Blob blob) { + std::ofstream file(filename, std::ios::binary); if (file.good()) { - std::stringstream data; - data << file.rdbuf(); - return data.str(); + file << *blob.uncompressedData(); } else { - throw std::runtime_error(std::string("Cannot read file ") + filename); + throw IOException(errno, "failed to write file"); } } -optional<std::string> readFile(const std::string &filename) { - std::ifstream file(filename); +Blob readFile(const std::string &filename) { + std::ifstream file(filename, std::ios::binary); if (file.good()) { - std::stringstream data; - data << file.rdbuf(); - return data.str(); + return { { std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>() }, + false }; + } else { + return {}; } - return {}; } void deleteFile(const std::string& filename) { |