Skip to content
This repository was archived by the owner on Aug 26, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
/Debug
/trpglogger/Debug
/.vs/trpglogger/v16
/.vs
40 changes: 40 additions & 0 deletions trpglogger/GlobalVar.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "GlobalVar.h"
#include <string>
#include <map>
#include <vector>
#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>


// ȫ��Ӧ��Ŀ¼
std::string fileLoc;
// ȫ�����ݿ�·��
Expand All @@ -18,3 +21,40 @@ std::map<long long, time_t> LogInfo; // Ⱥ
std::map<long long, time_t> LogInfoDiscuss; // ������
// �汾��Ϣ
const std::string TrpgLoggerVer = "TrpgLogger by w4123��� Version 1.0.4(18)";
//�Զ����ִ
std::string CustomReplyLoc_UTF8;
std::map<std::string, std::string> CustomReply
{
{"self", "��"},
{"strAlreadyLogging", "���ڽ�����־��¼, �޷��ٴο�ʼ!"},
{"strStartLogging", "��ʼ��־��¼"},
{"strStartSaveLog", "���ڱ�����־"},
{"strSuccessSaveLog", "��־��¼�ѽ������ļ��ѱ��棬�����ϴ���������"},
{"strFailSaveLog", "����ʧ�ܣ������Գ����Ժ����Դ������ٴα���\n������Ϣ:"},
{"strSuccessUploadLogBefore", "�ϴ�����ɣ������"},
{"strSuccessUploadLogAfter", "�Բ鿴��¼"},
{"strFailUploadLog", "�ϴ������з�����������ϵ����Ա���Ժ��ٴ�ʹ�ô����������ϴ�\n������Ϣ:"},
{"strNeverLog", "û���ѿ�ʼ����־��¼!"},
{"strLogHlp", "\n.log \t ������־��¼\n.log stop\t ֹͣ��־��¼\n.log help\t ��־��¼����"}
};
const std::vector<std::string> CustomReplyFilter
{
"self"
};
//��̬�滻
std::string& replace_all(std::string& str, const std::string& old_value, const std::string& new_value)
{
while (true)
{
std::string::size_type pos(0);
if ((pos = str.find(old_value)) != std::string::npos)
{
str.replace(pos, old_value.length(), new_value);
}
else
{
break;
}
}
return str;
}
7 changes: 7 additions & 0 deletions trpglogger/GlobalVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define TRPGLOGGER_GLOBALVAR

#include <string>
#include <map>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/Aws.h>

Expand All @@ -21,5 +22,11 @@ extern std::map<long long, time_t> LogInfo; // Ⱥ
extern std::map<long long, time_t> LogInfoDiscuss; // ������
// �汾��Ϣ
extern const std::string TrpgLoggerVer;
//�Զ����ִ
extern std::string CustomReplyLoc_UTF8;
extern std::map<std::string, std::string> CustomReply;
extern const std::vector<std::string> CustomReplyFilter;
//��̬�滻
std::string& replace_all(std::string& str, const std::string& old_value, const std::string& new_value);

#endif /*TRPGLOGGER_GLOBALVAR*/
36 changes: 36 additions & 0 deletions trpglogger/Jsonio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <filesystem>
#include <fstream>
#include "StrExtern.hpp"
#include "Jsonio.h"

nlohmann::json freadJson(const std::string& strPath)
{
std::ifstream fin(strPath);
if (!fin)return nlohmann::json();
nlohmann::json j;
try
{
fin >> j;
}
catch (...)
{
return nlohmann::json();
}
return j;
}

nlohmann::json freadJson(const std::filesystem::path& path)
{
std::ifstream fin(convert_w2a(path.wstring().c_str()));
if (!fin)return nlohmann::json();
nlohmann::json j;
try
{
fin >> j;
}
catch (...)
{
return nlohmann::json();
}
return j;
}
140 changes: 140 additions & 0 deletions trpglogger/Jsonio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Json信息获取以及写入
#pragma once
#include <fstream>
#include <map>
#include <vector>
#include <filesystem>
#include "json.hpp"
#include "EncodingConvert.h"

class JsonList
{
std::vector<std::string> vRes;
public:
std::string dump()
{
if (vRes.empty())return "{}";
if (vRes.size() == 1)return vRes[0];
std::string s;
for (auto it = vRes.begin(); it != vRes.end(); ++it)
{
if (it == vRes.begin())s = "[\n" + *it;
else s += ",\n" + *it;
}
s += "\n]";
return s;
}

JsonList& operator<<(std::string s)
{
vRes.push_back(s);
return *this;
}

[[nodiscard]] bool empty() const
{
return vRes.empty();
}

[[nodiscard]] size_t size() const
{
return vRes.size();
}
};

template <typename T>
std::enable_if_t<!std::is_arithmetic_v<T>, T> readJKey(const std::string& strJson)
{
return UTF8ToGBK(strJson);
}

template <typename T>
std::enable_if_t<std::is_arithmetic_v<T>, T> readJKey(const std::string& strJson)
{
return stoll(strJson);
}

nlohmann::json freadJson(const std::string& strPath);
nlohmann::json freadJson(const std::filesystem::path& path);

template <typename T1, typename T2, class sort>
int readJMap(const nlohmann::json& j, std::map<T1, T2, sort>& mapTmp)
{
int intCnt = 0;
for (auto it = j.cbegin(); it != j.cend(); ++it)
{
T1 tKey = readJKey<T1>(it.key());
T2 tVal = it.value();

tVal = UTF8toGBK(tVal);
mapTmp[tKey] = tVal;
intCnt++;
}
return intCnt;
}

template <typename T1, typename T2>
int readJson(const std::string& strJson, std::map<T1, T2>& mapTmp)
{
try
{
nlohmann::json j = nlohmann::json::parse(strJson);
return readJMap(j, mapTmp);
}
catch (...)
{
return -1;
}
}

template <typename T1, typename T2, typename sort>
int loadJMap(const std::string& strLoc, std::map<T1, T2, sort>& mapTmp)
{
std::ifstream fin(strLoc);
if (fin)
{
try
{
nlohmann::json j;
fin >> j;
fin.close();
return readJMap(j, mapTmp);
}
catch (...)
{
fin.close();
return -1;
}
}
return -2;
}

template <typename T>
std::string writeJKey(std::enable_if_t<!std::is_arithmetic_v<T>, T> strJson)
{
return GBKtoUTF8(strJson);
}

template <typename T>
std::string writeJKey(std::enable_if_t<std::is_arithmetic_v<T>, T> llJson)
{
return std::to_string(llJson);
}

template <typename T1, typename T2, typename sort>
int saveJMap(const std::string& strLoc, std::map<T1, T2, sort> mapTmp)
{
if (mapTmp.empty())return 0;
std::ofstream fout(strLoc);
if (fout)
{
nlohmann::json j;
for (auto it : mapTmp)
{
j[writeJKey<T1>(it.first)] = GBKtoUTF8(it.second);
}
fout << j.dump(2);
fout.close();
}
return 0;
}
28 changes: 28 additions & 0 deletions trpglogger/StrExtern.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
/**
* 字符串辅助函数
* Copyright (C) 2019-2020 String.Empty
*/

#include <string>

using std::string;
using std::wstring;
using std::to_string;

#define CP_GB18030 (54936)

string toString(int num, unsigned short size = 0);

template <typename Dig>
string to_signed_string(Dig num)
{
if (num > 0)return "+" + to_string(num);
return to_string(num);
}

int count_char(const string& s, char ch);

string convert_w2a(const wchar_t* wch);

wstring convert_a2w(const char* ch);
Loading