ResDataObject.cpp 35 KB

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