在项目中经常会用到一些配置文件,在Windows下其后缀是.ini。例如:端口配置.ini
配置文件由节、键、值组成。
节
[section]
键=值
name=value
下面主要用C来实现在下获取配置文件中键的值:
如配置文件为sysconfig,在Linux下一般配置文件放在对应的/etc目录下
//sysconfig文件信息
-
[Config1]
-
PORT=8080
-
#HOSTIP=192.168.1.1
-
HOSTIP=192.168.1.2
-
SENDIP=192.168.1.3
-
-
-
-
[Config2]
-
PORT=5050
-
#HOSTIP=192.168.1.1
-
HOSTIP=192.122.16.19
-
SENDIP=192.122.16.19
-
-
[Config3]
-
PORT=1666
-
#HOSTIP=192.168.1.1
-
HOSTIP=192.168.12.2
-
SENDIP=192.168.12.3
//config.h
-
-
-
-
-
-
-
-
-
#ifndef _CONFIG_H
-
#define _CONFIG_H
-
-
#ifdef __cplusplus
-
extern "C" {
-
#endif
-
-
#define SUCCESS 0x00 /*成功*/
-
#define FAILURE 0x01 /*失败*/
-
-
#define FILENAME_NOTEXIST 0x02 /*配置文件名不存在*/
-
#define SECTIONNAME_NOTEXIST 0x03 /*节名不存在*/
-
#define KEYNAME_NOTEXIST 0x04 /*键名不存在*/
-
#define STRING_LENNOTEQUAL 0x05 /*两个字符串长度不同*/
-
#define STRING_NOTEQUAL 0x06 /*两个字符串内容不相同*/
-
#define STRING_EQUAL 0x00 /*两个字符串内容相同*/
-
-
-
int CompareString(char *pInStr1,char *pInStr2);
-
int GetKeyValue(FILE *fpConfig,char *pInKeyName,char *pOutKeyValue);
-
int GetConfigIntValue(char *pInFileName,char *pInSectionName,char *pInKeyName,int *pOutKeyValue);
-
int GetConfigStringValue(char *pInFileName,char *pInSectionName,char *pInKeyName,char *pOutKeyValue);
-
-
#ifdef __cplusplus
-
}
-
#endif
-
-
#endif
//config.c
-
-
-
-
-
-
-
-
-
#include
-
#include
-
#include
-
#include "config.h"
-
-
int GetConfigStringValue(char *pInFileName,char *pInSectionName,char *pInKeyName,char *pOutKeyValue)
-
{
-
FILE *fpConfig;
-
char szBuffer[150];
-
char *pStr1,*pStr2;
-
int iRetCode = 0;
-
-
-
-
-
-
-
-
-
memset(szBuffer,0,sizeof(szBuffer));
-
if( NULL==( fpConfig=fopen(pInFileName,"r") ) )
-
return FILENAME_NOTEXIST;
-
-
while( !feof(fpConfig) )
-
{
-
if( NULL==fgets(szBuffer,150,fpConfig) )
-
break;
-
pStr1 = szBuffer ;
-
while( (' '==*pStr1) || ('\t'==*pStr1) )
-
pStr1++;
-
if( '['==*pStr1 )
-
{
-
pStr1++;
-
while( (' '==*pStr1) || ('\t'==*pStr1) )
-
pStr1++;
-
pStr2 = pStr1;
-
while( (']'!=*pStr1) && ('\0'!=*pStr1) )
-
pStr1++;
-
if( '\0'==*pStr1)
-
continue;
-
while( ' '==*(pStr1-1) )
-
pStr1--;
-
*pStr1 = '\0';
-
-
iRetCode = CompareString(pStr2,pInSectionName);
-
if( !iRetCode )
-
{
-
iRetCode = GetKeyValue(fpConfig,pInKeyName,pOutKeyValue);
-
fclose(fpConfig);
-
return iRetCode;
-
}
-
}
-
}
-
-
fclose(fpConfig);
-
return SECTIONNAME_NOTEXIST;
-
-
}
-
-
-
int CompareString(char *pInStr1,char *pInStr2)
-
{
-
if( strlen(pInStr1)!=strlen(pInStr2) )
-
{
-
return STRING_LENNOTEQUAL;
-
}
-
-
-
while( *pInStr1==*pInStr2 )
-
{
-
if( '\0'==*pInStr1 )
-
break;
-
pInStr1++;
-
pInStr2++;
-
}
-
-
if( '\0'==*pInStr1 )
-
return STRING_EQUAL;
-
-
return STRING_NOTEQUAL;
-
-
}
-
-
int GetKeyValue(FILE *fpConfig,char *pInKeyName,char *pOutKeyValue)
-
{
-
char szBuffer[150];
-
char *pStr1,*pStr2,*pStr3;
-
unsigned int uiLen;
-
int iRetCode = 0;
-
-
memset(szBuffer,0,sizeof(szBuffer));
-
while( !feof(fpConfig) )
-
{
-
if( NULL==fgets(szBuffer,150,fpConfig) )
-
break;
-
pStr1 = szBuffer;
-
while( (' '==*pStr1) || ('\t'==*pStr1) )
-
pStr1++;
-
if( '#'==*pStr1 )
-
continue;
-
if( ('/'==*pStr1)&&('/'==*(pStr1+1)) )
-
continue;
-
if( ('\0'==*pStr1)||(0x0d==*pStr1)||(0x0a==*pStr1) )
-
continue;
-
if( '['==*pStr1 )
-
{
-
pStr2 = pStr1;
-
while( (']'!=*pStr1)&&('\0'!=*pStr1) )
-
pStr1++;
-
if( ']'==*pStr1 )
-
break;
-
pStr1 = pStr2;
-
}
-
pStr2 = pStr1;
-
while( ('='!=*pStr1)&&('\0'!=*pStr1) )
-
pStr1++;
-
if( '\0'==*pStr1 )
-
continue;
-
pStr3 = pStr1+1;
-
if( pStr2==pStr1 )
-
continue;
-
*pStr1 = '\0';
-
pStr1--;
-
while( (' '==*pStr1)||('\t'==*pStr1) )
-
{
-
*pStr1 = '\0';
-
pStr1--;
-
}
-
-
iRetCode = CompareString(pStr2,pInKeyName);
-
if( !iRetCode )
-
{
-
pStr1 = pStr3;
-
while( (' '==*pStr1)||('\t'==*pStr1) )
-
pStr1++;
-
pStr3 = pStr1;
-
while( ('\0'!=*pStr1)&&(0x0d!=*pStr1)&&(0x0a!=*pStr1) )
-
{
-
if( ('/'==*pStr1)&&('/'==*(pStr1+1)) )
-
break;
-
pStr1++;
-
}
-
*pStr1 = '\0';
-
uiLen = strlen(pStr3);
-
memcpy(pOutKeyValue,pStr3,uiLen);
-
*(pOutKeyValue+uiLen) = '\0';
-
return SUCCESS;
-
}
-
}
-
-
return KEYNAME_NOTEXIST;
-
}
-
-
int GetConfigIntValue(char *pInFileName,char *pInSectionName,char *pInKeyName,int *pOutKeyValue)
-
{
-
int iRetCode = 0;
-
char szKeyValue[16],*pStr;
-
-
memset(szKeyValue,0,sizeof(szKeyValue));
-
iRetCode = GetConfigStringValue(pInFileName,pInSectionName,pInKeyName,szKeyValue);
-
if( iRetCode )
-
return iRetCode;
-
pStr = szKeyValue;
-
while( (' '==*pStr)||('\t'==*pStr))
-
pStr++;
-
if( ('0'==*pStr)&&( ('x'==*(pStr+1))||('X'==*(pStr+1)) ) )
-
sscanf(pStr+2,"%x",pOutKeyValue);
-
else
-
sscanf(pStr,"%d",pOutKeyValue);
-
-
return SUCCESS;
-
-
}
//实现功能test.c
-
-
-
-
-
-
-
-
-
#include
-
#include
-
#include
-
#include "config.h"
-
-
int main (int argc,char *argv[])
-
{
-
char szFileName[100];
-
char szSectionName[100];
-
char szKeyName[100];
-
char szKeyValue[100];
-
int iRetCode = 0;
-
int iPort = -1;
-
char szHostIp[30];
-
-
memset(szFileName,0,sizeof(szFileName));
-
sprintf(szFileName,"%s/etc/sysconfig",getenv("HOME"));
-
memset(szSectionName,0,sizeof(szSectionName));
-
memcpy(szSectionName,argv[1],sizeof(argv[1]));
-
memset(szKeyName,0,sizeof(szKeyName));
-
memcpy(szKeyName,argv[2],sizeof(argv[2]));
-
memset(szKeyValue,0,sizeof(szKeyValue));
-
memset(szHostIp,0,sizeof(szHostIp));
-
-
iRetCode = GetConfigStringValue(szFileName,argv[1],argv[2],szHostIp);
-
if( iRetCode )
-
{
-
printf("iRetCode : %d !\n",iRetCode );
-
}
-
else
-
{
-
printf("HOSTIP: %s\n",szHostIp);
-
}
-
-
iRetCode = GetConfigIntValue(szFileName,"Config1","PORT",&iPort);
-
if( iRetCode )
-
{
-
printf("iRetCode : %d !\n",iRetCode );
-
}
-
else
-
{
-
printf("PORT: %d\n",iPort);
-
}
-
-
return 0;
-
}
写一个makefile文件
//makefile
-
#makefile开始
-
all:Manager
-
-
#定义宏
-
OBJS = test.o config.o
-
-
#which compiler
-
CC = gcc
-
-
#where are include files kept
-
INCLUDE = .
-
-
#Options -O for release and -g for development
-
#-Wall:输出所有的警告信息
-
#-O:在编译时进行优化
-
#-g:表示编译debug版本
-
CFLAGS = -g -Wall -ansi
-
#CFLAGS = -O -Wall -ansi
-
-
#前面加@不回显执行的命令在标准输出上
-
Manager:$(OBJS)
-
@$(CC) -o Manager $(OBJS)
-
#gcc test.o config.o -o Manager
-
test.o:test.c config.h
-
@$(CC) -I$(INCLUDE) $(CFLAGS) -c test.c -o test.o
-
config.o:config.c config.h
-
@$(CC) -I$(INCLUDE) $(CFLAGS) -c config.c -o config.o
-
clean :
-
@rm -rf *.o
-
#makefile结束
测试运行结果:
[Sunrier@localhost Sunrier]$ make
[Sunrier@localhost Sunrier]$ ./Manager Config1 HOSTIP
HOSTIP: 192.168.1.2
PORT: 8080
[Sunrier@localhost Sunrier]$
阅读(2786) | 评论(0) | 转发(0) |