ResDataObject.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  1. //#include "StdAfx.h"
  2. #define BOOST_SPIRIT_THREADSAFE
  3. #include "ResDataObject.h"
  4. #include <filesystem>
  5. #include <boost/property_tree/ptree.hpp>
  6. #include <boost/property_tree/xml_parser.hpp>
  7. #include <boost/property_tree/json_parser.hpp>
  8. #include <boost/property_tree/ini_parser.hpp>
  9. #include <boost/typeof/typeof.hpp>
  10. #include <boost/exception/all.hpp>
  11. #include <boost/foreach.hpp>
  12. #include "CommonFun.h"
  13. #define CRTDBG_MAP_ALLOC
  14. #include <stdlib.h>
  15. #include <fstream>
  16. //#include <crtdbg.h>
  17. #include <locale>
  18. #include <codecvt>
  19. #include <iostream>
  20. using namespace boost::property_tree;
  21. //general data object--------------------------------------
  22. std::string wc2mb( const wchar_t* wcstr)
  23. {
  24. std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
  25. std::string strVal = conv.to_bytes(wcstr); // 将宽字符字符串转换为UTF-8编码的多字节字符串
  26. return strVal;
  27. }
  28. std::wstring mb2wc(const char* mbstr)
  29. {
  30. std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
  31. std::wstring strVal = conv.from_bytes(mbstr); // 将多字节字符串转换为宽字符字符串
  32. return strVal;
  33. }
  34. //
  35. //
  36. //
  37. //std::string tostring(const wchar_t *pKey)
  38. //{
  39. // std::string temp;
  40. // temp = wc2mb(pKey);
  41. //
  42. // return temp;
  43. //
  44. //}
  45. //
  46. //
  47. //std::string tostring(const char *pKey)
  48. //{
  49. // std::string temp;
  50. // temp = pKey;
  51. //
  52. // return temp;
  53. //
  54. //}
  55. //
  56. //std::wstring towstring(const wchar_t *pKey)
  57. //{
  58. // std::wstring temp;
  59. // temp = pKey;
  60. //
  61. // return temp;
  62. //}
  63. //
  64. //std::wstring towstring(const char *pKey)
  65. //{
  66. // std::wstring temp;
  67. // temp = mb2wc(pKey);
  68. //
  69. // return temp;
  70. //}
  71. //----------------------------------------------------------------------------------------------
  72. template <class Type> bool StrToIntegerT(const char * str, Type *result)
  73. {
  74. Type value = 0;
  75. Type sign = 1;
  76. Type radix;
  77. if (str == NULL)
  78. {
  79. return false;
  80. }
  81. if (strlen(str) == 0)
  82. {
  83. return false;
  84. }
  85. if (*str == '-')
  86. {
  87. sign = -1;
  88. str++;
  89. }
  90. if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X'))
  91. {
  92. radix = 16;
  93. str += 2;
  94. }
  95. //else if(*str == '0')
  96. //{
  97. // radix = 8;
  98. // str++;
  99. //}
  100. else
  101. {
  102. radix = 10;
  103. }
  104. while (*str)
  105. {
  106. if (radix == 16)
  107. {
  108. if (*str >= '0' && *str <= '9')
  109. {
  110. value = value * radix + *str - '0';
  111. }
  112. else
  113. {
  114. if ((*str | 0x20) >= 'a' && (*str | 0x20) <= 'f')
  115. {
  116. value = value * radix + (*str | 0x20) - 'a' + 10;
  117. }
  118. else
  119. {
  120. return false;
  121. }
  122. }
  123. }
  124. else
  125. {
  126. value = value * radix + *str - '0';
  127. }
  128. str++;
  129. }
  130. *result = sign*value;
  131. return true;
  132. };
  133. bool PrintBoostPtObject(ptree &pt,const char *pParentKey)
  134. {
  135. for (boost::property_tree::ptree::iterator it = pt.begin(); it != pt.end(); it++) {
  136. boost::property_tree::ptree subPt = it->second;
  137. size_t Count = subPt.size();
  138. ResDataObject subObject;
  139. if (Count == 0)
  140. {
  141. //if (pParentKey)
  142. //{
  143. // printf("parent[%s]:it[%s]:sub[%s]\n", pParentKey, it->first.c_str(), subPt.data().c_str());
  144. //}
  145. //else
  146. //{
  147. // printf("parent[NULL]:it[%s]:sub[%s]\n", it->first.c_str(), subPt.data().c_str());
  148. //}
  149. continue;
  150. }
  151. //we got more sub pts
  152. if (PrintBoostPtObject(subPt,it->first.c_str()) == false)
  153. {
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. bool ParsePtToDataObject(ResDataObject &obj,ptree &pt)
  160. {
  161. for (boost::property_tree::ptree::iterator it = pt.begin(); it!=pt.end(); it++) {
  162. boost::property_tree::ptree subPt = it->second;
  163. size_t Count = subPt.size();
  164. ResDataObject subObject;
  165. if(Count == 0)
  166. {
  167. subObject = (subPt.data().c_str());
  168. if (obj.add(it->first.c_str(), subObject) == false)
  169. {
  170. return false;
  171. }
  172. continue;
  173. }
  174. //we got more sub pts
  175. if(ParsePtToDataObject(subObject,subPt) == false)
  176. {
  177. return false;
  178. }
  179. if (obj.add(it->first.c_str(), subObject) == false)
  180. {
  181. return false;
  182. }
  183. }
  184. return true;
  185. }
  186. bool ParseDataObjectToPt(ptree& pt, ResDataObject& obj) {
  187. auto recursive_parse = [&](auto& self, ptree& current_pt, ResDataObject& current_obj, int depth) -> bool {
  188. // 增强的空对象检查
  189. if (!current_obj.IsObject() || current_obj.size() == 0) {
  190. return true;
  191. }
  192. if (depth > 100) { // 降低深度限制到更安全的100层
  193. std::cerr << "Recursion depth limit exceeded (100)" << std::endl;
  194. return false;
  195. }
  196. const size_t objSize = current_obj.size();
  197. for (size_t i = 0; i < objSize; ++i) {
  198. // 双重索引验证
  199. if (i >= objSize || i >= current_obj.size()) {
  200. std::cerr << "Index " << i << " out of range (size: "
  201. << current_obj.size() << ")" << std::endl;
  202. return false;
  203. }
  204. try {
  205. const char* key = current_obj.GetKey(i);
  206. if (!key || *key == '\0') {
  207. std::cerr << "Depth " << depth << ": Invalid key at index " << i
  208. << " (size: " << current_obj.size() << ")" << std::endl;
  209. // 跳过无效键而不是中止整个解析
  210. continue;
  211. }
  212. ResDataObject& child = current_obj[i];
  213. // 更健壮的类型检查
  214. if (child.IsObject() && child.size() > 0) {
  215. ptree subpt;
  216. if (!self(self, subpt, child, depth + 1)) {
  217. std::cerr << "Depth " << depth << ": Failed parsing nested object '"
  218. << key << "' (size: " << child.size() << ")" << std::endl;
  219. return false;
  220. }
  221. current_pt.add_child(key, subpt);
  222. }
  223. else {
  224. try {
  225. // 安全的类型转换
  226. const char* value = static_cast<const char*>(child);
  227. current_pt.put(key, value ? value : "");
  228. }
  229. catch (...) {
  230. current_pt.put(key, "");
  231. }
  232. }
  233. }
  234. catch (const ResDataObjectExption& e) {
  235. std::cerr << "Depth " << depth << ": Exception at index " << i
  236. << ": " << e.what() << std::endl;
  237. return false;
  238. }
  239. catch (const std::exception& e) {
  240. std::cerr << "Depth " << depth << ": Std exception at index " << i
  241. << ": " << e.what() << std::endl;
  242. return false;
  243. }
  244. catch (...) {
  245. std::cerr << "Depth " << depth << ": Unknown exception at index " << i << std::endl;
  246. return false;
  247. }
  248. }
  249. return true;
  250. };
  251. // 初始调用添加保护
  252. try {
  253. return recursive_parse(recursive_parse, pt, obj, 0);
  254. }
  255. catch (...) {
  256. std::cerr << "Top-level exception in ParseDataObjectToPt" << std::endl;
  257. return false;
  258. }
  259. }
  260. ResDataObjectExption::ResDataObjectExption(const char *pExp) : m_ExpContext(std::make_unique<std::string>(pExp ? pExp : "")) {}
  261. ResDataObjectExption::ResDataObjectExption(const ResDataObjectExption &tValue) : m_ExpContext(std::make_unique<std::string>(*tValue.m_ExpContext)) {}
  262. ResDataObjectExption::~ResDataObjectExption(void) = default;
  263. const char *ResDataObjectExption::what() const
  264. {
  265. if(m_ExpContext->size() == 0)
  266. {
  267. (*m_ExpContext) = " ";
  268. }
  269. return m_ExpContext->c_str();
  270. }
  271. ResDataObjectExption& ResDataObjectExption::operator = (const ResDataObjectExption &tValue)
  272. {
  273. if (this != &tValue)
  274. {
  275. (*m_ExpContext) = (*(tValue.m_ExpContext));
  276. }
  277. return (*this);
  278. }
  279. ResDataObject_Pair::ResDataObject_Pair() :
  280. first(std::make_unique<std::string>()),
  281. second(std::make_unique<ResDataObject>()) {}
  282. ResDataObject_Pair::ResDataObject_Pair(const ResDataObject_Pair& other)
  283. : first(other.first ? std::make_unique<std::string>(*other.first)
  284. : std::make_unique<std::string>()),
  285. second(other.second ? std::make_unique<ResDataObject>(*other.second)
  286. : std::make_unique<ResDataObject>())
  287. {
  288. }
  289. ResDataObject_Pair& ResDataObject_Pair::operator=(const ResDataObject_Pair& other)
  290. {
  291. if (this != &other) {
  292. *first = *other.first;
  293. *second = *other.second;
  294. }
  295. return *this;
  296. }
  297. ResDataObject::ResDataObject(void) :
  298. m_encode(std::make_unique<std::string>()),
  299. m_value(std::make_unique<std::string>()),
  300. m_vec(std::make_unique<std::vector<ResDataObject_Pair>>()) {}
  301. ResDataObject::ResDataObject(const ResDataObject& tValue)
  302. : m_encode(std::make_unique<std::string>(*tValue.m_encode)),
  303. m_value(std::make_unique<std::string>(*tValue.m_value)),
  304. m_vec(std::make_unique<std::vector<ResDataObject_Pair>>())
  305. {
  306. m_vec->reserve(tValue.m_vec->size());
  307. for (const auto& pair : *tValue.m_vec) {
  308. m_vec->push_back(pair);
  309. }
  310. //std::cerr << "[ResDataObject] Copied to " << this
  311. //<< ", m_vec=" << m_vec.get() << std::endl;
  312. }
  313. ResDataObject::~ResDataObject(void)
  314. {
  315. }
  316. //commons
  317. void ResDataObject::clear()
  318. {
  319. m_encode->clear();
  320. m_value->clear();
  321. m_vec->clear();
  322. }
  323. size_t ResDataObject::size() {
  324. return m_vec->size();
  325. }
  326. bool ResDataObject::IsObject()
  327. {
  328. return !m_vec->empty();
  329. }
  330. const char *ResDataObject::encode()
  331. {
  332. if (m_vec->empty() && !m_value->empty()) {
  333. return m_value->c_str();
  334. }
  335. boost::property_tree::ptree pt;
  336. if(ParseDataObjectToPt(pt,(*this)) == true)
  337. {
  338. std::stringstream strm;
  339. json_parser::write_json(strm, pt, false);
  340. (*m_encode) = strm.str();
  341. return m_encode->c_str();
  342. }
  343. throw ResDataObjectExption("encode failed. ");
  344. }
  345. bool ResDataObject::decode( const char *pdata )
  346. {
  347. bool IsJsonStr = false;
  348. size_t i = 0;
  349. while (pdata[i] != 0)
  350. {
  351. if (pdata[i] == ':')
  352. {
  353. IsJsonStr = true;
  354. break;
  355. }
  356. ++i;
  357. }
  358. if (pdata && strlen(pdata) > 0)
  359. {
  360. if (IsJsonStr)
  361. {
  362. std::stringstream strm;
  363. strm << pdata;
  364. ptree pt;
  365. try
  366. {
  367. json_parser::read_json(strm, pt);
  368. clear();
  369. return ParsePtToDataObject(*this, pt);
  370. }
  371. catch (...)
  372. {
  373. throw ResDataObjectExption("decode failed. ");
  374. return false;
  375. }
  376. }
  377. else
  378. {
  379. (*this) = pdata;
  380. }
  381. }
  382. else
  383. {
  384. (*this) = "";
  385. }
  386. return true;
  387. }
  388. bool ResDataObject::DumpJsonFile(const char *pfileName)
  389. {
  390. if (pfileName == 0)
  391. {
  392. throw ResDataObjectExption("open file failed");
  393. return false;
  394. }
  395. ptree pt;
  396. std::string fileName = (pfileName);
  397. if (fileName.size() == 0)
  398. {
  399. throw ResDataObjectExption("file name is empty");
  400. return false;
  401. }
  402. try
  403. {
  404. std::string filenameLow = fileName;
  405. std::transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(),
  406. [](unsigned char c) { return std::tolower(c); });
  407. static const basic_string <char>::size_type npos = -1;
  408. basic_string <char>::size_type foundini = filenameLow.rfind(".ini");
  409. basic_string <char>::size_type foundxml = filenameLow.rfind(".xml");
  410. basic_string <char>::size_type foundjson = filenameLow.rfind(".json");
  411. if (foundxml != npos && ((foundxml + strlen(".xml")) == filenameLow.size()))
  412. {
  413. //found xml file
  414. //xml_parser::read_xml(fileName,pt,xml_parser::trim_whitespace|xml_parser::no_comments);
  415. xml_parser::read_xml(fileName, pt, xml_parser::no_comments);
  416. }
  417. else if (foundjson != npos && ((foundjson + strlen(".json")) == filenameLow.size()))
  418. {
  419. //found json file
  420. json_parser::read_json(fileName, pt);
  421. }
  422. else if (foundini != npos && ((foundini + strlen(".ini")) == filenameLow.size()))
  423. {
  424. //found ini file
  425. ini_parser::read_ini(fileName, pt);
  426. }
  427. else
  428. {
  429. throw ResDataObjectExption("open file failed. file extention is not clear");
  430. return false;
  431. }
  432. //got pt
  433. clear();
  434. return PrintBoostPtObject(pt, 0);
  435. }
  436. catch (...)
  437. {
  438. throw ResDataObjectExption("open file failed");
  439. return false;
  440. }
  441. return true;
  442. }
  443. bool ResDataObject::loadFile( const char *pfileName )
  444. {
  445. if (!pfileName) {
  446. throw ResDataObjectExption("Null filename pointer");
  447. return false;
  448. }
  449. namespace fs = std::filesystem;
  450. const fs::path filePath(pfileName);
  451. // 验证文件存在且可读
  452. if (!fs::exists(filePath)) {
  453. throw ResDataObjectExption(("File does not exist: " + filePath.string()).c_str());
  454. }
  455. if (!fs::is_regular_file(filePath)) {
  456. throw ResDataObjectExption(("Not a regular file: " + filePath.string()).c_str());
  457. }
  458. const uintmax_t fileSize = fs::file_size(filePath);
  459. if (fileSize == 0) {
  460. throw ResDataObjectExption(("Empty file: " + filePath.string()).c_str());
  461. }
  462. const uintmax_t MAX_FILE_SIZE = 100 * 1024 * 1024; // 100MB
  463. if (fileSize > MAX_FILE_SIZE) {
  464. throw ResDataObjectExption(("File too large: " + filePath.string()).c_str());
  465. }
  466. ptree pt;
  467. try
  468. {
  469. std::string fileName(pfileName);
  470. std::string filenameLow = fileName;
  471. std::transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(),
  472. [](unsigned char c) { return std::tolower(c); });
  473. static const std::string::size_type npos = -1;
  474. std::string::size_type foundxml = filenameLow.rfind(".xml");
  475. std::string::size_type foundjson = filenameLow.rfind(".json");
  476. std::string::size_type foundini = filenameLow.rfind(".ini");
  477. auto throwError = [](const std::string& msg) {
  478. throw ResDataObjectExption(msg.c_str());
  479. };
  480. if (foundxml != npos && ((foundxml + 4) == filenameLow.size()))
  481. {
  482. // XML 文件处理 - 修正了标志问题
  483. const int flags =
  484. boost::property_tree::xml_parser::no_comments |
  485. boost::property_tree::xml_parser::trim_whitespace;
  486. // 使用文件流代替直接路径
  487. std::ifstream ifs(fileName, std::ios::binary);
  488. if (!ifs) {
  489. throwError("Cannot open file: " + fileName);
  490. }
  491. // 读取文件内容
  492. std::vector<char> buffer;
  493. buffer.reserve(fileSize + 1); // 预分配空间
  494. buffer.assign(std::istreambuf_iterator<char>(ifs),
  495. std::istreambuf_iterator<char>());
  496. buffer.push_back('\0');
  497. // 使用字符串流解析
  498. std::istringstream iss(buffer.data());
  499. boost::property_tree::read_xml(iss, pt, flags);
  500. }
  501. else if(foundjson != npos && ((foundjson+strlen(".json")) == filenameLow.size()))
  502. {
  503. // JSON 文件处理
  504. std::ifstream ifs(fileName);
  505. if (!ifs) {
  506. throwError("Cannot open file: " + fileName);
  507. }
  508. try {
  509. boost::property_tree::read_json(ifs, pt);
  510. }
  511. catch (const boost::property_tree::json_parser_error& e) {
  512. throwError(std::string("JSON parse error: ") + e.what());
  513. }
  514. }
  515. else if(foundini != npos && ((foundini+strlen(".ini")) == filenameLow.size()))
  516. {
  517. // INI 文件处理
  518. std::ifstream ifs(fileName, std::ios::binary);
  519. if (!ifs) {
  520. throwError("Cannot open file: " + fileName);
  521. }
  522. try {
  523. boost::property_tree::ini_parser::read_ini(ifs, pt);
  524. }
  525. catch (const boost::property_tree::ini_parser_error& e) {
  526. throwError(std::string("INI parse error: ") + e.what());
  527. }
  528. }
  529. else
  530. {
  531. throwError("Unsupported file extension: " + filenameLow);
  532. }
  533. //got pt
  534. clear();
  535. return ParsePtToDataObject(*this,pt);
  536. }
  537. catch (const boost::property_tree::ptree_error& e) {
  538. throw ResDataObjectExption(("Property tree error: " + std::string(e.what())).c_str());
  539. return false;
  540. }
  541. catch (const std::exception& e) {
  542. throw ResDataObjectExption(("Standard error: " + std::string(e.what())).c_str());
  543. return false;
  544. }
  545. catch (...) {
  546. throw ResDataObjectExption("Unknown error occurred");
  547. return false;
  548. }
  549. return true;
  550. }
  551. bool ResDataObject::SaveFile( const char *pfileName )
  552. {
  553. if(pfileName == 0)
  554. {
  555. throw ResDataObjectExption("Save file failed");
  556. return false;
  557. }
  558. std::string fileName = (pfileName);
  559. if(fileName.size() == 0)
  560. {
  561. throw ResDataObjectExption("file name is empty");
  562. return false;
  563. }
  564. try
  565. {
  566. ptree pt;
  567. if(ParseDataObjectToPt(pt,*this) == false)
  568. {
  569. throw ResDataObjectExption("ParseDataObjectToPt failed");
  570. return false;
  571. }
  572. std::string filenameLow = fileName;
  573. std::transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(),
  574. [](unsigned char c) { return std::tolower(c); });
  575. static const basic_string <char>::size_type npos = -1;
  576. basic_string <char>::size_type foundini = filenameLow.rfind(".ini");
  577. basic_string <char>::size_type foundxml = filenameLow.rfind(".xml");
  578. basic_string <char>::size_type foundjson = filenameLow.rfind(".json");
  579. if(foundxml != npos && ((foundxml+strlen(".xml")) == filenameLow.size()))
  580. {
  581. //found xml file
  582. {
  583. //xml_parser::xml_writer_settings<char> settings(' ', 4);//Don't use TAB
  584. auto settings = boost::property_tree::xml_writer_make_settings<string>(' ', 4);
  585. xml_parser::write_xml(fileName,pt,std::locale(),settings);
  586. }
  587. }
  588. else if(foundjson != npos && ((foundjson+strlen(".json")) == filenameLow.size()))
  589. {
  590. //found json file
  591. json_parser::write_json(fileName,pt);
  592. }
  593. else if(foundini != npos && ((foundini+strlen(".ini")) == filenameLow.size()))
  594. {
  595. //found ini file
  596. ini_parser::write_ini(fileName,pt);
  597. }
  598. else
  599. {
  600. throw ResDataObjectExption("save file failed");
  601. return false;
  602. }
  603. }
  604. catch (...)
  605. {
  606. throw ResDataObjectExption("save file failed");
  607. return false;
  608. }
  609. return true;
  610. }
  611. void ResDataObject::MakeKeyLower()
  612. {
  613. std::string key;
  614. for (size_t i = 0; i < size(); i++)
  615. {
  616. key = GetKey(i);
  617. std::transform(key.begin(), key.end(), key.begin(),
  618. [](unsigned char c) { return std::tolower(c); });
  619. SetKey(i, key.c_str());
  620. if ((*this)[i].IsObject())
  621. {
  622. (*this)[i].MakeKeyLower();
  623. }
  624. }
  625. }
  626. ResDataObject& ResDataObject::operator = (const char* pVal)
  627. {
  628. clear();
  629. if (!pVal || reinterpret_cast<uintptr_t>(pVal) < 0x1000) { // 0x1000是常见的用户态有效内存起始阈值
  630. *m_value = ""; // 用空字符串替代无效指针
  631. }
  632. else {
  633. *m_value = pVal; // 仅在指针有效时赋值
  634. }
  635. return (*this);
  636. };
  637. #pragma warning( push )
  638. #pragma warning( disable : 4018 )
  639. //get
  640. ResDataObject &ResDataObject::operator [](size_t idx)
  641. {
  642. if(size() <= idx)
  643. {
  644. throw ResDataObjectExption("overflow of Idx Of []");
  645. }
  646. ResDataObject_Pair& pair = (*m_vec)[idx];
  647. return *pair.second;
  648. }
  649. ResDataObject &ResDataObject::operator [](int idx)
  650. {
  651. if(idx < 0)
  652. {
  653. throw ResDataObjectExption("using minus idx to operate []");
  654. }
  655. size_t idx_local = (size_t)idx;
  656. return (*this)[idx_local];
  657. }
  658. ResDataObject &ResDataObject::operator [](const char *pKey)
  659. {
  660. if (!pKey) {
  661. throw ResDataObjectExption("operator[] param is NULL");
  662. }
  663. //keyname = CCommonFun::MAKELOWER(keyname);
  664. const std::string keyname(pKey);
  665. for (auto& pair : *m_vec) {
  666. if (keyname == *pair.first) {
  667. return *pair.second;
  668. }
  669. }
  670. // 添加新元素而不是返回静态对象
  671. m_vec->emplace_back();
  672. auto& newPair = m_vec->back();
  673. *newPair.first = keyname;
  674. return *newPair.second;
  675. };
  676. const char* ResDataObject::GetKey(size_t idx)
  677. {
  678. if (m_vec && (m_vec->size() <= idx))
  679. {
  680. throw ResDataObjectExption("overflow of Idx Of []");
  681. }
  682. return (*m_vec)[idx].first->c_str();
  683. }
  684. const char* ResDataObject::GetKey(int idx)
  685. {
  686. if(idx < 0)
  687. {
  688. throw ResDataObjectExption("try to get key with minus idx");
  689. }
  690. size_t idx_local = (size_t)idx;
  691. return(*m_vec)[idx_local].first->c_str();
  692. }
  693. bool ResDataObject::SetKey(size_t idx,const char *pKey)
  694. {
  695. if(m_vec && (*m_vec).size() <= idx)
  696. {
  697. throw ResDataObjectExption("overflow of Idx Of []");
  698. return false;
  699. }
  700. (*m_vec)[idx].first = std::make_unique<std::string>(pKey);
  701. return true;
  702. }
  703. ResDataObject::operator bool()
  704. {
  705. if(IsObject())
  706. {
  707. throw ResDataObjectExption("try to convert a object");
  708. }
  709. bool ret1 = 0;
  710. std::stringstream strm;
  711. strm << (*m_value);
  712. strm >> ret1;
  713. return ret1;
  714. };
  715. ResDataObject::operator char()
  716. {
  717. if(IsObject())
  718. {
  719. throw ResDataObjectExption("try to convert a object");
  720. }
  721. char ret1 = 0;
  722. if (StrToIntegerT((*m_value).c_str(), &ret1))
  723. {
  724. return ret1;
  725. }
  726. throw ResDataObjectExption("try to convert a none char");
  727. return ret1;
  728. };
  729. ResDataObject::operator unsigned char()
  730. {
  731. if(IsObject())
  732. {
  733. throw ResDataObjectExption("try to convert a object");
  734. }
  735. unsigned char ret1 = 0;
  736. if (StrToIntegerT((*m_value).c_str(), &ret1))
  737. {
  738. return ret1;
  739. }
  740. throw ResDataObjectExption("try to convert a none uchar");
  741. return ret1;
  742. };
  743. ResDataObject::operator short()
  744. {
  745. if(IsObject())
  746. {
  747. throw ResDataObjectExption("try to convert a object");
  748. }
  749. short ret1 = 0;
  750. if (StrToIntegerT((*m_value).c_str(), &ret1))
  751. {
  752. return ret1;
  753. }
  754. throw ResDataObjectExption("try to convert a none short");
  755. return ret1;
  756. }
  757. ResDataObject::operator unsigned short()
  758. {
  759. if(IsObject())
  760. {
  761. throw ResDataObjectExption("try to convert a object");
  762. }
  763. unsigned short ret1 = 0;
  764. if (StrToIntegerT((*m_value).c_str(), &ret1))
  765. {
  766. return ret1;
  767. }
  768. throw ResDataObjectExption("try to convert a none ushort");
  769. return ret1;
  770. }
  771. ResDataObject::operator int()
  772. {
  773. if(IsObject())
  774. {
  775. throw ResDataObjectExption("try to convert a object");
  776. }
  777. int ret1 = 0;
  778. if (StrToIntegerT((*m_value).c_str(), &ret1))
  779. {
  780. return ret1;
  781. }
  782. throw ResDataObjectExption("try to convert a none int");
  783. return ret1;
  784. }
  785. ResDataObject::operator unsigned int()
  786. {
  787. if(IsObject())
  788. {
  789. throw ResDataObjectExption("try to convert a object");
  790. }
  791. unsigned int ret1 = 0;
  792. if (StrToIntegerT((*m_value).c_str(), &ret1))
  793. {
  794. return ret1;
  795. }
  796. throw ResDataObjectExption("try to convert a none uint");
  797. return ret1;
  798. }
  799. ResDataObject::operator long()
  800. {
  801. if(IsObject())
  802. {
  803. throw ResDataObjectExption("try to convert a object");
  804. }
  805. long ret1 = 0;
  806. if (StrToIntegerT((*m_value).c_str(), &ret1))
  807. {
  808. return ret1;
  809. }
  810. throw ResDataObjectExption("try to convert a none long");
  811. return ret1;
  812. }
  813. ResDataObject::operator unsigned long()
  814. {
  815. if(IsObject())
  816. {
  817. throw ResDataObjectExption("try to convert a object");
  818. }
  819. unsigned long ret1 = 0;
  820. if (StrToIntegerT((*m_value).c_str(), &ret1))
  821. {
  822. return ret1;
  823. }
  824. throw ResDataObjectExption("try to convert a none ulong");
  825. return ret1;
  826. }
  827. ResDataObject::operator long long()
  828. {
  829. if(IsObject())
  830. {
  831. throw ResDataObjectExption("try to convert a object");
  832. }
  833. long long ret1 = 0;
  834. if (StrToIntegerT((*m_value).c_str(), &ret1))
  835. {
  836. return ret1;
  837. }
  838. throw ResDataObjectExption("try to convert a none longlong");
  839. return ret1;
  840. }
  841. ResDataObject::operator unsigned long long()
  842. {
  843. if(IsObject())
  844. {
  845. throw ResDataObjectExption("try to convert a object");
  846. }
  847. unsigned long long ret1 = 0;
  848. if (StrToIntegerT((*m_value).c_str(), &ret1))
  849. {
  850. return ret1;
  851. }
  852. throw ResDataObjectExption("try to convert a none ulonglong");
  853. return ret1;
  854. }
  855. ResDataObject::operator float()
  856. {
  857. if (IsObject())
  858. {
  859. throw ResDataObjectExption("try to convert a object");
  860. }
  861. return (float)((double)(*this));
  862. }
  863. ResDataObject::operator double()
  864. {
  865. if (IsObject()) {
  866. throw ResDataObjectExption("try to convert a object");
  867. }
  868. try {
  869. return std::stod(*m_value);
  870. }
  871. catch (...) {
  872. throw ResDataObjectExption("Invalid double conversion");
  873. return 0.0;
  874. }
  875. }
  876. ResDataObject::operator const char*()
  877. {
  878. if (!m_value) {
  879. std::cerr << "[ResDataObject] operator const char*: m_value is null" << std::endl;
  880. return ""; // 返回空字符串避免崩溃
  881. }
  882. return (*m_value).c_str();
  883. };
  884. size_t ResDataObject::GetKeyCount(const char *pKey)
  885. {
  886. size_t Idx = 0;
  887. std::string keyname = (pKey);
  888. //keyname = CCommonFun::MAKELOWER(keyname);
  889. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  890. while(iter1 != m_vec->end())
  891. {
  892. if(*(iter1->first) == keyname)
  893. {
  894. ++Idx;
  895. }
  896. iter1++;
  897. }
  898. return Idx;
  899. }
  900. int ResDataObject::GetFirstOf(const char *pKey)
  901. {
  902. if(pKey == NULL)
  903. {
  904. return -1;
  905. }
  906. int Idx = 0;
  907. std::string keyname = (pKey);
  908. //keyname = CCommonFun::MAKELOWER(keyname);
  909. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  910. while(iter1 != m_vec->end())
  911. {
  912. if(*(iter1->first) == keyname)
  913. {
  914. return Idx;
  915. }
  916. iter1++;
  917. ++Idx;
  918. }
  919. return -1;
  920. }
  921. int ResDataObject::GetNextOf(const char *pKey,int PrevIdx)
  922. {
  923. if(pKey == NULL)
  924. {
  925. return -1;
  926. }
  927. int Idx = 0;
  928. std::string keyname = (pKey);
  929. //keyname = CCommonFun::MAKELOWER(keyname);
  930. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  931. while(iter1 != m_vec->end())
  932. {
  933. if(*(iter1->first) == keyname)
  934. {
  935. if(Idx > PrevIdx)
  936. {
  937. return Idx;
  938. }
  939. }
  940. iter1++;
  941. ++Idx;
  942. }
  943. return -1;
  944. }
  945. #pragma warning( pop )
  946. //erase
  947. bool ResDataObject::eraseAllOf(const char *pKey)
  948. {
  949. if (pKey == NULL) {
  950. throw ResDataObjectExption("eraseAllOf: pKey cannot be NULL");
  951. return false;
  952. }
  953. std::string keyname = (pKey);
  954. //keyname = CCommonFun::MAKELOWER(keyname);
  955. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  956. while(iter1 != m_vec->end())
  957. {
  958. if(*(iter1->first) == (keyname))
  959. {
  960. iter1 = m_vec->erase(iter1);
  961. continue;
  962. }
  963. iter1++;
  964. }
  965. return true;
  966. };
  967. bool ResDataObject::eraseOneOf(const char *pKey,size_t idx)
  968. {
  969. if(pKey == NULL)
  970. {
  971. throw ResDataObjectExption("eraseOneOf failed");
  972. return false;
  973. }
  974. size_t curIdx = 0;
  975. std::string keyname = (pKey);
  976. //keyname = CCommonFun::MAKELOWER(keyname);
  977. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  978. for(;iter1 != m_vec->end();iter1++)
  979. {
  980. if(*(iter1->first) == (keyname))
  981. {
  982. if(curIdx == idx)
  983. {
  984. iter1 = m_vec->erase(iter1);
  985. return true;
  986. }
  987. else
  988. {
  989. ++curIdx;
  990. }
  991. }
  992. }
  993. return false;
  994. };
  995. ResDataObject& ResDataObject::operator += (const ResDataObject &tValue)
  996. {
  997. if (this != &tValue)
  998. {
  999. const auto& src_vec = *tValue.m_vec;
  1000. for (const auto& src_pair : src_vec)
  1001. {
  1002. ResDataObject_Pair new_pair;
  1003. *new_pair.first = *src_pair.first;
  1004. *new_pair.second = *src_pair.second;
  1005. m_vec->push_back(new_pair);
  1006. }
  1007. }
  1008. return *this;
  1009. }
  1010. ResDataObject& ResDataObject::operator=(const ResDataObject& tValue) {
  1011. if (this != &tValue) {
  1012. *m_encode = tValue.m_encode ? *tValue.m_encode : "";
  1013. *m_value = tValue.m_value ? *tValue.m_value : "";
  1014. m_vec->clear();
  1015. if (tValue.m_vec) {
  1016. m_vec->reserve(tValue.m_vec->size());
  1017. for (const auto& src_pair : *tValue.m_vec) {
  1018. ResDataObject_Pair new_pair(src_pair);
  1019. m_vec->push_back(std::move(new_pair));
  1020. }
  1021. }
  1022. }
  1023. return *this;
  1024. }
  1025. ResDataObject& ResDataObject::operator = (const bool tValue)
  1026. {
  1027. std::stringstream strm;
  1028. strm << tValue;
  1029. (*this) = strm.str().c_str();
  1030. return (*this);
  1031. }
  1032. ResDataObject& ResDataObject::operator = (const char tValue)
  1033. {
  1034. std::stringstream strm;
  1035. strm << tValue;
  1036. (*this) = strm.str().c_str();
  1037. return (*this);
  1038. }
  1039. ResDataObject& ResDataObject::operator = (const unsigned char tValue)
  1040. {
  1041. std::stringstream strm;
  1042. strm << tValue;
  1043. (*this) = strm.str().c_str();
  1044. return (*this);
  1045. }
  1046. ResDataObject& ResDataObject::operator = (const short tValue)
  1047. {
  1048. std::stringstream strm;
  1049. strm << tValue;
  1050. (*this) = strm.str().c_str();
  1051. return (*this);
  1052. }
  1053. ResDataObject& ResDataObject::operator = (const unsigned short tValue)
  1054. {
  1055. std::stringstream strm;
  1056. strm << tValue;
  1057. (*this) = strm.str().c_str();
  1058. return (*this);
  1059. }
  1060. ResDataObject& ResDataObject::operator = (const int tValue)
  1061. {
  1062. std::stringstream strm;
  1063. strm << tValue;
  1064. (*this) = strm.str().c_str();
  1065. return (*this);
  1066. }
  1067. ResDataObject& ResDataObject::operator = (const unsigned int tValue)
  1068. {
  1069. std::stringstream strm;
  1070. strm << tValue;
  1071. (*this) = strm.str().c_str();
  1072. return (*this);
  1073. }
  1074. ResDataObject& ResDataObject::operator = (const long tValue)
  1075. {
  1076. std::stringstream strm;
  1077. strm << tValue;
  1078. (*this) = strm.str().c_str();
  1079. return (*this);
  1080. }
  1081. ResDataObject& ResDataObject::operator = (const unsigned long tValue)
  1082. {
  1083. std::stringstream strm;
  1084. strm << tValue;
  1085. (*this) = strm.str().c_str();
  1086. return (*this);
  1087. }
  1088. ResDataObject& ResDataObject::operator = (const long long tValue)
  1089. {
  1090. std::stringstream strm;
  1091. strm << tValue;
  1092. (*this) = strm.str().c_str();
  1093. return (*this);
  1094. }
  1095. ResDataObject& ResDataObject::operator = (const unsigned long long tValue)
  1096. {
  1097. std::stringstream strm;
  1098. strm << tValue;
  1099. (*this) = strm.str().c_str();
  1100. return (*this);
  1101. }
  1102. ResDataObject& ResDataObject::operator = (const float tValue)
  1103. {
  1104. double DtValue = tValue;
  1105. (*this) = DtValue;
  1106. return (*this);
  1107. }
  1108. ResDataObject& ResDataObject::operator = (const double tValue)
  1109. {
  1110. //unsigned long long temp,*pF ;
  1111. std::stringstream strm;
  1112. //strm.precision(numeric_limits<double>::digits10);
  1113. //新方案(丢精度)
  1114. char szStr[64];
  1115. snprintf(szStr, sizeof(szStr), "%.17g", tValue);
  1116. strm << szStr;
  1117. (*this) = strm.str().c_str();
  1118. return (*this);
  1119. //旧方案(保持精度)
  1120. //assert(sizeof(double) == sizeof(unsigned long long));
  1121. //pF = (unsigned long long*)&tValue;
  1122. //temp = (*pF);
  1123. //strm << temp;
  1124. //(*this) = strm.str().c_str();
  1125. //return (*this);
  1126. }
  1127. bool ResDataObject::add(const char* pKey,bool tValue)
  1128. {
  1129. ResDataObject obj;
  1130. obj = tValue;
  1131. return add(pKey,obj);
  1132. }
  1133. bool ResDataObject::add(const char* pKey,char tValue)
  1134. {
  1135. ResDataObject obj;
  1136. obj = tValue;
  1137. return add(pKey,obj);
  1138. }
  1139. bool ResDataObject::add(const char* pKey,unsigned char tValue)
  1140. {
  1141. ResDataObject obj;
  1142. obj = tValue;
  1143. return add(pKey,obj);
  1144. }
  1145. bool ResDataObject::add(const char* pKey,short tValue)
  1146. {
  1147. ResDataObject obj;
  1148. obj = tValue;
  1149. return add(pKey,obj);
  1150. }
  1151. bool ResDataObject::add(const char* pKey,unsigned short tValue)
  1152. {
  1153. ResDataObject obj;
  1154. obj = tValue;
  1155. return add(pKey,obj);
  1156. }
  1157. bool ResDataObject::add(const char* pKey,int tValue)
  1158. {
  1159. ResDataObject obj;
  1160. obj = tValue;
  1161. return add(pKey,obj);
  1162. }
  1163. bool ResDataObject::add(const char* pKey,unsigned int tValue)
  1164. {
  1165. ResDataObject obj;
  1166. obj = tValue;
  1167. return add(pKey,obj);
  1168. }
  1169. bool ResDataObject::add(const char* pKey,long tValue)
  1170. {
  1171. ResDataObject obj;
  1172. obj = tValue;
  1173. return add(pKey,obj);
  1174. }
  1175. bool ResDataObject::add(const char* pKey,unsigned long tValue)
  1176. {
  1177. ResDataObject obj;
  1178. obj = tValue;
  1179. return add(pKey,obj);
  1180. }
  1181. bool ResDataObject::add(const char* pKey,long long tValue)
  1182. {
  1183. ResDataObject obj;
  1184. obj = tValue;
  1185. return add(pKey,obj);
  1186. }
  1187. bool ResDataObject::add(const char* pKey,unsigned long long tValue)
  1188. {
  1189. ResDataObject obj;
  1190. obj = tValue;
  1191. return add(pKey,obj);
  1192. }
  1193. bool ResDataObject::add(const char* pKey,float tValue)
  1194. {
  1195. ResDataObject obj;
  1196. obj = tValue;
  1197. return add(pKey,obj);
  1198. }
  1199. bool ResDataObject::add(const char* pKey,double tValue)
  1200. {
  1201. ResDataObject obj;
  1202. obj = tValue;
  1203. return add(pKey,obj);
  1204. }
  1205. bool ResDataObject::add(const char* pKey,const char* pValue)
  1206. {
  1207. ResDataObject obj;
  1208. if (!pValue || reinterpret_cast<uintptr_t>(pValue) < 0x1000) {
  1209. obj = ""; // 无效值用空字符串替代
  1210. }
  1211. else {
  1212. obj = pValue;
  1213. }
  1214. return add(pKey,obj);
  1215. }
  1216. bool ResDataObject::add(const char* pKey,ResDataObject &dataobj)
  1217. {
  1218. if (!m_vec) {
  1219. std::cerr << "Error: m_vec is null in ResDataObject::add" << std::endl;
  1220. m_vec = std::make_unique<std::vector<ResDataObject_Pair>>();
  1221. }
  1222. if (!pKey) pKey = ""; // 处理空指针
  1223. try {
  1224. m_vec->emplace_back();
  1225. auto& new_pair = m_vec->back();
  1226. *new_pair.first = pKey;
  1227. *new_pair.second = dataobj;
  1228. return true;
  1229. }
  1230. catch (...) {
  1231. std::cerr << "[add] vector@" << m_vec.get() << " is invalid (memory corrupted)" << std::endl;
  1232. return false;
  1233. }
  1234. };
  1235. bool ResDataObject::update(const char* pKey,bool tValue)
  1236. {
  1237. eraseAllOf(pKey);
  1238. return add(pKey,tValue);
  1239. }
  1240. bool ResDataObject::update(const char* pKey,char tValue)
  1241. {
  1242. eraseAllOf(pKey);
  1243. return add(pKey,tValue);
  1244. }
  1245. bool ResDataObject::update(const char* pKey,unsigned char tValue)
  1246. {
  1247. eraseAllOf(pKey);
  1248. return add(pKey,tValue);
  1249. }
  1250. bool ResDataObject::update(const char* pKey,short tValue)
  1251. {
  1252. eraseAllOf(pKey);
  1253. return add(pKey,tValue);
  1254. }
  1255. bool ResDataObject::update(const char* pKey,unsigned short tValue)
  1256. {
  1257. eraseAllOf(pKey);
  1258. return add(pKey,tValue);
  1259. }
  1260. bool ResDataObject::update(const char* pKey,int tValue)
  1261. {
  1262. eraseAllOf(pKey);
  1263. return add(pKey,tValue);
  1264. }
  1265. bool ResDataObject::update(const char* pKey,unsigned int tValue)
  1266. {
  1267. eraseAllOf(pKey);
  1268. return add(pKey,tValue);
  1269. }
  1270. bool ResDataObject::update(const char* pKey,long tValue)
  1271. {
  1272. eraseAllOf(pKey);
  1273. return add(pKey,tValue);
  1274. }
  1275. bool ResDataObject::update(const char* pKey,unsigned long tValue)
  1276. {
  1277. eraseAllOf(pKey);
  1278. return add(pKey,tValue);
  1279. }
  1280. bool ResDataObject::update(const char* pKey,long long tValue)
  1281. {
  1282. eraseAllOf(pKey);
  1283. return add(pKey,tValue);
  1284. }
  1285. bool ResDataObject::update(const char* pKey,unsigned long long tValue)
  1286. {
  1287. eraseAllOf(pKey);
  1288. return add(pKey,tValue);
  1289. }
  1290. bool ResDataObject::update(const char* pKey,float tValue)
  1291. {
  1292. eraseAllOf(pKey);
  1293. return add(pKey,tValue);
  1294. }
  1295. bool ResDataObject::update(const char* pKey,double tValue)
  1296. {
  1297. eraseAllOf(pKey);
  1298. return add(pKey,tValue);
  1299. }
  1300. bool ResDataObject::update(const char* pKey,const char* pValue)
  1301. {
  1302. eraseAllOf(pKey);
  1303. return add(pKey,pValue);
  1304. }
  1305. bool ResDataObject::update(const char* pKey,ResDataObject &dataobj)
  1306. {
  1307. eraseAllOf(pKey);
  1308. return add(pKey,dataobj);
  1309. }
  1310. bool ResDataObject::operator == (const char* pVal)
  1311. {
  1312. std::string exVal = pVal;
  1313. return ((*m_value) == exVal);
  1314. }
  1315. bool ResDataObject::operator == (const ResDataObject &Obj)
  1316. {
  1317. //compare val
  1318. if ((*m_value) != (*(Obj.m_value)))
  1319. {
  1320. return false;
  1321. }
  1322. //compare count
  1323. if (m_vec->size() != Obj.m_vec->size())
  1324. {
  1325. return false;
  1326. }
  1327. //compare vec
  1328. for (size_t i = 0; i < m_vec->size(); i++)
  1329. {
  1330. if ((*((*m_vec)[i]).first) != (*((*Obj.m_vec)[i]).first))
  1331. {
  1332. return false;
  1333. }
  1334. if ((*((*m_vec)[i]).second) == (*((*Obj.m_vec)[i]).second))
  1335. {
  1336. continue;
  1337. }
  1338. return false;
  1339. }
  1340. return true;
  1341. }
  1342. RESDATAOBJECT_C_API bool TryGetValue(ResDataObject &obj, const char *pKey, ResDataObject &res)
  1343. {
  1344. bool ret = true;
  1345. try {
  1346. res.clear();
  1347. if (obj.GetFirstOf(pKey) >= 0)
  1348. {
  1349. res = obj[pKey];
  1350. }
  1351. else
  1352. {
  1353. ret = false;
  1354. }
  1355. }
  1356. catch (...)
  1357. {
  1358. res.clear();
  1359. ret = false;
  1360. }
  1361. return ret;
  1362. }
  1363. //----------------------------------------------------------------
  1364. ExJsonDataObject::ExJsonDataObject()
  1365. : m_pKey(std::make_unique<std::string>()),
  1366. m_ValString(std::make_unique<std::string>()),
  1367. m_pTargetObject(std::make_unique<ResDataObject>()) {
  1368. }
  1369. ExJsonDataObject::ExJsonDataObject(const ExJsonDataObject& tValue)
  1370. : m_pKey(std::make_unique<std::string>(*tValue.m_pKey)),
  1371. m_ValString(std::make_unique<std::string>(*tValue.m_ValString)),
  1372. m_pTargetObject(std::make_unique<ResDataObject>(*tValue.m_pTargetObject)) {
  1373. }
  1374. //base
  1375. void ExJsonDataObject::SetKey(const char *pKey)
  1376. {
  1377. *m_pKey = pKey ? pKey : "";
  1378. }
  1379. const char *ExJsonDataObject::GetKey()
  1380. {
  1381. return m_pKey->c_str();
  1382. }
  1383. ResDataObject &ExJsonDataObject::GetResDataObject()
  1384. {
  1385. return (*m_pTargetObject);
  1386. }
  1387. bool ExJsonDataObject::SetResDataObject(ResDataObject &obj)
  1388. {
  1389. bool ret = true;
  1390. try {
  1391. //check it first
  1392. for (size_t i = 0; i < m_pTargetObject->size(); i++)
  1393. {
  1394. const char* pKey = m_pTargetObject->GetKey(i);
  1395. if (pKey)
  1396. {
  1397. int IdxObj = obj.GetFirstOf(pKey);
  1398. if (IdxObj < 0)
  1399. {
  1400. ret = false;
  1401. break;
  1402. }
  1403. }
  1404. else
  1405. {
  1406. ret = false;
  1407. break;
  1408. }
  1409. }
  1410. //copy it
  1411. if (ret)
  1412. {
  1413. (*m_pTargetObject) = obj;
  1414. //for (size_t i = 0; i < m_pTargetObject->size(); i++)
  1415. //{
  1416. // (*m_pTargetObject)[i] = obj[obj.GetFirstOf(m_pTargetObject->GetKey(i))];
  1417. //}
  1418. }
  1419. }
  1420. catch (...)
  1421. {
  1422. ret = false;
  1423. }
  1424. return ret;
  1425. }
  1426. ResDataObject &ExJsonDataObject::operator [](const char *pKey)
  1427. {
  1428. try
  1429. {
  1430. return (*m_pTargetObject)[pKey];
  1431. }
  1432. catch (...)
  1433. {
  1434. assert(0);//make sure never gonna happen
  1435. }
  1436. return (*m_pTargetObject);
  1437. }
  1438. ExJsonDataObject &ExJsonDataObject::operator = (const ExJsonDataObject &tValue)
  1439. {
  1440. if (this != &tValue)
  1441. {
  1442. (*m_pKey) = *(tValue.m_pKey);
  1443. (*m_ValString) = *(tValue.m_ValString);
  1444. (*m_pTargetObject) = *(tValue.m_pTargetObject);
  1445. }
  1446. return (*this);
  1447. }