/*C语言读取配置文件*/
/*调用示例*/
#include "read_conf.h"
int main()
{
//initialization work
char demo[max_value];
memset(demo, 0, sizeof(demo));
get_key_value("SECTION_1", "KEY_1", demo, "./config.ini");
//or call the function like this:
get_key_value("SECTION_1", "KEY_1", demo);
return TRUE;
}
/*filename:read_conf.h*/
#ifndef_READ_CONF_H_
#define_READ_CONF_H_
/************************************************************************/
/*
*name: read_conf.h
*creat date: 2010-12-20
*ver: v1.0author: sarry
*email: shiluo.110@163.com
*使用本代码,需包含三个文件到你的工程中:
*read_conf.h, read_conf.c, config.ini.
*thx2冬眠的猪
*/
/************************************************************************/
#include
#include
//External Interface
unsigned int get_key_value(const char* section, const char* key, char* value, const char* file_name = "config.ini");
//Internal Interface
unsigned int get_section(FILE* fp, const char* section_name, char* section);
unsigned int get_value(char* section, const char* key, char* value);
#define TRUE1
#define FALSE0
#define max_section4096
#define max_key100
#define max_value100
//#define DEBUG_CONF
#endif
/*filename:read_conf.c*/
#include "read_conf.h"
/************************************************************************/
/*
*对外接口主函数
*section_name:字段名;key:键名;value:存储键值的数组;file_name:配置文件路径及名称
*成功返回TRUE,失败返回FALSE。
*/
/************************************************************************/
unsigned int get_key_value(const char* section_name, const char* key, char* value, const char* file_name)
{
char section[max_section];
memset(section, 0, max_section);
//open config file
FILE* fp = NULL;
if(NULL == (fp = fopen(file_name, "r"))){
printf("ERROR: open %s failed!/n", file_name);
return FALSE;
}
//get section content
if (FALSE == get_section(fp, section_name, section)){
printf("ERROR: can't get content of [%s]!/n", section_name);
return FALSE;
}
#ifdef DEBUG_CONF
printf("%s/n", section);
#endif
//get key value
if (FALSE == get_value(section, key, value)){
printf("ERROR: can't get value of key:[%s]!/n", key);
return FALSE;
}
return TRUE;
}
/************************************************************************/
/*
*将fp所指的文件中字段section_name下的内容复制到section指向的数组中。
*成功返回TRUE,失败返回FALSE。
*/
/************************************************************************/
unsigned int get_section(FILE* fp, const char* section_name, char* section)
{
if (!fp || !section_name || !section || !strlen(section_name)){
return FALSE;
}
int cnt_sn_app = 0;//count of section name appeared
int f_can_copy = 0;
char* cur_copyed = section;
char array_need_comp[20];
memset(array_need_comp, 0, sizeof(array_need_comp));
sprintf(array_need_comp, "[%s]", section_name);
#define MAX_READ100
char buf_read_fp[MAX_READ];
while(1){
memset(buf_read_fp, 0, sizeof(buf_read_fp));
if (NULL == fgets(buf_read_fp, MAX_READ, fp)){
break;//文件末尾
}
if ((cnt_sn_app == 1) && (buf_read_fp[0] == '[')){
f_can_copy = 0;
}
if (f_can_copy){
char* cpy = buf_read_fp;
while ((*cpy != '/0') &&
(*cpy != '#') &&
(*cpy != '#') &&
(*cpy != '/n') &&
(cpy < buf_read_fp + sizeof(buf_read_fp))){
*cur_copyed++ = *cpy++;
}
}
if ((buf_read_fp[0] == '[') && strstr(buf_read_fp, array_need_comp)){
f_can_copy = 1;
cnt_sn_app++;
//break;//如不需检测字段的重复性,则取消该mask
}
if (cnt_sn_app > 1){
printf("Error: 字段%s出现了%d次!", section_name, cnt_sn_app);
memset(section, 0, cur_copyed - section);
return FALSE;
}
}
return TRUE;
}
/************************************************************************/
/*
*从section中获取key的值并复制到value指向的位置
*成功返回TRUE,失败返回FALSE。
*/
/************************************************************************/
unsigned int get_value(char* section, const char* key, char* value)
{
if (!section || !key || !value || !strlen(key)){
return FALSE;
}
unsigned int cnt = 0;
unsigned int f_copy = 0;//1,start copy; 0,stop copy.
char* pos = strstr(section, key);
if (NULL == pos){
return FALSE;
}
pos += strlen(key);
#define start_copy(c)((c != '/t') && (c != '/n') && (c != ' ') && (c != '=') )
#define stop_copy(c)((c == ';') || (c == '/n'))
while (1){
if (start_copy(*pos)){
f_copy = 1;
}
if (stop_copy(*pos)){
f_copy = 0;
break;
}
if (f_copy){
value[cnt++] = *pos;
}
pos++;
}
return TRUE;
}
/*config.ini*/
#This the demo of read config file
#Config file contains three format: comment(#comment), section([SECTION]), and text(key = value;).
#You can use space or tab to keep your format pretty... Enjoy~:)
[TCP]
LOCAL_IP=10.212.29.75;#You can comment a row like this.
LOCAL_PORT=1233;
SERVER_IP=10.212.21.125;
#SERVER_IP=127.0.0.1;
SERVER_PORT=9999;
[TCP]#If you have two same section name like this, error will occur.
A1 =5102;
A2 =01654;