Linux下獲取配置文件信息

 

在項目中經常會用到一些配置文件,在Windows下其後綴是.ini。例如:端口配置.ini
配置文件由節、鍵、值組成。


[section]
鍵=值
name=value

下面主要用C來實現在Linux下獲取配置文件中鍵的值:

如配置文件爲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

/*************************************************************     
    FileName : config.h 
    FileFunc : 定義頭文件    
    Version  : V0.1     
    Author   : Sunrier     
    Date     : 2012-05-09 
    Descp    : Linux下獲取配置文件信息     
*************************************************************/
#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

/*************************************************************     
    FileName : config.c 
    FileFunc : 定義實現文件    
    Version  : V0.1     
    Author   : Sunrier     
    Date     : 2012-05-09 
    Descp    : Linux下獲取配置文件信息    
*************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"

int GetConfigStringValue(char *pInFileName,char *pInSectionName,char *pInKeyName,char *pOutKeyValue)
{
	FILE *fpConfig;
	char szBuffer[150];
	char *pStr1,*pStr2;
	int iRetCode = 0;
	
	/*test*/
	/*
	printf("pInFileName: %s !\n",pInFileName);
	printf("pInSectionName: %s !\n",pInSectionName);
	printf("pInKeyName: %s !\n",pInKeyName);
	*/
	
	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( toupper(*pInStr1)==toupper(*pInStr2) )*//*#include <ctype.h>*/
	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

/*************************************************************     
    FileName : test.c 
    FileFunc : 定義測試功能文件    
    Version  : V0.1     
    Author   : Sunrier     
    Date     : 2012-05-09 
    Descp    : Linux下獲取配置文件信息     
*************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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]$

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章