From 21ab76b9ea9340874c725ee67b585fa97b1d1013 Mon Sep 17 00:00:00 2001 From: BlueMatthew Date: Sat, 18 Nov 2023 12:31:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Profile_Hash.cpp | 1146 +++++++++++++++++++++--------------------- 1 file changed, 573 insertions(+), 573 deletions(-) diff --git a/src/Profile_Hash.cpp b/src/Profile_Hash.cpp index 31ec656..ca7bb5b 100644 --- a/src/Profile_Hash.cpp +++ b/src/Profile_Hash.cpp @@ -6,90 +6,90 @@ * * m.j.y 2001/12/07 * - * - * + * + * * $Name: $ * * $Revision: 1.2 $ * * $Date: 2006/08/04 03:37:33 $ - * + * * $State: Exp $ * * $Log: Profile_Hash.cpp,v $ * Revision 1.2 2006/08/04 03:37:33 zhuzhenhua * no message - * + * * Revision 1.1.2.1 2006/07/28 07:54:02 zhuzhenhua * no message - * + * * Revision 1.5 2003/07/14 02:22:38 scada * 使用def文件导出函数,而不是使用SCADA_API. - * + * * Revision 1.4 2003/01/02 03:14:50 scada * trim substitution line - * + * * Revision 1.3 2002/12/04 07:47:09 scada * for NT - * + * * Revision 1.2 2002/09/24 05:58:34 harold * take acount __OS_SOLARIS__ where __OS__ occur, set errno to 0 before use it - * + * * Revision 1.1.1.1 2002/08/21 07:16:39 harold * temporarily import - * + * * Revision 1.21 2002/06/03 10:06:27 mhorse * 更改了AddNode函数 - * + * * Revision 1.20 2002/03/20 03:40:21 harold * no message - * + * * Revision 1.1.1.1 2002/03/15 13:43:21 harold * no message - * + * * Revision 1.1.1.1 2002/03/15 08:17:11 harold * no message - * + * * Revision 1.19 2002/03/06 01:27:52 harold * *** empty log message *** - * + * * Revision 1.18 2002/02/25 07:30:21 harold * no message - * + * * Revision 1.17 2002/01/25 09:11:19 harold * no message - * + * * Revision 1.16 2002/01/23 02:38:06 harold * test_function - * + * * Revision 1.15 2002/01/22 08:51:42 harold * modify2002-01-22-16:46 - * + * * Revision 1.14 2002/01/21 06:15:49 harold * 修改了函数返回值及参数 - * + * * Revision 1.13 2002/01/21 05:55:25 mhorse * 修改了函数返回值及参数 - * + * * Revision 1.11 2002/01/18 12:57:15 harold * revise errno - * + * * Revision 1.10 1997/03/29 12:48:33 harold * revise bug in read/write/listsec function - * + * * Revision 1.9 2002/01/18 01:09:33 harold * revise bugs in write/close/listsec function - * + * * Revision 1.8 2002/01/09 11:37:11 harold * no message - * - * + * + * * Revision 1.2 2001/12/19 02:19:22 mhorse * 第2次修改 - * + * * Revision 1.1.1.1 2001/12/18 06:42:11 mhorse * 马健莹第一次把配置文件读写库模块加入CVS(2001/12/18) - * + * * ******************************************************************************/ @@ -102,7 +102,7 @@ #include #include #else -//#include + //#include #include //#include // #include @@ -124,7 +124,7 @@ static PGCBOOL g_bHandleInit = PGCFALSE; //定义行注释符 #define MARK_pgcchar_NUM 2 -static pgcchar gs_szMarkpgcchar[MARK_pgcchar_NUM] = {';', '#'}; +static pgcchar gs_szMarkpgcchar[MARK_pgcchar_NUM] = { ';', '#' }; //行结尾字符为\n,不包括\r //#define LINE_EXCLUDE_0A @@ -136,18 +136,18 @@ static pgcchar gs_szMarkpgcchar[MARK_pgcchar_NUM] = {';', '#'}; // 参数说明 : cCh -- 源字符 pgcchar* UpperCase(pgcchar *szSource) { - pgcchar* pcTemp = szSource, *pcFirst = szSource; - - if (pcTemp == NULL) + pgcchar* pcTemp = szSource, *pcFirst = szSource; + + if (pcTemp == NULL) return NULL; - + while (*pcTemp != '\0') { if ((*pcTemp >= 'a') && (*pcTemp <= 'z')) - *pcTemp = (pgcchar)(*pcTemp - ('a' - 'A')) ; + *pcTemp = (pgcchar)(*pcTemp - ('a' - 'A')); pcTemp++; } - + return pcFirst; } @@ -175,37 +175,37 @@ static int fnhash_compare(const void * pKey1, const void * pKey2); * 索引 * pphash_of_index -- 将pindex所指向的数组哈希化, 这样, 在随后的索引搜索 * 时, 使用该哈希索引. -* +* * 返回值 * SUCCESS -- 成功 * 其它 -- 失败代码 -* +* */ #define INDEX_HASH_TABLE_SIZE_FACTOR 20 -pgc32 profile_build_hash_index ( SItemInCache * pindex, - pgc32 count, - TChainHash ** pphash_of_index ) +pgc32 profile_build_hash_index(SItemInCache * pindex, + pgc32 count, + TChainHash ** pphash_of_index) { pgc32 i = 0; TChainHash * phash_table = NULL; - + phash_table = new TChainHash(count * INDEX_HASH_TABLE_SIZE_FACTOR, - fnhash_index_of, - fnhash_key_of, - fnhash_compare, - NULL/*fnhash_create_item*/, - NULL/*fnhash_free_item*/, - NULL/*fnhash_copy_item*/, - NULL/*fnhash_alloc*/, - NULL/*fnhash_dealloc*/); + fnhash_index_of, + fnhash_key_of, + fnhash_compare, + NULL/*fnhash_create_item*/, + NULL/*fnhash_free_item*/, + NULL/*fnhash_copy_item*/, + NULL/*fnhash_alloc*/, + NULL/*fnhash_dealloc*/); if (phash_table == NULL) return ERROR_MEMORY_ALLOC; - - for(i = 0; i < count; i++) + + for (i = 0; i < count; i++) phash_table->insertItem(pindex + i); - + *pphash_of_index = phash_table; - + return SUCCESS; } @@ -224,14 +224,14 @@ pgc32 profile_build_hash_index ( SItemInCache * pindex, * 索引在索引数组中的序号 -- 成功的查找到指定的配置项目的索引 * -1 -- 失败 */ -pgc32 profile_search_sequence ( SItemInCache **pindex_base, - TChainHash *phash_of_index, - const pgcchar *szSecName, - const pgcchar *szItemName) +pgc32 profile_search_sequence(SItemInCache **pindex_base, + TChainHash *phash_of_index, + const pgcchar *szSecName, + const pgcchar *szItemName) { SItemInCache *p = NULL, index; - - memset(&index, 0, sizeof(SItemInCache)); + + memset(&index, 0, sizeof(SItemInCache)); if (szSecName != NULL) { //以下执行大小写转换 @@ -246,15 +246,15 @@ pgc32 profile_search_sequence ( SItemInCache **pindex_base, if (szItemName != NULL) { #ifdef OS_WINDOWS - lstrcpyn((LPWSTR)index.szItemName, (LPCWSTR)szItemName, MAX_CHAR_LINE); - // strupr(index.szItemName); + lstrcpyn((LPWSTR)index.szItemName, (LPCWSTR)szItemName, MAX_CHAR_LINE); + // strupr(index.szItemName); #else - strncpy(index.szItemName, szItemName, MAX_CHAR_LINE - 1); - // UpperCase(index.szItemName); + strncpy(index.szItemName, szItemName, MAX_CHAR_LINE - 1); + // UpperCase(index.szItemName); #endif } p = phash_of_index->searchItem((void *)&index); - if(NULL == p) + if (NULL == p) return -1; *pindex_base = p; return 0; @@ -272,7 +272,7 @@ static u32 fnhash_index_of(const void * pKey, u32 hash_size) long index = 0; u_char *pbString = NULL; int iX = 0, iY = 0, i = 0; - + SItemInCache * pindex = (SItemInCache *)pKey, sindex; memcpy(&sindex, pindex, sizeof(SItemInCache)); @@ -284,7 +284,7 @@ static u32 fnhash_index_of(const void * pKey, u32 hash_size) UpperCase(sindex.szSecName); UpperCase(sindex.szItemName); #endif - + //哈希转换 pbString = (u_char*)sindex.szSecName; for (i = 0; i < MAX_CHAR_LINE; i++) @@ -292,16 +292,16 @@ static u32 fnhash_index_of(const void * pKey, u32 hash_size) iX = iX + pbString[i]; } iX = iX % MAX_SEC_NUM; - + pbString = (u_char*)sindex.szItemName; - for ( i = 0; i < MAX_CHAR_LINE; i++) + for (i = 0; i < MAX_CHAR_LINE; i++) { iY = iY + pbString[i]; } iY = iY % MAX_ITEM_NUM; - + index = iX * MAX_ITEM_NUM + iY; - + return (u32)(index % hash_size); } @@ -320,10 +320,10 @@ static int fnhash_compare(const void * pKey1, const void * pKey2) { SItemInCache * pindex1 = (SItemInCache *)pKey1; SItemInCache * pindex2 = (SItemInCache *)pKey2; - + if (pKey1 == NULL) return 1; -//#ifdef OS_WINDOWS + //#ifdef OS_WINDOWS if (pKey2 == NULL) { if (_stricmp(pindex1->szSecName, pindex2->szSecName) == 0) @@ -339,24 +339,24 @@ static int fnhash_compare(const void * pKey1, const void * pKey2) else return 1; } -/*#else - if (pKey2 == NULL) - { - - if (strcasecmp(pindex1->szSecName, pindex2->szSecName) == 0) - return 0; - else - return 1; - } - else - { - if ((strcasecmp(pindex1->szSecName, pindex2->szSecName) == 0) - && (strcasecmp(pindex1->szItemName, pindex2->szItemName) == 0)) - return 0; + /*#else + if (pKey2 == NULL) + { + + if (strcasecmp(pindex1->szSecName, pindex2->szSecName) == 0) + return 0; + else + return 1; + } else - return 1; - } -#endif*/ + { + if ((strcasecmp(pindex1->szSecName, pindex2->szSecName) == 0) + && (strcasecmp(pindex1->szItemName, pindex2->szItemName) == 0)) + return 0; + else + return 1; + } + #endif*/ } @@ -368,7 +368,7 @@ PGCBOOL LineIsMark(pgcchar *szLine) for (pgc32 i = 0; i < MARK_pgcchar_NUM; i++) if (*szLine == gs_szMarkpgcchar[i]) return PGCTRUE; - return PGCFALSE; + return PGCFALSE; } @@ -381,13 +381,13 @@ pgcchar* FindSecTail(pgcchar *szString) { return NULL; } - + while (szStart != NULL) { - szStart = strchr(szStart, '['); - if ((szStart != NULL) && (*(szStart - 1) == '\n')) - break; - if (szStart != NULL) szStart++; + szStart = strchr(szStart, '['); + if ((szStart != NULL) && (*(szStart - 1) == '\n')) + break; + if (szStart != NULL) szStart++; } if (szStart != NULL) @@ -397,10 +397,10 @@ pgcchar* FindSecTail(pgcchar *szString) { if (*szTemp == ']') return szStart; - szTemp ++; + szTemp++; } } - + return NULL; } @@ -411,7 +411,7 @@ PGCBOOL DigSecName(const pgcchar *szString, pgcchar *szRevBuffer, pgc32 iBufferL pgcchar *szStart = NULL, *szTemp = NULL; PGCBOOL bSecNameExist = PGCFALSE; pgc32 iLength = iBufferLength - 1; - + if ((szStart = (pgcchar *)strchr((const char*)szString, '[')) != NULL) { szTemp = szStart; @@ -422,24 +422,24 @@ PGCBOOL DigSecName(const pgcchar *szString, pgcchar *szRevBuffer, pgc32 iBufferL bSecNameExist = PGCTRUE; break; } - szTemp ++; + szTemp++; } - + if (bSecNameExist) { iLength = (pgc32)(szTemp - szStart) - 1; if (iLength >= iBufferLength) iLength = iBufferLength - 1; - for (pgc32 i = 0; i < iLength; i++) + for (pgc32 i = 0; i < iLength; i++) szRevBuffer[i] = *(szStart + 1 + i); szRevBuffer[iLength] = '\0'; - /* + /* #ifndef __unix__ strupr(szRevBuffer); #else UpperCase(szRevBuffer); #endif - */ + */ return PGCTRUE; } } @@ -448,29 +448,29 @@ PGCBOOL DigSecName(const pgcchar *szString, pgcchar *szRevBuffer, pgc32 iBufferL //提取项目名及项目值 -PGCBOOL DigItemContent( pgcchar *szString, pgcchar *szItemName, pgcchar *szItemVal, pgc32 iBufferLength) +PGCBOOL DigItemContent(pgcchar *szString, pgcchar *szItemName, pgcchar *szItemVal, pgc32 iBufferLength) { pgcchar *pcBeval = NULL, *pcTemp = NULL, szTemp[MAX_CHAR_LINE]; pgc32 iCount = 0, i = 0; - - + + if ((pcBeval = strchr(szString, '=')) == NULL) return PGCFALSE; - + //获取项目名 pcTemp = szString; - while ((pcTemp < pcBeval) && (iCount < (MAX_CHAR_LINE - 1))) + while ((pcTemp < pcBeval) && (iCount < (MAX_CHAR_LINE - 1))) { szTemp[iCount] = *pcTemp; pcTemp++; iCount++; } szTemp[iCount] = '\0'; - + //删除前面多余空格 - for ( i = 0; i < MAX_CHAR_LINE; i++) + for (i = 0; i < MAX_CHAR_LINE; i++) { - if (szTemp[i] != ' ') + if (szTemp[i] != ' ') { for (pgc32 j = i; j < MAX_CHAR_LINE; j++) { @@ -479,11 +479,11 @@ PGCBOOL DigItemContent( pgcchar *szString, pgcchar *szItemName, pgcchar *szItemV break; }// end if }// end for - + //删除后面多余空格 for (i = iCount - 1; i >= 0; i--) { - if (szTemp[i] != ' ') + if (szTemp[i] != ' ') break; szTemp[i] = '\0'; }// end for @@ -493,18 +493,18 @@ PGCBOOL DigItemContent( pgcchar *szString, pgcchar *szItemName, pgcchar *szItemV UpperCase(szTemp); #endif*/ strcpy(szItemName, szTemp); - + //获取项目值 memset(szTemp, 0, MAX_CHAR_LINE); #ifdef OS_WINDOWS lstrcpyn((LPWSTR)szTemp, (LPCWSTR)pcBeval + 1, MAX_CHAR_LINE); #else - strncpy(szTemp ,pcBeval + 1, MAX_CHAR_LINE - 1); + strncpy(szTemp, pcBeval + 1, MAX_CHAR_LINE - 1); #endif //删除前面多余空格 for (i = 0; i < MAX_CHAR_LINE; i++) { - if (szTemp[i] != ' ') + if (szTemp[i] != ' ') { for (pgc32 j = i; j < MAX_CHAR_LINE; j++) { @@ -513,16 +513,16 @@ PGCBOOL DigItemContent( pgcchar *szString, pgcchar *szItemName, pgcchar *szItemV break; }// end if }// end for - + //删除后面多余空格 - for ( i = (pgc32)strlen(szTemp) - 1 ; i >= 0; i--) + for (i = (pgc32)strlen(szTemp) - 1; i >= 0; i--) { - if (szTemp[i] != ' ') + if (szTemp[i] != ' ') break; szTemp[i] = '\0'; }// end for strcpy(szItemVal, szTemp); - + return PGCTRUE; } @@ -537,13 +537,13 @@ PGCBOOL DigItemContent( pgcchar *szString, pgcchar *szItemName, pgcchar *szItemV PGCBOOL InsertNode(SItemSequence * &psHead, pgc32 iPos, SItemInCache *psCacheEntry, pgc32 iSecPos) { SItemSequence *psTemp = NULL, *psNewNode = NULL, *psPrior = NULL; - + if ((psHead == NULL) || (psCacheEntry == NULL)) return PGCFALSE; psNewNode = new SItemSequence[1]; psNewNode->iPos = iPos; - psNewNode->NextNode = NULL; - + psNewNode->NextNode = NULL; + psTemp = psHead; while (psTemp != NULL) { @@ -562,7 +562,7 @@ PGCBOOL InsertNode(SItemSequence * &psHead, pgc32 iPos, SItemInCache *psCacheEnt return PGCTRUE; } } - delete []psNewNode; + delete[]psNewNode; return PGCFALSE; } @@ -577,10 +577,10 @@ PGCBOOL InsertNode(SItemSequence * &psHead, pgc32 iPos, SItemInCache *psCacheEnt PGCBOOL DisableSec(SItemSequence * &psHead, pgc32 iSecPos, SItemInCache *psCacheEntry) { SItemSequence *psTemp = NULL; - - if (psHead == NULL) + + if (psHead == NULL) return PGCFALSE; - + psTemp = psHead; while (psTemp != NULL) { @@ -606,25 +606,25 @@ PGCBOOL DisableSec(SItemSequence * &psHead, pgc32 iSecPos, SItemInCache *psCache PGCBOOL AddNode(SItemSequence * &psHead, SItemSequence * &pCurSeq, pgc32 iPos) { SItemSequence *psNewNode = NULL; - + psNewNode = new SItemSequence[1]; psNewNode->iPos = iPos; psNewNode->NextNode = NULL; - + if (psHead == NULL) { psHead = psNewNode; - pCurSeq = psHead; + pCurSeq = psHead; } else { if (pCurSeq) { - pCurSeq->NextNode = psNewNode; - pCurSeq = psNewNode; + pCurSeq->NextNode = psNewNode; + pCurSeq = psNewNode; } } - + return PGCTRUE; } @@ -634,28 +634,28 @@ void ReleaseProfileCache(HPROFILE hProfile) FILE *fstream = NULL; TChainHash *pHashTable = NULL; SItemSequence *pSequence = NULL, *psPrior = NULL; - SItemInCache *psCacheEntry = NULL; + SItemInCache *psCacheEntry = NULL; SMemoChain *psMemo = NULL, *psMemoPrior = NULL; pgc32 iCurItemCount = 0; - - fstream = g_pHandleIndex[hProfile].fstream ; + + fstream = g_pHandleIndex[hProfile].fstream; if (fstream != NULL) fclose(fstream); - + pHashTable = g_pHandleIndex[hProfile].pHashTable; if (pHashTable != NULL) delete pHashTable; - + pSequence = g_pHandleIndex[hProfile].pSequence; //删除该链表的所有节点 while (pSequence != NULL) { psPrior = pSequence; pSequence = pSequence->NextNode; - delete []psPrior; + delete[]psPrior; } - - + + iCurItemCount = g_pHandleIndex[hProfile].CurUseItems; psCacheEntry = g_pHandleIndex[hProfile].pCacheEntry; //首先释放注释节点 @@ -668,13 +668,13 @@ void ReleaseProfileCache(HPROFILE hProfile) { psMemoPrior = psMemo; psMemo = psMemo->NextNode; - delete []psMemoPrior->szMemo; - delete []psMemoPrior; + delete[]psMemoPrior->szMemo; + delete[]psMemoPrior; } } free(psCacheEntry); } - + return; } @@ -685,10 +685,10 @@ pgcu32 AdjustCache(HPROFILE hCacher, pgc32 iNewItem) pgc32 iOldSize = g_pHandleIndex[hCacher].CurItemCount, iNewSize = 0; pgc32 iOldUse = g_pHandleIndex[hCacher].CurUseItems; SItemInCache *pCacheEntry = NULL; - - if ((g_pHandleIndex[hCacher].CurUseItems + iNewItem ) >= iOldSize) + + if ((g_pHandleIndex[hCacher].CurUseItems + iNewItem) >= iOldSize) { - + iNewSize = iOldSize + RESIZE_ITEM_NUM; pCacheEntry = (SItemInCache*)malloc(iNewSize * sizeof(SItemInCache)); if (pCacheEntry == NULL) @@ -698,14 +698,14 @@ pgcu32 AdjustCache(HPROFILE hCacher, pgc32 iNewItem) free(g_pHandleIndex[hCacher].pCacheEntry); g_pHandleIndex[hCacher].CurItemCount = iNewSize; g_pHandleIndex[hCacher].pCacheEntry = pCacheEntry; - + //分离哈希表的所有数据项 g_pHandleIndex[hCacher].pHashTable->detachItems(); for (pgc32 i = 0; i < iOldUse; i++) g_pHandleIndex[hCacher].pHashTable->insertItem(pCacheEntry + i); return SUCCESS; } - return ERROR_FAIL; + return ERROR_FAIL; } @@ -722,12 +722,12 @@ pgc32 get_file_lastMTime(char * pszfname, timeval *pt) struct stat buf; memset((void *)&buf, 0, sizeof(buf)); - + ret = stat(pszfname, &buf); - if(-1 == ret) + if (-1 == ret) return ERROR_FAIL; - pt->tv_sec = buf.st_mtime; + pt->tv_sec = buf.st_mtime; pt->tv_usec = 0; return SUCCESS; } @@ -741,17 +741,17 @@ pgc32 ReWriteProfile(HPROFILE hProfile) pgcchar *pcFileBuffer = NULL, *pcTemp = NULL; pgcchar *pcHead = NULL; PGCBOOL bSecExist = PGCFALSE; - SItemSequence * psSeq = g_pHandleIndex[hProfile].pSequence ; + SItemSequence * psSeq = g_pHandleIndex[hProfile].pSequence; SItemInCache *psCacheEntry = g_pHandleIndex[hProfile].pCacheEntry, *psTemp = NULL; SMemoChain *psMemo = NULL; FILE *stream = /*g_pHandleIndex[hProfile].fstream*/0; iSize = (g_pHandleIndex[hProfile].CurLines + 2) * (MAX_CHAR_LINE + 2); - - if (( pcHead = (pgcchar*)malloc(iSize)) != NULL) + + if ((pcHead = (pgcchar*)malloc(iSize)) != NULL) { pcFileBuffer = pcHead; memset(pcFileBuffer, 0, iSize); - while (psSeq != NULL) + while (psSeq != NULL) { psTemp = psCacheEntry + psSeq->iPos; if (psTemp->valid) @@ -778,7 +778,7 @@ pgc32 ReWriteProfile(HPROFILE hProfile) #endif psMemo = psMemo->NextNode; } - + //是段吗? if (psTemp->bIsSecName) { @@ -795,7 +795,7 @@ pgc32 ReWriteProfile(HPROFILE hProfile) } else bSecExist = PGCTRUE; - + *pcFileBuffer = '['; pcFileBuffer++; pcTemp = psTemp->szSecName; @@ -814,7 +814,7 @@ pgc32 ReWriteProfile(HPROFILE hProfile) *(pcFileBuffer + 2) = '\n'; pcFileBuffer += 3; #endif - + } else { @@ -846,12 +846,12 @@ pgc32 ReWriteProfile(HPROFILE hProfile) }// end if "valid" psSeq = psSeq->NextNode; }// end while - + //2002/03/05 杨小波++++++++++++++++++++++++++++++++++++++++++++ #if 0 - pgcu32 numwritten = (pgcu32)pcFileBuffer - (pgcu32)pcHead ; + pgcu32 numwritten = (pgcu32)pcFileBuffer - (pgcu32)pcHead; #endif - pgcu32 numwritten = (pgcu32)(pcFileBuffer - pcHead) ; + pgcu32 numwritten = (pgcu32)(pcFileBuffer - pcHead); //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -875,7 +875,7 @@ pgc32 ReWriteProfile(HPROFILE hProfile) } } while (errno == EINTR); - + if (!bOpenOK) { free(pcHead); @@ -884,15 +884,15 @@ pgc32 ReWriteProfile(HPROFILE hProfile) //ghConfig = DSOpenFile("g_pHandleIndex[hProfile].szPathName"); #endif - + fseek(stream, 0, SEEK_SET); //函数fwrite在写数据到文件时,如遇字符NULL,会产生错误,从而使文件无法定位结尾 - pgc32 numwritted = (pgc32)fwrite(pcHead, sizeof( pgcchar ), numwritten , stream ); + pgc32 numwritted = (pgc32)fwrite(pcHead, sizeof(pgcchar), numwritten, stream); free(pcHead); fclose(stream); //记录下修改文件的时间 get_file_lastMTime(g_pHandleIndex[hProfile].szPathName, &g_pHandleIndex[hProfile].tLastModify); - if (numwritted != numwritten) + if (numwritted != numwritten) { return ERROR_FWRITE; } @@ -918,28 +918,28 @@ int timeval_compare(timeval * ptv1, timeval * ptv2) // 参数说明 : hProfile -- void ReCreateCache(HPROFILE hProfile) { - timeval tLastModify, *ptRecModify = NULL; - pgcchar *szProfileName = g_pHandleIndex[hProfile].szPathName; - ptRecModify = &g_pHandleIndex[hProfile].tLastModify; - if (szProfileName == 0) - return; - - if (get_file_lastMTime(szProfileName, &tLastModify) == SUCCESS) - { - if (timeval_compare(&tLastModify, ptRecModify) > 0) + timeval tLastModify, *ptRecModify = NULL; + pgcchar *szProfileName = g_pHandleIndex[hProfile].szPathName; + ptRecModify = &g_pHandleIndex[hProfile].tLastModify; + if (szProfileName == 0) + return; + + if (get_file_lastMTime(szProfileName, &tLastModify) == SUCCESS) { - ReleaseProfileCache(hProfile); - ProcessCreateCache(hProfile); + if (timeval_compare(&tLastModify, ptRecModify) > 0) + { + ReleaseProfileCache(hProfile); + ProcessCreateCache(hProfile); + } } - } - return; + return; } pgc32 ProcessCreateCache(HPROFILE hProfile) { pgcchar *pcFileBuffer = NULL, *pcTemp = NULL, *pcStart = NULL, *pcEnd = NULL; - pgcchar *pcNextLine = NULL, *pcTemp1 = NULL ; + pgcchar *pcNextLine = NULL, *pcTemp1 = NULL; pgcchar szSecName[MAX_CHAR_LINE], CurItemName[MAX_CHAR_LINE], CurItemVal[MAX_CHAR_LINE]; SItemInCache *psTemp = NULL; SMemoChain *psMemoTep = NULL, *psMemo = NULL; @@ -962,349 +962,349 @@ pgc32 ProcessCreateCache(HPROFILE hProfile) #endif memset(&g_pHandleIndex[hProfile], 0, sizeof(SProfileHanleInfo)); -//#ifdef OS_WINDOWS - //windows下打开文件 - if ((fstream = fopen(szProfileName, "r+b")) == NULL) - { - return ERROR_FOPEN; - } -/*#else - //unix下打开文件 - PGCBOOL bOpenOK = PGCFALSE; - do - { - errno = 0; - if ((fstream = fopen(szProfileName, "r+b")) != NULL) - { - bOpenOK = PGCTRUE; - break; - } - } - while (errno == EINTR); - - if (!bOpenOK) - return ERROR_FOPEN; -#endif*/ - - if (fseek(fstream, 0, SEEK_END) != 0) - { - fclose(fstream); - return 0; - } - - //获取文件长度 - lFileLength = ftell(fstream); - pcTemp = new pgcchar[lFileLength + 1]; - if (pcTemp == NULL) - { - fclose(fstream); - return 0; - } - memset(pcTemp, 0, lFileLength + 1); - if (fseek(fstream, 0, SEEK_SET) != 0) - { - fclose(fstream); - return 0; - } - //将文件读入缓冲 - if ((iCount = (pgc32)fread(pcTemp, sizeof( pgcchar ), lFileLength, fstream)) != lFileLength) - { - fclose(fstream); - return ERROR_FREAD; - } - - - - //格式化流(去除每一行前面的多余空格) - pcStart = pcTemp; - pcEnd = pcTemp + iCount; - while ((pcStart != NULL) && (pcStart < pcEnd)) - { - if (*pcStart == ' ') - { - *pcStart = 0; - pcStart ++; - } - else + //#ifdef OS_WINDOWS + //windows下打开文件 + if ((fstream = fopen(szProfileName, "r+b")) == NULL) + { + return ERROR_FOPEN; + } + /*#else + //unix下打开文件 + PGCBOOL bOpenOK = PGCFALSE; + do { - if (*pcStart == '\n') - pcStart ++; - else + errno = 0; + if ((fstream = fopen(szProfileName, "r+b")) != NULL) { - if ((pcStart = strchr(pcStart, '\n')) == NULL) break; - pcStart ++; + bOpenOK = PGCTRUE; + break; } } - }// end while - - - - //格式化流(去除每一行结束标志"\r\n"中的"\r"标志,仅保留"\n"标志) - pcStart = pcTemp; - while ((pcStart != NULL) && (pcStart < pcEnd)) + while (errno == EINTR); + + if (!bOpenOK) + return ERROR_FOPEN; + #endif*/ + + if (fseek(fstream, 0, SEEK_END) != 0) + { + fclose(fstream); + return 0; + } + + //获取文件长度 + lFileLength = ftell(fstream); + pcTemp = new pgcchar[lFileLength + 1]; + if (pcTemp == NULL) + { + fclose(fstream); + return 0; + } + memset(pcTemp, 0, lFileLength + 1); + if (fseek(fstream, 0, SEEK_SET) != 0) + { + fclose(fstream); + return 0; + } + //将文件读入缓冲 + if ((iCount = (pgc32)fread(pcTemp, sizeof(pgcchar), lFileLength, fstream)) != lFileLength) + { + fclose(fstream); + return ERROR_FREAD; + } + + + + //格式化流(去除每一行前面的多余空格) + pcStart = pcTemp; + pcEnd = pcTemp + iCount; + while ((pcStart != NULL) && (pcStart < pcEnd)) + { + if (*pcStart == ' ') { - if ((*pcStart == '\r') && (*(pcStart + 1) == '\n')) - { - *pcStart = 0; - pcStart +=2; - } - else - pcStart++; - }// end while - - - - //统计行数 - pcStart = pcTemp; - while ((pcStart != NULL) && (pcStart < pcEnd)) + *pcStart = 0; + pcStart++; + } + else { if (*pcStart == '\n') + pcStart++; + else { - iLines++; + if ((pcStart = strchr(pcStart, '\n')) == NULL) break; + pcStart++; } + } + }// end while + + + + //格式化流(去除每一行结束标志"\r\n"中的"\r"标志,仅保留"\n"标志) + pcStart = pcTemp; + while ((pcStart != NULL) && (pcStart < pcEnd)) + { + if ((*pcStart == '\r') && (*(pcStart + 1) == '\n')) + { + *pcStart = 0; + pcStart += 2; + } + else pcStart++; - }// end while - iLines++; - g_pHandleIndex[hProfile].CurLines = iLines; - - //重建文件缓冲 - pcFileBuffer = new pgcchar[iCount + 1]; - memset(pcFileBuffer, 0, iCount + 1); - for ( i = 0; i < iCount; i++) + }// end while + + + + //统计行数 + pcStart = pcTemp; + while ((pcStart != NULL) && (pcStart < pcEnd)) + { + if (*pcStart == '\n') { - if (pcTemp[i] != 0) - { - pcFileBuffer[iPos] = pcTemp[i]; - iPos++; - } + iLines++; } - delete []pcTemp; - - - - //分配配置项缓冲 - psCacheEntry = (SItemInCache *)malloc(INITCACHE_ITEM_NUM * sizeof(SItemInCache)); - if (psCacheEntry == NULL) + pcStart++; + }// end while + iLines++; + g_pHandleIndex[hProfile].CurLines = iLines; + + //重建文件缓冲 + pcFileBuffer = new pgcchar[iCount + 1]; + memset(pcFileBuffer, 0, iCount + 1); + for (i = 0; i < iCount; i++) + { + if (pcTemp[i] != 0) { - if (pcFileBuffer != NULL) - delete []pcFileBuffer; - fclose(fstream); - return ERROR_MEMORY_ALLOC; + pcFileBuffer[iPos] = pcTemp[i]; + iPos++; } - memset(psCacheEntry, 0, INITCACHE_ITEM_NUM * sizeof(SItemInCache)); - iCurItemCount = INITCACHE_ITEM_NUM; - g_pHandleIndex[hProfile].fstream = fstream; - g_pHandleIndex[hProfile].pHashTable = NULL; - g_pHandleIndex[hProfile].pSequence = NULL; - g_pHandleIndex[hProfile].pCurSeq = NULL; - g_pHandleIndex[hProfile].CurItemCount = iCurItemCount; - g_pHandleIndex[hProfile].pCacheEntry = psCacheEntry; - - //提取各段 - pcStart = pcFileBuffer; - pcEnd = NULL; - while ((pcStart != NULL) && (pcStart < (pcFileBuffer + iPos))) + } + delete[]pcTemp; + + + + //分配配置项缓冲 + psCacheEntry = (SItemInCache *)malloc(INITCACHE_ITEM_NUM * sizeof(SItemInCache)); + if (psCacheEntry == NULL) + { + if (pcFileBuffer != NULL) + delete[]pcFileBuffer; + fclose(fstream); + return ERROR_MEMORY_ALLOC; + } + memset(psCacheEntry, 0, INITCACHE_ITEM_NUM * sizeof(SItemInCache)); + iCurItemCount = INITCACHE_ITEM_NUM; + g_pHandleIndex[hProfile].fstream = fstream; + g_pHandleIndex[hProfile].pHashTable = NULL; + g_pHandleIndex[hProfile].pSequence = NULL; + g_pHandleIndex[hProfile].pCurSeq = NULL; + g_pHandleIndex[hProfile].CurItemCount = iCurItemCount; + g_pHandleIndex[hProfile].pCacheEntry = psCacheEntry; + + //提取各段 + pcStart = pcFileBuffer; + pcEnd = NULL; + while ((pcStart != NULL) && (pcStart < (pcFileBuffer + iPos))) + { + //寻找段尾 + pcEnd = FindSecTail(pcStart + 1); + + if (pcEnd != NULL) + *(pcEnd - 1) = '\0'; + + //检查行类型 + while (pcStart != NULL) { - //寻找段尾 - pcEnd = FindSecTail(pcStart + 1); - - if (pcEnd != NULL) - *(pcEnd - 1) = '\0'; - - //检查行类型 - while (pcStart != NULL) + //确定下一行的位置 + if ((pcNextLine = strchr(pcStart, '\n')) != NULL) + { + //如果为空行 + if (pcStart == pcNextLine) + iCurLineType = 4; + *pcNextLine = '\0'; + pcNextLine++; + } + + if (iCurLineType != 4) { - //确定下一行的位置 - if ((pcNextLine = strchr(pcStart, '\n')) != NULL) + //检查当前行是否为注释行 + if (LineIsMark(pcStart)) { - //如果为空行 - if (pcStart == pcNextLine) - iCurLineType = 4; - *pcNextLine = '\0'; - pcNextLine++; + iCurLineType = 1; } - - if (iCurLineType != 4) + else { - //检查当前行是否为注释行 - if (LineIsMark(pcStart)) + pcTemp = strchr(pcStart, '['); + pcTemp1 = strchr(pcStart, ']'); + //如果当前行是段名,则提取段名 + if (pcTemp && pcTemp1 && (pcTemp1 > pcTemp)) { - iCurLineType = 1; + DigSecName(pcStart, szSecName, MAX_CHAR_LINE); + iCurLineType = 2; } else { - pcTemp = strchr(pcStart, '['); - pcTemp1 = strchr(pcStart, ']'); - //如果当前行是段名,则提取段名 - if ( pcTemp && pcTemp1 && (pcTemp1 > pcTemp)) + pcTemp = strchr(pcStart, '='); + //如果当前行是项目记录,则提取项目名及项目值 + if (pcTemp != NULL) { - DigSecName(pcStart, szSecName, MAX_CHAR_LINE); - iCurLineType = 2; + DigItemContent(pcStart, CurItemName, CurItemVal, MAX_CHAR_LINE); + iCurLineType = 3; } else - { - pcTemp = strchr(pcStart, '='); - //如果当前行是项目记录,则提取项目名及项目值 - if (pcTemp != NULL) - { - DigItemContent(pcStart, CurItemName, CurItemVal, MAX_CHAR_LINE); - iCurLineType = 3; - } - else - iCurLineType = 0; - } - }// end else - } - - switch (iCurLineType) + iCurLineType = 0; + } + }// end else + } + + switch (iCurLineType) + { + //如果是注释行,或空行 + case 1: + case 4: + //将注释行添加入节点 + psMemoTep = psMemo; + if (psMemoTep != NULL) { - //如果是注释行,或空行 - case 1: - case 4: - //将注释行添加入节点 - psMemoTep = psMemo; - if (psMemoTep != NULL) + while (psMemoTep->NextNode != NULL) + { + psMemoTep = psMemoTep->NextNode; + }// end while + psMemoTep->NextNode = new SMemoChain[1]; + if (iCurLineType == 1) + { + psMemoTep->NextNode->szMemo = new pgcchar[strlen(pcStart) + 1]; + strcpy(psMemoTep->NextNode->szMemo, pcStart); + } + else if (iCurLineType == 4) { - while (psMemoTep->NextNode != NULL) - { - psMemoTep = psMemoTep->NextNode; - }// end while - psMemoTep->NextNode = new SMemoChain[1]; - if (iCurLineType == 1) - { - psMemoTep->NextNode->szMemo = new pgcchar[strlen(pcStart) + 1]; - strcpy(psMemoTep->NextNode->szMemo, pcStart); - } - else if (iCurLineType == 4) - { #ifdef LINE_EXCLUDE_0A - psMemoTep->NextNode->szMemo = new pgcchar[2]; - psMemoTep->NextNode->szMemo[0] = '\0'; - psMemoTep->NextNode->szMemo[1] = '\0'; + psMemoTep->NextNode->szMemo = new pgcchar[2]; + psMemoTep->NextNode->szMemo[0] = '\0'; + psMemoTep->NextNode->szMemo[1] = '\0'; #else - psMemoTep->NextNode->szMemo = new pgcchar[3]; - psMemoTep->NextNode->szMemo[0] = '\0'; - psMemoTep->NextNode->szMemo[1] = '\n'; - psMemoTep->NextNode->szMemo[2] = '\0'; + psMemoTep->NextNode->szMemo = new pgcchar[3]; + psMemoTep->NextNode->szMemo[0] = '\0'; + psMemoTep->NextNode->szMemo[1] = '\n'; + psMemoTep->NextNode->szMemo[2] = '\0'; #endif - } - psMemoTep->NextNode->NextNode = NULL; - }// end if - else + } + psMemoTep->NextNode->NextNode = NULL; + }// end if + else + { + psMemo = new SMemoChain[1]; + if (iCurLineType == 1) + { + psMemo->szMemo = new pgcchar[strlen(pcStart) + 1]; + strcpy(psMemo->szMemo, pcStart); + } + else if (iCurLineType == 4) { - psMemo = new SMemoChain[1]; - if (iCurLineType == 1) - { - psMemo->szMemo = new pgcchar[strlen(pcStart) + 1]; - strcpy(psMemo->szMemo, pcStart); - } - else if (iCurLineType == 4) - { #ifdef LINE_EXCLUDE_0A - psMemo->szMemo = new pgcchar[2]; - psMemo->szMemo[0] = '\0'; - psMemo->szMemo[1] = '\0'; + psMemo->szMemo = new pgcchar[2]; + psMemo->szMemo[0] = '\0'; + psMemo->szMemo[1] = '\0'; #else - psMemo->szMemo = new pgcchar[3]; - psMemo->szMemo[0] = '\0'; - psMemo->szMemo[1] = '\n'; - psMemo->szMemo[2] = '\0'; + psMemo->szMemo = new pgcchar[3]; + psMemo->szMemo[0] = '\0'; + psMemo->szMemo[1] = '\n'; + psMemo->szMemo[2] = '\0'; #endif - } - psMemo->NextNode = NULL; - }// end else - - break; - - //如果是段名 - case 2: + } + psMemo->NextNode = NULL; + }// end else + + break; + + //如果是段名 + case 2: + psTemp = &psCacheEntry[iCurItem]; + strcpy(psTemp->szSecName, szSecName); + psTemp->valid = PGCTRUE; + psTemp->bIsSecName = PGCTRUE; + psTemp->memo = psMemo; + + AddNode(g_pHandleIndex[hProfile].pSequence, g_pHandleIndex[hProfile].pCurSeq, iCurItem); + bSecNameExist = PGCTRUE; + iCurItem++; + psMemo = NULL; + break; + //如果是项目赋值 + case 3: + if (bSecNameExist) + { psTemp = &psCacheEntry[iCurItem]; strcpy(psTemp->szSecName, szSecName); + strcpy(psTemp->szItemName, CurItemName); + strcpy(psTemp->szItemVal, CurItemVal); psTemp->valid = PGCTRUE; - psTemp->bIsSecName = PGCTRUE; + psTemp->bIsSecName = PGCFALSE; psTemp->memo = psMemo; - + AddNode(g_pHandleIndex[hProfile].pSequence, g_pHandleIndex[hProfile].pCurSeq, iCurItem); - bSecNameExist = PGCTRUE; - iCurItem ++; + iCurItem++; psMemo = NULL; - break; - //如果是项目赋值 - case 3: - if (bSecNameExist) - { - psTemp = &psCacheEntry[iCurItem]; - strcpy(psTemp->szSecName, szSecName); - strcpy(psTemp->szItemName, CurItemName); - strcpy(psTemp->szItemVal, CurItemVal); - psTemp->valid = PGCTRUE; - psTemp->bIsSecName = PGCFALSE; - psTemp->memo = psMemo; - - AddNode(g_pHandleIndex[hProfile].pSequence, g_pHandleIndex[hProfile].pCurSeq, iCurItem); - iCurItem ++; - psMemo = NULL; - }//end if - break; - //default: - }// end switch - - iCurLineType = 0; + }//end if + break; + //default: + }// end switch + + iCurLineType = 0; - //如果原先的缓冲区太小,则递增缓冲区 - if (iCurItem >= iCurItemCount) + //如果原先的缓冲区太小,则递增缓冲区 + if (iCurItem >= iCurItemCount) + { + psCacheTep = (SItemInCache*)malloc((iCurItemCount + RESIZE_ITEM_NUM) * sizeof(SItemInCache)); + if (psCacheTep == NULL) { - psCacheTep = (SItemInCache*)malloc((iCurItemCount + RESIZE_ITEM_NUM) * sizeof(SItemInCache)); - if (psCacheTep == NULL) - { - ReleaseProfileCache(hProfile); - return ERROR_MEMORY_ALLOC; - } - memset(psCacheTep, 0, (iCurItemCount + RESIZE_ITEM_NUM) * sizeof(SItemInCache)); - memmove(psCacheTep, psCacheEntry, sizeof(SItemInCache) * iCurItemCount); - free(psCacheEntry); - psCacheEntry = psCacheTep; - iCurItemCount = iCurItemCount + RESIZE_ITEM_NUM; - g_pHandleIndex[hProfile].CurItemCount = iCurItemCount; - g_pHandleIndex[hProfile].pCacheEntry = psCacheEntry; - }// end if - - pcStart = pcNextLine; - }// end while - bSecNameExist = PGCFALSE; - pcStart = pcEnd; - }// end while - - if (pcFileBuffer != NULL) - delete []pcFileBuffer; - - pgc32 iRev = -1; - - //创建哈希表 - iRev = profile_build_hash_index(psCacheEntry, iCurItem , &phash_table); - if (iRev != SUCCESS) - { - ReleaseProfileCache(hProfile); - return ERROR_CONF_CREATEHASH ; - } - - g_pHandleIndex[hProfile].pHashTable = phash_table; - g_pHandleIndex[hProfile].CurUseItems = iCurItem ; - g_pHandleIndex[hProfile].CurSecNum = iCurSec; - - fstream = g_pHandleIndex[hProfile].fstream; - g_pHandleIndex[hProfile].fstream = 0; - fclose(fstream); - //保存文件全路径名 + ReleaseProfileCache(hProfile); + return ERROR_MEMORY_ALLOC; + } + memset(psCacheTep, 0, (iCurItemCount + RESIZE_ITEM_NUM) * sizeof(SItemInCache)); + memmove(psCacheTep, psCacheEntry, sizeof(SItemInCache) * iCurItemCount); + free(psCacheEntry); + psCacheEntry = psCacheTep; + iCurItemCount = iCurItemCount + RESIZE_ITEM_NUM; + g_pHandleIndex[hProfile].CurItemCount = iCurItemCount; + g_pHandleIndex[hProfile].pCacheEntry = psCacheEntry; + }// end if + + pcStart = pcNextLine; + }// end while + bSecNameExist = PGCFALSE; + pcStart = pcEnd; + }// end while + + if (pcFileBuffer != NULL) + delete[]pcFileBuffer; + + pgc32 iRev = -1; + + //创建哈希表 + iRev = profile_build_hash_index(psCacheEntry, iCurItem, &phash_table); + if (iRev != SUCCESS) + { + ReleaseProfileCache(hProfile); + return ERROR_CONF_CREATEHASH; + } + + g_pHandleIndex[hProfile].pHashTable = phash_table; + g_pHandleIndex[hProfile].CurUseItems = iCurItem; + g_pHandleIndex[hProfile].CurSecNum = iCurSec; + + fstream = g_pHandleIndex[hProfile].fstream; + g_pHandleIndex[hProfile].fstream = 0; + fclose(fstream); + //保存文件全路径名 #ifdef OS_WINDOWS lstrcpyn((LPWSTR)g_pHandleIndex[hProfile].szPathName, (LPCWSTR)szProfileName, MAX_PATH_NUM); #else strncpy(g_pHandleIndex[hProfile].szPathName, szProfileName, MAX_PATH_NUM - 1); #endif - //记录下修改文件的时间 - get_file_lastMTime(szProfileName, &g_pHandleIndex[hProfile].tLastModify); + //记录下修改文件的时间 + get_file_lastMTime(szProfileName, &g_pHandleIndex[hProfile].tLastModify); - return SUCCESS; + return SUCCESS; } @@ -1334,7 +1334,7 @@ int create_profile_cache(pgcchar *szProfileName, int *phfile) HPROFILE hCurCacher = 0; if (szProfileName == NULL) - return ERROR_FOPEN ; + return ERROR_FOPEN; if (phfile == NULL) return ERROR_CONF_NULL_HANDEPTR; @@ -1345,7 +1345,7 @@ int create_profile_cache(pgcchar *szProfileName, int *phfile) memset(g_pHandleIndex, 0, MAX_PROFILEHANDLE_NUM * sizeof(SProfileHanleInfo)); g_bHandleInit = PGCTRUE; } - + //为当前缓冲寻找一个空闲句柄 for (int i = 1; i < MAX_PROFILEHANDLE_NUM; i++) if (g_pHandleIndex[i].pCacheEntry == NULL) @@ -1353,24 +1353,24 @@ int create_profile_cache(pgcchar *szProfileName, int *phfile) hCurCacher = i; break; } - - if ( hCurCacher < 1) - { - return ERROR_CONF_NOHANDLE; - } - //保存文件全路径名 + if (hCurCacher < 1) + { + return ERROR_CONF_NOHANDLE; + } + + //保存文件全路径名 #ifdef OS_WINDOWS - lstrcpyn((LPWSTR)g_pHandleIndex[hCurCacher].szPathName, (LPWSTR)szProfileName, MAX_PATH_NUM); + lstrcpyn((LPWSTR)g_pHandleIndex[hCurCacher].szPathName, (LPWSTR)szProfileName, MAX_PATH_NUM); #else - strncpy(g_pHandleIndex[hCurCacher].szPathName, szProfileName, MAX_PATH_NUM - 1); + strncpy(g_pHandleIndex[hCurCacher].szPathName, szProfileName, MAX_PATH_NUM - 1); #endif - pgc32 iExeRev = 0; - if ((iExeRev = ProcessCreateCache(hCurCacher)) != SUCCESS) - return iExeRev; - - *phfile = hCurCacher; - return SUCCESS; + pgc32 iExeRev = 0; + if ((iExeRev = ProcessCreateCache(hCurCacher)) != SUCCESS) + return iExeRev; + + *phfile = hCurCacher; + return SUCCESS; } @@ -1429,7 +1429,7 @@ int close_profile_handle(HPROFILE hProfile) // ERROR_CONF_INVALIDHANDLE 输入了非法的配置文件缓冲句柄 // ERROR_FAIL 函数执行失败,即未拷贝任何字符到目标缓冲区 - + // 参数说明 : lpAppName --[in]段名 // 参数说明 : lpKeyName --[in]项目名 // 参数说明 : lpDefault --[in] 当获取指定的项目值失败时,将该缓冲区的字符串写入lpReturnedString目标缓冲 @@ -1437,24 +1437,24 @@ int close_profile_handle(HPROFILE hProfile) // 参数说明 : pdwSize -- [in][out]指向缓冲区的长度的指针,同时在调用结束后,将取出的项目字符串的长度(包括'\0'字符)写入该地址 // 参数说明 : hProfile --[in] 配置文件缓冲句柄 int get_pgcprofile_string( - const pgcchar * lpAppName, // points to section name - const pgcchar * lpKeyName, // point to key name - const pgcchar * lpDefault, // point to default string - pgcchar * lpReturnedString, // point to destination buffer - pgcu32 * pdwSize, // point to size of destination buffer - HPROFILE hProfile // point to initialization filename - ) + const pgcchar * lpAppName, // points to section name + const pgcchar * lpKeyName, // point to key name + const pgcchar * lpDefault, // point to default string + pgcchar * lpReturnedString, // point to destination buffer + pgcu32 * pdwSize, // point to size of destination buffer + HPROFILE hProfile // point to initialization filename +) { SItemInCache *psTemp = NULL; TChainHash *phash_table = NULL; pgcu32 dwBuffer = 0, dwCount = 0, dwNeed = 0; //bRevBuffer - PGCTRUE表示接收缓冲区足够大 PGCBOOL bRevBuffer = PGCTRUE; - + if ((hProfile < 1) || (hProfile >= MAX_PROFILEHANDLE_NUM)) return ERROR_CONF_INVALIDHANDLE; - - if ((lpAppName == NULL) || ( lpKeyName == NULL)) + + if ((lpAppName == NULL) || (lpKeyName == NULL)) return ERROR_FAIL; ReCreateCache(hProfile); @@ -1462,7 +1462,7 @@ int get_pgcprofile_string( phash_table = g_pHandleIndex[hProfile].pHashTable; if (phash_table == NULL) return ERROR_CONF_INVALIDHANDLE; - + profile_search_sequence(&psTemp, phash_table, lpAppName, lpKeyName); if ((psTemp != NULL) && (psTemp->valid) && (pdwSize != NULL)) { @@ -1477,7 +1477,7 @@ int get_pgcprofile_string( { for (dwCount = 0; dwCount < dwBuffer - 1; dwCount++) lpReturnedString[dwCount] = psTemp->szItemVal[dwCount]; - + lpReturnedString[dwCount] = '\0'; } } @@ -1499,9 +1499,9 @@ int get_pgcprofile_string( #if 0 for (pgcu32 dwCount = 0; dwCount < dwBuffer - 1; dwCount++) #endif - for (dwCount = 0; dwCount < dwBuffer - 1; dwCount++) - //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - lpReturnedString[dwCount] = lpDefault[dwCount]; + for (dwCount = 0; dwCount < dwBuffer - 1; dwCount++) + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + lpReturnedString[dwCount] = lpDefault[dwCount]; lpReturnedString[dwCount] = '\0'; } @@ -1513,10 +1513,10 @@ int get_pgcprofile_string( if (dwBuffer == 0) return ERROR_FAIL; - - if (!bRevBuffer) + + if (!bRevBuffer) return ERROR_CONF_REVBUFFER; - + return SUCCESS; } @@ -1541,42 +1541,42 @@ int get_pgcprofile_string( // ERROR_CONF_INVALIDHANDLE 输入了非法的配置文件缓冲句柄 // ERROR_CONF_INPUTSTRING_TOOLARGE 输入的段名,项目名,项目值的字符串长度超长 // ERROR_FAIL 函数执行失败,即未将任何字符写到配置文件缓冲区以及文件 - + // 参数说明 : lpAppName[in] -- 段名 // 参数说明 : lpKeyName[in] -- 项目名 // 参数说明 : lpString[in] -- 要写入的字符串 // 参数说明 : hProfile[in] -- 配置文件缓冲句柄 // 参数说明 : bInsertItem[in] -- 当指定的段名或项目名不存在时,是否添加该段或项目;PGCTRUE-添加;PGCFALSE-不添加(缺省) int write_pgcprofile_string( - const pgcchar * lpAppName, // point to section name - const pgcchar * lpKeyName, // point to key name - const pgcchar * lpString, // point to string to add - HPROFILE hProfile, // handle to Profile Cache - PGCBOOL bInsertItem - ) + const pgcchar * lpAppName, // point to section name + const pgcchar * lpKeyName, // point to key name + const pgcchar * lpString, // point to string to add + HPROFILE hProfile, // handle to Profile Cache + PGCBOOL bInsertItem +) { SItemInCache *psTemp = NULL, *psCacheEntry = NULL, *psSec = NULL; TChainHash *phash_table = NULL; pgc32 iBuffer = 0, iCurItem = 0, iSecPos = 0, i = 0, iWriteSuccess = 0; PGCBOOL bSecExist = PGCFALSE; - PGCBOOL bTrunc = PGCFALSE ;/*写入的字符串是否被截断*/ - + PGCBOOL bTrunc = PGCFALSE;/*写入的字符串是否被截断*/ + //输入句柄合法性检查 if ((hProfile < 1) || (hProfile >= MAX_PROFILEHANDLE_NUM)) return ERROR_CONF_INVALIDHANDLE; - + //输入段名及项目名合法性检查 - if ((lpAppName == NULL) || ( lpKeyName == NULL)) + if ((lpAppName == NULL) || (lpKeyName == NULL)) return ERROR_FAIL; //判断输入字符串是否超长 - if (strlen(lpAppName) >= MAX_CHAR_LINE) + if (strlen(lpAppName) >= MAX_CHAR_LINE) bTrunc = PGCTRUE; - if (strlen(lpKeyName) >= MAX_CHAR_LINE) + if (strlen(lpKeyName) >= MAX_CHAR_LINE) bTrunc = PGCTRUE; - if ( (lpString != NULL) && (strlen(lpString) >= MAX_CHAR_LINE)) + if ((lpString != NULL) && (strlen(lpString) >= MAX_CHAR_LINE)) bTrunc = PGCTRUE; - if (bTrunc) + if (bTrunc) return ERROR_CONF_INPUTSTRING_TOOLARGE; //如果配置文件被其它进程修改,则重构缓冲 @@ -1586,10 +1586,10 @@ int write_pgcprofile_string( phash_table = g_pHandleIndex[hProfile].pHashTable; if (phash_table == NULL) return ERROR_CONF_INVALIDHANDLE; - + psCacheEntry = g_pHandleIndex[hProfile].pCacheEntry; - - psTemp = NULL; + + psTemp = NULL; profile_search_sequence(&psTemp, phash_table, lpAppName, lpKeyName); if ((psTemp == NULL) && (bInsertItem)) { @@ -1607,31 +1607,31 @@ int write_pgcprofile_string( //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ a2 = (pgcu32)sizeof(SItemInCache); - iSecPos = a1 / a2; + iSecPos = a1 / a2; bSecExist = PGCTRUE; psSec->valid = PGCTRUE; } - + //重构缓冲区大小 - AdjustCache(hProfile, 2); + AdjustCache(hProfile, 2); psCacheEntry = g_pHandleIndex[hProfile].pCacheEntry; iCurItem = g_pHandleIndex[hProfile].CurUseItems; - + //应该插入新段名吗? if (!bSecExist) { psTemp = &psCacheEntry[iCurItem]; #ifdef OS_WINDOWS - lstrcpyn((LPWSTR)psTemp->szSecName, (LPCWSTR)lpAppName, MAX_CHAR_LINE); + lstrcpyn((LPWSTR)psTemp->szSecName, (LPCWSTR)lpAppName, MAX_CHAR_LINE); #else memset(psTemp->szSecName, 0, MAX_CHAR_LINE); strncpy(psTemp->szSecName, lpAppName, MAX_CHAR_LINE - 1); - + #endif psTemp->valid = PGCTRUE; psTemp->bIsSecName = PGCTRUE; psTemp->memo = NULL; - + phash_table->insertItem(psTemp); AddNode(g_pHandleIndex[hProfile].pSequence, g_pHandleIndex[hProfile].pCurSeq, iCurItem); iSecPos = iCurItem; @@ -1640,36 +1640,36 @@ int write_pgcprofile_string( } //插入新项目名及项目值 psTemp = &psCacheEntry[iCurItem]; - + #ifdef OS_WINDOWS lstrcpyn((LPWSTR)psTemp->szSecName, (LPCWSTR)lpAppName, MAX_CHAR_LINE); lstrcpyn((LPWSTR)psTemp->szItemName, (LPCWSTR)lpKeyName, MAX_CHAR_LINE); lstrcpyn((LPWSTR)psTemp->szItemVal, (LPCWSTR)lpString, MAX_CHAR_LINE); #else - memset(psTemp, 0, sizeof(SItemInCache)); + memset(psTemp, 0, sizeof(SItemInCache)); strncpy(psTemp->szSecName, lpAppName, MAX_CHAR_LINE - 1); strncpy(psTemp->szItemName, lpKeyName, MAX_CHAR_LINE - 1); strncpy(psTemp->szItemVal, lpString, MAX_CHAR_LINE - 1); #endif - + psTemp->valid = PGCTRUE; psTemp->bIsSecName = PGCFALSE; psTemp->memo = NULL; - - + + phash_table->insertItem(psTemp); InsertNode(g_pHandleIndex[hProfile].pSequence, iCurItem, psCacheEntry, iSecPos); iCurItem++; - + g_pHandleIndex[hProfile].CurUseItems = iCurItem; g_pHandleIndex[hProfile].CurLines++; - + iBuffer = (pgc32)strlen(psTemp->szItemVal); psTemp = NULL; if ((iWriteSuccess = ReWriteProfile(hProfile)) != SUCCESS) return iWriteSuccess; } - + if ((psTemp == NULL) && (!bInsertItem)) return ERROR_CONF_NOTEXIST; @@ -1684,7 +1684,7 @@ int write_pgcprofile_string( for (i = 0; i < iBuffer; i++) psTemp->szItemVal[i] = lpString[i]; psTemp->szItemVal[i] = '\0'; - + psTemp->valid = PGCTRUE; if ((iWriteSuccess = ReWriteProfile(hProfile)) != SUCCESS) return iWriteSuccess; @@ -1706,17 +1706,17 @@ int write_pgcprofile_string( // 参数说明 : pgcu32 *pdwSize -- [in][out]写入缓冲区的按字节长度 // 参数说明 : pgcu32 *nSecNum --[out]将获取的段总数写入该地址 // 参数说明 : hProfile-- [in] handle to Profile Cache -int get_pgcprofile_secnames( pgcchar * lpszReturnBuffer, // address of return buffer - pgcu32 * pdwSize, // size of return buffer - pgcu32 *nSecNum, - HPROFILE hProfile // handle to Profile Cache - ) +int get_pgcprofile_secnames(pgcchar * lpszReturnBuffer, // address of return buffer + pgcu32 * pdwSize, // size of return buffer + pgcu32 *nSecNum, + HPROFILE hProfile // handle to Profile Cache +) { - pgc32 iCount = g_pHandleIndex[hProfile].CurUseItems , iSecNum = 0, i = -1; + pgc32 iCount = g_pHandleIndex[hProfile].CurUseItems, iSecNum = 0, i = -1; pgcu32 dwWrite = 0, dwSize = 0, dwNeed = 0; SItemInCache *psCacheEntry = g_pHandleIndex[hProfile].pCacheEntry; pgcchar *szSec = NULL; - + if ((hProfile < 1) || (hProfile >= MAX_PROFILEHANDLE_NUM)) return ERROR_CONF_INVALIDHANDLE; @@ -1724,7 +1724,7 @@ int get_pgcprofile_secnames( pgcchar * lpszReturnBuffer, // address of return if ((pdwSize != NULL) && (*pdwSize > 2)) dwSize = *pdwSize - 2; - + if ((dwSize > 2) && (lpszReturnBuffer != NULL)) { for (i = 0; i < iCount; i++) @@ -1734,13 +1734,13 @@ int get_pgcprofile_secnames( pgcchar * lpszReturnBuffer, // address of return szSec = psCacheEntry[i].szSecName; dwNeed = dwNeed + (pgcu32)strlen(szSec) + 1; iSecNum++; - while ((dwWrite < dwSize ) && (*szSec != '\0')) + while ((dwWrite < dwSize) && (*szSec != '\0')) { *lpszReturnBuffer = *szSec; dwWrite++; lpszReturnBuffer++; szSec++; - } + } *lpszReturnBuffer = '\0'; lpszReturnBuffer++; dwWrite++; @@ -1776,24 +1776,24 @@ int get_pgcprofile_secnames( pgcchar * lpszReturnBuffer, // address of return // 函数名 : del_pgcprofile_key // 函数说明 : 删除指定的项目 // 函数逻辑 :在缓冲中删除指定的项目,以及该项目上方的注释,然后以覆盖方式重写文件。 - + // 返回值说明 : int -- 成功则返回SUCCESS,失败则返回具体的执行失败的代码 // 参数说明 : lpAppName --[in] 段名 // 参数说明 : lpKeyName -- [in]项目名 // 参数说明 : HPROFILE hProfile --[in]配置文件的缓冲句柄 -int del_pgcprofile_key( const pgcchar * lpAppName, // point to section name - const pgcchar * lpKeyName, // point to key name - HPROFILE hProfile // point to initialization filename - ) +int del_pgcprofile_key(const pgcchar * lpAppName, // point to section name + const pgcchar * lpKeyName, // point to key name + HPROFILE hProfile // point to initialization filename +) { SItemInCache *psTemp = NULL; TChainHash *phash_table = NULL; pgc32 iWriteSuccess = 0; - + if ((hProfile < 1) || (hProfile >= MAX_PROFILEHANDLE_NUM)) return ERROR_CONF_INVALIDHANDLE; - - if ((lpAppName == NULL) || ( lpKeyName == NULL)) + + if ((lpAppName == NULL) || (lpKeyName == NULL)) return ERROR_FAIL; ReCreateCache(hProfile); @@ -1801,7 +1801,7 @@ int del_pgcprofile_key( const pgcchar * lpAppName, // point to section name phash_table = g_pHandleIndex[hProfile].pHashTable; if (phash_table == NULL) return ERROR_CONF_INVALIDHANDLE; - + profile_search_sequence(&psTemp, phash_table, lpAppName, lpKeyName); if (psTemp != NULL) { @@ -1810,7 +1810,7 @@ int del_pgcprofile_key( const pgcchar * lpAppName, // point to section name return iWriteSuccess; return SUCCESS; } - return ERROR_CONF_NOTEXIST; + return ERROR_CONF_NOTEXIST; } @@ -1820,21 +1820,21 @@ int del_pgcprofile_key( const pgcchar * lpAppName, // point to section name // 函数说明 : 删除指定的段 // 函数逻辑 :在缓冲中删除指定的段,以及该段上方的注释,然后以覆盖方式重写文件; // 在删除一个段的同时,该段下属的所有项目(包括项目上方的注释)将被删除 - + // 返回值说明 : int -- 成功则返回SUCCESS,失败则返回具体的执行失败的代码 // 参数说明 : lpAppName --[in] 段名 // 参数说明 : HPROFILE hProfile --[in]配置文件的缓冲句柄 -int del_pgcprofile_sec( const pgcchar * lpAppName, - HPROFILE hProfile - ) +int del_pgcprofile_sec(const pgcchar * lpAppName, + HPROFILE hProfile +) { SItemInCache *psSec = NULL, *psCacheEntry = NULL; TChainHash *phash_table = NULL; pgc32 iSecPos = 0; - + if ((hProfile < 1) || (hProfile >= MAX_PROFILEHANDLE_NUM)) return ERROR_CONF_INVALIDHANDLE; - + if (lpAppName == NULL) return ERROR_FAIL; @@ -1847,7 +1847,7 @@ int del_pgcprofile_sec( const pgcchar * lpAppName, phash_table = g_pHandleIndex[hProfile].pHashTable; if (phash_table == NULL) return ERROR_CONF_INVALIDHANDLE; - + profile_search_sequence(&psSec, phash_table, lpAppName, NULL); if (psSec != NULL) { @@ -1861,12 +1861,12 @@ int del_pgcprofile_sec( const pgcchar * lpAppName, //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ a2 = (pgcu32)sizeof(SItemInCache); - iSecPos = a1 / a2; + iSecPos = a1 / a2; if (DisableSec(g_pHandleIndex[hProfile].pSequence, iSecPos, psCacheEntry)) { return ReWriteProfile(hProfile); } } - return ERROR_CONF_NOTEXIST; + return ERROR_CONF_NOTEXIST; }