Added class Json.

This commit is contained in:
XMRig 2019-02-14 18:14:38 +07:00
parent 2df204f8a8
commit b368ffacdb
10 changed files with 309 additions and 52 deletions

View file

@ -20,6 +20,7 @@ include (cmake/cpu.cmake)
set(HEADERS
src/api/NetworkState.h
src/App.h
src/base/io/Json.h
src/base/tools/String.h
src/common/config/CommonConfig.h
src/common/config/ConfigLoader.h
@ -98,6 +99,7 @@ endif()
set(SOURCES
src/api/NetworkState.cpp
src/App.cpp
src/base/io/Json.cpp
src/base/tools/String.cpp
src/common/config/CommonConfig.cpp
src/common/config/ConfigLoader.cpp
@ -143,6 +145,7 @@ if (WIN32)
set(SOURCES_OS
res/app.rc
src/App_win.cpp
src/base/io/Json_win.cpp
src/common/Platform_win.cpp
src/Mem_win.cpp
)
@ -152,12 +155,14 @@ if (WIN32)
elseif (APPLE)
set(SOURCES_OS
src/App_unix.cpp
src/base/io/Json_unix.cpp
src/common/Platform_mac.cpp
src/Mem_unix.cpp
)
else()
set(SOURCES_OS
src/App_unix.cpp
src/base/io/Json_unix.cpp
src/common/Platform_unix.cpp
src/Mem_unix.cpp
)

38
src/base/io/Json.cpp Normal file
View file

@ -0,0 +1,38 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/io/Json.h"
#include "rapidjson/document.h"
bool xmrig::Json::getBool(const rapidjson::Value &obj, const char *key, bool defaultValue)
{
auto i = obj.FindMember(key);
if (i != obj.MemberEnd() && i->value.IsBool()) {
return i->value.GetBool();
}
return defaultValue;
}

48
src/base/io/Json.h Normal file
View file

@ -0,0 +1,48 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_JSON_H
#define XMRIG_JSON_H
#include "rapidjson/fwd.h"
namespace xmrig {
class Json
{
public:
static bool getBool(const rapidjson::Value &obj, const char *key, bool defaultValue = false);
static bool get(const char *fileName, rapidjson::Document &doc);
static bool save(const char *fileName, const rapidjson::Document &doc);
};
} /* namespace xmrig */
#endif /* XMRIG_JSON_H */

62
src/base/io/Json_unix.cpp Normal file
View file

@ -0,0 +1,62 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fstream>
#include "base/io/Json.h"
#include "rapidjson/document.h"
#include "rapidjson/istreamwrapper.h"
#include "rapidjson/ostreamwrapper.h"
#include "rapidjson/prettywriter.h"
bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc)
{
std::ifstream ifs(fileName, std::ios_base::in | std::ios_base::binary);
if (!ifs.is_open()) {
return false;
}
rapidjson::IStreamWrapper isw(ifs);
doc.ParseStream<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(isw);
return !doc.HasParseError() && doc.IsObject();
}
bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc)
{
std::ofstream ofs(fileName, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
if (!ofs.is_open()) {
return false;
}
rapidjson::OStreamWrapper osw(ofs);
rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(osw);
doc.Accept(writer);
return true;
}

124
src/base/io/Json_win.cpp Normal file
View file

@ -0,0 +1,124 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#ifdef __GNUC__
# include <fcntl.h>
# include <ext/stdio_filebuf.h>
#endif
#include <fstream>
#include "base/io/Json.h"
#include "rapidjson/document.h"
#include "rapidjson/istreamwrapper.h"
#include "rapidjson/ostreamwrapper.h"
#include "rapidjson/prettywriter.h"
#if defined(_MSC_VER) || defined (__GNUC__)
static std::wstring toUtf16(const char *str)
{
const int size = static_cast<int>(strlen(str));
std::wstring ret;
int len = MultiByteToWideChar(CP_UTF8, 0, str, size, nullptr, 0);
if (len > 0) {
ret.resize(static_cast<size_t>(len));
MultiByteToWideChar(CP_UTF8, 0, str, size, &ret[0], len);
}
return ret;
}
#endif
bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc)
{
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);
doc.ParseStream<kParseCommentsFlag | kParseTrailingCommasFlag>(isw);
return !doc.HasParseError() && doc.IsObject();
}
bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc)
{
using namespace rapidjson;
constexpr const std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary | std::ios_base::trunc;
# if defined(_MSC_VER)
std::ofstream ofs(toUtf16(fileName), mode);
if (!ofs.is_open()) {
return false;
}
# elif defined(__GNUC__)
const int fd = _wopen(toUtf16(fileName).c_str(), _O_WRONLY | _O_BINARY | _O_TRUNC);
if (fd == -1) {
return false;
}
__gnu_cxx::stdio_filebuf<char> buf(fd, mode);
std::ostream ofs(&buf);
# else
std::ofstream ofs(fileName, mode);
if (!ofs.is_open()) {
return false;
}
# endif
OStreamWrapper osw(ofs);
PrettyWriter<OStreamWrapper> writer(osw);
doc.Accept(writer);
return true;
}

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -53,6 +54,7 @@
#endif
#include "base/io/Json.h"
#include "common/config/CommonConfig.h"
#include "common/log/Log.h"
#include "donate.h"
@ -203,31 +205,15 @@ bool xmrig::CommonConfig::save()
return false;
}
uv_fs_t req;
const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr);
if (fd < 0) {
return false;
}
uv_fs_req_cleanup(&req);
rapidjson::Document doc;
getJSON(doc);
FILE *fp = fdopen(fd, "w");
if (Json::save(m_fileName, doc)) {
LOG_NOTICE("configuration saved to: \"%s\"", m_fileName.data());
return true;
}
char buf[4096];
rapidjson::FileWriteStream os(fp, buf, sizeof(buf));
rapidjson::PrettyWriter<rapidjson::FileWriteStream> writer(os);
doc.Accept(writer);
fflush(fp);
uv_fs_close(uv_default_loop(), &req, fd, nullptr);
uv_fs_req_cleanup(&req);
LOG_NOTICE("configuration saved to: \"%s\"", m_fileName.data());
return true;
return false;
}

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -28,9 +29,9 @@
#include <vector>
#include "base/tools/String.h"
#include "common/interfaces/IConfig.h"
#include "common/net/Pool.h"
#include "common/utils/c_str.h"
#include "common/xmrig.h"
@ -103,12 +104,12 @@ protected:
State m_state;
std::vector<Pool> m_activePools;
std::vector<Pool> m_pools;
xmrig::c_str m_apiId;
xmrig::c_str m_apiToken;
xmrig::c_str m_apiWorkerId;
xmrig::c_str m_fileName;
xmrig::c_str m_logFile;
xmrig::c_str m_userAgent;
xmrig::String m_apiId;
xmrig::String m_apiToken;
xmrig::String m_apiWorkerId;
xmrig::String m_fileName;
xmrig::String m_logFile;
xmrig::String m_userAgent;
private:
bool parseInt(int key, int arg);

View file

@ -5,6 +5,7 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
@ -37,6 +38,7 @@
#endif
#include "base/io/Json.h"
#include "common/config/ConfigLoader.h"
#include "common/config/ConfigWatcher.h"
#include "common/interfaces/IConfig.h"
@ -151,7 +153,7 @@ xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator
int key;
while (1) {
key = getopt_long(argc, argv, short_options, options, NULL);
key = getopt_long(argc, argv, short_options, options, nullptr);
if (key < 0) {
break;
}
@ -207,30 +209,18 @@ void xmrig::ConfigLoader::release()
bool xmrig::ConfigLoader::getJSON(const char *fileName, rapidjson::Document &doc)
{
uv_fs_t req;
const int fd = uv_fs_open(uv_default_loop(), &req, fileName, O_RDONLY, 0644, nullptr);
if (fd < 0) {
fprintf(stderr, "unable to open %s: %s\n", fileName, uv_strerror(fd));
return false;
if (Json::get(fileName, doc)) {
return true;
}
uv_fs_req_cleanup(&req);
FILE *fp = fdopen(fd, "rb");
char buf[8192];
rapidjson::FileReadStream is(fp, buf, sizeof(buf));
doc.ParseStream(is);
uv_fs_close(uv_default_loop(), &req, fd, nullptr);
uv_fs_req_cleanup(&req);
if (doc.HasParseError()) {
printf("%s<%d>: %s\n", fileName, (int) doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError()));
return false;
printf("%s<offset:%zu>: \"%s\"\n", fileName, doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError()));
}
else {
fprintf(stderr, "unable to open \"%s\".\n", fileName);
}
return doc.IsObject();
return false;
}

View file

@ -5,6 +5,7 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify

View file

@ -4,7 +4,9 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2018 XMRig <support@xmrig.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -125,7 +127,7 @@ public:
CudaMaxUsageKey = 1206,
};
virtual ~IConfig() {}
virtual ~IConfig() = default;
virtual bool finalize() = 0;
virtual bool isWatch() const = 0;