diff --git a/src/base/io/json/Json.cpp b/src/base/io/json/Json.cpp index fb4c0e470..b239777b3 100644 --- a/src/base/io/json/Json.cpp +++ b/src/base/io/json/Json.cpp @@ -191,7 +191,7 @@ rapidjson::Value xmrig::Json::normalize(double value, bool zero) } -bool xmrig::Json::convertOffset(std::istream& ifs, size_t offset, size_t& line, size_t& pos, std::vector& s) +bool xmrig::Json::convertOffset(std::istream &ifs, size_t offset, size_t &line, size_t &pos, std::vector &s) { std::string prev_t; std::string t; diff --git a/src/base/io/json/Json.h b/src/base/io/json/Json.h index e6b772a17..10e6e696f 100644 --- a/src/base/io/json/Json.h +++ b/src/base/io/json/Json.h @@ -53,12 +53,11 @@ public: static bool get(const char *fileName, rapidjson::Document &doc); static bool save(const char *fileName, const rapidjson::Document &doc); - static bool convertOffset(const char *fileName, size_t offset, size_t &line, size_t &pos, std::vector& s); - + static bool convertOffset(const char *fileName, size_t offset, size_t &line, size_t &pos, std::vector &s); static rapidjson::Value normalize(double value, bool zero); private: - static bool convertOffset(std::istream& ifs, size_t offset, size_t& line, size_t& pos, std::vector& s); + static bool convertOffset(std::istream &ifs, size_t offset, size_t &line, size_t &pos, std::vector &s); }; diff --git a/src/base/io/json/Json_win.cpp b/src/base/io/json/Json_win.cpp index 693594832..a9c4065a3 100644 --- a/src/base/io/json/Json_win.cpp +++ b/src/base/io/json/Json_win.cpp @@ -43,6 +43,9 @@ #include "rapidjson/prettywriter.h" +namespace xmrig { + + #if defined(_MSC_VER) || defined (__GNUC__) static std::wstring toUtf16(const char *str) { @@ -60,31 +63,36 @@ static std::wstring toUtf16(const char *str) #endif +#if defined(_MSC_VER) +# define OPEN_IFS(name) \ + std::ifstream ifs(toUtf16(name), std::ios_base::in | std::ios_base::binary); \ + if (!ifs.is_open()) { \ + return false; \ + } +#elif defined(__GNUC__) +# define OPEN_IFS(name) \ + const int fd = _wopen(toUtf16(name).c_str(), _O_RDONLY | _O_BINARY); \ + if (fd == -1) { \ + return false; \ + } \ + __gnu_cxx::stdio_filebuf buf(fd, std::ios_base::in | std::ios_base::binary); \ + std::istream ifs(&buf); +#else +# define OPEN_IFS(name) \ + std::ifstream ifs(name, std::ios_base::in | std::ios_base::binary); \ + if (!ifs.is_open()) { \ + return false; \ + } +#endif + +} // namespace xmrig + + bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc) { + OPEN_IFS(fileName) + using namespace rapidjson; - constexpr const std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary; - -# if defined(_MSC_VER) - std::ifstream ifs(toUtf16(fileName), mode); - if (!ifs.is_open()) { - return false; - } -# elif defined(__GNUC__) - const int fd = _wopen(toUtf16(fileName).c_str(), _O_RDONLY | _O_BINARY); - if (fd == -1) { - return false; - } - - __gnu_cxx::stdio_filebuf buf(fd, mode); - std::istream ifs(&buf); -# else - std::ifstream ifs(fileName, mode); - if (!ifs.is_open()) { - return false; - } -# endif - IStreamWrapper isw(ifs); doc.ParseStream(isw); @@ -127,29 +135,9 @@ bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc) } -bool xmrig::Json::convertOffset(const char* fileName, size_t offset, size_t& line, size_t& pos, std::vector& s) +bool xmrig::Json::convertOffset(const char *fileName, size_t offset, size_t &line, size_t &pos, std::vector &s) { - constexpr const std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary; - -# if defined(_MSC_VER) - std::ifstream ifs(toUtf16(fileName), mode); - if (!ifs.is_open()) { - return false; - } -# elif defined(__GNUC__) - const int fd = _wopen(toUtf16(fileName).c_str(), _O_RDONLY | _O_BINARY); - if (fd == -1) { - return false; - } - - __gnu_cxx::stdio_filebuf buf(fd, mode); - std::istream ifs(&buf); -# else - std::ifstream ifs(fileName, mode); - if (!ifs.is_open()) { - return false; - } -# endif + OPEN_IFS(fileName) return convertOffset(ifs, offset, line, pos, s); }