SpMisc.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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_Root && (((FrameworkStateEnum)sp_get_env()->cfg->shell_ini->shell_state) == FrameworkState_NotConfig)) {
  325. /** Skip and allow to modify root.ini [Gifur@20211014]*/
  326. } else if (m_type == Config_Hardware || m_type == Config_Shell || m_type == Config_Root || m_type == Config_CenterSetting) {
  327. return Error_Readonly;
  328. }
  329. CSimpleStringA path;
  330. ErrorCodeEnum Error = _GetMappFilePath(path);
  331. if (Error == Error_Succeed) {
  332. Error = SpIniFileWriteString(path, pszSection, pszKey, pszValue);
  333. }
  334. return Error;
  335. }
  336. ErrorCodeEnum SpIniConfig::WriteConfigValueInt(const char *pszSection, const char *pszKey, int iValue)
  337. {
  338. char tmp[32];
  339. _itoa(iValue, tmp, 10);
  340. return WriteConfigValue(pszSection, pszKey, tmp);
  341. }
  342. ErrorCodeEnum SpIniConfig::WriteConfigValueHexInt(const char *pszSection, const char *pszKey, UINT64 nValue)
  343. {
  344. char tmp[32];
  345. #if defined(_MSC_VER)
  346. sprintf_s(tmp, sizeof(tmp), "0x%I64X", nValue);
  347. #else
  348. sprintf_s(tmp, sizeof(tmp), "0x%" PRIx64 "", nValue);
  349. #endif //_MSC_VER
  350. return WriteConfigValue(pszSection, pszKey, tmp);
  351. }
  352. ErrorCodeEnum SpIniConfig::ReadConfigValueHexInt(const char *pszSection, const char *pszKey, UINT64 &nValue)
  353. {
  354. nValue = 0;
  355. CSimpleStringA strValue;
  356. ErrorCodeEnum Error = ReadConfigValue(pszSection, pszKey, strValue);
  357. if (Error == Error_Succeed && strValue.GetLength() > 0) {
  358. #if defined(_MSC_VER)
  359. if (strValue.IsStartWith("0x")) // support hex string
  360. sscanf(strValue, "0x%I64X", &nValue);
  361. else
  362. sscanf(strValue, "%I64X", &nValue);
  363. #else
  364. if (strValue.IsStartWith("0x")) // support hex string
  365. sscanf(strValue, "0x%" PRIx64 "", &nValue);
  366. else
  367. sscanf(strValue, "%" PRIx64 "", &nValue);
  368. #endif //_MSC_VER
  369. }
  370. return Error;
  371. }
  372. ErrorCodeEnum SpIniConfig::EstimateBeforeRW()
  373. {
  374. ErrorCodeEnum result(Error_Succeed);
  375. if (m_type == Config_Hardware
  376. || m_type == Config_Shell
  377. || m_type == Config_Root
  378. || m_type == Config_CenterSetting
  379. || m_type == Config_Software) {
  380. CSimpleStringA strAbsolutePath(true);
  381. result = _GetMappFilePath(strAbsolutePath);
  382. if (Error_Succeed == result && !strAbsolutePath.IsNullOrEmpty()) {
  383. if (!ExistsFileA(strAbsolutePath)) {
  384. result = Error_NotExist;
  385. }
  386. } else if (Error_Succeed == result) {
  387. result = Error_Null;
  388. }
  389. }
  390. return result;
  391. }
  392. ErrorCodeEnum SpIniConfig::_GetMappFilePath(CSimpleStringA& strPath)
  393. {
  394. char tmp[MAX_PATH];
  395. ErrorCodeEnum Error;
  396. sp_env_t* env = sp_get_env();
  397. int flag;
  398. if (m_type == Config_Hardware) {
  399. flag = SP_DIR_DEVICE_ENTITY_INI;
  400. } else if (m_type == Config_Software) {
  401. flag = SP_DIR_ENTITY_INI;
  402. } else if (m_type == Config_Run) {
  403. flag = SP_DIR_RUNINFO_INI;
  404. } else if (m_type == Config_Shell) {
  405. flag = SP_DIR_SHELL_INI;
  406. } else if (m_type == Config_Root) {
  407. flag = SP_DIR_ROOT_INI;
  408. } else if (m_type == Config_CenterSetting) {
  409. flag = SP_DIR_CENTER_SETTING_INI;
  410. } else if (m_type == Config_Cache) {
  411. flag = SP_DIR_RUNINFO_GLOBAL_INI;
  412. } else {
  413. return Error_Param;
  414. }
  415. /** 集中配置文件的获取单独处理*/
  416. if (m_type == Config_CenterSetting) {
  417. CSimpleStringA strCtsPath;
  418. Error = m_pEntity->GetPath("CenterSetting", strCtsPath);
  419. strcpy_s(tmp, MAX_PATH, strCtsPath);
  420. } else {
  421. Error = (ErrorCodeEnum)sp_dir_get_path_new(env->dir, flag, m_pEntity->get_ent()->cfg->name, tmp, MAX_PATH,
  422. detect_env_test_mode(env->cfg->args->test_mode));
  423. }
  424. strPath = tmp;
  425. return Error;
  426. }
  427. ErrorCodeEnum SpIniConfig::ReadAllKeys(const char *pszSection, CAutoArray<CSimpleStringA> &strKeys)
  428. {
  429. _ASSERT(pszSection);
  430. CSimpleStringA path;
  431. ErrorCodeEnum Error = _GetMappFilePath(path);
  432. if (Error == Error_Succeed)
  433. Error = SpIniFileReadAllKeys(path, pszSection, strKeys);
  434. return Error;
  435. }
  436. ErrorCodeEnum SpIniConfig::ReadAllSections(CAutoArray<CSimpleStringA> &strSections)
  437. {
  438. CSimpleStringA path;
  439. ErrorCodeEnum Error = _GetMappFilePath(path);
  440. if (Error == Error_Succeed)
  441. Error = SpIniFileReadAllSections(path, strSections);
  442. return Error;
  443. }
  444. class SpRootMemConfig::Impl {
  445. public:
  446. static std::string toLowerCase(std::string str) {
  447. #ifdef _WIN32
  448. std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
  449. return std::tolower(c, std::locale());
  450. });
  451. #else
  452. for (char& c : str) {
  453. c = std::tolower(c);
  454. }
  455. #endif
  456. return str;
  457. }
  458. ConfigTypeEnum type;
  459. std::map<std::string, std::map<std::string, std::string>> m_deviceConfig;
  460. std::map<std::string, std::map<std::string, std::string>> m_centerConfig;
  461. std::map<std::string, std::map<std::string, std::string>> m_shellConfig;
  462. std::map<std::string, std::map<std::string, std::string>> m_nullConfig;
  463. std::map<std::string, std::map<std::string, std::string>>& getCurConfig()
  464. {
  465. if (type == ConfigTypeEnum::Config_CenterSetting)
  466. return m_centerConfig;
  467. else if (type == ConfigTypeEnum::Config_Root)
  468. return m_deviceConfig;
  469. else if (type == ConfigTypeEnum::Config_Shell)
  470. return m_shellConfig;
  471. else
  472. return m_nullConfig;
  473. }
  474. static std::pair<bool, std::string> ConvertStrToDeviceConfig(std::string input, std::map<std::string, std::map<std::string, std::string>> &config)
  475. {
  476. // 解析为Json::Value
  477. Json::Reader reader;
  478. Json::Value jsonValue;
  479. bool success = reader.parse(input, jsonValue);
  480. if (!success)
  481. return std::make_pair(false,
  482. CSimpleStringA::Format("ConvertStrToDeviceConfig Failed to parse input: %s", reader.getFormattedErrorMessages().c_str()).GetData());
  483. // 转换为std::map
  484. config.clear();
  485. Json::Value::Members outer_members = jsonValue.getMemberNames();
  486. for (Json::Value::Members::iterator outer_it = outer_members.begin(); outer_it != outer_members.end(); ++outer_it) {
  487. const std::string& outer_key = *outer_it;
  488. const Json::Value& inner_value = jsonValue[outer_key];
  489. std::map<std::string, std::string> inner_map;
  490. Json::Value::Members inner_members = inner_value.getMemberNames();
  491. for (Json::Value::Members::iterator inner_it = inner_members.begin(); inner_it != inner_members.end(); ++inner_it) {
  492. const std::string& inner_key = *inner_it;
  493. const std::string& inner_value = jsonValue[outer_key][inner_key].asString();
  494. inner_map.insert(std::make_pair(toLowerCase(inner_key), inner_value));
  495. }
  496. config.insert(std::make_pair(toLowerCase(outer_key), inner_map));
  497. }
  498. // 输出结果
  499. return std::make_pair(true, "");
  500. }
  501. };
  502. SpRootMemConfig::SpRootMemConfig(SpEntity* pEntity, ConfigTypeEnum type)
  503. : m_pEntity(pEntity)
  504. , m_type(type)
  505. , m_impl(new Impl())
  506. {
  507. sp_env_t* env = sp_get_env();
  508. sp_cfg_t* cfg = env->cfg;
  509. sp_cfg_root_ini_t* root_ini = cfg->root_ini;
  510. m_impl->type = type;
  511. if (m_impl->m_deviceConfig.size() == 0 && type == ConfigTypeEnum::Config_Root)//首次初始化
  512. m_impl->ConvertStrToDeviceConfig(root_ini->root_config, m_impl->m_deviceConfig);
  513. if (m_impl->m_centerConfig.size() == 0 && type == ConfigTypeEnum::Config_CenterSetting)
  514. m_impl->ConvertStrToDeviceConfig(root_ini->center_config, m_impl->m_centerConfig);
  515. if (m_impl->m_shellConfig.size() == 0 && type == ConfigTypeEnum::Config_Shell)
  516. m_impl->ConvertStrToDeviceConfig(root_ini->shell_config, m_impl->m_shellConfig);
  517. }
  518. SpRootMemConfig::~SpRootMemConfig()
  519. {
  520. if (m_impl != NULL)
  521. {
  522. delete m_impl;
  523. }
  524. }
  525. ErrorCodeEnum SpRootMemConfig::ReadConfigValue(const char* pszSection, const char* pszKey, CSimpleStringA& strValue)
  526. {
  527. _ASSERT(pszSection);
  528. _ASSERT(pszKey);
  529. auto config = m_impl->getCurConfig();
  530. //can not find section
  531. //都转换成小写,避免查不到
  532. auto dstSection = m_impl->toLowerCase(pszSection);
  533. auto dstKey = m_impl->toLowerCase(pszKey);
  534. //因为存储名字不同,terminal需要进行转换
  535. if (dstSection == "terminal")
  536. dstSection = "terminalinfo";
  537. DWORD dwInitCapacity = 16;
  538. CSimpleStringA strTmp('\0', dwInitCapacity);
  539. if (config.find(dstSection) == config.end())
  540. {
  541. strValue = strTmp;
  542. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("ReadConfigValue can not find section:%s", dstSection.c_str());
  543. return Error_Succeed;
  544. }
  545. //can not find key
  546. if (config[dstSection].find(dstKey) == config[dstSection].end())
  547. {
  548. strValue = strTmp;
  549. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("ReadConfigValue can not find value:%s", dstKey.c_str());
  550. return Error_Succeed;
  551. }
  552. strValue = config[dstSection][dstKey].c_str();
  553. return Error_Succeed;
  554. }
  555. ErrorCodeEnum SpRootMemConfig::ReadConfigValueInt(const char* pszSection, const char* pszKey, int& iValue)
  556. {
  557. CSimpleStringA strValue;
  558. ErrorCodeEnum Error = ReadConfigValue(pszSection, pszKey, strValue);
  559. if (Error == Error_Succeed) {
  560. if (strValue.IsStartWith("0x")) // support hex string
  561. sscanf(strValue, "0x%X", &iValue);
  562. else
  563. iValue = atoi(strValue);
  564. }
  565. return Error;
  566. }
  567. ErrorCodeEnum SpRootMemConfig::WriteConfigValue(const char* pszSection, const char* pszKey, const char* pszValue)
  568. {
  569. return Error_Readonly;
  570. }
  571. ErrorCodeEnum SpRootMemConfig::WriteConfigValueInt(const char* pszSection, const char* pszKey, int iValue)
  572. {
  573. char tmp[32];
  574. _itoa(iValue, tmp, 10);
  575. return WriteConfigValue(pszSection, pszKey, tmp);
  576. }
  577. ErrorCodeEnum SpRootMemConfig::ReadAllKeys(const char* pszSection, CAutoArray<CSimpleStringA>& strKeys)
  578. {
  579. _ASSERT(pszSection);
  580. auto config = m_impl->getCurConfig();
  581. auto dstSection = m_impl->toLowerCase(pszSection);
  582. //can not find section
  583. if (config.find(dstSection) == config.end())
  584. return Error_NotExist;
  585. strKeys.Init(config[dstSection].size());
  586. int keys_i = 0;
  587. for (auto it = config[dstSection].begin(); it != config[dstSection].end(); it++)
  588. strKeys[keys_i++] = it->first.c_str();
  589. return Error_Succeed;
  590. }
  591. ErrorCodeEnum SpRootMemConfig::ReadAllSections(CAutoArray<CSimpleStringA>& strSections)
  592. {
  593. auto config = m_impl->getCurConfig();
  594. strSections.Init(config.size());
  595. int sections_i = 0;
  596. for (auto it = config.begin(); it != config.end(); it++)
  597. strSections[sections_i++] = it->first.c_str();
  598. return Error_Succeed;
  599. }
  600. ErrorCodeEnum SpRootMemConfig::WriteConfigValueHexInt(const char* pszSection, const char* pszKey, UINT64 nValue)
  601. {
  602. char tmp[32];
  603. sprintf_s(tmp, sizeof(tmp), "0x%I64X", nValue);
  604. return WriteConfigValue(pszSection, pszKey, tmp);
  605. }
  606. ErrorCodeEnum SpRootMemConfig::ReadConfigValueHexInt(const char* pszSection, const char* pszKey, UINT64& nValue)
  607. {
  608. nValue = 0;
  609. CSimpleStringA strValue;
  610. ErrorCodeEnum Error = ReadConfigValue(pszSection, pszKey, strValue);
  611. if (Error == Error_Succeed && strValue.GetLength() > 0) {
  612. if (strValue.IsStartWith("0x")) // support hex string
  613. sscanf(strValue, "0x%I64X", &nValue);
  614. else
  615. sscanf(strValue, "%I64X", &nValue);
  616. }
  617. return Error;
  618. }