CINI.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /**************************************************************************************************
  2. 版本所有(C), 2020-2022, 北京普达迪泰
  3. ***************************************************************************************************
  4. 文 件 名: CINI.cpp
  5. 功能描述: .ini配置文件操作类
  6. 版 本 号: 1.00初版
  7. 作 者: HYF
  8. 生成日期; 2021-12-20
  9. 最近修改:
  10. 函数列表:
  11. 修改历史:
  12. 1.日 期 :2021-12-20
  13. 作 者 : HYF
  14. 修改内容 : 创建文件
  15. **************************************************************************************************/
  16. #include "pch.h"
  17. #include "CINI.h"
  18. CINI::CINI()
  19. {
  20. CString szPath = Utils::GetLocalUrlDir();
  21. szPath.Append("/");
  22. szPath.Append(g_szConfigFile);
  23. m_bDebug = FALSE;
  24. #ifdef _UNICODE
  25. //wprintf(m_cFileName,_T("%s"),FileName);
  26. memset(m_cFileName, 0, sizeof(TCHAR)*BUFF_MAX);
  27. memcpy(m_cFileName, FileName, sizeof(TCHAR)*_tcslen(FileName));
  28. #else
  29. sprintf_s(m_cFileName, _T("%s"), szPath.GetBuffer());
  30. #endif
  31. }
  32. CINI::~CINI()
  33. {
  34. RemoveAll();
  35. }
  36. BOOL CINI::ReadIniNodes(CStringList *li, TCHAR *FileName)
  37. {
  38. RemoveAll();
  39. FILE *m_fp;
  40. long filelen = 0, tempoffe;
  41. CString t;
  42. NodeData *m_nodedata = NULL;
  43. #ifdef _UNICODE
  44. if (FileName != NULL)
  45. wcscpy(m_cFileName, FileName);
  46. m_fp = _wfopen(m_cFileName, _T("r"));
  47. if (m_fp == NULL) return false;
  48. if (li != NULL)
  49. li->RemoveAll();
  50. fseek(m_fp, 0, SEEK_END);
  51. filelen = ftell(m_fp);
  52. fseek(m_fp, 0, SEEK_SET);
  53. while (ftell(m_fp) < filelen)
  54. {
  55. fgetws(m_cReadBuff, BUFF_MAX, m_fp);
  56. tempoffe = ftell(m_fp);
  57. t.Format(_T("%s"), m_cReadBuff);
  58. t.TrimLeft();
  59. t.MakeUpper();
  60. t.Remove(10);
  61. t.Remove(13);
  62. t.Remove(9);
  63. swprintf(m_cReadBuff, _T("%s"), t.GetBuffer(t.GetLength()));
  64. t.Delete(t.GetLength() - 1);
  65. t.Delete(0);
  66. if (m_cReadBuff[0] == ';')
  67. continue;
  68. if (t.GetLength() > 0)
  69. {
  70. if (m_cReadBuff[0] == '[')
  71. {
  72. if (li != NULL)
  73. li->AddTail(t);
  74. m_nodedata = new NodeData;
  75. m_nodedata->m_nIndex = m_pNodeList.GetCount() + 1;
  76. m_nodedata->m_SubCount = 0;
  77. m_nodedata->size = wcslen(m_cReadBuff);
  78. m_nodedata->strNode = new TCHAR[m_nodedata->size];
  79. m_nodedata->m_ByteOffe = tempoffe;
  80. wsprintf(m_nodedata->strNode, _T("%s"), m_cReadBuff);
  81. m_pNodeList.InsertAfter(m_pNodeList.GetTailPosition(), m_nodedata);
  82. }
  83. }
  84. }
  85. _fcloseall();
  86. #else
  87. if (FileName != NULL) sprintf_s(m_cFileName, "%s", FileName);
  88. if (m_cFileName[0] == NULL)
  89. {
  90. MessageBox(GetFocus(), "m_cFileName = NULL", "CINI", 0);
  91. return false;
  92. }
  93. m_fp = fopen(m_cFileName, "r");
  94. if (m_fp == NULL)
  95. {
  96. MessageBox(GetFocus(), "m_fp=fopen(m_cFileName,r)==NULL", "CINI", 0);
  97. return false;
  98. }
  99. if (li != NULL)
  100. if (li->GetCount() > 0)
  101. li->RemoveAll();
  102. fseek(m_fp, 0, SEEK_END);
  103. filelen = ftell(m_fp);
  104. fseek(m_fp, 0, SEEK_SET);
  105. while (ftell(m_fp) < filelen)
  106. {
  107. fgets(m_cReadBuff, BUFF_MAX, m_fp);
  108. tempoffe = ftell(m_fp);
  109. t.Format("%s", m_cReadBuff);
  110. t.MakeUpper();
  111. t.Remove(10);
  112. t.Remove(13);
  113. t.Remove(9);
  114. sprintf_s(m_cReadBuff, "%s", t.GetBuffer(t.GetLength()));
  115. t.Delete(t.GetLength() - 1);
  116. t.Delete(0);
  117. t.TrimLeft();
  118. if (m_cReadBuff[0] == ';')
  119. continue;
  120. if (t.GetLength() > 0)
  121. {
  122. if (m_cReadBuff[0] == '[')
  123. {
  124. if (li != NULL)
  125. li->AddTail(t);
  126. m_nodedata = new NodeData;
  127. m_nodedata->m_nIndex = m_pNodeList.GetCount() + 1;
  128. m_nodedata->m_SubCount = 0;
  129. m_nodedata->size = strlen(m_cReadBuff);
  130. m_nodedata->strNode = new char[m_nodedata->size];
  131. m_nodedata->m_ByteOffe = tempoffe;
  132. sprintf(m_nodedata->strNode, "%s", m_cReadBuff);
  133. m_pNodeList.InsertAfter(m_pNodeList.GetTailPosition(), m_nodedata);
  134. }
  135. }
  136. }
  137. fclose(m_fp);
  138. #endif
  139. return true;
  140. }
  141. BOOL CINI::ReadIniNodeSubData(LPCTSTR Nodestr, CStringList *li)
  142. {
  143. if (m_bDebug)MessageBox(NULL, _T("Start"), _T("ReadIniNodeSubData"), 0);
  144. if (m_cFileName[0] == NULL)
  145. return false;
  146. if (m_bDebug)MessageBox(NULL, _T("Start2"), _T("ReadIniNodeSubData"), 0);
  147. FILE *m_fp;
  148. long filelen = 0, tempoffe;
  149. CString t;
  150. NodeData *m_nodedata = NULL;
  151. if (m_bDebug)MessageBox(NULL, _T("Start3"), _T("ReadIniNodeSubData"), 0);
  152. if (Nodestr != NULL)
  153. {
  154. m_nodedata = GetNode(Nodestr);
  155. if (m_nodedata == NULL)
  156. {
  157. return false;
  158. }
  159. tempoffe = m_nodedata->m_ByteOffe;
  160. }
  161. else
  162. return false;
  163. if (m_bDebug)MessageBox(NULL, _T("Start4"), _T("ReadIniNodeSubData"), 0);
  164. #ifdef _UNICODE
  165. m_fp = _wfopen(m_cFileName, _T("r"));
  166. if (m_fp == NULL)
  167. return false;
  168. fseek(m_fp, 0, SEEK_END);
  169. filelen = ftell(m_fp);
  170. fseek(m_fp, tempoffe, SEEK_SET);
  171. while (ftell(m_fp) < filelen)
  172. {
  173. tempoffe = ftell(m_fp);
  174. fgetws(m_cReadBuff, BUFF_MAX, m_fp);
  175. t.Format(_T("%s"), m_cReadBuff);
  176. t.MakeUpper();
  177. t.Remove(10);
  178. t.Remove(13);
  179. t.TrimLeft();
  180. wsprintf(m_cReadBuff, _T("%s"), t.GetBuffer(t.GetLength()));
  181. if (m_cReadBuff[0] == ';')
  182. continue;
  183. if (t.GetLength() > 0)
  184. {
  185. if (m_cReadBuff[0] != '[')
  186. {
  187. if (li != NULL)
  188. li->AddTail(t);
  189. m_nodedata = new NodeData;
  190. m_nodedata->m_nIndex = m_pNodeList.GetCount() + 1;
  191. m_nodedata->m_SubCount = 0;
  192. m_nodedata->size = wcslen(m_cReadBuff);
  193. m_nodedata->strNode = new TCHAR[m_nodedata->size];
  194. m_nodedata->m_ByteOffe = tempoffe;
  195. wsprintf(m_nodedata->strNode, _T("%s"), m_cReadBuff);
  196. m_pNodeList.InsertAfter(m_pNodeList.GetTailPosition(), m_nodedata);
  197. }
  198. else
  199. break;
  200. }
  201. }
  202. fclose(m_fp);
  203. #else
  204. if (m_bDebug)MessageBox(NULL, "Start5", "ReadIniNodeSubData", 0);
  205. if (li != NULL) li->RemoveAll();
  206. m_fp = fopen(m_cFileName, "r");
  207. if (m_fp == NULL) return false;
  208. if (m_bDebug)MessageBox(NULL, "Start6", "ReadIniNodeSubData", 0);
  209. fseek(m_fp, 0, SEEK_END);
  210. filelen = ftell(m_fp);
  211. fseek(m_fp, tempoffe, SEEK_SET);
  212. if (m_bDebug)MessageBox(NULL, "Start7", "ReadIniNodeSubData", 0);
  213. while (ftell(m_fp) < filelen)
  214. {
  215. if (m_bDebug)MessageBox(NULL, "Start81", "ReadIniNodeSubData", 0);
  216. tempoffe = ftell(m_fp);
  217. fgets(m_cReadBuff, BUFF_MAX, m_fp);
  218. t.Format("%s", m_cReadBuff);
  219. if (t.GetLength() > 0)
  220. {
  221. t.MakeUpper();
  222. t.Remove(10);
  223. t.Remove(13);
  224. t.TrimLeft();
  225. }
  226. if (m_bDebug)MessageBox(NULL, m_cReadBuff, "ReadIniNodeSubData:82", 0);
  227. sprintf_s(m_cReadBuff, "%s", t.GetBuffer(t.GetLength()));
  228. if (m_cReadBuff[0] == ';') continue;
  229. if (t.GetLength() > 0)
  230. {
  231. if (m_cReadBuff[0] != '[')
  232. {
  233. if (li != NULL)
  234. {
  235. if (li != NULL)
  236. li->AddTail(t);
  237. m_nodedata = new NodeData;
  238. m_nodedata->m_nIndex = m_pNodeList.GetCount() + 1;
  239. m_nodedata->m_SubCount = 0;
  240. m_nodedata->size = strlen(m_cReadBuff);
  241. m_nodedata->strNode = new char[m_nodedata->size];
  242. m_nodedata->m_ByteOffe = tempoffe;
  243. sprintf(m_nodedata->strNode, "%s", m_cReadBuff);
  244. m_pNodeList.InsertAfter(m_pNodeList.GetTailPosition(), m_nodedata);
  245. }
  246. }
  247. else
  248. break;
  249. }
  250. if (m_bDebug)MessageBox(NULL, "Start83", "ReadIniNodeSubData", 0);
  251. }
  252. fclose(m_fp);
  253. #endif
  254. return true;
  255. }
  256. int CINI::GetNodeSubCount(LPCTSTR str)
  257. {
  258. NodeData *pnd = GetNode(str);
  259. if (pnd != NULL)
  260. return pnd->m_SubCount;
  261. return 0;
  262. }
  263. BOOL CINI::WriteStrVal(LPCTSTR Node, LPCTSTR Key, TCHAR* Value)
  264. {
  265. return WritePrivateProfileString(Node, Key, Value, m_cFileName);
  266. }
  267. BOOL CINI::WriteIntVal(LPCTSTR Node, LPCTSTR Key, int n)
  268. {
  269. CString wbuff;
  270. wbuff.Format(_T("%d"), n);
  271. return WritePrivateProfileString(Node, Key, (LPCTSTR)wbuff, m_cFileName);
  272. }
  273. BOOL CINI::DeleteKey(LPCTSTR Node, LPCTSTR Key)
  274. {
  275. return WritePrivateProfileString(Node, Key, NULL, m_cFileName);
  276. }
  277. BOOL CINI::DeleteNode(LPCTSTR Node)
  278. {
  279. return WritePrivateProfileString(Node, NULL, NULL, m_cFileName);
  280. }
  281. void CINI::SetFileName(LPCTSTR Filename)
  282. {
  283. #ifdef _UNICODE
  284. wcscpy(m_cFileName, Filename);
  285. #else
  286. strcpy(m_cFileName, Filename);
  287. #endif
  288. }
  289. void CINI::RemoveAll()
  290. {
  291. if (m_pNodeList.GetCount() <= 0)
  292. return;
  293. TCHAR *d;
  294. for (POSITION pos = m_pNodeList.GetHeadPosition(); pos != NULL;)
  295. {
  296. NodeData* ndtemp = (NodeData*)m_pNodeList.GetNext(pos);
  297. if (ndtemp != NULL)
  298. {
  299. d = ndtemp->strNode;
  300. if (d != NULL)
  301. {
  302. //delete[] d;
  303. ndtemp->strNode = NULL;
  304. delete ndtemp;
  305. ndtemp = NULL;
  306. }
  307. }
  308. }
  309. m_pNodeList.RemoveAll();
  310. }
  311. NodeData *CINI::GetNode(LPCTSTR findc)
  312. {
  313. if (m_pNodeList.GetCount() <= 0)
  314. return NULL;
  315. TCHAR findBuff[512];
  316. for (POSITION pos = m_pNodeList.GetHeadPosition(); pos != NULL;)
  317. {
  318. NodeData *ndtemp = (NodeData*)m_pNodeList.GetNext(pos);
  319. if (ndtemp != NULL)
  320. {
  321. #ifdef _UNICODE
  322. wsprintf(findBuff, _T("%s"), ndtemp->strNode);
  323. if (wcscmp(findc, findBuff) == 0)
  324. return ndtemp;
  325. #else
  326. sprintf_s(findBuff, _T("%s"), ndtemp->strNode);
  327. // MessageBox(GetFocus(),findBuff,findc,0);
  328. if (strcmp(findc, findBuff) == 0)
  329. return ndtemp;
  330. #endif
  331. }
  332. }
  333. return NULL;
  334. }
  335. float CINI::ReadFloatVal(LPCTSTR strHead, LPCTSTR strKey, const float idefault)
  336. {
  337. TCHAR buff[512];
  338. if (ReadStrVal(strHead, strKey, buff, 512))
  339. //return (float)atof(buff);
  340. return (float)_ttof(buff);
  341. else
  342. {
  343. WriteFloatVal(strHead, strKey, idefault);
  344. return idefault;
  345. }
  346. }
  347. double CINI::ReadDoubleVal(LPCTSTR strHead, LPCTSTR strKey, const double idefault)
  348. {
  349. TCHAR buff[512];
  350. if (ReadStrVal(strHead, strKey, buff, 512))
  351. return (double)_ttof(buff);
  352. else
  353. {
  354. _stprintf(buff, _T("%lf"), idefault);
  355. WriteDoubleVal(strHead, strKey, idefault);
  356. return idefault;
  357. }
  358. }
  359. BOOL CINI::GetInitFlag()
  360. {
  361. if (_tcslen(m_cFileName) == 0) return FALSE;
  362. else return TRUE;
  363. }
  364. BOOL CINI::ReadStrVal(LPCTSTR strHead, LPCTSTR strKey, CString &buff)
  365. {
  366. TCHAR buffx[2048];
  367. if (ReadStrVal(strHead, strKey, buffx, 2048))
  368. {
  369. buff.Format(_T("%s"), buffx);
  370. return TRUE;
  371. }
  372. else
  373. {
  374. _stprintf(buffx, _T("%s"), buff);
  375. WriteStrVal(strHead, strKey, buffx);
  376. return FALSE;
  377. }
  378. }
  379. BOOL CINI::ReadStrVal(LPCTSTR strHead, LPCTSTR strKey, TCHAR *buff, int size)
  380. {
  381. if (m_cFileName[0] == _T('\0')) return FALSE;
  382. return GetPrivateProfileString(strHead, strKey, _T(""), buff, size, m_cFileName);
  383. }
  384. UINT CINI::ReadIntVal(LPCTSTR strHead, LPCTSTR strKey, const UINT idefault)
  385. {
  386. TCHAR buff[512];
  387. if (ReadStrVal(strHead, strKey, buff, 512))
  388. return (UINT)_ttol(buff);
  389. else
  390. {
  391. WriteIntVal(strHead, strKey, idefault);
  392. return idefault;
  393. }
  394. }
  395. BOOL CINI::GetSubValueFromStr(LPCTSTR str, CString &strDest, int index)
  396. {
  397. CString strTemp, strTemp2;
  398. int ni = 0;
  399. strTemp = str;
  400. strTemp.Remove(10);
  401. strTemp.Remove(13);
  402. strTemp.Remove(9);
  403. strTemp.Remove(' ');
  404. int nStart = strTemp.Find(_T('='), 0);
  405. if (nStart > -1) strTemp.SetAt(nStart, _T(','));
  406. strDest = _T("");
  407. //MessageBox(NULL,strTemp,"CINI",0);
  408. while (strTemp.GetLength() > 0)
  409. {
  410. if (ni == index)
  411. {
  412. strDest += strTemp.GetAt(0);
  413. strTemp.Delete(0);
  414. if (strTemp.GetLength() == 0)
  415. {
  416. if (strDest.GetLength() > 0)
  417. return TRUE;
  418. }
  419. else
  420. if (strTemp.GetAt(0) == _T(',')) return TRUE;
  421. }
  422. else
  423. {
  424. if (strTemp.GetAt(0) == _T(',')) ni++;
  425. strTemp.Delete(0);
  426. }
  427. }
  428. if (strDest.GetLength() == 0) return FALSE;
  429. else
  430. return TRUE;
  431. }
  432. BOOL CINI::GetSubValueFromStr(LPCTSTR str, int &iDest, int index)
  433. {
  434. CString strTemp;
  435. if (GetSubValueFromStr(str, strTemp, index))
  436. {
  437. iDest = _ttoi(strTemp);
  438. return TRUE;
  439. }
  440. return FALSE;
  441. }
  442. BOOL CINI::GetSubValueFromStr(LPCTSTR str, double &fDest, int index)
  443. {
  444. CString strTemp;
  445. if (GetSubValueFromStr(str, strTemp, index))
  446. {
  447. fDest = _ttof(strTemp);
  448. return TRUE;
  449. }
  450. return FALSE;
  451. }
  452. BOOL CINI::GetSubValueFromStrEx(LPCTSTR str, CString &strDest, int index)
  453. {
  454. CString strTemp, strTemp2;
  455. int ni = 0;
  456. strTemp = str;
  457. strTemp.Remove(10);
  458. strTemp.Remove(13);
  459. strTemp.TrimLeft();
  460. strTemp.TrimRight();
  461. BOOL bdot = FALSE;
  462. strDest = "";
  463. int i = 0;
  464. while (i < strTemp.GetLength())
  465. {
  466. if (strTemp.GetAt(i) == _T('='))
  467. {
  468. if (!bdot)
  469. strTemp.SetAt(i, _T(','));
  470. else strTemp.Delete(i);
  471. bdot = TRUE;
  472. }
  473. else if (strTemp.GetAt(i) == 9)
  474. {
  475. if (!bdot)
  476. strTemp.SetAt(i, _T(','));
  477. else strTemp.Delete(i);
  478. bdot = TRUE;
  479. }
  480. else if (strTemp.GetAt(i) == _T(' '))
  481. {
  482. if (!bdot)
  483. strTemp.SetAt(i, _T(','));
  484. else strTemp.Delete(i);
  485. bdot = TRUE;
  486. }
  487. else if (strTemp.GetAt(i) == _T(','))
  488. {
  489. if (bdot)
  490. strTemp.Delete(i);
  491. bdot = TRUE;
  492. }
  493. else bdot = FALSE;
  494. i++;
  495. }
  496. while (strTemp.GetLength() > 0)
  497. {
  498. if (ni == index)
  499. {
  500. strDest += strTemp.GetAt(0);
  501. strTemp.Delete(0);
  502. if (strTemp.GetLength() == 0)
  503. {
  504. if (strDest.GetLength() > 0)
  505. return TRUE;
  506. }
  507. else
  508. if (strTemp.GetAt(0) == _T(',')) return TRUE;
  509. }
  510. else
  511. {
  512. if (strTemp.GetAt(0) == _T(',')) ni++;
  513. strTemp.Delete(0);
  514. }
  515. }
  516. if (strDest.GetLength() == 0) return FALSE;
  517. else
  518. return TRUE;
  519. }
  520. BOOL CINI::GetSubValueFromStrEx(LPCTSTR str, int &iDest, int index)
  521. {
  522. CString strTemp;
  523. if (GetSubValueFromStrEx(str, strTemp, index))
  524. {
  525. iDest = _ttoi(strTemp);
  526. return TRUE;
  527. }
  528. return FALSE;
  529. }
  530. BOOL CINI::GetSubValueFromStrEx(LPCTSTR str, double &fDest, int index)
  531. {
  532. CString strTemp;
  533. if (GetSubValueFromStrEx(str, strTemp, index))
  534. {
  535. fDest = _ttof(strTemp);
  536. return TRUE;
  537. }
  538. return FALSE;
  539. }
  540. BOOL CINI::WriteDoubleVal(LPCTSTR Node, LPCTSTR Key, double n)
  541. {
  542. CString wbuff;
  543. wbuff.Format(_T("%f"), n);
  544. return WritePrivateProfileString(Node, Key, (LPCTSTR)wbuff, m_cFileName);
  545. }
  546. BOOL CINI::WriteFloatVal(LPCTSTR Node, LPCTSTR Key, float n)
  547. {
  548. CString wbuff;
  549. wbuff.Format(_T("%f"), n);
  550. return WritePrivateProfileString(Node, Key, (LPCTSTR)wbuff, m_cFileName);
  551. }
  552. BOOL CINI::WriteStrVal(LPCTSTR strHead, LPCTSTR strKey, LPCTSTR strValue)
  553. {
  554. BOOL rb;
  555. CString str = strValue;
  556. TCHAR *cbuff = new TCHAR[str.GetLength() + 1];
  557. _stprintf(cbuff, _T("%s"), strValue);
  558. rb = WriteStrVal(strHead, strKey, cbuff);
  559. delete[]cbuff;
  560. cbuff = NULL;
  561. return rb;
  562. }