📄 readini.c
字号:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "readini.h"
char _pStrpara1[501][31]; // record the item name in option file
char _pStrpara2[501][31]; // record the item value in option file
int _ParaNum; // the number of parameters in option file
//******************************************************************************************
// Function : READINI_ParseFile
// Description : Parse a option file
// Arguments : filename : option filename
// Return : 1:Success ; 0:Failure
// Side Effect :
//******************************************************************************************
BOOL READINI_ParseFile(char* filename)
{
int ret=0;
FILE *fp;
char string[101];
char strtmp1[21], strtmp2[21];
char strspace[21];
if((fp=fopen(filename,"r"))==NULL)
{
printf("The file : %s doesn't exist !!\n");
exit(0);
}
else
{
_ParaNum=0;
while(fgets(string,100,fp) != NULL) { // scan a line
if(strcmp(string,"\n")==0)
continue;
sscanf(string,"%s",strtmp1); // parse the first field
if(strncmp(strtmp1,"//",2)==0 || strncmp(strtmp1,";",1)==0) // destory the line if mark by "//"
continue;
else if(strncmp(strtmp1,"[",1)==0) { // read version definition
strcpy(_pStrpara1[_ParaNum],strtmp1);
strcpy(_pStrpara2[_ParaNum],"NULL");
_ParaNum++;
}
else { // read item definition
sscanf(string,"%s %s",strtmp1,strtmp2); // parse the first two fields
if(strcmp(strtmp2,"=")==0) // second item is "="
sscanf(string,"%s %s %s",strtmp1,strspace,strtmp2); // parse the first three field
strcpy(_pStrpara1[_ParaNum],strtmp1);
strcpy(_pStrpara2[_ParaNum],strtmp2);
_ParaNum++;
}
}
ret=1;
}
fclose(fp);
return ret;
}
//***************************************************************************
// Function : READINI_GoVersion
// Description : Get the desired version position
// Arguments : section:the desired version ; pos: return position
// Return : 1:Success ; 0:Failure
// Side Effect :
//***************************************************************************
BOOL READINI_GoSection(char* section, int* pos)
{
int i;
int ret=0;
for(i=0;i<_ParaNum;i++)
{
if(strcmpi(section,_pStrpara1[i])==0)
{
*pos = i;
ret=1;
break;
}
}
return ret;
}
//**************************************************************************
// Function : READINI_GetNext
// Description : Get the next item from the current item
// Arguments : pos:current postion ; item:item name ; val:item value
// Return : 1:Success ; 0:Failure
// Side Effect :
//**************************************************************************
BOOL READINI_GetNext(int* pos,char* item, char* val)
{
int ret=0;
(*pos)++;
if((*pos)<_ParaNum && strncmp(_pStrpara1[*pos],"[",1)!=0) {
strcpy(item,_pStrpara1[*pos]);
strcpy(val,_pStrpara2[*pos]);
ret=1;
}
return ret;
}
//***************************************************************************
// Function : READINI_GetEntryInt
// Description : Get a integer value of a parameter from the option file
// Arguments : section:desired section ; item:desired item ; val:value
// Return : 1:Success ; 0:Failure
// Side Effect :
//***************************************************************************
BOOL READINI_GetEntryInt(char* section, char* item, DWORD* val)
{
int pos;
int ret=0;
if(READINI_GoSection(section,&pos))
{
pos++;
while((strncmp(_pStrpara1[pos],"[",1)!=0) && (pos<_ParaNum))
{
if(strcmpi(_pStrpara1[pos],item)==0)
{
if(strncmp(_pStrpara2[pos],"0x",2)!=0)
*val=atol(_pStrpara2[pos]);
else
{
*val=READINI_Hex2Dec(_pStrpara2[pos]);
}
ret=1;
break;
}
else
{
pos++;
continue;
}
}
}
return ret;
}
//******************************************************************************
// Function : READINI_GetEntryStr
// Description : Get a string value of a parameter from the option file
// Arguments : section:desired section ; item:desired item ; val:value
// Return : 1:Success ; 0:Failure
// Side Effect :
//******************************************************************************
BOOL READINI_GetEntryStr(char* section, char* item, char* val)
{
int pos;
int ret=0;
if(READINI_GoSection(section,&pos))
{
pos++;
while((strncmp(_pStrpara1[pos],"[",1)!=0) && (pos<_ParaNum))
{
if(strcmpi(_pStrpara1[pos],item)==0)
{
strcpy(val,_pStrpara2[pos]);
ret=1;
break;
}
else
{
pos++;
continue;
}
}
}
return ret;
}
//******************************************************************************
// Function : READINI_Hex2Dec
// Description : Converse a Hex value to an decimal value
// Arguments : hex:a string of the hex value
// Return : DWORD:the decimal value
// Side Effect :
//******************************************************************************
DWORD READINI_Hex2Dec(char* hex)
{
int len; // the length of valid hex value
DWORD dwVal; // the integer value
int i;
len=strlen(hex)-2;
dwVal=0;
for(i=1;i<=len;i++) {
if(isalpha(hex[1+i])) // the character is alpha
{
if(islower(hex[1+i])) // the character is lower
dwVal=dwVal+(hex[1+i]-97+10)*(DWORD)pow(16,(len-i)); // 97 is ASCII of 'a'
else // the character is upper
dwVal=dwVal+(hex[1+i]-65+10)*(DWORD)pow(16,(len-i)); // 65 is ASCII of 'A'
}
else // the character is digit
dwVal=dwVal+(hex[1+i]-48)*(DWORD)pow(16,(len-i)); // 48 is the ASCII of "0"
}
return dwVal;
}
//***************************************************************************
// Function : READINI_PrintPara
// Description : Print out the parameter and value in option file
// Arguments : none
// Return : none
// Side Effect :
//***************************************************************************
void READINI_PrintPara(void)
{
int i;
printf("***** The parameters and values in option file are :\n");
for(i=0;i<_ParaNum;i++) {
if(strncmp(_pStrpara1[i],"[",1)==0)
printf("%s\n",_pStrpara1[i]);
else
printf("%s\t%s\n",_pStrpara1[i],_pStrpara2[i]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -