1070 lines
17 KiB
C++
1070 lines
17 KiB
C++
#include "StdAfx.h"
|
|
#include "WDictItem.h"
|
|
#include <fstream>
|
|
|
|
using namespace DIMJson;
|
|
|
|
#pragma region WDictItem
|
|
WDictItem::WDictItem(const TCHAR* sName, const TCHAR* sDesc)
|
|
: m_sName(sName), m_sDesc(sDesc)
|
|
{
|
|
|
|
}
|
|
|
|
WDictItem::~WDictItem()
|
|
{
|
|
|
|
}
|
|
|
|
int WDictItem::GetType() const
|
|
{
|
|
return TYPE_WDictItem;
|
|
}
|
|
|
|
//只复制数据
|
|
void WDictItem::CopyData(const WDictItem&src)
|
|
{
|
|
|
|
}
|
|
|
|
bool WDictItem::IsDict() const
|
|
{
|
|
return GetType() == 0;
|
|
}
|
|
|
|
bool WDictItem::IsValid()
|
|
{
|
|
return m_sName != _T("");
|
|
}
|
|
|
|
bool WDictItem::operator==(const WDictItem&item) const
|
|
{
|
|
return m_sName == item.m_sName;
|
|
}
|
|
|
|
bool WDictItem::operator != (const WDictItem&item) const
|
|
{
|
|
return m_sName != item.m_sName;
|
|
}
|
|
|
|
bool WDictItem::operator < (const WDictItem&item) const
|
|
{
|
|
return item > *this;
|
|
}
|
|
|
|
bool WDictItem::operator > (const WDictItem &item) const
|
|
{
|
|
return strcmp(_AS(m_sName),_AS(item.m_sName)) > 0;
|
|
}
|
|
|
|
void WDictItem::operator <<(CArchive&ar)
|
|
{
|
|
ar >> m_sName >> m_sDesc; //类型号之前必须读出!
|
|
}
|
|
|
|
void WDictItem::operator >>(CArchive&ar)
|
|
{
|
|
ar << GetType() << m_sName << m_sDesc; //先保存类型号,用于动态创建时识别类型
|
|
}
|
|
|
|
void WDictItem::saveToJson(WValue & v)
|
|
{
|
|
v.AddMember(StringRef(_T("name")), StringRef((LPCTSTR)m_sName), SHJson::getJsonAllocator());
|
|
v.AddMember(StringRef(_T("desc")), StringRef((LPCTSTR)m_sDesc), SHJson::getJsonAllocator());
|
|
v.AddMember(StringRef(_T("type")), WValue(GetType()), SHJson::getJsonAllocator());
|
|
}
|
|
|
|
void WDictItem::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();
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region WBoolItem
|
|
WBoolItem::~WBoolItem()
|
|
{
|
|
|
|
}
|
|
|
|
int WBoolItem::GetType()const
|
|
{
|
|
return TYPE_WBoolItem;
|
|
}
|
|
|
|
void WBoolItem::operator << (CArchive &ar)
|
|
{
|
|
WDictItem::operator << (ar);
|
|
ar >> m_data;
|
|
}
|
|
|
|
void WBoolItem::operator >> (CArchive &ar)
|
|
{
|
|
WDictItem::operator >> (ar);
|
|
ar << m_data;
|
|
}
|
|
|
|
void WBoolItem::CopyData(const WDictItem &src)
|
|
{
|
|
if (GetType() != src.GetType())
|
|
{
|
|
return;
|
|
}
|
|
|
|
const WBoolItem& si = (const WBoolItem&)src;
|
|
m_data = si.m_data;
|
|
}
|
|
|
|
void WBoolItem::saveToJson(WValue & v)
|
|
{
|
|
WDictItem::saveToJson(v);
|
|
v.AddMember(StringRef(_T("data")), WValue(m_data == FALSE ? false : true), SHJson::getJsonAllocator());
|
|
}
|
|
|
|
void WBoolItem::loadByJson(const WValue &v)
|
|
{
|
|
WDictItem::loadByJson(v);
|
|
MCIt it = v.FindMember(_T("data"));
|
|
if (it != v.MemberEnd())
|
|
m_data = it->value.GetBool() ? TRUE : FALSE;
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region WCharItem
|
|
WCharItem::~WCharItem()
|
|
{
|
|
|
|
}
|
|
|
|
int WCharItem::GetType()const
|
|
{
|
|
return TYPE_WCharItem;
|
|
}
|
|
|
|
void WCharItem::operator <<(CArchive &ar)
|
|
{
|
|
WDictItem::operator << (ar);
|
|
ar >> m_data;
|
|
}
|
|
|
|
void WCharItem::operator >> (CArchive &ar)
|
|
{
|
|
WDictItem::operator >> (ar);
|
|
ar << m_data;
|
|
}
|
|
|
|
void WCharItem::CopyData(const WDictItem &src)
|
|
{
|
|
if (GetType() != src.GetType())
|
|
{
|
|
return;
|
|
}
|
|
|
|
const WCharItem& si = (const WCharItem&)src;
|
|
m_data = si.m_data;
|
|
}
|
|
|
|
void WCharItem::saveToJson(WValue & v)
|
|
{
|
|
WDictItem::saveToJson(v);
|
|
v.AddMember(StringRef(_T("data")), WValue((int)m_data), SHJson::getJsonAllocator());
|
|
}
|
|
|
|
void WCharItem::loadByJson(const WValue &v)
|
|
{
|
|
WDictItem::loadByJson(v);
|
|
MCIt it = v.FindMember(_T("data"));
|
|
if (it != v.MemberEnd())
|
|
m_data = (char)it->value.GetInt();
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region WCStringItem
|
|
WCStringItem::~WCStringItem()
|
|
{
|
|
|
|
}
|
|
|
|
int WCStringItem::GetType()const
|
|
{
|
|
return TYPE_WCStringItem;
|
|
}
|
|
|
|
void WCStringItem::operator << (CArchive &ar)
|
|
{
|
|
WDictItem::operator << (ar);
|
|
ar >> m_data;
|
|
}
|
|
|
|
void WCStringItem::operator >> (CArchive &ar)
|
|
{
|
|
WDictItem::operator >> (ar);
|
|
ar << m_data;
|
|
}
|
|
|
|
void WCStringItem::CopyData(const WDictItem &src)
|
|
{
|
|
if (GetType() != src.GetType())
|
|
{
|
|
return;
|
|
}
|
|
|
|
const WCStringItem& si = (const WCStringItem&)src;
|
|
m_data = si.m_data;
|
|
}
|
|
|
|
void WCStringItem::saveToJson(WValue & v)
|
|
{
|
|
WDictItem::saveToJson(v);
|
|
v.AddMember(StringRef(_T("data")), StringRef((LPCTSTR)m_data), SHJson::getJsonAllocator());
|
|
}
|
|
|
|
void WCStringItem::loadByJson(const WValue &v)
|
|
{
|
|
WDictItem::loadByJson(v);
|
|
MCIt it = v.FindMember(_T("data"));
|
|
if (it != v.MemberEnd())
|
|
m_data = it->value.GetString();
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region WDoubleItem
|
|
WDoubleItem::~WDoubleItem()
|
|
{
|
|
|
|
}
|
|
int WDoubleItem::GetType()const
|
|
{
|
|
return TYPE_WDoubleItem;
|
|
}
|
|
|
|
void WDoubleItem::operator <<(CArchive &ar)
|
|
{
|
|
WDictItem::operator<<(ar);
|
|
ar >> m_data;
|
|
}
|
|
|
|
void WDoubleItem::operator >>(CArchive &ar)
|
|
{
|
|
WDictItem::operator >>(ar);
|
|
ar << m_data;
|
|
}
|
|
|
|
void WDoubleItem::CopyData(const WDictItem &src)
|
|
{
|
|
if (GetType() != src.GetType())
|
|
{
|
|
return;
|
|
}
|
|
|
|
const WDoubleItem& si = (const WDoubleItem&)src;
|
|
m_data = si.m_data;
|
|
}
|
|
|
|
void WDoubleItem::saveToJson(WValue & v)
|
|
{
|
|
WDictItem::saveToJson(v);
|
|
v.AddMember(StringRef(_T("data")), WValue(m_data), SHJson::getJsonAllocator());
|
|
}
|
|
|
|
void WDoubleItem::loadByJson(const WValue &v)
|
|
{
|
|
WDictItem::loadByJson(v);
|
|
MCIt it = v.FindMember(_T("data"));
|
|
if (it != v.MemberEnd())
|
|
m_data = it->value.GetDouble();
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region WLongItem
|
|
WLongItem::~WLongItem()
|
|
{
|
|
|
|
}
|
|
|
|
int WLongItem::GetType()const
|
|
{
|
|
return TYPE_WLongItem;
|
|
}
|
|
|
|
void WLongItem::operator <<(CArchive &ar)
|
|
{
|
|
WDictItem::operator<<(ar);
|
|
ar >> m_data;
|
|
}
|
|
|
|
void WLongItem::operator >>(CArchive &ar)
|
|
{
|
|
WDictItem::operator >>(ar);
|
|
ar << m_data;
|
|
}
|
|
|
|
void WLongItem::CopyData(const WDictItem&src)
|
|
{
|
|
if (GetType() != src.GetType())
|
|
{
|
|
return;
|
|
}
|
|
|
|
const WLongItem& si = (const WLongItem&)src;
|
|
m_data = si.m_data;
|
|
}
|
|
|
|
void WLongItem::saveToJson(WValue & v)
|
|
{
|
|
WDictItem::saveToJson(v);
|
|
v.AddMember(StringRef(_T("data")), WValue((int64_t)m_data), SHJson::getJsonAllocator());
|
|
}
|
|
|
|
void WLongItem::loadByJson(const WValue &v)
|
|
{
|
|
WDictItem::loadByJson(v);
|
|
MCIt it = v.FindMember(_T("data"));
|
|
if (it != v.MemberEnd())
|
|
m_data = it->value.GetInt();
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region WSelectItem
|
|
WSelectItem::~WSelectItem()
|
|
{
|
|
|
|
}
|
|
|
|
CString WSelectItem::GetSelString()const
|
|
{
|
|
return GetAt(m_data);
|
|
}
|
|
|
|
void WSelectItem::operator <<(CArchive &ar)
|
|
{
|
|
WDictItem::operator << (ar);
|
|
|
|
long len;
|
|
ar >> len;
|
|
|
|
Clear();
|
|
|
|
for (long i = 0; i < len; i++)
|
|
{
|
|
ar >> (*this)[i];
|
|
}
|
|
|
|
ar >> m_data;
|
|
}
|
|
|
|
void WSelectItem::operator >>(CArchive &ar)
|
|
{
|
|
WDictItem::operator >>(ar);
|
|
|
|
ar << GetLength();
|
|
for (MoveToFirst(); !IsOut(); MoveToNext())
|
|
{
|
|
ar << GetCurData();
|
|
}
|
|
|
|
ar << m_data;
|
|
}
|
|
|
|
void WSelectItem::CopyData(const WDictItem&src)
|
|
{
|
|
if (GetType() != src.GetType())
|
|
{
|
|
return;
|
|
}
|
|
|
|
const WSelectItem& si = (const WSelectItem&)src;
|
|
if (!si.IsValidIndex(si.m_data))
|
|
{
|
|
return;
|
|
}
|
|
|
|
CString sData = *(si.GetAt(si.m_data));
|
|
for (MoveToFirst(); !IsOut(); MoveToNext())
|
|
{
|
|
if (sData == *GetCurData())
|
|
{
|
|
m_data = GetCur();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int WSelectItem::GetType()const
|
|
{
|
|
return TYPE_WSelectItem;
|
|
}
|
|
|
|
void WSelectItem::saveToJson(WValue & v)
|
|
{
|
|
WDictItem::saveToJson(v);
|
|
v.AddMember(StringRef(_T("data")), WValue(m_data), SHJson::getJsonAllocator());
|
|
|
|
WValue arr;
|
|
arr.SetArray();
|
|
|
|
for (MoveToFirst(); !IsOut(); MoveToNext())
|
|
{
|
|
CString &s(GetCurData());
|
|
arr.PushBack(StringRef((LPCTSTR)s), SHJson::getJsonAllocator());
|
|
}
|
|
|
|
v.AddMember(StringRef(_T("datas")), arr, SHJson::getJsonAllocator());
|
|
}
|
|
|
|
void WSelectItem::loadByJson(const WValue &v)
|
|
{
|
|
WDictItem::loadByJson(v);
|
|
|
|
MCIt it = v.FindMember(_T("data"));
|
|
if (it != v.MemberEnd())
|
|
m_data = it->value.GetInt();
|
|
|
|
Clear();
|
|
|
|
it = v.FindMember(_T("datas"));
|
|
if (it != v.MemberEnd() && it->value.IsArray())
|
|
{
|
|
CArr ar = it->value.GetArray();
|
|
for (size_t i = 0; i < ar.Size(); ++i)
|
|
{
|
|
(*this)[i] = ar[i].GetString();
|
|
}
|
|
}
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region WDictPath
|
|
WDictPath::~WDictPath()
|
|
{
|
|
|
|
}
|
|
|
|
WDictPath::WDictPath(const CString&sPath)
|
|
{
|
|
for (int iFrom = 0, iTo = -1; (iTo = sPath.Find(_T('\\'), iFrom)) != -1; iFrom = iTo + 1)
|
|
{
|
|
AddTail(new WDict(sPath.Mid(iFrom, iTo - iFrom)));
|
|
}
|
|
|
|
AddTail(new WDictItem(sPath.Right(sPath.GetLength() - iFrom)));//最后一层
|
|
}
|
|
|
|
bool WDictPath::IsValid()
|
|
{
|
|
if (IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (MoveToFirst(); !IsOut(); MoveToNext())
|
|
{
|
|
if (!GetCurData()->IsValid())
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region WDict
|
|
WDict::WDict(const TCHAR* sName, const TCHAR *sDesc)
|
|
: WDictItem(sName, sDesc)
|
|
{
|
|
|
|
}
|
|
|
|
WDict::~WDict()
|
|
{
|
|
|
|
}
|
|
|
|
int WDict::GetType()const
|
|
{
|
|
return TYPE_WDict;
|
|
}
|
|
|
|
WDictItem* WDict::FindItem(const CString &sPath)
|
|
{
|
|
WDictPath p(sPath);
|
|
return FindItem(p);
|
|
}
|
|
|
|
//无论是否添加成功,GetCurPData()==*pNewItem!
|
|
bool WDict::AppendItem(WDictItem*pNewItem)
|
|
{
|
|
if (!pNewItem->IsValid())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return SortIn(pNewItem);//二分法查找添加
|
|
}
|
|
|
|
bool WDict::AppendItem(const CString &sPath, WDictItem *pNewItem)
|
|
{
|
|
WDictItem *pItem = FindItem(sPath);
|
|
if (pItem == NULL || !pItem->IsDict())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WDict*pDict = (WDict*)pItem;
|
|
return pDict->AppendItem(pNewItem);
|
|
}
|
|
|
|
WDictItem* WDict::FindItem(WDictPath &path)
|
|
{
|
|
if (path.IsEmpty())
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
if (BFindDataTo(path(0))) //二分法查找
|
|
{
|
|
WDictItem* pItem = GetCurData();
|
|
if (path.GetLength() == 1)
|
|
{
|
|
return pItem;
|
|
}
|
|
else if (pItem->IsDict()) //继续在子字典中找
|
|
{
|
|
WDict* pDict = (WDict*)pItem;
|
|
path.DelFirst();
|
|
|
|
return pDict->FindItem(path);
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
WDict* WDict::FindOrAddDict(WDictPath&path)
|
|
{
|
|
if (path.IsEmpty())
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
WDict*pDict = new WDict(path(0).m_sName);
|
|
if (!AppendItem(pDict)) //是否已经存在
|
|
{
|
|
delete pDict;
|
|
if (GetCurData()->IsDict())
|
|
{
|
|
pDict = (WDict*)GetCurData();
|
|
}
|
|
else //非字典项占用
|
|
{
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
path.DelFirst();
|
|
if (path.IsEmpty()) //直接子字典
|
|
{
|
|
return pDict;
|
|
}
|
|
else //继续查找
|
|
{
|
|
return pDict->FindOrAddDict(path);
|
|
}
|
|
}
|
|
|
|
WDict* WDict::FindOrAddDict(const CString &sPath)
|
|
{
|
|
WDictPath path(sPath);
|
|
if (!path.IsValid())
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
return FindOrAddDict(path);
|
|
}
|
|
|
|
WDictItem* WDict::FindOrAddItem(const CString &sPath, int type)
|
|
{
|
|
WDictPath path(sPath);
|
|
if (!path.IsValid())
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
if (path.GetLength() == 1) //无中间层
|
|
{
|
|
WDictItem *pItem = DynCreateItem(type);
|
|
if (pItem == NULL) //无法识别的数据类型!
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
pItem->m_sName = path(0).m_sName;
|
|
if (!AppendItem(pItem)) //已经存在
|
|
{
|
|
delete pItem;
|
|
}
|
|
|
|
return GetCurData();
|
|
}
|
|
else //需要中间层
|
|
{
|
|
CString sLast = path(path.GetLastIndex()).m_sName;
|
|
path.DelTail(); //成为子字典路径
|
|
|
|
WDict *pDict = FindOrAddDict(path);
|
|
if (pDict == NULL) //中间层建立失败
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
WDictItem *pItem = DynCreateItem(type);
|
|
if (pItem == NULL) //无法识别的数据类型!
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
pItem->m_sName = sLast;
|
|
if (!pDict->AppendItem(pItem)) //已经存在
|
|
{
|
|
delete pItem;
|
|
}
|
|
|
|
return pDict->GetCurData();
|
|
}
|
|
}
|
|
|
|
bool WDict::SetValue(const CString &sPath, bool value)
|
|
{
|
|
WDictItem *pItem = FindOrAddItem(sPath, 1);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WBoolItem *pItem1 = (WBoolItem*)pItem;
|
|
pItem1->m_data = value;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::SetValue(const CString &sPath, char value)
|
|
{
|
|
WDictItem *pItem = FindOrAddItem(sPath, 2);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WCharItem *pItem1 = (WCharItem*)pItem;
|
|
pItem1->m_data = value;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::SetValue(const CString &sPath, int value)
|
|
{
|
|
WDictItem *pItem = FindOrAddItem(sPath, 3);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WLongItem *pItem1 = (WLongItem*)pItem;
|
|
pItem1->m_data = value;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::SetValue(const CString &sPath, long value)
|
|
{
|
|
WDictItem *pItem = FindOrAddItem(sPath, 3);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WLongItem *pItem1 = (WLongItem*)pItem;
|
|
pItem1->m_data = value;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::SetValue(const CString &sPath, double value)
|
|
{
|
|
WDictItem *pItem = FindOrAddItem(sPath, 4);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WDoubleItem *pItem1 = (WDoubleItem*)pItem;
|
|
pItem1->m_data = value;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::SetValue(const CString &sPath, const CString& value)
|
|
{
|
|
WDictItem *pItem = FindOrAddItem(sPath, 5);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WCStringItem *pItem1 = (WCStringItem*)pItem;
|
|
pItem1->m_data = value;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::SetValue(const CString &sPath, unsigned int value)
|
|
{
|
|
WDictItem *pItem = FindOrAddItem(sPath, 4);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WSelectItem *pItem1 = (WSelectItem*)pItem;
|
|
if (pItem1->IsValidIndex(value))
|
|
{
|
|
pItem1->m_data = value;
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool WDict::GetValue(const CString &sPath, bool &value)
|
|
{
|
|
WDictItem *pItem = FindItem(sPath);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WBoolItem *pItem1 = (WBoolItem*)pItem;
|
|
value = pItem1->m_data == TRUE;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::GetValue(const CString &sPath, char &value)
|
|
{
|
|
WDictItem *pItem = FindItem(sPath);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WCharItem *pItem1 = (WCharItem*)pItem;
|
|
value = pItem1->m_data;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::GetValue(const CString &sPath, long &value)
|
|
{
|
|
WDictItem *pItem = FindItem(sPath);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WLongItem *pItem1 = (WLongItem*)pItem;
|
|
value = pItem1->m_data;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::GetValue(const CString &sPath, int &value)
|
|
{
|
|
WDictItem *pItem = FindItem(sPath);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WLongItem *pItem1 = (WLongItem*)pItem;
|
|
value = pItem1->m_data;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::GetValue(const CString &sPath, double &value)
|
|
{
|
|
WDictItem *pItem = FindItem(sPath);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WDoubleItem *pItem1 = (WDoubleItem*)pItem;
|
|
value = pItem1->m_data;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::GetValue(const CString &sPath, CString &value)
|
|
{
|
|
WDictItem *pItem = FindItem(sPath);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WCStringItem *pItem1 = (WCStringItem*)pItem;
|
|
value = pItem1->m_data;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::GetCombValue(const CString & sPath, int & value)
|
|
{
|
|
WDictItem *pItem = FindItem(sPath);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WSelectItem *pItem1 = (WSelectItem*)pItem;
|
|
value = pItem1->m_data;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::GetValue(const CString &sPath, unsigned int &value)
|
|
{
|
|
WDictItem *pItem = FindItem(sPath);
|
|
if (pItem == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
WSelectItem *pItem1 = (WSelectItem*)pItem;
|
|
value = pItem1->m_data;
|
|
|
|
return true;
|
|
}
|
|
|
|
void WDict::operator>>(CArchive &ar)
|
|
{
|
|
WDictItem::operator >> (ar);
|
|
ar << GetLength();
|
|
|
|
for (MoveToFirst(); !IsOut(); MoveToNext())
|
|
{
|
|
GetCurPData() >> ar;
|
|
}
|
|
}
|
|
|
|
void WDict::operator << (CArchive &ar)
|
|
{
|
|
WDictItem::operator << (ar);
|
|
|
|
long len;
|
|
ar >> len;
|
|
|
|
Clear();
|
|
for (long i = 0; i < len; i++)
|
|
{
|
|
int type;
|
|
ar >> type;
|
|
|
|
WDictItem *pItem = DynCreateItem(type);
|
|
if (pItem == NULL)
|
|
{
|
|
break; //无法识别的数据类型!
|
|
}
|
|
else
|
|
{
|
|
*pItem << ar;
|
|
AddTail(pItem);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool WDict::ReadFile(const TCHAR *sFile)
|
|
{
|
|
CString strFile(sFile);
|
|
strFile.Replace(_T(".ini"), _T(".json"));
|
|
|
|
WDocument doc;
|
|
doc.SetObject();
|
|
DIMJson::file2Json(strFile, doc);
|
|
|
|
loadByJson(doc);
|
|
/*CFile file;
|
|
if (!file.Open(sFile,CFile::modeRead))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
CArchive ar(&file, CArchive::load);
|
|
int type;
|
|
ar >> type;
|
|
(*this) << ar;
|
|
ar.Flush();
|
|
|
|
file.Close();*/
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::WriteFile(const TCHAR *sFile)
|
|
{
|
|
/*
|
|
CFile file;
|
|
if (!file.Open(sFile,CFile::modeCreate | CFile::modeWrite))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
file.SetLength(0);
|
|
file.Flush();
|
|
|
|
CArchive ar(&file,CArchive::store);
|
|
(*this) >> ar;
|
|
ar.Flush();
|
|
|
|
file.Close();
|
|
*/
|
|
|
|
CString strFile(sFile);
|
|
strFile.Replace(_T(".ini"), _T(".json"));
|
|
|
|
WValue v;
|
|
v.SetObject();
|
|
saveToJson(v);
|
|
DIMJson::json2File(strFile, v, true, false);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WDict::ReadByDWG()
|
|
{
|
|
CString str = GlobalFunc::getOptionString();
|
|
CString path = GlobalFunc::getSystemTemporaryFolder() + _T("\\dimconfig.json");
|
|
CFile ff(path, CFile::modeCreate | CFile::modeWrite);
|
|
if (ff.m_hFile)
|
|
{
|
|
ff.Write((LPCTSTR)str, str.GetLength() * sizeof(TCHAR));
|
|
ff.Close();
|
|
}
|
|
return ReadFile(path);
|
|
}
|
|
|
|
bool WDict::WriteToDWG()
|
|
{
|
|
//static int g_version = 1;
|
|
// 添加了管线标注的精度
|
|
static int g_version = 2;
|
|
WValue v(kObjectType);
|
|
SHJson::addMember(v, _T("version"), g_version);
|
|
saveToJson(v);
|
|
return GlobalFunc::setOptionString(v);
|
|
}
|
|
|
|
void WDict::CopyData(const WDictItem&src)
|
|
{
|
|
if (GetType() != src.GetType())
|
|
{
|
|
return;
|
|
}
|
|
|
|
const WDict& sd = (const WDict&)src;
|
|
for (long i = 0; i < sd.GetLength(); i++)
|
|
{
|
|
const WDictItem *pItem0 = sd.GetAt(i);
|
|
|
|
for (MoveToFirst(); !IsOut(); MoveToNext())
|
|
{
|
|
WDictItem *pItem = GetCurData();
|
|
if (pItem->m_sName == pItem0->m_sName)
|
|
{
|
|
pItem->CopyData(*pItem0);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
WDictItem* WDict::DynCreateItem(int type)
|
|
{
|
|
switch (type)
|
|
{
|
|
case TYPE_WDictItem:
|
|
return new WDictItem;
|
|
case TYPE_WDict:
|
|
return new WDict;
|
|
case TYPE_WBoolItem:
|
|
return new WBoolItem;
|
|
case TYPE_WCharItem:
|
|
return new WCharItem;
|
|
case TYPE_WLongItem:
|
|
return new WLongItem;
|
|
case TYPE_WDoubleItem:
|
|
return new WDoubleItem;
|
|
case TYPE_WCStringItem:
|
|
return new WCStringItem;
|
|
case TYPE_WSelectItem:
|
|
return new WSelectItem;
|
|
default:
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
void WDict::saveToJson(WValue & v)
|
|
{
|
|
WDictItem::saveToJson(v);
|
|
|
|
WValue arr;
|
|
arr.SetArray();
|
|
for (MoveToFirst(); !IsOut(); MoveToNext())
|
|
{
|
|
WDictItem &Item = GetCurPData();
|
|
WValue vv;
|
|
vv.SetObject();
|
|
Item.saveToJson(vv);
|
|
arr.PushBack(vv, SHJson::getJsonAllocator());
|
|
}
|
|
v.AddMember(StringRef(_T("datas")), arr, SHJson::getJsonAllocator());
|
|
}
|
|
|
|
void WDict::loadByJson(const WValue &v)
|
|
{
|
|
WDictItem::loadByJson(v);
|
|
|
|
Clear();
|
|
|
|
int type(-1);
|
|
MCIt it = v.FindMember(_T("datas"));
|
|
if (it != v.MemberEnd() && it->value.IsArray())
|
|
{
|
|
CArr ar = it->value.GetArray();
|
|
for (size_t i = 0; i < ar.Size(); ++i)
|
|
{
|
|
const WValue &vv = ar[i];
|
|
MCIt it = vv.FindMember(_T("type"));
|
|
if (it != vv.MemberEnd())
|
|
{
|
|
type = it->value.GetInt();
|
|
WDictItem *pItem = DynCreateItem(type);
|
|
if (pItem)
|
|
{
|
|
pItem->loadByJson(vv);
|
|
AddTail(pItem);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#pragma endregion
|