SpMisc.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpMisc.h"
  4. #include "SpEntity.h"
  5. #include "SpModule.h"
  6. #include "sp_def.h"
  7. #include "sp_dbg_export.h"
  8. #include "sp_svc.h"
  9. #include "sp_env.h"
  10. #include "dbgutil.h"
  11. #include "spinlock.h"
  12. #include "memutil.h"
  13. #include "fileutil.h"
  14. #ifdef _WIN32
  15. #include "sp_checkEntity.h"
  16. #endif //_WIN32
  17. #include "json/json.h"
  18. #include <algorithm>
  19. #include <locale>
  20. #include <winpr/ini.h>
  21. #include <winpr/string.h>
  22. #include <winpr/crt.h>
  23. //////////////////////////////////////////////////////////////////////////
  24. // CSmallDateTime
  25. //////////////////////////////////////////////////////////////////////////
  26. #define __reference_point 946656000LL // _mktime64 2000-01-01 00:00:00
  27. // 2000-01-01 00:00:00
  28. CSmallDateTime CSmallDateTime::BeginTime(0);
  29. // about 136 years after 2000-01-01
  30. CSmallDateTime CSmallDateTime::EndTime(0xffffffff);
  31. CSmallDateTime CSmallDateTime::GetNow()
  32. {
  33. #ifdef _WIN32
  34. __time64_t now;
  35. //struct tm t;
  36. _time64(&now);
  37. //_localtime64_s(&t, &now);
  38. #else
  39. time_t now;
  40. time(&now);
  41. #endif
  42. return CSmallDateTime((DWORD)(now - __reference_point));
  43. }
  44. __time64_t CSmallDateTime::GetTime64()
  45. {
  46. return (__time64_t)(__reference_point + m_nTimeTicks);
  47. }
  48. CSimpleStringA CSmallDateTime::ToTimeString()
  49. {
  50. char buff[80];
  51. struct tm when;
  52. #ifdef _WIN32
  53. __time64_t t = __reference_point + m_nTimeTicks;
  54. _localtime64_s(&when, &t);
  55. #else
  56. time_t t = __reference_point + m_nTimeTicks;
  57. localtime_r(&t, &when);
  58. #endif
  59. //asctime_s( buff, sizeof(buff), &when );
  60. sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d", when.tm_year + 1900, when.tm_mon+1, when.tm_mday,
  61. when.tm_hour, when.tm_min, when.tm_sec);
  62. return CSimpleStringA(buff);
  63. }
  64. SYSTEMTIME CSmallDateTime::ToSystemTime()
  65. {
  66. struct tm when;
  67. #ifdef _WIN32
  68. __time64_t t = __reference_point + m_nTimeTicks;
  69. _localtime64_s(&when, &t);
  70. #else
  71. time_t t = __reference_point + m_nTimeTicks;
  72. localtime_r(&t, &when);
  73. #endif
  74. SYSTEMTIME st = {};
  75. st.wYear = when.tm_year + 1900;
  76. st.wMonth = when.tm_mon+1;
  77. st.wDay = when.tm_mday;
  78. st.wHour = when.tm_hour;
  79. st.wMinute = when.tm_min;
  80. st.wSecond = when.tm_sec;
  81. return st;
  82. }
  83. void CSmallDateTime::FromSystemTime(const SYSTEMTIME &st)
  84. {
  85. struct tm when;
  86. when.tm_year = st.wYear - 1900;
  87. when.tm_mon = st.wMonth - 1;
  88. when.tm_mday = st.wDay;
  89. when.tm_hour = st.wHour;
  90. when.tm_min = st.wMinute;
  91. when.tm_sec = st.wSecond;
  92. #ifdef _WIN32
  93. __time64_t t = _mktime64(&when);
  94. #else
  95. time_t t = mktime(&when);
  96. #endif
  97. m_nTimeTicks = t - __reference_point;
  98. }
  99. //////////////////////////////////////////////////////////////////////////
  100. // CUUID
  101. //////////////////////////////////////////////////////////////////////////
  102. #ifndef _WIN32
  103. WORD CUUID::m_gAppID(0);
  104. #endif //NOT _WIN32
  105. void SpInitUUID(WORD wId)
  106. {
  107. TOOLKIT_ASSERT(wId != 0);
  108. #ifdef _WIN32
  109. getEntityResource()->m_gAppID = wId;
  110. #else
  111. CUUID::m_gAppID = wId;
  112. #endif //_WIN32
  113. }
  114. CUUID CUUID::Create(const CUUID &LastUUID)
  115. {
  116. #ifdef _WIN32
  117. TOOLKIT_ASSERT(getEntityResource()->m_gAppID != 0);
  118. #endif //_WIN32
  119. CUUID t(0);
  120. #ifdef _WIN32
  121. t.m_nAppID = getEntityResource()->m_gAppID;
  122. #else
  123. t.m_nAppID = CUUID::m_gAppID;
  124. #endif //_WIN32
  125. DWORD now = (DWORD) CSmallDateTime::GetNow();
  126. if (now == LastUUID.m_nTimeTicks)
  127. {
  128. t.m_nDsn = LastUUID.m_nDsn+1;
  129. if (t.m_nDsn <=0)
  130. {
  131. t.m_nTimeTicks = now + 1;
  132. t.m_nDsn = 0;
  133. }
  134. else
  135. {
  136. t.m_nTimeTicks = now;
  137. }
  138. }
  139. else
  140. {
  141. t.m_nTimeTicks = now;
  142. t.m_nDsn = 0;
  143. }
  144. //LastUUID.m_nUUID64 = t.m_nUUID64;
  145. return t;
  146. }
  147. CUUID CUUID::Create(const CUUID& LastUUID, const CSmallDateTime& DateTime)
  148. {
  149. #ifdef _WIN32
  150. TOOLKIT_ASSERT(getEntityResource()->m_gAppID != 0);
  151. #endif //_WIN32
  152. CUUID t(0);
  153. int repeat = 0;
  154. #ifdef _WIN32
  155. t.m_nAppID = getEntityResource()->m_gAppID;
  156. #else
  157. t.m_nAppID = CUUID::m_gAppID;
  158. #endif //_WIN32
  159. t.m_nTimeTicks = DateTime;
  160. t.m_nDsn = 0;
  161. //LastUUID.m_nUUID64 = t.m_nUUID64;
  162. return t;
  163. }
  164. /////////////////////////////////////////////////////////////////////////////////////////////////////
  165. /// CSphereVector
  166. /////////////////////////////////////////////////////////////////////////////////////////////////////
  167. // 将float转成度分秒表示法,第4个字节表示经度或纬度方向
  168. void CSphereVector::GetBinaryLongitude(BYTE longitude[4])
  169. {
  170. float fTemp = m_fLongitude;
  171. if (fTemp < 0)
  172. {
  173. longitude[3] = 'W';
  174. fTemp = -fTemp;
  175. }
  176. else
  177. longitude[3] = 'E';
  178. longitude[0] = (int)fTemp;
  179. longitude[1] = (int)(fTemp * 60) % 60;
  180. longitude[2] = (int)(fTemp * 3600) % 60;
  181. }
  182. void CSphereVector::GetBinaryLatitude(BYTE latitude[4])
  183. {
  184. float fTemp = m_fLongitude;
  185. if (fTemp < 0)
  186. {
  187. latitude[3] = 'S';
  188. fTemp = -fTemp;
  189. }
  190. else
  191. latitude[3] = 'N';
  192. latitude[0] = (int)fTemp;
  193. latitude[1] = (int)(fTemp * 60) % 60;
  194. latitude[2] = (int)(fTemp * 3600) % 60;
  195. }
  196. /////////////////////////////////////////////////////////////////////////////////////////////////////
  197. ErrorCodeEnum SpIniFileReadString( const char *pszFile, const char *pszSection, const char *pszKey, CSimpleStringA &strValue )
  198. {
  199. DWORD dwInitCapacity = 16, dwSize = 0;
  200. const int tries = 20;
  201. int i;
  202. for (i = 0; i < tries; ++i) {
  203. CSimpleStringA strTmp('\0', dwInitCapacity);
  204. dwSize = ::GetPrivateProfileStringA(pszSection, pszKey, "", &strTmp[0], dwInitCapacity, pszFile);
  205. if (dwSize == dwInitCapacity-1) {
  206. dwInitCapacity = dwInitCapacity * 2; // buffer is too small, double size
  207. } else {
  208. strValue = strTmp;
  209. break;
  210. }
  211. }
  212. if (i == tries)
  213. return Error_Unexpect;
  214. return Error_Succeed;
  215. }
  216. ErrorCodeEnum SpIniFileWriteString( const char *pszFile, const char *pszSection, const char *pszKey, const char *pszValue )
  217. {
  218. BOOL bRet = ::WritePrivateProfileStringA(pszSection, pszKey, pszValue, pszFile);
  219. WritePrivateProfileStringA(NULL, NULL, NULL, pszFile);
  220. if (bRet)
  221. return Error_Succeed;
  222. return Error_Unexpect;
  223. }
  224. ErrorCodeEnum SpIniFileReadAllKeys(const char *pszFile, const char *pszSection, CAutoArray<CSimpleStringA> &Keys)
  225. {
  226. DWORD dwInitialCapacity = 16;
  227. DWORD dwSize;
  228. char *p = NULL;
  229. const int tries = 20;
  230. ErrorCodeEnum Error = Error_Succeed;
  231. int i;
  232. for (i = 0; i < tries; ++i) {
  233. p = (char*)realloc(p, dwInitialCapacity);
  234. dwSize = ::GetPrivateProfileStringA(pszSection, NULL, NULL, p, dwInitialCapacity, pszFile);
  235. if (dwSize == dwInitialCapacity-2) { // buffer is too small
  236. dwInitialCapacity = dwInitialCapacity * 2;
  237. } else {
  238. break;
  239. }
  240. }
  241. if (i < tries) {
  242. int cnt = 0;
  243. char *t = p;
  244. size_t n;
  245. while ((n = strlen(t)) > 0) {
  246. cnt ++;
  247. t += n+1;
  248. }
  249. Keys.Init(cnt);
  250. t = p;
  251. for (i = 0; i < cnt; ++i) {
  252. Keys[i] = t;
  253. t = t + strlen(t)+1;
  254. }
  255. } else {
  256. Error = Error_Unexpect;
  257. }
  258. free(p);
  259. return Error;
  260. }
  261. ErrorCodeEnum SpIniFileReadAllSections(const char *pszFile, CAutoArray<CSimpleStringA> &Sections)
  262. {
  263. DWORD dwInitialCapacity = 16;
  264. DWORD dwSize;
  265. char *p = NULL;
  266. const int tries = 20;
  267. ErrorCodeEnum Error = Error_Succeed;
  268. int i;
  269. for (i = 0; i < tries; ++i) {
  270. p = (char*)realloc(p, dwInitialCapacity);
  271. dwSize = ::GetPrivateProfileSectionNamesA(p, dwInitialCapacity, pszFile);
  272. if (dwSize == dwInitialCapacity-2) { // buffer is too small
  273. dwInitialCapacity = dwInitialCapacity * 2;
  274. } else {
  275. break;
  276. }
  277. }
  278. if (i < tries) {
  279. int cnt = 0;
  280. char *t = p;
  281. size_t n;
  282. while ((n = strlen(t)) > 0) {
  283. cnt ++;
  284. t += n+1;
  285. }
  286. Sections.Init(cnt);
  287. t = p;
  288. for (i = 0; i < cnt; ++i) {
  289. Sections[i] = t;
  290. t = t + strlen(t)+1;
  291. }
  292. } else {
  293. Error = Error_Unexpect;
  294. }
  295. free(p);
  296. return Error;
  297. }
  298. ErrorCodeEnum SpIniConfig::ReadConfigValue(const char *pszSection, const char *pszKey, CSimpleStringA &strValue)
  299. {
  300. _ASSERT(pszSection);
  301. _ASSERT(pszKey);
  302. CSimpleStringA path;
  303. ErrorCodeEnum Error = _GetMappFilePath(path);
  304. if (Error == Error_Succeed)
  305. Error = SpIniFileReadString(path, pszSection, pszKey, strValue);
  306. return Error;
  307. }
  308. ErrorCodeEnum SpIniConfig::ReadConfigValueInt(const char *pszSection, const char *pszKey, int &iValue)
  309. {
  310. CSimpleStringA strValue;
  311. ErrorCodeEnum Error = ReadConfigValue(pszSection, pszKey, strValue);
  312. if (Error == Error_Succeed) {
  313. if (strValue.IsStartWith("0x")) // support hex string
  314. sscanf(strValue, "0x%X", &iValue);
  315. else
  316. iValue = atoi(strValue);
  317. }
  318. return Error;
  319. }
  320. //TODO: forbid from write cfg/entity.ini
  321. ErrorCodeEnum SpIniConfig::WriteConfigValue(const char *pszSection, const char *pszKey, const char *pszValue)
  322. {
  323. if (m_type == Config_Hardware || m_type == Config_Shell || m_type == Config_Root || m_type == Config_CenterSetting) {
  324. return Error_Readonly;
  325. }
  326. CSimpleStringA path;
  327. ErrorCodeEnum Error = _GetMappFilePath(path);
  328. if (Error == Error_Succeed) {
  329. Error = SpIniFileWriteString(path, pszSection, pszKey, pszValue);
  330. }
  331. return Error;
  332. }
  333. ErrorCodeEnum SpIniConfig::WriteConfigValueInt(const char *pszSection, const char *pszKey, int iValue)
  334. {
  335. char tmp[32];
  336. _itoa(iValue, tmp, 10);
  337. return WriteConfigValue(pszSection, pszKey, tmp);
  338. }
  339. ErrorCodeEnum SpIniConfig::WriteConfigValueHexInt(const char *pszSection, const char *pszKey, UINT64 nValue)
  340. {
  341. char tmp[32];
  342. #if defined(_MSC_VER)
  343. sprintf_s(tmp, sizeof(tmp), "0x%I64X", nValue);
  344. #else
  345. sprintf_s(tmp, sizeof(tmp), "0x%" PRIx64 "", nValue);
  346. #endif //_MSC_VER
  347. return WriteConfigValue(pszSection, pszKey, tmp);
  348. }
  349. ErrorCodeEnum SpIniConfig::ReadConfigValueHexInt(const char *pszSection, const char *pszKey, UINT64 &nValue)
  350. {
  351. nValue = 0;
  352. CSimpleStringA strValue;
  353. ErrorCodeEnum Error = ReadConfigValue(pszSection, pszKey, strValue);
  354. if (Error == Error_Succeed && strValue.GetLength() > 0) {
  355. #if defined(_MSC_VER)
  356. if (strValue.IsStartWith("0x")) // support hex string
  357. sscanf(strValue, "0x%I64X", &nValue);
  358. else
  359. sscanf(strValue, "%I64X", &nValue);
  360. #else
  361. if (strValue.IsStartWith("0x")) // support hex string
  362. sscanf(strValue, "0x%" PRIx64 "", &nValue);
  363. else
  364. sscanf(strValue, "%" PRIx64 "", &nValue);
  365. #endif //_MSC_VER
  366. }
  367. return Error;
  368. }
  369. ErrorCodeEnum SpIniConfig::EstimateBeforeRW()
  370. {
  371. ErrorCodeEnum result(Error_Succeed);
  372. if (m_type == Config_Hardware
  373. || m_type == Config_Shell
  374. || m_type == Config_Root
  375. || m_type == Config_CenterSetting
  376. || m_type == Config_Software) {
  377. CSimpleStringA strAbsolutePath(true);
  378. result = _GetMappFilePath(strAbsolutePath);
  379. if (Error_Succeed == result && !strAbsolutePath.IsNullOrEmpty()) {
  380. if (!ExistsFileA(strAbsolutePath)) {
  381. result = Error_NotExist;
  382. }
  383. } else if (Error_Succeed == result) {
  384. result = Error_Null;
  385. }
  386. }
  387. return result;
  388. }
  389. ErrorCodeEnum SpIniConfig::_GetMappFilePath(CSimpleStringA& strPath)
  390. {
  391. char tmp[MAX_PATH];
  392. ErrorCodeEnum Error;
  393. sp_env_t* env = sp_get_env();
  394. int flag;
  395. if (m_type == Config_Hardware) {
  396. flag = SP_DIR_DEVICE_ENTITY_INI;
  397. } else if (m_type == Config_Software) {
  398. flag = SP_DIR_ENTITY_INI;
  399. } else if (m_type == Config_Run) {
  400. flag = SP_DIR_RUNINFO_INI;
  401. } else if (m_type == Config_Shell) {
  402. flag = SP_DIR_SHELL_INI;
  403. } else if (m_type == Config_Root) {
  404. flag = SP_DIR_ROOT_INI;
  405. } else if (m_type == Config_CenterSetting) {
  406. flag = SP_DIR_CENTER_SETTING_INI;
  407. } else if (m_type == Config_Cache) {
  408. flag = SP_DIR_RUNINFO_GLOBAL_INI;
  409. } else {
  410. return Error_Param;
  411. }
  412. /** 集中配置文件的获取单独处理*/
  413. if (m_type == Config_CenterSetting) {
  414. CSimpleStringA strCtsPath;
  415. Error = m_pEntity->GetPath("CenterSetting", strCtsPath);
  416. strcpy_s(tmp, MAX_PATH, strCtsPath);
  417. } else {
  418. Error = (ErrorCodeEnum)sp_dir_get_path_new(env->dir, flag, m_pEntity->get_ent()->cfg->name, tmp, MAX_PATH,
  419. detect_env_test_mode(env->cfg->args->test_mode));
  420. }
  421. strPath = tmp;
  422. return Error;
  423. }
  424. ErrorCodeEnum SpIniConfig::ReadAllKeys(const char *pszSection, CAutoArray<CSimpleStringA> &strKeys)
  425. {
  426. _ASSERT(pszSection);
  427. CSimpleStringA path;
  428. ErrorCodeEnum Error = _GetMappFilePath(path);
  429. if (Error == Error_Succeed)
  430. Error = SpIniFileReadAllKeys(path, pszSection, strKeys);
  431. return Error;
  432. }
  433. ErrorCodeEnum SpIniConfig::ReadAllSections(CAutoArray<CSimpleStringA> &strSections)
  434. {
  435. CSimpleStringA path;
  436. ErrorCodeEnum Error = _GetMappFilePath(path);
  437. if (Error == Error_Succeed)
  438. Error = SpIniFileReadAllSections(path, strSections);
  439. return Error;
  440. }
  441. class SpRootMemConfig::Impl {
  442. public:
  443. static std::string toLowerCase(std::string str) {
  444. #ifdef _WIN32
  445. std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
  446. return std::tolower(c, std::locale());
  447. });
  448. #else
  449. for (char& c : str) {
  450. c = std::tolower(c);
  451. }
  452. #endif
  453. return str;
  454. }
  455. ConfigTypeEnum type;
  456. std::map<std::string, std::map<std::string, std::string>> m_deviceConfig;
  457. std::map<std::string, std::map<std::string, std::string>> m_centerConfig;
  458. std::map<std::string, std::map<std::string, std::string>> m_shellConfig;
  459. std::map<std::string, std::map<std::string, std::string>> m_nullConfig;
  460. std::map<std::string, std::map<std::string, std::string>>& getCurConfig()
  461. {
  462. if (type == ConfigTypeEnum::Config_CenterSetting)
  463. return m_centerConfig;
  464. else if (type == ConfigTypeEnum::Config_Root)
  465. return m_deviceConfig;
  466. else if (type == ConfigTypeEnum::Config_Shell)
  467. return m_shellConfig;
  468. else
  469. return m_nullConfig;
  470. }
  471. static std::pair<bool, std::string> ConvertStrToDeviceConfig(std::string input, std::map<std::string, std::map<std::string, std::string>> &config)
  472. {
  473. // 解析为Json::Value
  474. Json::Reader reader;
  475. Json::Value jsonValue;
  476. bool success = reader.parse(input, jsonValue);
  477. if (!success)
  478. return std::make_pair(false,
  479. CSimpleStringA::Format("ConvertStrToDeviceConfig Failed to parse input: %s", reader.getFormattedErrorMessages().c_str()).GetData());
  480. // 转换为std::map
  481. config.clear();
  482. Json::Value::Members outer_members = jsonValue.getMemberNames();
  483. for (Json::Value::Members::iterator outer_it = outer_members.begin(); outer_it != outer_members.end(); ++outer_it) {
  484. const std::string& outer_key = *outer_it;
  485. const Json::Value& inner_value = jsonValue[outer_key];
  486. std::map<std::string, std::string> inner_map;
  487. Json::Value::Members inner_members = inner_value.getMemberNames();
  488. for (Json::Value::Members::iterator inner_it = inner_members.begin(); inner_it != inner_members.end(); ++inner_it) {
  489. const std::string& inner_key = *inner_it;
  490. const std::string& inner_value = jsonValue[outer_key][inner_key].asString();
  491. inner_map.insert(std::make_pair(toLowerCase(inner_key), inner_value));
  492. }
  493. config.insert(std::make_pair(toLowerCase(outer_key), inner_map));
  494. }
  495. // 输出结果
  496. return std::make_pair(true, "");
  497. }
  498. };
  499. SpRootMemConfig::SpRootMemConfig(SpEntity* pEntity, ConfigTypeEnum type)
  500. : m_pEntity(pEntity)
  501. , m_type(type)
  502. , m_impl(new Impl())
  503. {
  504. sp_env_t* env = sp_get_env();
  505. sp_cfg_t* cfg = env->cfg;
  506. sp_cfg_root_ini_t* root_ini = cfg->root_ini;
  507. m_impl->type = type;
  508. if (m_impl->m_deviceConfig.size() == 0 && type == ConfigTypeEnum::Config_Root)//首次初始化
  509. m_impl->ConvertStrToDeviceConfig(root_ini->root_config, m_impl->m_deviceConfig);
  510. if (m_impl->m_centerConfig.size() == 0 && type == ConfigTypeEnum::Config_CenterSetting)
  511. m_impl->ConvertStrToDeviceConfig(root_ini->center_config, m_impl->m_centerConfig);
  512. if (m_impl->m_shellConfig.size() == 0 && type == ConfigTypeEnum::Config_Shell)
  513. m_impl->ConvertStrToDeviceConfig(root_ini->shell_config, m_impl->m_shellConfig);
  514. }
  515. SpRootMemConfig::~SpRootMemConfig()
  516. {
  517. if (m_impl != NULL)
  518. {
  519. delete m_impl;
  520. }
  521. }
  522. ErrorCodeEnum SpRootMemConfig::ReadConfigValue(const char* pszSection, const char* pszKey, CSimpleStringA& strValue)
  523. {
  524. _ASSERT(pszSection);
  525. _ASSERT(pszKey);
  526. auto config = m_impl->getCurConfig();
  527. //can not find section
  528. //都转换成小写,避免查不到
  529. auto dstSection = m_impl->toLowerCase(pszSection);
  530. auto dstKey = m_impl->toLowerCase(pszKey);
  531. //因为存储名字不同,terminal需要进行转换
  532. if (dstSection == "terminal")
  533. dstSection = "terminalinfo";
  534. DWORD dwInitCapacity = 16;
  535. CSimpleStringA strTmp('\0', dwInitCapacity);
  536. if (config.find(dstSection) == config.end())
  537. {
  538. strValue = strTmp;
  539. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("ReadConfigValue can not find section:%s", dstSection.c_str());
  540. return Error_Succeed;
  541. }
  542. //can not find key
  543. if (config[dstSection].find(dstKey) == config[dstSection].end())
  544. {
  545. strValue = strTmp;
  546. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("ReadConfigValue can not find value:%s", dstKey.c_str());
  547. return Error_Succeed;
  548. }
  549. strValue = config[dstSection][dstKey].c_str();
  550. return Error_Succeed;
  551. }
  552. ErrorCodeEnum SpRootMemConfig::ReadConfigValueInt(const char* pszSection, const char* pszKey, int& iValue)
  553. {
  554. CSimpleStringA strValue;
  555. ErrorCodeEnum Error = ReadConfigValue(pszSection, pszKey, strValue);
  556. if (Error == Error_Succeed) {
  557. if (strValue.IsStartWith("0x")) // support hex string
  558. sscanf(strValue, "0x%X", &iValue);
  559. else
  560. iValue = atoi(strValue);
  561. }
  562. return Error;
  563. }
  564. ErrorCodeEnum SpRootMemConfig::WriteConfigValue(const char* pszSection, const char* pszKey, const char* pszValue)
  565. {
  566. return Error_Readonly;
  567. }
  568. ErrorCodeEnum SpRootMemConfig::WriteConfigValueInt(const char* pszSection, const char* pszKey, int iValue)
  569. {
  570. char tmp[32];
  571. _itoa(iValue, tmp, 10);
  572. return WriteConfigValue(pszSection, pszKey, tmp);
  573. }
  574. ErrorCodeEnum SpRootMemConfig::ReadAllKeys(const char* pszSection, CAutoArray<CSimpleStringA>& strKeys)
  575. {
  576. _ASSERT(pszSection);
  577. auto config = m_impl->getCurConfig();
  578. auto dstSection = m_impl->toLowerCase(pszSection);
  579. //can not find section
  580. if (config.find(dstSection) == config.end())
  581. return Error_NotExist;
  582. strKeys.Init(config[dstSection].size());
  583. int keys_i = 0;
  584. for (auto it = config[dstSection].begin(); it != config[dstSection].end(); it++)
  585. strKeys[keys_i++] = it->first.c_str();
  586. return Error_Succeed;
  587. }
  588. ErrorCodeEnum SpRootMemConfig::ReadAllSections(CAutoArray<CSimpleStringA>& strSections)
  589. {
  590. auto config = m_impl->getCurConfig();
  591. strSections.Init(config.size());
  592. int sections_i = 0;
  593. for (auto it = config.begin(); it != config.end(); it++)
  594. strSections[sections_i++] = it->first.c_str();
  595. return Error_Succeed;
  596. }
  597. ErrorCodeEnum SpRootMemConfig::WriteConfigValueHexInt(const char* pszSection, const char* pszKey, UINT64 nValue)
  598. {
  599. char tmp[32];
  600. sprintf_s(tmp, sizeof(tmp), "0x%I64X", nValue);
  601. return WriteConfigValue(pszSection, pszKey, tmp);
  602. }
  603. ErrorCodeEnum SpRootMemConfig::ReadConfigValueHexInt(const char* pszSection, const char* pszKey, UINT64& nValue)
  604. {
  605. nValue = 0;
  606. CSimpleStringA strValue;
  607. ErrorCodeEnum Error = ReadConfigValue(pszSection, pszKey, strValue);
  608. if (Error == Error_Succeed && strValue.GetLength() > 0) {
  609. if (strValue.IsStartWith("0x")) // support hex string
  610. sscanf(strValue, "0x%I64X", &nValue);
  611. else
  612. sscanf(strValue, "%I64X", &nValue);
  613. }
  614. return Error;
  615. }