| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- // Profile.cpp: implementation of the Profile class.
- //
- //////////////////////////////////////////////////////////////////////
- #include<stdio.h>
- #include<string.h>
- #include "Profile.h"
- #include<ctype.h>
- //////////////////////////////////////////////////////////////////////
- // 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 <TargetConditionals.h>
- #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<strlen(lpDefault) ? nSize-1 : strlen(lpDefault);
- memcpy(lpReturnedString, lpDefault, dwSize);
- delete []pBuf;
- return dwSize;
- }
- pLeftSeciton = strstr(pKeySection, "=");
- pRightSection = strstr(pKeySection, "\n");
- dwSize = nSize<(pRightSection-pLeftSeciton-1) ? nSize-1 : (pRightSection-pLeftSeciton-1);
- memcpy(lpReturnedString, pLeftSeciton+1, dwSize);
- delete []pBuf;
- return dwSize;
- }
|