ResDataObject.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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. (*m_encode) = strm.str();
  343. return m_encode->c_str();
  344. }
  345. throw ResDataObjectExption("encode failed. ");
  346. }
  347. bool ResDataObject::decode( const char *pdata )
  348. {
  349. bool IsJsonStr = false;
  350. size_t i = 0;
  351. while (pdata[i] != 0)
  352. {
  353. if (pdata[i] == ':')
  354. {
  355. IsJsonStr = true;
  356. break;
  357. }
  358. ++i;
  359. }
  360. if (pdata && strlen(pdata) > 0)
  361. {
  362. if (IsJsonStr)
  363. {
  364. std::stringstream strm;
  365. strm << pdata;
  366. ptree pt;
  367. try
  368. {
  369. json_parser::read_json(strm, pt);
  370. clear();
  371. return ParsePtToDataObject(*this, pt);
  372. }
  373. catch (...)
  374. {
  375. throw ResDataObjectExption("decode failed. ");
  376. return false;
  377. }
  378. }
  379. else
  380. {
  381. (*this) = pdata;
  382. }
  383. }
  384. else
  385. {
  386. (*this) = "";
  387. }
  388. return true;
  389. }
  390. bool ResDataObject::DumpJsonFile(const char *pfileName)
  391. {
  392. if (pfileName == 0)
  393. {
  394. throw ResDataObjectExption("open file failed");
  395. return false;
  396. }
  397. ptree pt;
  398. std::string fileName = (pfileName);
  399. if (fileName.size() == 0)
  400. {
  401. throw ResDataObjectExption("file name is empty");
  402. return false;
  403. }
  404. try
  405. {
  406. std::string filenameLow = fileName;
  407. std::transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(),
  408. [](unsigned char c) { return std::tolower(c); });
  409. static const basic_string <char>::size_type npos = -1;
  410. basic_string <char>::size_type foundini = filenameLow.rfind(".ini");
  411. basic_string <char>::size_type foundxml = filenameLow.rfind(".xml");
  412. basic_string <char>::size_type foundjson = filenameLow.rfind(".json");
  413. if (foundxml != npos && ((foundxml + strlen(".xml")) == filenameLow.size()))
  414. {
  415. //found xml file
  416. //xml_parser::read_xml(fileName,pt,xml_parser::trim_whitespace|xml_parser::no_comments);
  417. xml_parser::read_xml(fileName, pt, xml_parser::no_comments);
  418. }
  419. else if (foundjson != npos && ((foundjson + strlen(".json")) == filenameLow.size()))
  420. {
  421. //found json file
  422. json_parser::read_json(fileName, pt);
  423. }
  424. else if (foundini != npos && ((foundini + strlen(".ini")) == filenameLow.size()))
  425. {
  426. //found ini file
  427. ini_parser::read_ini(fileName, pt);
  428. }
  429. else
  430. {
  431. throw ResDataObjectExption("open file failed. file extention is not clear");
  432. return false;
  433. }
  434. //got pt
  435. clear();
  436. return PrintBoostPtObject(pt, 0);
  437. }
  438. catch (...)
  439. {
  440. throw ResDataObjectExption("open file failed");
  441. return false;
  442. }
  443. return true;
  444. }
  445. bool ResDataObject::loadFile( const char *pfileName )
  446. {
  447. if (!pfileName) {
  448. throw ResDataObjectExption("Null filename pointer");
  449. return false;
  450. }
  451. namespace fs = std::filesystem;
  452. const fs::path filePath(pfileName);
  453. // 验证文件存在且可读
  454. if (!fs::exists(filePath)) {
  455. throw ResDataObjectExption(("File does not exist: " + filePath.string()).c_str());
  456. }
  457. if (!fs::is_regular_file(filePath)) {
  458. throw ResDataObjectExption(("Not a regular file: " + filePath.string()).c_str());
  459. }
  460. const uintmax_t fileSize = fs::file_size(filePath);
  461. if (fileSize == 0) {
  462. throw ResDataObjectExption(("Empty file: " + filePath.string()).c_str());
  463. }
  464. const uintmax_t MAX_FILE_SIZE = 100 * 1024 * 1024; // 100MB
  465. if (fileSize > MAX_FILE_SIZE) {
  466. throw ResDataObjectExption(("File too large: " + filePath.string()).c_str());
  467. }
  468. std::unique_ptr<ptree> ptPtr = std::make_unique<ptree>();
  469. try
  470. {
  471. std::string fileName(pfileName);
  472. std::string filenameLow = fileName;
  473. std::transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(),
  474. [](unsigned char c) { return std::tolower(c); });
  475. static const std::string::size_type npos = -1;
  476. std::string::size_type foundxml = filenameLow.rfind(".xml");
  477. std::string::size_type foundjson = filenameLow.rfind(".json");
  478. std::string::size_type foundini = filenameLow.rfind(".ini");
  479. auto throwError = [](const std::string& msg) {
  480. throw ResDataObjectExption(msg.c_str());
  481. };
  482. if (foundxml != npos && ((foundxml + 4) == filenameLow.size()))
  483. {
  484. // XML 文件处理 - 修正了标志问题
  485. const int flags =
  486. boost::property_tree::xml_parser::no_comments |
  487. boost::property_tree::xml_parser::trim_whitespace;
  488. // 使用文件流代替直接路径
  489. std::ifstream ifs(fileName, std::ios::binary);
  490. if (!ifs) {
  491. throwError("Cannot open file: " + fileName);
  492. }
  493. // 读取文件内容
  494. std::vector<char> buffer;
  495. buffer.reserve(fileSize + 1); // 预分配空间
  496. buffer.assign(std::istreambuf_iterator<char>(ifs),
  497. std::istreambuf_iterator<char>());
  498. buffer.push_back('\0');
  499. // 使用字符串流解析
  500. std::istringstream iss(buffer.data());
  501. boost::property_tree::read_xml(iss, *ptPtr, flags);
  502. }
  503. else if(foundjson != npos && ((foundjson+strlen(".json")) == filenameLow.size()))
  504. {
  505. // JSON 文件处理
  506. std::ifstream ifs(fileName, std::ios::binary);
  507. if (!ifs) {
  508. throwError("Cannot open file: " + fileName);
  509. }
  510. const size_t MAX_DEPTH = 50;
  511. ptPtr->put("debug.max_depth", MAX_DEPTH);
  512. try {
  513. std::string jsonContent(
  514. (std::istreambuf_iterator<char>(ifs)),
  515. std::istreambuf_iterator<char>()
  516. );
  517. if (jsonContent.empty()) {
  518. throwError("Empty JSON content: " + fileName);
  519. }
  520. std::istringstream jsonStream(jsonContent);
  521. boost::property_tree::read_json(jsonStream, *ptPtr);
  522. }
  523. catch (const boost::property_tree::json_parser_error& e) {
  524. throwError(std::string("JSON parse error: ") + e.what());
  525. }
  526. }
  527. else if(foundini != npos && ((foundini+strlen(".ini")) == filenameLow.size()))
  528. {
  529. // INI 文件处理
  530. std::ifstream ifs(fileName, std::ios::binary);
  531. if (!ifs) {
  532. throwError("Cannot open file: " + fileName);
  533. }
  534. try {
  535. boost::property_tree::ini_parser::read_ini(ifs, *ptPtr);
  536. }
  537. catch (const boost::property_tree::ini_parser_error& e) {
  538. throwError(std::string("INI parse error: ") + e.what());
  539. }
  540. }
  541. else
  542. {
  543. throwError("Unsupported file extension: " + filenameLow);
  544. }
  545. //got pt
  546. clear();
  547. bool result = ParsePtToDataObject(*this, *ptPtr);
  548. ptPtr.reset();
  549. return result;
  550. }
  551. catch (const boost::property_tree::ptree_error& e) {
  552. throw ResDataObjectExption(("Property tree error: " + std::string(e.what())).c_str());
  553. return false;
  554. }
  555. catch (const std::exception& e) {
  556. throw ResDataObjectExption(("Standard error: " + std::string(e.what())).c_str());
  557. return false;
  558. }
  559. catch (...) {
  560. throw ResDataObjectExption("Unknown error occurred");
  561. return false;
  562. }
  563. return true;
  564. }
  565. bool ResDataObject::SaveFile( const char *pfileName )
  566. {
  567. if(pfileName == 0)
  568. {
  569. throw ResDataObjectExption("Save file failed");
  570. return false;
  571. }
  572. std::string fileName = (pfileName);
  573. if(fileName.size() == 0)
  574. {
  575. throw ResDataObjectExption("file name is empty");
  576. return false;
  577. }
  578. try
  579. {
  580. ptree pt;
  581. if(ParseDataObjectToPt(pt,*this) == false)
  582. {
  583. throw ResDataObjectExption("ParseDataObjectToPt failed");
  584. return false;
  585. }
  586. std::string filenameLow = fileName;
  587. std::transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(),
  588. [](unsigned char c) { return std::tolower(c); });
  589. static const basic_string <char>::size_type npos = -1;
  590. basic_string <char>::size_type foundini = filenameLow.rfind(".ini");
  591. basic_string <char>::size_type foundxml = filenameLow.rfind(".xml");
  592. basic_string <char>::size_type foundjson = filenameLow.rfind(".json");
  593. if(foundxml != npos && ((foundxml+strlen(".xml")) == filenameLow.size()))
  594. {
  595. //found xml file
  596. {
  597. //xml_parser::xml_writer_settings<char> settings(' ', 4);//Don't use TAB
  598. auto settings = boost::property_tree::xml_writer_make_settings<string>(' ', 4);
  599. xml_parser::write_xml(fileName,pt,std::locale(),settings);
  600. }
  601. }
  602. else if(foundjson != npos && ((foundjson+strlen(".json")) == filenameLow.size()))
  603. {
  604. //found json file
  605. json_parser::write_json(fileName,pt);
  606. }
  607. else if(foundini != npos && ((foundini+strlen(".ini")) == filenameLow.size()))
  608. {
  609. //found ini file
  610. ini_parser::write_ini(fileName,pt);
  611. }
  612. else
  613. {
  614. throw ResDataObjectExption("save file failed");
  615. return false;
  616. }
  617. }
  618. catch (...)
  619. {
  620. throw ResDataObjectExption("save file failed");
  621. return false;
  622. }
  623. return true;
  624. }
  625. void ResDataObject::MakeKeyLower()
  626. {
  627. std::string key;
  628. for (size_t i = 0; i < size(); i++)
  629. {
  630. key = GetKey(i);
  631. std::transform(key.begin(), key.end(), key.begin(),
  632. [](unsigned char c) { return std::tolower(c); });
  633. SetKey(i, key.c_str());
  634. if ((*this)[i].IsObject())
  635. {
  636. (*this)[i].MakeKeyLower();
  637. }
  638. }
  639. }
  640. ResDataObject& ResDataObject::operator = (const char* pVal)
  641. {
  642. clear();
  643. if (!pVal || reinterpret_cast<uintptr_t>(pVal) < 0x1000) { // 0x1000是常见的用户态有效内存起始阈值
  644. *m_value = ""; // 用空字符串替代无效指针
  645. }
  646. else {
  647. *m_value = pVal; // 仅在指针有效时赋值
  648. }
  649. return (*this);
  650. };
  651. #pragma warning( push )
  652. #pragma warning( disable : 4018 )
  653. //get
  654. ResDataObject &ResDataObject::operator [](size_t idx)
  655. {
  656. if(size() <= idx)
  657. {
  658. throw ResDataObjectExption("overflow of Idx Of []");
  659. }
  660. ResDataObject_Pair& pair = (*m_vec)[idx];
  661. return *pair.second;
  662. }
  663. ResDataObject &ResDataObject::operator [](int idx)
  664. {
  665. if(idx < 0)
  666. {
  667. throw ResDataObjectExption("using minus idx to operate []");
  668. }
  669. size_t idx_local = (size_t)idx;
  670. return (*this)[idx_local];
  671. }
  672. ResDataObject &ResDataObject::operator [](const char *pKey)
  673. {
  674. if (!pKey) {
  675. throw ResDataObjectExption("operator[] param is NULL");
  676. }
  677. //keyname = CCommonFun::MAKELOWER(keyname);
  678. const std::string keyname(pKey);
  679. for (auto& pair : *m_vec) {
  680. if (keyname == *pair.first) {
  681. return *pair.second;
  682. }
  683. }
  684. // 添加新元素而不是返回静态对象
  685. m_vec->emplace_back();
  686. auto& newPair = m_vec->back();
  687. *newPair.first = keyname;
  688. return *newPair.second;
  689. };
  690. const char* ResDataObject::GetKey(size_t idx)
  691. {
  692. if (m_vec && (m_vec->size() <= idx))
  693. {
  694. throw ResDataObjectExption("overflow of Idx Of []");
  695. }
  696. return (*m_vec)[idx].first->c_str();
  697. }
  698. const char* ResDataObject::GetKey(int idx)
  699. {
  700. if(idx < 0)
  701. {
  702. throw ResDataObjectExption("try to get key with minus idx");
  703. }
  704. size_t idx_local = (size_t)idx;
  705. return(*m_vec)[idx_local].first->c_str();
  706. }
  707. bool ResDataObject::SetKey(size_t idx,const char *pKey)
  708. {
  709. if(m_vec && (*m_vec).size() <= idx)
  710. {
  711. throw ResDataObjectExption("overflow of Idx Of []");
  712. return false;
  713. }
  714. (*m_vec)[idx].first = std::make_unique<std::string>(pKey);
  715. return true;
  716. }
  717. ResDataObject::operator bool()
  718. {
  719. if(IsObject())
  720. {
  721. throw ResDataObjectExption("try to convert a object");
  722. }
  723. bool ret1 = 0;
  724. std::stringstream strm;
  725. strm << (*m_value);
  726. strm >> ret1;
  727. return ret1;
  728. };
  729. ResDataObject::operator char()
  730. {
  731. if(IsObject())
  732. {
  733. throw ResDataObjectExption("try to convert a object");
  734. }
  735. char ret1 = 0;
  736. if (StrToIntegerT((*m_value).c_str(), &ret1))
  737. {
  738. return ret1;
  739. }
  740. throw ResDataObjectExption("try to convert a none char");
  741. return ret1;
  742. };
  743. ResDataObject::operator unsigned char()
  744. {
  745. if(IsObject())
  746. {
  747. throw ResDataObjectExption("try to convert a object");
  748. }
  749. unsigned char ret1 = 0;
  750. if (StrToIntegerT((*m_value).c_str(), &ret1))
  751. {
  752. return ret1;
  753. }
  754. throw ResDataObjectExption("try to convert a none uchar");
  755. return ret1;
  756. };
  757. ResDataObject::operator short()
  758. {
  759. if(IsObject())
  760. {
  761. throw ResDataObjectExption("try to convert a object");
  762. }
  763. short ret1 = 0;
  764. if (StrToIntegerT((*m_value).c_str(), &ret1))
  765. {
  766. return ret1;
  767. }
  768. throw ResDataObjectExption("try to convert a none short");
  769. return ret1;
  770. }
  771. ResDataObject::operator unsigned short()
  772. {
  773. if(IsObject())
  774. {
  775. throw ResDataObjectExption("try to convert a object");
  776. }
  777. unsigned short ret1 = 0;
  778. if (StrToIntegerT((*m_value).c_str(), &ret1))
  779. {
  780. return ret1;
  781. }
  782. throw ResDataObjectExption("try to convert a none ushort");
  783. return ret1;
  784. }
  785. ResDataObject::operator int()
  786. {
  787. if(IsObject())
  788. {
  789. throw ResDataObjectExption("try to convert a object");
  790. }
  791. int ret1 = 0;
  792. if (StrToIntegerT((*m_value).c_str(), &ret1))
  793. {
  794. return ret1;
  795. }
  796. throw ResDataObjectExption("try to convert a none int");
  797. return ret1;
  798. }
  799. ResDataObject::operator unsigned int()
  800. {
  801. if(IsObject())
  802. {
  803. throw ResDataObjectExption("try to convert a object");
  804. }
  805. unsigned int ret1 = 0;
  806. if (StrToIntegerT((*m_value).c_str(), &ret1))
  807. {
  808. return ret1;
  809. }
  810. throw ResDataObjectExption("try to convert a none uint");
  811. return ret1;
  812. }
  813. ResDataObject::operator long()
  814. {
  815. if(IsObject())
  816. {
  817. throw ResDataObjectExption("try to convert a object");
  818. }
  819. long ret1 = 0;
  820. if (StrToIntegerT((*m_value).c_str(), &ret1))
  821. {
  822. return ret1;
  823. }
  824. throw ResDataObjectExption("try to convert a none long");
  825. return ret1;
  826. }
  827. ResDataObject::operator unsigned long()
  828. {
  829. if(IsObject())
  830. {
  831. throw ResDataObjectExption("try to convert a object");
  832. }
  833. unsigned long ret1 = 0;
  834. if (StrToIntegerT((*m_value).c_str(), &ret1))
  835. {
  836. return ret1;
  837. }
  838. throw ResDataObjectExption("try to convert a none ulong");
  839. return ret1;
  840. }
  841. ResDataObject::operator long long()
  842. {
  843. if(IsObject())
  844. {
  845. throw ResDataObjectExption("try to convert a object");
  846. }
  847. long long ret1 = 0;
  848. if (StrToIntegerT((*m_value).c_str(), &ret1))
  849. {
  850. return ret1;
  851. }
  852. throw ResDataObjectExption("try to convert a none longlong");
  853. return ret1;
  854. }
  855. ResDataObject::operator unsigned long long()
  856. {
  857. if(IsObject())
  858. {
  859. throw ResDataObjectExption("try to convert a object");
  860. }
  861. unsigned long long ret1 = 0;
  862. if (StrToIntegerT((*m_value).c_str(), &ret1))
  863. {
  864. return ret1;
  865. }
  866. throw ResDataObjectExption("try to convert a none ulonglong");
  867. return ret1;
  868. }
  869. ResDataObject::operator float()
  870. {
  871. if (IsObject())
  872. {
  873. throw ResDataObjectExption("try to convert a object");
  874. }
  875. return (float)((double)(*this));
  876. }
  877. ResDataObject::operator double()
  878. {
  879. if (IsObject()) {
  880. throw ResDataObjectExption("try to convert a object");
  881. }
  882. try {
  883. return std::stod(*m_value);
  884. }
  885. catch (...) {
  886. throw ResDataObjectExption("Invalid double conversion");
  887. return 0.0;
  888. }
  889. }
  890. ResDataObject::operator const char*()
  891. {
  892. if (!m_value) {
  893. std::cerr << "[ResDataObject] operator const char*: m_value is null" << std::endl;
  894. return ""; // 返回空字符串避免崩溃
  895. }
  896. return (*m_value).c_str();
  897. };
  898. size_t ResDataObject::GetKeyCount(const char *pKey)
  899. {
  900. size_t Idx = 0;
  901. std::string keyname = (pKey);
  902. //keyname = CCommonFun::MAKELOWER(keyname);
  903. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  904. while(iter1 != m_vec->end())
  905. {
  906. if(*(iter1->first) == keyname)
  907. {
  908. ++Idx;
  909. }
  910. iter1++;
  911. }
  912. return Idx;
  913. }
  914. int ResDataObject::GetFirstOf(const char *pKey)
  915. {
  916. if(pKey == NULL)
  917. {
  918. return -1;
  919. }
  920. int Idx = 0;
  921. std::string keyname = (pKey);
  922. //keyname = CCommonFun::MAKELOWER(keyname);
  923. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  924. while(iter1 != m_vec->end())
  925. {
  926. if(*(iter1->first) == keyname)
  927. {
  928. return Idx;
  929. }
  930. iter1++;
  931. ++Idx;
  932. }
  933. return -1;
  934. }
  935. int ResDataObject::GetNextOf(const char *pKey,int PrevIdx)
  936. {
  937. if(pKey == NULL)
  938. {
  939. return -1;
  940. }
  941. int Idx = 0;
  942. std::string keyname = (pKey);
  943. //keyname = CCommonFun::MAKELOWER(keyname);
  944. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  945. while(iter1 != m_vec->end())
  946. {
  947. if(*(iter1->first) == keyname)
  948. {
  949. if(Idx > PrevIdx)
  950. {
  951. return Idx;
  952. }
  953. }
  954. iter1++;
  955. ++Idx;
  956. }
  957. return -1;
  958. }
  959. #pragma warning( pop )
  960. //erase
  961. bool ResDataObject::eraseAllOf(const char *pKey)
  962. {
  963. if (pKey == NULL) {
  964. throw ResDataObjectExption("eraseAllOf: pKey cannot be NULL");
  965. return false;
  966. }
  967. std::string keyname = (pKey);
  968. //keyname = CCommonFun::MAKELOWER(keyname);
  969. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  970. while(iter1 != m_vec->end())
  971. {
  972. if(*(iter1->first) == (keyname))
  973. {
  974. iter1 = m_vec->erase(iter1);
  975. continue;
  976. }
  977. iter1++;
  978. }
  979. return true;
  980. };
  981. bool ResDataObject::eraseOneOf(const char *pKey,size_t idx)
  982. {
  983. if(pKey == NULL)
  984. {
  985. throw ResDataObjectExption("eraseOneOf failed");
  986. return false;
  987. }
  988. size_t curIdx = 0;
  989. std::string keyname = (pKey);
  990. //keyname = CCommonFun::MAKELOWER(keyname);
  991. vector<ResDataObject_Pair>::iterator iter1 = m_vec->begin();
  992. for(;iter1 != m_vec->end();iter1++)
  993. {
  994. if(*(iter1->first) == (keyname))
  995. {
  996. if(curIdx == idx)
  997. {
  998. iter1 = m_vec->erase(iter1);
  999. return true;
  1000. }
  1001. else
  1002. {
  1003. ++curIdx;
  1004. }
  1005. }
  1006. }
  1007. return false;
  1008. };
  1009. ResDataObject& ResDataObject::operator += (const ResDataObject &tValue)
  1010. {
  1011. if (this != &tValue)
  1012. {
  1013. const auto& src_vec = *tValue.m_vec;
  1014. for (const auto& src_pair : src_vec)
  1015. {
  1016. ResDataObject_Pair new_pair;
  1017. *new_pair.first = *src_pair.first;
  1018. *new_pair.second = *src_pair.second;
  1019. m_vec->push_back(new_pair);
  1020. }
  1021. }
  1022. return *this;
  1023. }
  1024. ResDataObject& ResDataObject::operator=(const ResDataObject& tValue) {
  1025. if (this == &tValue) {
  1026. return *this;
  1027. }
  1028. // 安全地处理字符串赋值
  1029. if (m_encode) {
  1030. *m_encode = tValue.m_encode ? *tValue.m_encode : "";
  1031. }
  1032. else if (tValue.m_encode) {
  1033. m_encode = std::make_unique<std::string>(*tValue.m_encode);
  1034. }
  1035. if (m_value) {
  1036. *m_value = tValue.m_value ? *tValue.m_value : "";
  1037. }
  1038. else if (tValue.m_value) {
  1039. m_value = std::make_unique<std::string>(*tValue.m_value);
  1040. }
  1041. // 处理向量数据
  1042. if (!tValue.m_vec) {
  1043. m_vec.reset();
  1044. }
  1045. else {
  1046. if (!m_vec) {
  1047. m_vec = std::make_unique<std::vector<ResDataObject_Pair>>();
  1048. }
  1049. m_vec->clear();
  1050. m_vec->reserve(tValue.m_vec->size());
  1051. for (const auto& src_pair : *tValue.m_vec) {
  1052. m_vec->emplace_back(src_pair);
  1053. }
  1054. }
  1055. return *this;
  1056. }
  1057. ResDataObject& ResDataObject::operator = (const bool tValue)
  1058. {
  1059. std::stringstream strm;
  1060. strm << tValue;
  1061. (*this) = strm.str().c_str();
  1062. return (*this);
  1063. }
  1064. ResDataObject& ResDataObject::operator = (const char tValue)
  1065. {
  1066. std::stringstream strm;
  1067. strm << tValue;
  1068. (*this) = strm.str().c_str();
  1069. return (*this);
  1070. }
  1071. ResDataObject& ResDataObject::operator = (const unsigned char tValue)
  1072. {
  1073. std::stringstream strm;
  1074. strm << tValue;
  1075. (*this) = strm.str().c_str();
  1076. return (*this);
  1077. }
  1078. ResDataObject& ResDataObject::operator = (const short tValue)
  1079. {
  1080. std::stringstream strm;
  1081. strm << tValue;
  1082. (*this) = strm.str().c_str();
  1083. return (*this);
  1084. }
  1085. ResDataObject& ResDataObject::operator = (const unsigned short tValue)
  1086. {
  1087. std::stringstream strm;
  1088. strm << tValue;
  1089. (*this) = strm.str().c_str();
  1090. return (*this);
  1091. }
  1092. ResDataObject& ResDataObject::operator = (const int tValue)
  1093. {
  1094. std::stringstream strm;
  1095. strm << tValue;
  1096. (*this) = strm.str().c_str();
  1097. return (*this);
  1098. }
  1099. ResDataObject& ResDataObject::operator = (const unsigned int tValue)
  1100. {
  1101. std::stringstream strm;
  1102. strm << tValue;
  1103. (*this) = strm.str().c_str();
  1104. return (*this);
  1105. }
  1106. ResDataObject& ResDataObject::operator = (const long tValue)
  1107. {
  1108. std::stringstream strm;
  1109. strm << tValue;
  1110. (*this) = strm.str().c_str();
  1111. return (*this);
  1112. }
  1113. ResDataObject& ResDataObject::operator = (const unsigned long tValue)
  1114. {
  1115. std::stringstream strm;
  1116. strm << tValue;
  1117. (*this) = strm.str().c_str();
  1118. return (*this);
  1119. }
  1120. ResDataObject& ResDataObject::operator = (const long long tValue)
  1121. {
  1122. std::stringstream strm;
  1123. strm << tValue;
  1124. (*this) = strm.str().c_str();
  1125. return (*this);
  1126. }
  1127. ResDataObject& ResDataObject::operator = (const unsigned long long tValue)
  1128. {
  1129. std::stringstream strm;
  1130. strm << tValue;
  1131. (*this) = strm.str().c_str();
  1132. return (*this);
  1133. }
  1134. ResDataObject& ResDataObject::operator = (const float tValue)
  1135. {
  1136. double DtValue = tValue;
  1137. (*this) = DtValue;
  1138. return (*this);
  1139. }
  1140. ResDataObject& ResDataObject::operator = (const double tValue)
  1141. {
  1142. //unsigned long long temp,*pF ;
  1143. std::stringstream strm;
  1144. //strm.precision(numeric_limits<double>::digits10);
  1145. //新方案(丢精度)
  1146. char szStr[64];
  1147. snprintf(szStr, sizeof(szStr), "%.17g", tValue);
  1148. strm << szStr;
  1149. (*this) = strm.str().c_str();
  1150. return (*this);
  1151. //旧方案(保持精度)
  1152. //assert(sizeof(double) == sizeof(unsigned long long));
  1153. //pF = (unsigned long long*)&tValue;
  1154. //temp = (*pF);
  1155. //strm << temp;
  1156. //(*this) = strm.str().c_str();
  1157. //return (*this);
  1158. }
  1159. bool ResDataObject::add(const char* pKey,bool tValue)
  1160. {
  1161. ResDataObject obj;
  1162. obj = tValue;
  1163. return add(pKey,obj);
  1164. }
  1165. bool ResDataObject::add(const char* pKey,char tValue)
  1166. {
  1167. ResDataObject obj;
  1168. obj = tValue;
  1169. return add(pKey,obj);
  1170. }
  1171. bool ResDataObject::add(const char* pKey,unsigned char tValue)
  1172. {
  1173. ResDataObject obj;
  1174. obj = tValue;
  1175. return add(pKey,obj);
  1176. }
  1177. bool ResDataObject::add(const char* pKey,short tValue)
  1178. {
  1179. ResDataObject obj;
  1180. obj = tValue;
  1181. return add(pKey,obj);
  1182. }
  1183. bool ResDataObject::add(const char* pKey,unsigned short tValue)
  1184. {
  1185. ResDataObject obj;
  1186. obj = tValue;
  1187. return add(pKey,obj);
  1188. }
  1189. bool ResDataObject::add(const char* pKey,int tValue)
  1190. {
  1191. ResDataObject obj;
  1192. obj = tValue;
  1193. return add(pKey,obj);
  1194. }
  1195. bool ResDataObject::add(const char* pKey,unsigned int tValue)
  1196. {
  1197. ResDataObject obj;
  1198. obj = tValue;
  1199. return add(pKey,obj);
  1200. }
  1201. bool ResDataObject::add(const char* pKey,long tValue)
  1202. {
  1203. ResDataObject obj;
  1204. obj = tValue;
  1205. return add(pKey,obj);
  1206. }
  1207. bool ResDataObject::add(const char* pKey,unsigned long tValue)
  1208. {
  1209. ResDataObject obj;
  1210. obj = tValue;
  1211. return add(pKey,obj);
  1212. }
  1213. bool ResDataObject::add(const char* pKey,long long tValue)
  1214. {
  1215. ResDataObject obj;
  1216. obj = tValue;
  1217. return add(pKey,obj);
  1218. }
  1219. bool ResDataObject::add(const char* pKey,unsigned long long tValue)
  1220. {
  1221. ResDataObject obj;
  1222. obj = tValue;
  1223. return add(pKey,obj);
  1224. }
  1225. bool ResDataObject::add(const char* pKey,float tValue)
  1226. {
  1227. ResDataObject obj;
  1228. obj = tValue;
  1229. return add(pKey,obj);
  1230. }
  1231. bool ResDataObject::add(const char* pKey,double tValue)
  1232. {
  1233. ResDataObject obj;
  1234. obj = tValue;
  1235. return add(pKey,obj);
  1236. }
  1237. bool ResDataObject::add(const char* pKey,const char* pValue)
  1238. {
  1239. ResDataObject obj;
  1240. if (!pValue || reinterpret_cast<uintptr_t>(pValue) < 0x1000) {
  1241. obj = ""; // 无效值用空字符串替代
  1242. }
  1243. else {
  1244. obj = pValue;
  1245. }
  1246. return add(pKey,obj);
  1247. }
  1248. bool ResDataObject::add(const char* pKey,ResDataObject &dataobj)
  1249. {
  1250. if (!m_vec) {
  1251. std::cerr << "Error: m_vec is null in ResDataObject::add" << std::endl;
  1252. m_vec = std::make_unique<std::vector<ResDataObject_Pair>>();
  1253. }
  1254. const char* safeKey = pKey ? pKey : "";
  1255. try {
  1256. m_vec->emplace_back();
  1257. auto& new_pair = m_vec->back();
  1258. if (new_pair.first) {
  1259. *new_pair.first = safeKey;
  1260. }
  1261. else {
  1262. new_pair.first = std::make_unique<std::string>(safeKey);
  1263. }
  1264. if (new_pair.second) {
  1265. *new_pair.second = dataobj;
  1266. }
  1267. else {
  1268. new_pair.second = std::make_unique<ResDataObject>(dataobj);
  1269. }
  1270. return true;
  1271. }
  1272. catch (const std::exception& e) {
  1273. std::cerr << "Error in ResDataObject::add: " << e.what() << std::endl;
  1274. return false;
  1275. }
  1276. catch (...) {
  1277. std::cerr << "Unknown error in ResDataObject::add" << std::endl;
  1278. return false;
  1279. }
  1280. };
  1281. bool ResDataObject::update(const char* pKey,bool tValue)
  1282. {
  1283. eraseAllOf(pKey);
  1284. return add(pKey,tValue);
  1285. }
  1286. bool ResDataObject::update(const char* pKey,char tValue)
  1287. {
  1288. eraseAllOf(pKey);
  1289. return add(pKey,tValue);
  1290. }
  1291. bool ResDataObject::update(const char* pKey,unsigned char tValue)
  1292. {
  1293. eraseAllOf(pKey);
  1294. return add(pKey,tValue);
  1295. }
  1296. bool ResDataObject::update(const char* pKey,short tValue)
  1297. {
  1298. eraseAllOf(pKey);
  1299. return add(pKey,tValue);
  1300. }
  1301. bool ResDataObject::update(const char* pKey,unsigned short tValue)
  1302. {
  1303. eraseAllOf(pKey);
  1304. return add(pKey,tValue);
  1305. }
  1306. bool ResDataObject::update(const char* pKey,int tValue)
  1307. {
  1308. eraseAllOf(pKey);
  1309. return add(pKey,tValue);
  1310. }
  1311. bool ResDataObject::update(const char* pKey,unsigned int tValue)
  1312. {
  1313. eraseAllOf(pKey);
  1314. return add(pKey,tValue);
  1315. }
  1316. bool ResDataObject::update(const char* pKey,long tValue)
  1317. {
  1318. eraseAllOf(pKey);
  1319. return add(pKey,tValue);
  1320. }
  1321. bool ResDataObject::update(const char* pKey,unsigned long tValue)
  1322. {
  1323. eraseAllOf(pKey);
  1324. return add(pKey,tValue);
  1325. }
  1326. bool ResDataObject::update(const char* pKey,long long tValue)
  1327. {
  1328. eraseAllOf(pKey);
  1329. return add(pKey,tValue);
  1330. }
  1331. bool ResDataObject::update(const char* pKey,unsigned long long tValue)
  1332. {
  1333. eraseAllOf(pKey);
  1334. return add(pKey,tValue);
  1335. }
  1336. bool ResDataObject::update(const char* pKey,float tValue)
  1337. {
  1338. eraseAllOf(pKey);
  1339. return add(pKey,tValue);
  1340. }
  1341. bool ResDataObject::update(const char* pKey,double tValue)
  1342. {
  1343. eraseAllOf(pKey);
  1344. return add(pKey,tValue);
  1345. }
  1346. bool ResDataObject::update(const char* pKey,const char* pValue)
  1347. {
  1348. eraseAllOf(pKey);
  1349. return add(pKey,pValue);
  1350. }
  1351. bool ResDataObject::update(const char* pKey,ResDataObject &dataobj)
  1352. {
  1353. eraseAllOf(pKey);
  1354. return add(pKey,dataobj);
  1355. }
  1356. bool ResDataObject::operator == (const char* pVal)
  1357. {
  1358. std::string exVal = pVal;
  1359. return ((*m_value) == exVal);
  1360. }
  1361. bool ResDataObject::operator == (const ResDataObject &Obj)
  1362. {
  1363. //compare val
  1364. if ((*m_value) != (*(Obj.m_value)))
  1365. {
  1366. return false;
  1367. }
  1368. //compare count
  1369. if (m_vec->size() != Obj.m_vec->size())
  1370. {
  1371. return false;
  1372. }
  1373. //compare vec
  1374. for (size_t i = 0; i < m_vec->size(); i++)
  1375. {
  1376. if ((*((*m_vec)[i]).first) != (*((*Obj.m_vec)[i]).first))
  1377. {
  1378. return false;
  1379. }
  1380. if ((*((*m_vec)[i]).second) == (*((*Obj.m_vec)[i]).second))
  1381. {
  1382. continue;
  1383. }
  1384. return false;
  1385. }
  1386. return true;
  1387. }
  1388. RESDATAOBJECT_C_API bool TryGetValue(ResDataObject &obj, const char *pKey, ResDataObject &res)
  1389. {
  1390. bool ret = true;
  1391. try {
  1392. res.clear();
  1393. if (obj.GetFirstOf(pKey) >= 0)
  1394. {
  1395. res = obj[pKey];
  1396. }
  1397. else
  1398. {
  1399. ret = false;
  1400. }
  1401. }
  1402. catch (...)
  1403. {
  1404. res.clear();
  1405. ret = false;
  1406. }
  1407. return ret;
  1408. }
  1409. //----------------------------------------------------------------
  1410. ExJsonDataObject::ExJsonDataObject()
  1411. : m_pKey(std::make_unique<std::string>()),
  1412. m_ValString(std::make_unique<std::string>()),
  1413. m_pTargetObject(std::make_unique<ResDataObject>()) {
  1414. }
  1415. ExJsonDataObject::ExJsonDataObject(const ExJsonDataObject& tValue)
  1416. : m_pKey(std::make_unique<std::string>(*tValue.m_pKey)),
  1417. m_ValString(std::make_unique<std::string>(*tValue.m_ValString)),
  1418. m_pTargetObject(std::make_unique<ResDataObject>(*tValue.m_pTargetObject)) {
  1419. }
  1420. //base
  1421. void ExJsonDataObject::SetKey(const char *pKey)
  1422. {
  1423. *m_pKey = pKey ? pKey : "";
  1424. }
  1425. const char *ExJsonDataObject::GetKey()
  1426. {
  1427. return m_pKey->c_str();
  1428. }
  1429. ResDataObject &ExJsonDataObject::GetResDataObject()
  1430. {
  1431. return (*m_pTargetObject);
  1432. }
  1433. bool ExJsonDataObject::SetResDataObject(ResDataObject &obj)
  1434. {
  1435. bool ret = true;
  1436. try {
  1437. //check it first
  1438. for (size_t i = 0; i < m_pTargetObject->size(); i++)
  1439. {
  1440. const char* pKey = m_pTargetObject->GetKey(i);
  1441. if (pKey)
  1442. {
  1443. int IdxObj = obj.GetFirstOf(pKey);
  1444. if (IdxObj < 0)
  1445. {
  1446. ret = false;
  1447. break;
  1448. }
  1449. }
  1450. else
  1451. {
  1452. ret = false;
  1453. break;
  1454. }
  1455. }
  1456. //copy it
  1457. if (ret)
  1458. {
  1459. (*m_pTargetObject) = obj;
  1460. //for (size_t i = 0; i < m_pTargetObject->size(); i++)
  1461. //{
  1462. // (*m_pTargetObject)[i] = obj[obj.GetFirstOf(m_pTargetObject->GetKey(i))];
  1463. //}
  1464. }
  1465. }
  1466. catch (...)
  1467. {
  1468. ret = false;
  1469. }
  1470. return ret;
  1471. }
  1472. ResDataObject &ExJsonDataObject::operator [](const char *pKey)
  1473. {
  1474. try
  1475. {
  1476. return (*m_pTargetObject)[pKey];
  1477. }
  1478. catch (...)
  1479. {
  1480. assert(0);//make sure never gonna happen
  1481. }
  1482. return (*m_pTargetObject);
  1483. }
  1484. ExJsonDataObject &ExJsonDataObject::operator = (const ExJsonDataObject &tValue)
  1485. {
  1486. if (this != &tValue)
  1487. {
  1488. (*m_pKey) = *(tValue.m_pKey);
  1489. (*m_ValString) = *(tValue.m_ValString);
  1490. (*m_pTargetObject) = *(tValue.m_pTargetObject);
  1491. }
  1492. return (*this);
  1493. }