Files
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

216 lines
4.5 KiB
C++

//-----------------------------------------------------------------------------+
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
// = FileName : TWDictItem 类
// = Version : ver2.0
// = Author : wlw
// = CreateDate : 2002-09-09
// = Description: TWDictItem 定义
// = Maintainers:
//
//-----------------------------------------------------------------------------+
#include "StdAfx.h"
#include "TWDictItem.h"
#include "TAString.h"
#include "TUString.h"
namespace
{
long getFileSize(FILE *fp)
{
if (!fp) return 0L;
fseek(fp, 0L, SEEK_END);
long fSize = ftell(fp);
fseek(fp, 0L, 0L);
return fSize;
}
std::string unicode2Ansii(LPCTSTR psz, const DWORD & codePage)
{
if (!psz) return std::string("");
char *pBuffer;
int nLength;
int nLen;
nLen = (int)wcslen(psz);
nLength = ::WideCharToMultiByte(codePage, 0, psz, nLen, NULL, 0, 0, 0);
int rc = ::GetLastError();
pBuffer = new char[nLength + 1];
memset(pBuffer, 0, (nLength + 1) * sizeof(char));
::WideCharToMultiByte(codePage, 0, psz, nLen, pBuffer, nLength, 0, 0);
std::string str(pBuffer);
delete[] pBuffer;
return str;
}
std::wstring write2Json(const WValue &v, bool pretty)
{
WStringBuffer buffer;
if (pretty)
{
PrettyWriter<WStringBuffer, UTF16<>, UTF16<>> writer(buffer);
v.Accept(writer);
}
else
{
Writer<WStringBuffer, UTF16<>, UTF16<>> writer(buffer);
v.Accept(writer);
}
return buffer.GetString();
}
bool json2File(LPCTSTR path, const WValue &v, bool pretty)
{
if (!path) return false;
std::wstring ws = write2Json(v, pretty);
if (ws.empty()) return false;
std::string s = unicode2Ansii(ws.c_str(), CP_UTF8);
CFile f;
CFileException e;
if (!f.Open(path, CFile::modeCreate | CFile::modeReadWrite, &e))
{
CString buffer;
e.GetErrorMessage(buffer.GetBuffer(1024), 1024);
AfxMessageBox(buffer);
buffer.ReleaseBuffer();
return false;
}
f.Write((char *)s.c_str(), sizeof(char) * s.size());
f.Close();
return true;
}
bool file2Json(LPCTSTR pszJson, WDocument &doc)
{
bool brel(false);
if (!pszJson) return false;
if (_taccess(pszJson, 0x0) != 0x0) return false;
FILE *fp(NULL);
char *readBuffer(NULL);
do
{
fp = _tfopen(pszJson, _T("rb"));
if (!fp) break;
long fSize = getFileSize(fp);
++fSize;
readBuffer = new char[fSize];
::memset(readBuffer, 0, sizeof(char) * (fSize));
FileReadStream bis(fp, readBuffer, fSize);
AutoUTFInputStream<unsigned, FileReadStream> eis(bis);
doc.ParseStream<0, AutoUTF<unsigned> >(eis);
if (readBuffer) delete[] readBuffer;
if (fp) fclose(fp);
return !doc.HasParseError();
} while (false);
return false;
}
}
WDocument::AllocatorType& getJsonAllocator()
{
static WDocument g_doc;
return g_doc.GetAllocator();
}
TWDictItem::TWDictItem(const TCHAR* sName, const TCHAR* sDesc)
: m_sName(sName), m_sDesc(sDesc)
{
}
TWDictItem::~TWDictItem()
{
}
int TWDictItem::GetType() const
{
return TYPE_WDictItem;
}
//只复制数据
void TWDictItem::CopyData(const TWDictItem&src)
{
}
bool TWDictItem::IsDict() const
{
return GetType() == 0;
}
bool TWDictItem::IsValid()
{
return m_sName != _T("");
}
bool TWDictItem::operator==(const TWDictItem&item) const
{
return m_sName == item.m_sName;
}
bool TWDictItem::operator != (const TWDictItem&item) const
{
return m_sName != item.m_sName;
}
bool TWDictItem::operator < (const TWDictItem&item) const
{
return item > *this;
}
bool TWDictItem::operator > (const TWDictItem &item) const
{
return strcmp(_AS(m_sName),_AS(item.m_sName)) > 0;
}
void TWDictItem::operator <<(CArchive&ar)
{
ar >> m_sName >> m_sDesc; //类型号之前必须读出!
}
void TWDictItem::operator >>(CArchive&ar)
{
ar << GetType() << m_sName << m_sDesc; //先保存类型号,用于动态创建时识别类型
}
void TWDictItem::saveToJson(WValue & v)
{
v.AddMember(StringRef(_T("name")), StringRef((LPCTSTR)m_sName), getJsonAllocator());
v.AddMember(StringRef(_T("desc")), StringRef((LPCTSTR)m_sDesc), getJsonAllocator());
v.AddMember(StringRef(_T("type")), WValue(GetType()), getJsonAllocator());
}
void TWDictItem::loadByJson(const WValue &v)
{
MCIt it = v.FindMember(_T("name"));
if (it != v.MemberEnd())
m_sName = it->value.GetString();
it = v.FindMember(_T("desc"));
if (it != v.MemberEnd())
m_sDesc = it->value.GetString();
}
bool TWDictItem::json2File(LPCTSTR path, const WValue & v, bool pretty)
{
return ::json2File(path, v, pretty);
}
bool TWDictItem::file2Json(LPCTSTR pszJson, WDocument & doc)
{
return ::file2Json(pszJson, doc);
}