Linuxでプロファイル情報を取得
プロジェクトでは、Windowsで接尾辞を付けるプロファイルがよく使用されます.ini.例えば、ポート構成.iniプロファイルは、セクション、キー、値から構成されます.
セクション[section]キー=値name=value
以下では、主にLinuxでプロファイル内のキーの値を取得するために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
/*************************************************************
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 !
",pInFileName);
printf("pInSectionName: %s !
",pInSectionName);
printf("pInKeyName: %s !
",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 !
",iRetCode );
}
else
{
printf("HOSTIP: %s
",szHostIp);
}
iRetCode = GetConfigIntValue(szFileName,"Config1","PORT",&iPort);
if( iRetCode )
{
printf("iRetCode : %d !
",iRetCode );
}
else
{
printf("PORT: %d
",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]$