mirror of
https://github.com/xmrig/xmrig.git
synced 2025-03-24 08:08:58 +00:00
Remove code duplication.
This commit is contained in:
parent
d33c91684d
commit
781f08a034
3 changed files with 35 additions and 48 deletions
|
@ -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<std::string>& s)
|
bool xmrig::Json::convertOffset(std::istream &ifs, size_t offset, size_t &line, size_t &pos, std::vector<std::string> &s)
|
||||||
{
|
{
|
||||||
std::string prev_t;
|
std::string prev_t;
|
||||||
std::string t;
|
std::string t;
|
||||||
|
|
|
@ -53,12 +53,11 @@ public:
|
||||||
static bool get(const char *fileName, rapidjson::Document &doc);
|
static bool get(const char *fileName, rapidjson::Document &doc);
|
||||||
static bool save(const char *fileName, const 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<std::string>& s);
|
static bool convertOffset(const char *fileName, size_t offset, size_t &line, size_t &pos, std::vector<std::string> &s);
|
||||||
|
|
||||||
static rapidjson::Value normalize(double value, bool zero);
|
static rapidjson::Value normalize(double value, bool zero);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static bool convertOffset(std::istream& ifs, size_t offset, size_t& line, size_t& pos, std::vector<std::string>& s);
|
static bool convertOffset(std::istream &ifs, size_t offset, size_t &line, size_t &pos, std::vector<std::string> &s);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,9 @@
|
||||||
#include "rapidjson/prettywriter.h"
|
#include "rapidjson/prettywriter.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
#if defined(_MSC_VER) || defined (__GNUC__)
|
#if defined(_MSC_VER) || defined (__GNUC__)
|
||||||
static std::wstring toUtf16(const char *str)
|
static std::wstring toUtf16(const char *str)
|
||||||
{
|
{
|
||||||
|
@ -60,31 +63,36 @@ static std::wstring toUtf16(const char *str)
|
||||||
#endif
|
#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<char> 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)
|
bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc)
|
||||||
{
|
{
|
||||||
|
OPEN_IFS(fileName)
|
||||||
|
|
||||||
using namespace rapidjson;
|
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<char> buf(fd, mode);
|
|
||||||
std::istream ifs(&buf);
|
|
||||||
# else
|
|
||||||
std::ifstream ifs(fileName, mode);
|
|
||||||
if (!ifs.is_open()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
# endif
|
|
||||||
|
|
||||||
IStreamWrapper isw(ifs);
|
IStreamWrapper isw(ifs);
|
||||||
doc.ParseStream<kParseCommentsFlag | kParseTrailingCommasFlag>(isw);
|
doc.ParseStream<kParseCommentsFlag | kParseTrailingCommasFlag>(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<std::string>& s)
|
bool xmrig::Json::convertOffset(const char *fileName, size_t offset, size_t &line, size_t &pos, std::vector<std::string> &s)
|
||||||
{
|
{
|
||||||
constexpr const std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary;
|
OPEN_IFS(fileName)
|
||||||
|
|
||||||
# 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<char> buf(fd, mode);
|
|
||||||
std::istream ifs(&buf);
|
|
||||||
# else
|
|
||||||
std::ifstream ifs(fileName, mode);
|
|
||||||
if (!ifs.is_open()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
# endif
|
|
||||||
|
|
||||||
return convertOffset(ifs, offset, line, pos, s);
|
return convertOffset(ifs, offset, line, pos, s);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue