// Profile.cpp: implementation of the Profile class. // ////////////////////////////////////////////////////////////////////// #include #include #include "Profile.h" #include ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// char* _strlwr(char *string) { for (char *p = string; p < string + strlen(string); p++) { if (isupper(*p)) { *p = tolower(*p); } } return string; } CProfile::CProfile() { } CProfile::~CProfile() { } /* *return value:-3:open file failed;-2:parameter error;-1:system error;other:return character string length */ DWORD CProfile::GetPrivateProfileString( const char* lpAppName, // section name const char* lpKeyName, // key name const char* lpDefault, // default string char* lpReturnedString, // destination buffer DWORD nSize, // size of destination buffer const char* lpFileName // initialization file name ) { if(NULL == lpReturnedString || nSize <= 0) { return -2; } memset(lpReturnedString, 0, nSize); fpos_t pos = {0}; char* pBuf = NULL; FILE* pFile = fopen(lpFileName, "r"); if(NULL == pFile) { return -3; } fseek(pFile, 0, SEEK_END); if( fgetpos(pFile, &pos) != 0) { fclose(pFile); return -1; } int npos = 0; #if (defined(WIN32)||defined(WIN64)) npos = pos; #else #if defined(__APPLE__) && defined(__MACH__) #include #if TARGET_OS_MAC == 1 npos = pos; #endif #else npos = pos.__pos; #endif #endif if(npos <= 0) { fclose(pFile); return -1; } pBuf = new char[npos+1]; if(NULL == pBuf) { return -1; } memset(pBuf, 0, npos+1); fseek(pFile, 0, SEEK_SET); int nRead = fread(pBuf, sizeof(char), (int)npos, pFile); fclose(pFile); _strlwr(pBuf); char szAppName[128] = {0}; char szKeyName[128] = {0}; strncpy(szAppName, lpAppName, sizeof(szAppName)-1); strncpy(szKeyName, lpKeyName, sizeof(szKeyName)-1); _strlwr(szAppName); _strlwr(szKeyName); char* pLeftSeciton = NULL; char* pRightSection = NULL; char* pKeySection = NULL; DWORD dwSize = 0; pLeftSeciton = strstr(pBuf, szAppName); if(pLeftSeciton != NULL) { pRightSection = strstr(pLeftSeciton, "["); if(pRightSection == NULL) { pRightSection = pBuf + nRead; } pKeySection = strstr(pLeftSeciton, szKeyName); } if(NULL == pLeftSeciton || NULL == pKeySection || pKeySection < pLeftSeciton || pKeySection >pRightSection)//关键字不在范围之内 { dwSize = nSize