ResDataObject.cpp 33 KB

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