SpMisc.cpp 18 KB

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