diff options
Diffstat (limited to 'src/support')
-rw-r--r-- | src/support/file.cpp | 25 | ||||
-rw-r--r-- | src/support/file.h | 20 |
2 files changed, 31 insertions, 14 deletions
diff --git a/src/support/file.cpp b/src/support/file.cpp index c93086990..b71361d99 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -20,16 +20,19 @@ #include <limits> template <typename T> -T wasm::read_file(const std::string &filename, bool debug) { - if (debug) std::cerr << "Loading '" << filename << "'..." << std::endl; - std::ifstream infile(filename); +T wasm::read_file(const std::string &filename, Flags::BinaryOption binary, Flags::DebugOption debug) { + if (debug == Flags::Debug) std::cerr << "Loading '" << filename << "'..." << std::endl; + std::ifstream infile; + auto flags = std::ifstream::in; + if (binary == Flags::Binary) flags |= std::ifstream::binary; + infile.open(filename, flags); if (!infile.is_open()) { std::cerr << "Failed opening '" << filename << "'" << std::endl; exit(EXIT_FAILURE); } infile.seekg(0, std::ios::end); std::streampos insize = infile.tellg(); - if (size_t(insize) >= std::numeric_limits<size_t>::max()) { + if (uint64_t(insize) >= std::numeric_limits<size_t>::max()) { // Building a 32-bit executable where size_t == 32 bits, we are not able to create strings larger than 2^32 bytes in length, so must abort here. std::cerr << "Failed opening '" << filename << "': Input file too large: " << insize << " bytes. Try rebuilding in 64-bit mode." << std::endl; exit(EXIT_FAILURE); @@ -41,15 +44,17 @@ T wasm::read_file(const std::string &filename, bool debug) { } // Explicit instantiations for the explicit specializations. -template std::string wasm::read_file<>(const std::string &, bool); -template std::vector<char> wasm::read_file<>(const std::string &, bool); +template std::string wasm::read_file<>(const std::string &, Flags::BinaryOption, Flags::DebugOption); +template std::vector<char> wasm::read_file<>(const std::string &, Flags::BinaryOption, Flags::DebugOption); -wasm::Output::Output(const std::string &filename, bool debug) - : outfile(), out([this, filename, debug]() { +wasm::Output::Output(const std::string &filename, Flags::BinaryOption binary, Flags::DebugOption debug) + : outfile(), out([this, filename, binary, debug]() { std::streambuf *buffer; if (filename.size()) { - if (debug) std::cerr << "Opening '" << filename << std::endl; - outfile.open(filename, std::ofstream::out | std::ofstream::trunc); + if (debug == Flags::Debug) std::cerr << "Opening '" << filename << std::endl; + auto flags = std::ofstream::out | std::ofstream::trunc; + if (binary == Flags::Binary) flags |= std::ofstream::binary; + outfile.open(filename, flags); if (!outfile.is_open()) { std::cerr << "Failed opening '" << filename << "'" << std::endl; exit(EXIT_FAILURE); diff --git a/src/support/file.h b/src/support/file.h index 47f7ececb..01c7a8546 100644 --- a/src/support/file.h +++ b/src/support/file.h @@ -28,16 +28,28 @@ #include <vector> namespace wasm { + +namespace Flags { + enum BinaryOption { + Binary, + Text + }; + enum DebugOption { + Debug, + Release + }; +} + template <typename T> -T read_file(const std::string &filename, bool debug); +T read_file(const std::string &filename, Flags::BinaryOption binary, Flags::DebugOption debug); // Declare the valid explicit specializations. -extern template std::string read_file<>(const std::string &, bool); -extern template std::vector<char> read_file<>(const std::string &, bool); +extern template std::string read_file<>(const std::string &, Flags::BinaryOption, Flags::DebugOption); +extern template std::vector<char> read_file<>(const std::string &, Flags::BinaryOption, Flags::DebugOption); class Output { public: // An empty filename will open stdout instead. - Output(const std::string &filename, bool debug); + Output(const std::string &filename, Flags::BinaryOption binary, Flags::DebugOption debug); ~Output() = default; template <typename T> std::ostream &operator<<(const T &v) { |