BaseFun.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. #include "stdafx.h"
  2. #include "BaseFun.h"
  3. #include "array.h"
  4. #ifdef RVC_OS_WIN
  5. #include <ShlDisp.h>
  6. #include <direct.h>
  7. #include <io.h>
  8. #else
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #endif // RVC_OS_WIN
  13. #ifndef RVC_MAX_INI_LEN
  14. #define RVC_MAX_INI_LEN 1024*16
  15. #endif // !RVC_MAX_INI_LEN
  16. bool Unzip2Folder(BSTR lpZipFile, BSTR lpFolder)
  17. {
  18. #ifdef RVC_OS_WIN
  19. IShellDispatch* pISD;
  20. Folder* pZippedFile = 0L;
  21. Folder* pDestination = 0L;
  22. long FilesCount = 0;
  23. IDispatch* pItem = 0L;
  24. FolderItems* pFilesInside = 0L;
  25. VARIANT Options, OutFolder, InZipFile, Item;
  26. CoInitialize(NULL);
  27. __try {
  28. if (CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)& pISD) != S_OK)
  29. return 1;
  30. InZipFile.vt = VT_BSTR;
  31. InZipFile.bstrVal = lpZipFile;
  32. pISD->NameSpace(InZipFile, &pZippedFile);
  33. if (!pZippedFile)
  34. {
  35. pISD->Release();
  36. return 1;
  37. }
  38. OutFolder.vt = VT_BSTR;
  39. OutFolder.bstrVal = lpFolder;
  40. pISD->NameSpace(OutFolder, &pDestination);
  41. if (!pDestination)
  42. {
  43. pZippedFile->Release();
  44. pISD->Release();
  45. return 1;
  46. }
  47. pZippedFile->Items(&pFilesInside);
  48. if (!pFilesInside)
  49. {
  50. pDestination->Release();
  51. pZippedFile->Release();
  52. pISD->Release();
  53. return 1;
  54. }
  55. pFilesInside->get_Count(&FilesCount);
  56. if (FilesCount < 1)
  57. {
  58. pFilesInside->Release();
  59. pDestination->Release();
  60. pZippedFile->Release();
  61. pISD->Release();
  62. return 0;
  63. }
  64. pFilesInside->QueryInterface(IID_IDispatch, (void**)& pItem);
  65. Item.vt = VT_DISPATCH;
  66. Item.pdispVal = pItem;
  67. Options.vt = VT_I4;
  68. Options.lVal = 1024 | 512 | 16 | 4;
  69. bool retval = pDestination->CopyHere(Item, Options) == S_OK;
  70. pItem->Release(); pItem = 0L;
  71. pFilesInside->Release(); pFilesInside = 0L;
  72. pDestination->Release(); pDestination = 0L;
  73. pZippedFile->Release(); pZippedFile = 0L;
  74. pISD->Release(); pISD = 0L;
  75. return retval;
  76. }
  77. __finally
  78. {
  79. CoUninitialize();
  80. }
  81. #else
  82. return true;
  83. #endif
  84. }
  85. void split(const string& src, const string& separator, vector<string>& dest)
  86. {
  87. string str = src;
  88. string substring;
  89. string::size_type start = 0, index;
  90. do
  91. {
  92. index = str.find_first_of(separator, start);
  93. if (index != string::npos)
  94. {
  95. substring = str.substr(start, index - start);
  96. dest.push_back(substring);
  97. start = str.find_first_not_of(separator, index);
  98. if (start == string::npos) return;
  99. }
  100. } while (index != string::npos);
  101. //the last token
  102. substring = str.substr(start);
  103. dest.push_back(substring);
  104. }
  105. bool checkDirExist(const string &strPath)
  106. {
  107. #ifdef RVC_OS_WIN
  108. WIN32_FIND_DATA wfd;
  109. bool rValue = false;
  110. HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
  111. if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  112. rValue = true;
  113. FindClose(hFind);
  114. return rValue;
  115. #else
  116. if (ExistsDir(strPath.c_str()))
  117. {
  118. return true;
  119. }
  120. else
  121. {
  122. return false;
  123. }
  124. #endif
  125. }
  126. bool checkFileExist(string fileName)
  127. {
  128. #ifdef RVC_OS_WIN
  129. WIN32_FIND_DATA FindFileData;
  130. HANDLE hFind;
  131. hFind = FindFirstFile(fileName.c_str(), &FindFileData);
  132. if (hFind == INVALID_HANDLE_VALUE)
  133. return false;
  134. FindClose(hFind);
  135. return true;
  136. #else
  137. if (ExistsFile(fileName.c_str()))
  138. {
  139. return true;
  140. }
  141. else
  142. {
  143. return false;
  144. }
  145. #endif
  146. }
  147. void Wchar_tToString(std::string& szDst, wchar_t *wchar)
  148. {
  149. #ifdef RVC_OS_WIN
  150. wchar_t* wText = wchar;
  151. DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);// WideCharToMultiByte的运用
  152. char* psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
  153. psText = new char[dwNum];
  154. WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);// WideCharToMultiByte的再次运用
  155. szDst = psText;// std::string赋值
  156. delete[]psText;// psText的清除
  157. #else
  158. std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
  159. setlocale(LC_ALL, "chs");
  160. size_t _Dsize = 2 * wcslen(wchar) + 1;
  161. char* _Dest = new char[_Dsize];
  162. memset(_Dest, 0, _Dsize);
  163. wcstombs(_Dest, wchar, _Dsize);
  164. std::string result = _Dest;
  165. delete[]_Dest;
  166. setlocale(LC_ALL, curLocale.c_str());
  167. szDst = result;
  168. #endif
  169. }
  170. // string to wstring
  171. void StringToWstring(std::wstring& szDst, std::string str)
  172. {
  173. #ifdef RVC_OS_WIN
  174. std::string temp = str;
  175. int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL, 0);
  176. wchar_t* wszUtf8 = new wchar_t[len + 1];
  177. memset(wszUtf8, 0, len * 2 + 2);
  178. MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len);
  179. szDst = wszUtf8;
  180. std::wstring r = wszUtf8;
  181. delete[] wszUtf8;
  182. #else
  183. setlocale(LC_ALL, "chs");
  184. const char* _Source = str.c_str();
  185. size_t _Dsize = str.size() + 1;
  186. wchar_t* _Dest = new wchar_t[_Dsize];
  187. wmemset(_Dest, 0, _Dsize);
  188. mbstowcs(_Dest, _Source, _Dsize);
  189. std::wstring result = _Dest;
  190. delete[]_Dest;
  191. setlocale(LC_ALL, "C");
  192. szDst = result;
  193. #endif
  194. }
  195. bool createDir(const string &filePath)
  196. {
  197. #ifdef RVC_OS_WIN
  198. return 0 == _mkdir(filePath.c_str());
  199. #else
  200. if (CreateDirRecursive(filePath.c_str()))
  201. {
  202. return true;
  203. }
  204. else
  205. {
  206. return false;
  207. }
  208. #endif
  209. }
  210. BOOL IsDirectory(const char *pDir)
  211. {
  212. char szCurPath[500] = {0};
  213. sprintf_s(szCurPath, 500, "%s//*", pDir);
  214. #ifdef RVC_OS_WIN
  215. WIN32_FIND_DATAA FindFileData;
  216. ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
  217. HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); /**< find first file by given path. */
  218. if (hFile == INVALID_HANDLE_VALUE)
  219. {
  220. FindClose(hFile);
  221. return FALSE; /** 如果不能找到第一个文件,那么没有目录 */
  222. }
  223. else
  224. {
  225. FindClose(hFile);
  226. return TRUE;
  227. }
  228. #else
  229. struct stat buf;
  230. if (0 == stat(szCurPath, &buf))
  231. {
  232. return S_ISDIR(buf.st_mode);
  233. }
  234. else
  235. {
  236. return FALSE;
  237. }
  238. #endif // RVC_OS_WIN
  239. }
  240. BOOL DeleteDirectory(const char * DirName)
  241. {
  242. #ifdef RVC_OS_WIN
  243. // CFileFind tempFind; //声明一个CFileFind类变量,以用来搜索
  244. char szCurPath[MAX_PATH]; //用于定义搜索格式
  245. _snprintf(szCurPath, MAX_PATH, "%s//*.*", DirName); //匹配格式为*.*,即该目录下的所有文件
  246. WIN32_FIND_DATAA FindFileData;
  247. ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
  248. HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData);
  249. BOOL IsFinded = TRUE;
  250. while (IsFinded)
  251. {
  252. IsFinded = FindNextFileA(hFile, &FindFileData); //递归搜索其他的文件
  253. if (strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, "..")) //如果不是"." ".."目录
  254. {
  255. std::string strFileName = "";
  256. strFileName = strFileName + DirName + "//" + FindFileData.cFileName;
  257. std::string strTemp;
  258. strTemp = strFileName;
  259. if (IsDirectory(strFileName.c_str())) //如果是目录,则递归地调用
  260. {
  261. printf("目录为:%s/n", strFileName.c_str());
  262. DeleteDirectory(strTemp.c_str());
  263. }
  264. else
  265. {
  266. DeleteFileA(strTemp.c_str());
  267. }
  268. }
  269. }
  270. FindClose(hFile);
  271. BOOL bRet = RemoveDirectoryA(DirName);
  272. if (bRet == 0) //删除目录
  273. {
  274. printf("删除%s目录失败!/n", DirName);
  275. return FALSE;
  276. }
  277. return TRUE;
  278. #else
  279. return RemoveDirRecursive(DirName);
  280. #endif // RVC_OS_WIN
  281. }
  282. bool removeDir(const string &filePaht)
  283. {
  284. return DeleteDirectory(filePaht.c_str());
  285. }
  286. void stopForDebug()
  287. {
  288. #ifdef RVC_OS_WIN
  289. DWORD processId = GetCurrentProcessId();
  290. char showMsg[100] = "";
  291. sprintf_s(showMsg, "Current pross id is %d", processId);
  292. MessageBox(NULL, showMsg, NULL, 0);
  293. #else
  294. #endif // RVC_OS_WIN
  295. }
  296. void getDirs(string path, vector<string> &ownname)
  297. {
  298. #ifdef RVC_OS_WIN
  299. long hFile = 0;
  300. struct _finddata_t fileinfo;
  301. string p;
  302. if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
  303. {
  304. do
  305. {
  306. //如果是目录加入列表
  307. if ((fileinfo.attrib & _A_SUBDIR) && strcmp(fileinfo.name, ".") && strcmp(fileinfo.name, ".."))
  308. ownname.push_back(fileinfo.name);
  309. } while (_findnext(hFile, &fileinfo) == 0);
  310. _findclose(hFile);
  311. }
  312. #else
  313. array_header_t* subdirs = fileutil_get_sub_files(path.c_str());
  314. int i;
  315. for (i = 0; i < subdirs->nelts; ++i) {
  316. char* strsubdir = ARRAY_IDX(subdirs, i, char*);
  317. ownname.push_back(strsubdir);
  318. }
  319. #endif // RVC_OS_WIN
  320. }
  321. bool getUniqueDir(string path, string &dirName)
  322. {
  323. vector<string> dirs;
  324. getDirs(path, dirs);
  325. if (1 == dirs.size())
  326. {
  327. dirName = dirs[0];
  328. return true;
  329. }
  330. return false;
  331. }
  332. UINT GetPrivateProfileIntEx(LPCSTR lpAppName,LPCSTR lpKeyName,INT nDefault,LPCSTR lpFileName)
  333. {
  334. char lpReturnedString[MAX_PATH] = { 0 };
  335. DWORD nRet = GetPrivateProfileStringEx(
  336. lpAppName,
  337. lpKeyName,
  338. "",
  339. lpReturnedString,
  340. sizeof(lpReturnedString),
  341. lpFileName
  342. );
  343. if (nRet == 0) {
  344. return nDefault;
  345. }
  346. return atoi(lpReturnedString);
  347. }
  348. BOOL WritePrivateProfileStringEx(LPCSTR lpAppName,LPCSTR lpKeyName,LPCSTR lpString,LPCSTR lpFileName)
  349. {
  350. FILE* fp = NULL;
  351. static char szLine[RVC_MAX_INI_LEN] = { 0 };
  352. static char tmpstr[RVC_MAX_INI_LEN] = { 0 };
  353. memset(szLine, 0, sizeof(szLine));
  354. memset(tmpstr, 0, sizeof(tmpstr));
  355. int rtnval;
  356. int i = 0;
  357. int secFlag = 0;
  358. if ((fp = fopen(lpFileName, "rw+")) == NULL){
  359. return FALSE;
  360. }
  361. int lineLen = 0;//整行长度
  362. int orgEqualPos = 0;//=号在原行中的位置
  363. int equalPos = 0; //=号在去空格后的位置
  364. strcpy(tmpstr, "[");
  365. strcat(tmpstr, lpAppName);
  366. strcat(tmpstr, "]");
  367. int endFlag;
  368. while (!feof(fp))
  369. {
  370. rtnval = fgetc(fp);
  371. if (rtnval == EOF){
  372. //最后一行可能无换行符号
  373. rtnval = '\n';
  374. endFlag = 1;
  375. }
  376. //注释行
  377. if ('#' == rtnval || ';' == rtnval)
  378. {
  379. fgets(szLine, sizeof(szLine), fp);
  380. //reset
  381. i = 0;
  382. lineLen = 0;
  383. orgEqualPos = 0;
  384. equalPos = 0;
  385. memset(szLine, 0, sizeof(szLine));
  386. continue;
  387. }
  388. else if ('/' == rtnval)
  389. {
  390. szLine[i++] = rtnval;
  391. lineLen++;
  392. if ('/' == (rtnval = fgetc(fp))) //注释行
  393. {
  394. fgets(szLine, sizeof(szLine), fp);
  395. //reset
  396. i = 0;
  397. lineLen = 0;
  398. orgEqualPos = 0;
  399. equalPos = 0;
  400. memset(szLine, 0, sizeof(szLine));
  401. continue;
  402. }
  403. }
  404. if (rtnval != ' ' && rtnval != '\t')
  405. {
  406. szLine[i++] = rtnval; //去掉空格和tab后的字符串
  407. if (rtnval == '=')
  408. {
  409. orgEqualPos = lineLen;
  410. equalPos = i - 1;
  411. }
  412. }
  413. lineLen++; //字符
  414. if (rtnval == '\n'){
  415. szLine[--i] = '\0';
  416. if (szLine[--i] == '\r')
  417. szLine[i--] = '\0';
  418. if ((equalPos != 0) && (secFlag == 1))
  419. {
  420. szLine[equalPos] = '\0';
  421. if (strcasecmp(szLine, lpKeyName) == 0)
  422. {
  423. //找到key对应变量
  424. int leftPos = ftell(fp);
  425. int writePos = leftPos - lineLen + orgEqualPos + 1;
  426. fseek(fp, 0, SEEK_END);
  427. int leftLen = ftell(fp) - leftPos;
  428. char* pLeft = new char[leftLen];
  429. fseek(fp, leftPos, SEEK_SET);
  430. fread(pLeft, leftLen, 1, fp);
  431. fseek(fp, writePos, SEEK_SET);
  432. fwrite(lpString, strlen(lpString), 1, fp);
  433. fwrite("\n", sizeof(char), 1, fp);
  434. fwrite(pLeft, leftLen, 1, fp);
  435. delete[]pLeft;
  436. pLeft = 0;
  437. fclose(fp);
  438. return TRUE;
  439. }
  440. }
  441. else
  442. {
  443. if (strcasecmp(tmpstr, szLine) == 0)
  444. {
  445. //找到section
  446. secFlag = 1;
  447. }
  448. else if (secFlag == 1 && szLine[0] == '[' && szLine[i] == ']')
  449. {//进入下个section了,说明没找到
  450. int leftPos = ftell(fp) - lineLen;
  451. int writePos = leftPos;
  452. fseek(fp, 0, SEEK_END);
  453. int leftLen = ftell(fp) - leftPos;
  454. char* pLeft = new char[leftLen];
  455. fseek(fp, leftPos, SEEK_SET);
  456. fread(pLeft, leftLen, 1, fp);
  457. fseek(fp, writePos, SEEK_SET);
  458. fwrite("\n", sizeof(char), 1, fp);
  459. fwrite(lpKeyName, strlen(lpKeyName), 1, fp);
  460. fwrite("=", sizeof(char), 1, fp);
  461. fwrite(lpString, strlen(lpString), 1, fp);
  462. fwrite("\n", sizeof(char), 1, fp);
  463. fwrite(pLeft, leftLen, 1, fp);
  464. delete[]pLeft;
  465. pLeft = 0;
  466. fclose(fp);
  467. return TRUE;
  468. }
  469. }
  470. //reset
  471. if (endFlag == 1)
  472. break;
  473. i = 0;
  474. lineLen = 0;
  475. orgEqualPos = 0;
  476. equalPos = 0;
  477. memset(szLine, 0, sizeof(szLine));
  478. }
  479. }
  480. //到文件尾了
  481. if (secFlag)
  482. {//必须有section
  483. fseek(fp, 0, SEEK_END);
  484. fwrite("\n", sizeof(char), 1, fp);
  485. fwrite(lpKeyName, strlen(lpKeyName), 1, fp);
  486. fwrite("=", sizeof(char), 1, fp);
  487. fwrite(lpString, strlen(lpString), 1, fp);
  488. fwrite("\n", sizeof(char), 1, fp);
  489. }
  490. fclose(fp);
  491. return TRUE;
  492. }
  493. DWORD GetPrivateProfileStringEx(LPCSTR lpAppName, LPCSTR lpKeyName, LPCSTR lpDefault, LPSTR lpReturnedString, DWORD nSize, LPCSTR lpFileName)
  494. {
  495. FILE* fp = NULL;
  496. static char szLine[RVC_MAX_INI_LEN] = { 0 };
  497. static char tmpstr[RVC_MAX_INI_LEN] = { 0 };
  498. int rtnval;
  499. int i = 0;
  500. int secFlag = 0;
  501. if ((fp = fopen(lpFileName, "r")) == NULL){
  502. Dbg("have no such file[%s]", lpFileName);
  503. return -1;
  504. }
  505. int equalPos = 0; //=号在去空格后的位置
  506. strcpy(tmpstr, "[");
  507. strcat(tmpstr, lpAppName);
  508. strcat(tmpstr, "]");
  509. int endFlag = 0;
  510. while (!feof(fp))
  511. {
  512. rtnval = fgetc(fp);
  513. if (rtnval == EOF){
  514. //最后一行可能无换行符号
  515. rtnval = '\n';
  516. endFlag = 1;
  517. }
  518. //注释行
  519. if ('#' == rtnval || ';' == rtnval)
  520. {
  521. fgets(szLine, sizeof(szLine), fp);
  522. //reset
  523. i = 0;
  524. equalPos = 0;
  525. memset(szLine, 0, sizeof(szLine));
  526. continue;
  527. }
  528. else if ('/' == rtnval)
  529. {
  530. szLine[i++] = rtnval;
  531. if ('/' == (rtnval = fgetc(fp))) //注释行
  532. {
  533. fgets(szLine, sizeof(szLine), fp);
  534. //reset
  535. i = 0;
  536. equalPos = 0;
  537. memset(szLine, 0, sizeof(szLine));
  538. continue;
  539. }
  540. }
  541. if (rtnval != ' ' && rtnval != '\t')
  542. {
  543. szLine[i++] = rtnval; //去掉空格和tab后的字符串
  544. if (rtnval == '=')
  545. {
  546. equalPos = i - 1;
  547. }
  548. }
  549. if (rtnval == '\n'){
  550. szLine[--i] = '\0';
  551. if (szLine[--i] == '\r')
  552. szLine[i--] = '\0';
  553. if ((equalPos != 0) && (secFlag == 1))
  554. {
  555. szLine[equalPos] = '\0'; //=号变0
  556. if (strcasecmp(szLine, lpKeyName) == 0)
  557. {
  558. //找到key对应变量
  559. strncpy(lpReturnedString, szLine + equalPos + 1, nSize - 1);
  560. lpReturnedString[nSize - 1] = '\0';
  561. fclose(fp);
  562. return 1;
  563. }
  564. }
  565. else
  566. {
  567. if (strcasecmp(tmpstr, szLine) == 0)
  568. {
  569. //找到section
  570. secFlag = 1;
  571. }
  572. else if (secFlag == 1 && szLine[0] == '[' && szLine[i] == ']')
  573. {//进入下个section了,说明没找到
  574. break;
  575. }
  576. }
  577. if (endFlag == 1)
  578. break;
  579. //reset
  580. i = 0;
  581. equalPos = 0;
  582. memset(szLine, 0, sizeof(szLine));
  583. }
  584. }
  585. fclose(fp);
  586. //没找到则用默认
  587. strncpy(lpReturnedString, lpDefault, nSize - 1);
  588. lpReturnedString[nSize - 1] = '\0';
  589. return 0;
  590. }
  591. DWORD GetPrivateProfileSectionEx(LPCSTR lpAppName, LPSTR lpReturnedString, DWORD nSize, LPCSTR lpFileName)
  592. {
  593. //由于项目中未使用,暂未实现
  594. assert(0);
  595. return 0;
  596. }
  597. DWORD GetPrivateProfileSectionNamesEx(LPSTR lpszReturnBuffer, DWORD nSize, LPCSTR lpFileName)
  598. {
  599. FILE* fp = NULL;
  600. static char szLine[RVC_MAX_INI_LEN] = { 0 };
  601. static char tmpstr[RVC_MAX_INI_LEN] = { 0 };
  602. int rPos = 0;
  603. memset(lpszReturnBuffer, 0, nSize);
  604. int rtnval;
  605. int i = 0;
  606. int endFlag;
  607. if ((fp = fopen(lpFileName, "r")) == NULL){
  608. return -1;
  609. }
  610. while (!feof(fp))
  611. {
  612. rtnval = fgetc(fp);
  613. if (rtnval == EOF){
  614. //最后一行可能无换行符号
  615. rtnval = '\n';
  616. endFlag = 1;
  617. }
  618. //注释行
  619. if ('#' == rtnval || ';' == rtnval)
  620. {
  621. fgets(szLine, sizeof(szLine), fp);
  622. //reset
  623. i = 0;
  624. memset(szLine, 0, sizeof(szLine));
  625. continue;
  626. }
  627. else if ('/' == rtnval)
  628. {
  629. szLine[i++] = rtnval;
  630. if ('/' == (rtnval = fgetc(fp))) //注释行
  631. {
  632. fgets(szLine, sizeof(szLine), fp);
  633. //reset
  634. i = 0;
  635. memset(szLine, 0, sizeof(szLine));
  636. continue;
  637. }
  638. }
  639. if (rtnval != ' ' && rtnval != '\t')
  640. {
  641. szLine[i++] = rtnval; //去掉空格和tab后的字符串
  642. }
  643. if (rtnval == '\n')
  644. {
  645. szLine[--i] = '\0';
  646. if (szLine[--i] == '\r')
  647. szLine[i--] = '\0';
  648. if (szLine[0] == '[' && szLine[i] == ']')
  649. {
  650. //找到section
  651. for (int j = 1; j < i && rPos < nSize - 1; j++)
  652. lpszReturnBuffer[rPos++] = szLine[j];
  653. lpszReturnBuffer[rPos++] = '\0';
  654. if (rPos >= nSize)
  655. {
  656. break;
  657. }
  658. }
  659. if (endFlag == 1)
  660. break;
  661. //reset
  662. i = 0;
  663. memset(szLine, 0, sizeof(szLine));
  664. }
  665. }
  666. lpszReturnBuffer[rPos] = '\0';
  667. fclose(fp);
  668. return 0;
  669. }