ResDataObject.cpp 34 KB

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