Use better string wrapper.

This commit is contained in:
XMRig
2018-10-25 16:32:48 +07:00
parent 49aea7e80e
commit 4981dc3444
6 changed files with 159 additions and 74 deletions

View File

@@ -14,6 +14,7 @@ include (CheckIncludeFile)
set(HEADERS
src/3rdparty/align.h
src/App.h
src/base/tools/String.h
src/common/config/CommonConfig.h
src/common/config/ConfigLoader.h
src/common/config/ConfigWatcher.h
@@ -89,6 +90,7 @@ set(HEADERS
set(SOURCES
src/App.cpp
src/base/tools/String.cpp
src/common/config/CommonConfig.cpp
src/common/config/ConfigLoader.cpp
src/common/config/ConfigWatcher.cpp

42
src/base/tools/String.cpp Normal file
View File

@@ -0,0 +1,42 @@
/* 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 2016-2018 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/tools/String.h"
#include "rapidjson/document.h"
rapidjson::Value xmrig::String::toJSON() const
{
using namespace rapidjson;
return isNull() ? Value(kNullType) : Value(StringRef(m_data));
}
rapidjson::Value xmrig::String::toJSON(rapidjson::Document &doc) const
{
using namespace rapidjson;
return isNull() ? Value(kNullType) : Value(m_data, doc.GetAllocator());
}

104
src/base/tools/String.h Normal file
View File

@@ -0,0 +1,104 @@
/* 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 2016-2018 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_STRING_H
#define XMRIG_STRING_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rapidjson/fwd.h"
namespace xmrig {
/**
* @brief Simple C string wrapper.
*
* 1. I know about std:string.
* 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds.
* 3. nullptr and JSON conversion supported.
*/
class String
{
public:
inline String() : m_data(nullptr) {}
inline String(char *str) : m_data(str) {}
inline String(const char *str) : m_data(nullptr) { set(str); }
inline String(const String &other) : m_data(nullptr) { set(other.data()); }
inline String(String &&other) : m_data(other.m_data) { other.m_data = nullptr; }
inline ~String() { free(m_data); }
inline bool isEqual(const char *str) const { return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && m_data == nullptr); }
inline bool contains(const char *str) const { return strstr(m_data, str) != nullptr; }
inline bool isEmpty() const { return size() == 0; }
inline bool isNull() const { return m_data == nullptr; }
inline const char *data() const { return m_data; }
inline size_t size() const { return isNull() ? 0 : strlen(m_data); }
inline bool operator!=(const char *str) const { return !isEqual(str); }
inline bool operator!=(const String &str) const { return !isEqual(str.data()); }
inline bool operator<(const String &str) const { return strcmp(data(), str.data()) < 0; }
inline bool operator==(const char *str) const { return isEqual(str); }
inline bool operator==(const String &str) const { return isEqual(str.data()); }
inline String &operator=(char *str) { set(str); return *this; }
inline String &operator=(const char *str) { set(str); return *this; }
inline String &operator=(const String &str) { set(str.data()); return *this; }
inline String &operator=(String &&str) { m_data = str.m_data; str.m_data = nullptr; return *this; }
rapidjson::Value toJSON() const;
rapidjson::Value toJSON(rapidjson::Document &doc) const;
private:
inline void set(const char *str)
{
free(m_data);
m_data = str != nullptr ? strdup(str) : nullptr;
}
inline void set(char *str)
{
free(m_data);
m_data = str;
}
char *m_data;
};
} /* namespace xmrig */
#endif /* XMRIG_STRING_H */

View File

@@ -21,82 +21,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __C_STR_H__
#define __C_STR_H__
#ifndef XMRIG_C_STR_H
#define XMRIG_C_STR_H
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "base/tools/String.h"
namespace xmrig {
/**
* @brief Simple C string wrapper.
*
* 1. I know about std:string.
* 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds.
*/
class c_str
{
public:
inline c_str() : m_data(nullptr) {}
inline c_str(c_str &&other) { m_data = other.m_data; other.m_data = nullptr; }
inline c_str(const c_str &other) : m_data(nullptr) { set(other.data()); }
inline c_str(const char *str) : m_data(nullptr) { set(str); }
inline ~c_str() { free(m_data); }
inline void set(const char *str)
{
free(m_data);
m_data = str != nullptr ? strdup(str) : nullptr;
}
inline void set(char *str)
{
free(m_data);
m_data = str;
}
inline bool isEqual(const char *str) const
{
return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && m_data == nullptr);
}
inline bool contains(const char *str) const
{
return strstr(m_data, str) != nullptr;
}
inline bool isNull() const { return m_data == nullptr; }
inline const char *data() const { return m_data; }
inline size_t size() const { return m_data == nullptr ? 0 : strlen(m_data); }
inline bool operator!=(const c_str &str) const { return !isEqual(str.data()); }
inline bool operator!=(const char *str) const { return !isEqual(str); }
inline bool operator==(const c_str &str) const { return isEqual(str.data()); }
inline bool operator==(const char *str) const { return isEqual(str); }
inline c_str &operator=(char *str) { set(str); return *this; }
inline c_str &operator=(const c_str &str) { set(str.data()); return *this; }
inline c_str &operator=(const char *str) { set(str); return *this; }
private:
char *m_data;
};
typedef String c_str;
} /* namespace xmrig */
#endif /* __C_STR_H__ */
#endif /* XMRIG_C_STR_H */

View File

@@ -51,8 +51,8 @@ rapidjson::Value xmrig::TlsConfig::toJSON(rapidjson::Document &doc) const
auto &allocator = doc.GetAllocator();
Value obj(kObjectType);
obj.AddMember("cert", cert() ? Value(StringRef(cert())).Move() : Value(kNullType).Move(), allocator);
obj.AddMember("key", key() ? Value(StringRef(key())).Move() : Value(kNullType).Move(), allocator);
obj.AddMember("cert", m_cert.toJSON(), allocator);
obj.AddMember("key", m_key.toJSON(), allocator);
return obj;
}

View File

@@ -26,7 +26,7 @@
#define XMRIG_TLSCONFIG_H
#include "common/utils/c_str.h"
#include "base/tools/String.h"
#include "rapidjson/fwd.h"
@@ -40,7 +40,7 @@ public:
TlsConfig(const rapidjson::Value &object);
~TlsConfig();
inline bool isValid() const { return !m_cert.isNull() && !m_key.isNull(); }
inline bool isValid() const { return !m_cert.isEmpty() && !m_key.isEmpty(); }
inline const char *cert() const { return m_cert.data(); }
inline const char *key() const { return m_key.data(); }
inline void setCert(const char *cert) { m_cert = cert; }
@@ -49,8 +49,8 @@ public:
rapidjson::Value toJSON(rapidjson::Document &doc) const;
private:
c_str m_cert;
c_str m_key;
String m_cert;
String m_key;
};