350 lines
6.1 KiB
C++
350 lines
6.1 KiB
C++
#pragma once
|
|
|
|
using namespace std;
|
|
#include <string>
|
|
|
|
class CJsonLog
|
|
{
|
|
public:
|
|
CJsonLog() : m_pString(NULL) {}
|
|
~CJsonLog() { Clear(); }
|
|
|
|
public:
|
|
void Clear();
|
|
|
|
BOOL Import(wchar_t* pJsonData);
|
|
|
|
INT Export(wchar_t* OUT pReturn);
|
|
string ExportA();
|
|
|
|
INT GetExportSize();
|
|
|
|
INT GetExportLength();
|
|
|
|
void InsertString(const wchar_t* pValueName, wchar_t* pString);
|
|
void InsertListString(const wchar_t* pValueName, wchar_t* pString);
|
|
|
|
void InsertInteger(const wchar_t* pValueName, INT iInteger);
|
|
|
|
void InsertBoolean(const wchar_t* pValueName, BOOL bBoolean);
|
|
inline void AppendJson(const wchar_t* pValueName, Json::Value JsonValue);
|
|
|
|
PWCHAR GetString(const wchar_t* pValueName);
|
|
|
|
INT GetInteger(const wchar_t* pValueName, INT iDefault);
|
|
|
|
BOOL GetBoolean(const wchar_t* pValueName);
|
|
|
|
private:
|
|
BOOL _UnicodeToUTF8(const wchar_t* pSource, string* OUT pReturn);
|
|
INT _UTF8ToUnicode(string* pSource, wchar_t* OUT pReturn);
|
|
|
|
public:
|
|
Json::Value m_jvRoot;
|
|
PWCHAR m_pString;
|
|
};
|
|
|
|
|
|
inline void CJsonLog::Clear()
|
|
{
|
|
m_jvRoot.clear();
|
|
|
|
if (m_pString)
|
|
{
|
|
delete[] m_pString;
|
|
m_pString = NULL;
|
|
}
|
|
}
|
|
|
|
inline BOOL CJsonLog::Import(wchar_t* pJsonData)
|
|
{
|
|
BOOL bResult = FALSE;
|
|
Json::Reader Reader;
|
|
string strJsonData;
|
|
|
|
if (pJsonData)
|
|
{
|
|
m_jvRoot.clear();
|
|
|
|
if (_UnicodeToUTF8(pJsonData, &strJsonData))
|
|
{
|
|
bResult = (BOOL)Reader.parse(strJsonData, m_jvRoot);
|
|
}
|
|
else
|
|
{
|
|
// [DEBUG]
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// [DEBUG]
|
|
}
|
|
|
|
return bResult;
|
|
}
|
|
|
|
inline INT CJsonLog::Export(wchar_t* OUT pReturn)
|
|
{
|
|
INT iResult = -1;
|
|
//Json::StyledWriter Writer;
|
|
Json::FastWriter Writer;
|
|
string strJsonData;
|
|
|
|
if (pReturn)
|
|
{
|
|
strJsonData = Writer.write(m_jvRoot);
|
|
|
|
if (strJsonData.size() > 0)
|
|
{
|
|
iResult = _UTF8ToUnicode(&strJsonData, pReturn);
|
|
|
|
if (iResult <= 0)
|
|
{
|
|
// [DEBUG]
|
|
iResult = -1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// [DEBUG]
|
|
iResult = -1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// [DEBUG]
|
|
}
|
|
|
|
return iResult;
|
|
}
|
|
|
|
|
|
inline string CJsonLog::ExportA()
|
|
{
|
|
//Json::StyledWriter Writer;
|
|
Json::FastWriter Writer;
|
|
string strJsonData;
|
|
|
|
strJsonData = Writer.write(m_jvRoot);
|
|
|
|
return strJsonData;
|
|
}
|
|
|
|
|
|
inline INT CJsonLog::GetExportSize()
|
|
{
|
|
return sizeof(WCHAR) * GetExportLength();
|
|
}
|
|
|
|
inline INT CJsonLog::GetExportLength()
|
|
{
|
|
//Json::StyledWriter Writer;
|
|
string strJsonData;
|
|
Json::FastWriter Writer;
|
|
strJsonData = Writer.write(m_jvRoot);
|
|
//CDS_DEBUGA("strJsonData size(%d)", strJsonData.length());
|
|
return MultiByteToWideChar(CP_UTF8, 0, strJsonData.c_str(), -1, NULL, 0);
|
|
}
|
|
|
|
inline void CJsonLog::InsertString(const wchar_t * pValueName, wchar_t* pString)
|
|
{
|
|
string strValueNameUTF8;
|
|
string strStringUTF8;
|
|
|
|
if (pValueName && pString)
|
|
{
|
|
_UnicodeToUTF8(pValueName, &strValueNameUTF8);
|
|
_UnicodeToUTF8(pString, &strStringUTF8);
|
|
}
|
|
else
|
|
{
|
|
// [DEBUG]
|
|
}
|
|
|
|
m_jvRoot[strValueNameUTF8] = strStringUTF8;
|
|
}
|
|
|
|
inline void CJsonLog::AppendJson(const wchar_t* pValueName, Json::Value JsonValue)
|
|
{
|
|
string strValueNameUTF8;
|
|
string strStringUTF8;
|
|
|
|
|
|
_UnicodeToUTF8(pValueName, &strValueNameUTF8);
|
|
|
|
m_jvRoot[strValueNameUTF8].append(JsonValue);
|
|
}
|
|
|
|
inline void CJsonLog::InsertListString(const wchar_t* pValueName, wchar_t* pString)
|
|
{
|
|
string strValueNameUTF8;
|
|
string strStringUTF8;
|
|
|
|
int iSize = 0;
|
|
|
|
if (pValueName && pString)
|
|
{
|
|
_UnicodeToUTF8(pValueName, &strValueNameUTF8);
|
|
_UnicodeToUTF8(pString, &strStringUTF8);
|
|
}
|
|
else
|
|
{
|
|
// [DEBUG]
|
|
}
|
|
|
|
iSize = m_jvRoot[strValueNameUTF8].size();
|
|
|
|
m_jvRoot[strValueNameUTF8][iSize] = strStringUTF8;
|
|
|
|
}
|
|
|
|
inline void CJsonLog::InsertInteger(const wchar_t* pValueName, INT iInteger)
|
|
{
|
|
string strValueNameUTF8;
|
|
|
|
if (pValueName)
|
|
{
|
|
_UnicodeToUTF8(pValueName, &strValueNameUTF8);
|
|
}
|
|
else
|
|
{
|
|
// [DEBUG]
|
|
}
|
|
|
|
m_jvRoot[strValueNameUTF8] = iInteger;
|
|
}
|
|
|
|
inline void CJsonLog::InsertBoolean(const wchar_t* pValueName, BOOL bBoolean)
|
|
{
|
|
string strValueNameUTF8;
|
|
|
|
if (pValueName)
|
|
{
|
|
_UnicodeToUTF8(pValueName, &strValueNameUTF8);
|
|
}
|
|
else
|
|
{
|
|
// [DEBUG]
|
|
}
|
|
|
|
m_jvRoot[strValueNameUTF8] = bBoolean ? true : false;
|
|
}
|
|
|
|
inline PWCHAR CJsonLog::GetString(const wchar_t* pValueName)
|
|
{
|
|
string strValueNameUTF8;
|
|
string strValue;
|
|
DWORD dwUnicodeSize;
|
|
|
|
if (m_pString)
|
|
{
|
|
delete[] m_pString;
|
|
m_pString = NULL;
|
|
}
|
|
|
|
if (pValueName)
|
|
{
|
|
_UnicodeToUTF8(pValueName, &strValueNameUTF8);
|
|
strValue = m_jvRoot[strValueNameUTF8].asString();
|
|
|
|
dwUnicodeSize = MultiByteToWideChar(CP_UTF8, 0, strValue.c_str(), -1, NULL, 0);
|
|
|
|
m_pString = new WCHAR[dwUnicodeSize + 1];
|
|
|
|
ZeroMemory(m_pString, sizeof(WCHAR) * (dwUnicodeSize + 1));
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, strValue.c_str(), -1, m_pString, dwUnicodeSize);
|
|
}
|
|
|
|
return m_pString;
|
|
}
|
|
|
|
inline INT CJsonLog::GetInteger(const wchar_t* pValueName, INT iDefault)
|
|
{
|
|
string strValueNameUTF8;
|
|
INT iResult = 0;
|
|
|
|
if (pValueName)
|
|
{
|
|
_UnicodeToUTF8(pValueName, &strValueNameUTF8);
|
|
iResult = m_jvRoot[strValueNameUTF8].asInt();
|
|
}
|
|
|
|
return iResult;
|
|
}
|
|
|
|
inline BOOL CJsonLog::GetBoolean(const wchar_t* pValueName)
|
|
{
|
|
string strValueNameUTF8;
|
|
BOOL bResult = FALSE;
|
|
|
|
if (pValueName)
|
|
{
|
|
_UnicodeToUTF8(pValueName, &strValueNameUTF8);
|
|
bResult = m_jvRoot[strValueNameUTF8].asBool();
|
|
}
|
|
|
|
return bResult;
|
|
}
|
|
|
|
inline BOOL CJsonLog::_UnicodeToUTF8(const wchar_t* pSource, string* OUT pReturn)
|
|
{
|
|
BOOL bResult = FALSE;
|
|
PCHAR pUTF8 = NULL;
|
|
INT iUTF8Size = 0;
|
|
|
|
if (pSource && pReturn)
|
|
{
|
|
iUTF8Size = WideCharToMultiByte(CP_UTF8, 0, pSource, -1, NULL, 0, NULL, NULL);
|
|
|
|
if (iUTF8Size > 0)
|
|
{
|
|
pUTF8 = new CHAR[iUTF8Size];
|
|
ZeroMemory(pUTF8, sizeof(CHAR) * iUTF8Size);
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, pSource, -1, pUTF8, iUTF8Size, NULL, NULL);
|
|
|
|
*pReturn = pUTF8;
|
|
|
|
delete[] pUTF8;
|
|
pUTF8 = NULL;
|
|
|
|
bResult = TRUE;
|
|
}
|
|
}
|
|
|
|
return bResult;
|
|
}
|
|
|
|
inline INT CJsonLog::_UTF8ToUnicode(string* pSource, wchar_t* OUT pReturn)
|
|
{
|
|
INT iResult = 0;
|
|
wchar_t* pUnicode = NULL;
|
|
INT iUnicodeSize = 0;
|
|
|
|
if (pSource && pReturn)
|
|
{
|
|
iUnicodeSize = MultiByteToWideChar(CP_UTF8, 0, pSource->c_str(), -1, NULL, 0);
|
|
|
|
if (iUnicodeSize > 0)
|
|
{
|
|
pUnicode = new WCHAR[iUnicodeSize];
|
|
ZeroMemory(pUnicode, sizeof(WCHAR) * iUnicodeSize);
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, pSource->c_str(), -1, pUnicode, iUnicodeSize);
|
|
|
|
wcscpy(pReturn, pUnicode);
|
|
|
|
delete[] pUnicode;
|
|
pUnicode = NULL;
|
|
|
|
iResult = sizeof(WCHAR) * iUnicodeSize;
|
|
}
|
|
}
|
|
|
|
return iResult;
|
|
}
|
|
|
|
|
|
|