while(fgets(_buf,size,file) != NULL && (count < nSize - 2))
{
if (_buf[0] != '[') continue;
for ( int i = 1;
count < nSize - 2 &&
_buf[i] != ']' &&
_buf[i] != '\r' &&
_buf[i] != '\n';
i++)
{
*lpret = _buf[i];
lpret++;
count++;
}
*lpret = '\0';
lpret++;
count++;
}
*lpret = '\0';
if (count > 0) count--;
fclose(file);
file = NULL;
return ret = count;
}// end of lpAppName == ""
if (lpKeyName == "")
{
char srcbuf[256];
char desbuf[256];
int size = 256;
char* _srcbuf = srcbuf;
char* _desbuf = desbuf;
char target = 'S'; //'S' find section name,'K' find key name
char* lpret = lpReturnedString;
DWORD count = 0;
while(fgets(_srcbuf,size,file) != NULL && (count < nSize - 2))
{
BOOL flag = TRUE;
if (target == 'S')
{
if (_srcbuf[0] != '[') continue;
strcpy(_desbuf,lpAppName);
int len = strlen(_desbuf);
int i;
for (i = 0;i < len;i++)
{
if (_desbuf[i] != _srcbuf[i + 1])
{
flag = FALSE;
break;
}
}
if (flag == FALSE) continue;
if (_srcbuf[i + 1] != ']') continue;
target = 'K';
}
else
{
if (_srcbuf[0] == '[') break;
for ( int i = 0;
count < nSize - 2 &&
_srcbuf[i] != ' ' &&
_srcbuf[i] != '=' &&
_srcbuf[i] != '\r' &&
_srcbuf[i] != '\n';
i++)
{
*lpret = _srcbuf[i];
lpret++;
count++;
}
*lpret = '\0';
lpret++;
count++;
}// end of if
}// end of while
*lpret = '\0';
if (count > 0) count--;
fclose(file);
file = NULL;
return ret = count;
}// end of lpKeyName == ""
char srcbuf[256];
char desbuf[256];
int size = 256;
char* _srcbuf = srcbuf;
char* _desbuf = desbuf;
char target = 'S'; //'S' find section name,'K' find key name
while(fgets(_srcbuf,size,file) != NULL)
{
BOOL flag = TRUE;
if (target == 'S')
{
if (_srcbuf[0] != '[') continue;
strcpy(_desbuf,lpAppName);
int len = strlen(_desbuf);
int i;
for (i = 0;i < len;i++)
{
if (_desbuf[i] != _srcbuf[i + 1])
{
flag = FALSE;
break;
}
}
if (flag == FALSE) continue;
if (_srcbuf[i + 1] != ']') continue;
target = 'K';
}
else
{
if (_srcbuf[0] == '[') break;
strcpy(_desbuf,lpKeyName);
int len = strlen(_desbuf);
int i;
for (i = 0;i < len;i++)
{
if (_desbuf[i] != _srcbuf[i])
{
flag = FALSE;
break;
}
}
if (flag == FALSE) continue;
BOOL _flag = FALSE;
while (_srcbuf[i] != '\0')
{
if (_srcbuf[i] == ' ')
{
i++;
continue;
}
if (_srcbuf[i] == '=')
{
_flag = TRUE;
i++;
continue;
}
if (_flag == FALSE) break;
char* lpresult = &_srcbuf[i];
char* lpret = lpReturnedString;
int count = 0;
//过滤换车和换行符
while ( *lpresult != '\r' &&
*lpresult != '\n' &&
*lpresult != '\0'
)
{
*lpret = *lpresult;
lpret++;
lpresult++;
count++;
}
*lpret = '\0';
fclose(file);
file = NULL;
return ret = count;
}// end of while
}// end of if
}// end of while
fclose(file);
file = NULL;
return ret;
}//end of try
catch(...)
{
if (file != NULL)
{
fclose(file);
file = NULL;
}
ret = strlen(lpDefault);
if (ret < nSize)
strcpy(lpReturnedString,lpDefault);
else
{
int i;
for (i = 0;i < nSize - 1;i++)
lpReturnedString[i] = lpDefault[i];
lpReturnedString[i] = '\0';
ret = nSize - 1;
}
return ret;
}
#ifndef VC
#ifndef _BCB
typedef unsigned int UINT;
typedef int INT;
typedef const char* LPCTSTR;
typedef char* LPCSTR;
typedef unsigned long DWORD;
#endif
char chrupr(const char chr) //char to upper
{
char ret = chr;
if (ret >= 'a' && ret <= 'z') ret -= 32;
return ret;
}
UINT GetPrivateProfileInt(
LPCTSTR lpAppName, // address of section name
LPCTSTR lpKeyName, // address of key name
INT nDefault, // return value if key name is not found
LPCTSTR lpFileName // address of initialization filename
)
{
enum BOOL { FALSE,TRUE };
UINT ret = nDefault;
FILE* file = NULL;
try
{
file = fopen(lpFileName,"r");
if (file == NULL)
return ret;
char srcbuf[256];
char desbuf[256];
int size = 256;
char* _srcbuf = srcbuf;
char* _desbuf = desbuf;
char target = 'S'; //'S' find section name,'K' find key name
while(fgets(_srcbuf,size,file) != NULL)
{
BOOL flag = TRUE;
if (target == 'S')
{
if (_srcbuf[0] != '[') continue;
strcpy(_desbuf,lpAppName);
int len = strlen(_desbuf);
int i;
for (i = 0;i < len;i++)
{
if (_desbuf[i] != _srcbuf[i + 1])
{
flag = FALSE;
break;
}
}
if (flag == FALSE) continue;
if (_srcbuf[i + 1] != ']') continue;
target = 'K';
}
else
{
if (_srcbuf[0] == '[') break;
strcpy(_desbuf,lpKeyName);
int len = strlen(_desbuf);
int i;
for (i = 0;i < len;i++)
{
if (_desbuf[i] != _srcbuf[i])
{
flag = FALSE;
break;
}
}
if (flag == FALSE) continue;
BOOL _flag = FALSE;
while (_srcbuf[i] != '\0')
{
if (_srcbuf[i] == ' ')
{
i++;
continue;
}
if (_srcbuf[i] == '=')
{
_flag = TRUE;
i++;
continue;
}
if (_flag == FALSE) break;
char* lpresult = &_srcbuf[i];
if (lpresult == "0")
{
fclose(file);
file = NULL;
return 0;
}
UINT value = strtoul(lpresult,NULL,0);
if (value != 0) ret = value;
fclose(file);
file = NULL;
return ret;
}
}//end of if
}// end of while
fclose(file);
file = NULL;
return ret;
}// end of try
catch (...)
{
if (file != NULL)
{
fclose(file);
file = NULL;
}
return ret;
}
}//end of GetPrivateProfileInt