Linux下解析配置文件的通用程序_沉浸在在代码世界的小小程序猿_新浪博客

在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"

#[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
extern "C" {
#endif
int CompareString(char *str1,char *str2);
int GetKeyValue(FILE *Config,char *KeyName,char *KeyValue);
#ifdef __cplusplus
}
#endif
 
#endif
********************************************代码分隔符********************************************



config.c
********************************************代码分隔符********************************************
#include
#include
#include
#include "config.h"
int CompareString(char *str1,char *str2)
{
 if( strlen(str1)!=strlen(str2) )
 {
  return -1;
 } 
 while( *str1==*str2 )
 {
  if( '\0'==*str1 )
  {
   break; 
  }
  str1++;
  str2++;
 }
 if( '\0'==*str1 )
 {
  return 1;
 }
 return -1; 
}
int GetKeyValue(FILE *Config,char *KeyName,char *KeyValue)
{
 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;
  } 
  pStr2 = pStr1;
  while( ('='!=*pStr1)&&('\0'!=*pStr1)&&(' '!=*pStr1)&&(':'!=*pStr1) )
  {
   pStr1++;
  }
  if( '\0'==*pStr1 ) 
  {
   continue;
  }
  pStr3 = pStr1+1;
  if( pStr2==pStr1 )
  {
   continue; 
  }
  *pStr1 = '\0';
  pStr1--;
  while( (' '==*pStr1)||('\t'==*pStr1) )
  {
   *pStr1 = '\0';
   pStr1--;
  }
  temp = CompareString(pStr2,KeyName);
  if( temp == 1 )
  {
   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';
   Len = strlen(pStr3);
   memcpy(KeyValue,pStr3,Len);
   *(KeyValue+Len) = '\0';
   
    return 1;
  }
 } 
 return -1;
}
********************************************代码分隔符********************************************



main.c(用于测试)
********************************************代码分隔符********************************************
#include
#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");
  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;
}
********************************************代码分隔符********************************************



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
#makefile结束
********************************************代码分隔符********************************************



运行程序可得:
Linux下解析配置文件的通用程序


代码如果有BUG,欢迎提出,共同进步。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值