在linux中/etc目录下有许多类似于后缀.conf文件的配置文件,在这些文件中有对应的key和value
我们在项目中有时候要用到这些值。需要根据相应的key得到对应的value
所以我们要解析这些配置文件,但是众所周知,linux底下的配置文件的格式几乎都不一样
大多数的格式是“key=value”,也有“key:value”,"key value"等等
如有其他格式,请读者自行修改。
本文不讨论section ,如有需要请读者自行修改;
话不多说,上代码
test.conf(用于测试的配置文件)
********************************************代码分隔符***************************************
#for check notes
[Section1]
key1=value1
key1.0="asfasdfgasdfaaaaaaaaaaaaaaaaaaaasadfasdasdgabjkabsuydfyueyqriwetbnkadsfkmsdbfiwoaini"
[Section1]
key1=value1
key1.0="asfasdfgasdfaaaaaaaaaaaaaaaaaaaasadfasdasdgabjkabsuydfyueyqriwetbnkadsfkmsdbfiwoaini"
#[check section]
[Section2]
key2.0 = value2.0
key2.1 = value2.1
key2.2 = value2.2
#for check key=check value
[Section3]
key3.0 value3.0
key3.1 value3.1
key3.2 value3.2
//for test
[Section4]
key4: value4
key1: value4
********************************************代码分隔符********************************************
config.h
********************************************代码分隔符********************************************
#ifndef _CONFIG_H
#define _CONFIG_H
#ifdef __cplusplus
#define _CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
int CompareString(char *str1,char *str2);
int GetKeyValue(FILE *Config,char *KeyName,char *KeyValue);
#ifdef __cplusplus
}
#endif
#endif
#endif
********************************************代码分隔符********************************************
config.c
********************************************代码分隔符********************************************
#include
#include
#include
#include "config.h"
#include
#include
#include "config.h"
int CompareString(char *str1,char *str2)
{
if( strlen(str1)!=strlen(str2) )
{
return -1;
}
{
if( strlen(str1)!=strlen(str2) )
{
return -1;
}
while( *str1==*str2 )
{
if( '\0'==*str1 )
{
break;
}
str1++;
str2++;
}
{
if( '\0'==*str1 )
{
break;
}
str1++;
str2++;
}
if( '\0'==*str1 )
{
return 1;
}
return -1;
}
{
return 1;
}
return -1;
}
int GetKeyValue(FILE *Config,char *KeyName,char *KeyValue)
{
char Buff[100];
char *pStr1,*pStr2,*pStr3;
unsigned int Len;
int temp;
{
char Buff[100];
char *pStr1,*pStr2,*pStr3;
unsigned int Len;
int temp;
memset(Buff,0,sizeof(Buff));
while( !feof(Config) )
{
if( NULL==fgets(Buff,100,Config) )
{
break;
}
pStr1 = Buff;
while( (' '==*pStr1) || ('\t'==*pStr1) )
{
pStr1++;
}
if( '#'==*pStr1 )
{
continue;
}
if( ('/'==*pStr1)&&('/'==*(pStr1+1)) )
{
continue;
}
if( ('\0'==*pStr1)||(0x0d==*pStr1)||(0x0a==*pStr1) )
{
continue;
}
if( '['==*pStr1 )
{
continue;
}
{
break;
}
pStr1 = Buff;
while( (' '==*pStr1) || ('\t'==*pStr1) )
{
pStr1++;
}
if( '#'==*pStr1 )
{
continue;
}
if( ('/'==*pStr1)&&('/'==*(pStr1+1)) )
{
continue;
}
if( ('\0'==*pStr1)||(0x0d==*pStr1)||(0x0a==*pStr1) )
{
continue;
}
if( '['==*pStr1 )
{
continue;
}
pStr2 = pStr1;
while( ('='!=*pStr1)&&('\0'!=*pStr1)&&(' '!=*pStr1)&&(':'!=*pStr1) )
{
pStr1++;
}
if( '\0'==*pStr1 )
{
continue;
}
while( ('='!=*pStr1)&&('\0'!=*pStr1)&&(' '!=*pStr1)&&(':'!=*pStr1) )
{
pStr1++;
}
if( '\0'==*pStr1 )
{
continue;
}
pStr3 = pStr1+1;
if( pStr2==pStr1 )
{
continue;
}
*pStr1 = '\0';
pStr1--;
if( pStr2==pStr1 )
{
continue;
}
*pStr1 = '\0';
pStr1--;
while( (' '==*pStr1)||('\t'==*pStr1) )
{
*pStr1 = '\0';
pStr1--;
}
{
*pStr1 = '\0';
pStr1--;
}
temp = CompareString(pStr2,KeyName);
if( temp == 1 )
{
pStr1 = pStr3;
while( (' '==*pStr1)||('\t'==*pStr1) )
{
pStr1++;
}
while( ('='==*pStr1)||(' '==*pStr1) )
{
pStr1++;
}
pStr3 = pStr1;
{
pStr1 = pStr3;
while( (' '==*pStr1)||('\t'==*pStr1) )
{
pStr1++;
}
while( ('='==*pStr1)||(' '==*pStr1) )
{
pStr1++;
}
pStr3 = pStr1;
while( ('\0'!=*pStr1)&&(0x0d!=*pStr1)&&(0x0a!=*pStr1) )
{
if( ('/'==*pStr1)&&('/'==*(pStr1+1)) )
{
break;
}
pStr1++;
}
*pStr1 = '\0';
{
if( ('/'==*pStr1)&&('/'==*(pStr1+1)) )
{
break;
}
pStr1++;
}
*pStr1 = '\0';
Len = strlen(pStr3);
memcpy(KeyValue,pStr3,Len);
*(KeyValue+Len) = '\0';
return 1;
}
}
return -1;
memcpy(KeyValue,pStr3,Len);
*(KeyValue+Len) = '\0';
return 1;
}
}
return -1;
}
********************************************代码分隔符********************************************
main.c(用于测试)
********************************************代码分隔符********************************************
#include
#include
#include
#include "config.h"
#include
#include
#include "config.h"
int main (int argc,char *argv[])
{
char KeyName[100];
char KeyValue[100];
int temp;
FILE *config;
config = fopen(argv[1],"r");
if(config == NULL)
{
printf("Config is not exist!\n");
{
char KeyName[100];
char KeyValue[100];
int temp;
FILE *config;
config = fopen(argv[1],"r");
if(config == NULL)
{
printf("Config is not exist!\n");
return 0;
}
}
memset(KeyName,0,sizeof(KeyName));
memcpy(KeyName,argv[2],strlen(argv[2]+1));
memset(KeyValue,0,sizeof(KeyValue));
temp = GetKeyValue(config,argv[2],KeyValue);
if( temp == 1 )
{
printf("%s\n",KeyValue);
}
else
{
printf("KeyName is not exist\n");/*键名不存在
}
return 0;
{
printf("%s\n",KeyValue);
}
else
{
printf("KeyName is not exist\n");/*键名不存在
}
return 0;
}
********************************************代码分隔符********************************************
Makefile(用于编译运行的makefile文件)
********************************************代码分隔符********************************************
#makefile开始
#makefile开始
all:config
#定义宏
OBJS = main.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
#前面加@不回显执行的命令在标准输出上
config:$(OBJS)
@$(CC) -o config $(OBJS)
#gcc test.o config.o -o config
main.o:main.c config.h
@$(CC) -I$(INCLUDE) $(CFLAGS) -c main.c -o main.o
config.o:config.c config.h
@$(CC) -I$(INCLUDE) $(CFLAGS) -c config.c -o config.o
clean :
@rm -rf *.o
@rm -rf config
@rm -rf config
#makefile结束
********************************************代码分隔符********************************************
运行程序可得:
代码如果有BUG,欢迎提出,共同进步。