/*
无论是在桌面机或者嵌入式应用里,用配置文件来保存不同参数是很常见的应用。
如果用C语言来处理配置文件,是一个很实际的应用。
主要使用函数指针,大量用到字符串解析,
可以通过定义不同static CONF_ITEM conf_list[]来实现读不同配置参数
*/
//----------------------------------------------------------------------------
//
// FILE NAME VERSION
// read_conf.c 1.0
//
// DESCRIPTION
// Generally get some variable settings from configure file
/*
操作配置文件的代码,每一行不超过1024字符。
1.有注释,以#打头,行首空格要忽略
2.一个参数占一行,配置项格式
变量名 = 变量值
变量名= (也合法. =两边有无空格不影响结果,值可以为空格)
3.写程序的人一定知道他要读取哪一些变量,以及变量的含义和格式
4.没有出现变量自动采用默认值
5.变量前后顺序不影响最终结果。即配置变量可以调整顺序.
#this is a comment
set = 1
ipaddr = 192.168.0.1
host=developer.com
扩展:1.增加段支持 [network]
2.变量归属于某一个段的
3.没有属于某段的变量自动变成全局
无限分段的设计
[com1]
baudrate=115200
stopbit = 1
...
[com2]
baudrate=9600
stopbit=2
...
...
*/
// DEPENDENCIES
// stdio.h
// stdlib.h
// string.h
//
// HISTORY:
// 2013.3.15 Arthur Zhu Initial begin
//----------------------------------------------------------------------------
#include "read_conf.h"
//----------------------------------------------------------------------------
// Function name : find_conf_item
// Description : To find the variable to be configured, or return NULL
// check the configure item struct list whether is end with NULL pointer
// DEPENDENCIES : string
// Return type : CONF_ITEM *
// Argument : CONF_ITEM * p_conf_list: configure item struct list
// char * conf_name: variable name
//----------------------------------------------------------------------------
#ifdef __CONF_FIELD_SUPPORT__
CONF_ITEM * find_conf_item(CONF_ITEM * p_conf_list, char * conf_name, char * conf_field)
#else
CONF_ITEM * find_conf_item(CONF_ITEM * p_conf_list, char * conf_name)
#endif
{
CONF_ITEM * p_find = NULL;
for(p_find = p_conf_list; p_find->name != NULL; p_find++)
{
if ( (0 == strcmp(conf_name, p_find->name))
#ifdef __CONF_FIELD_SUPPORT__
&& (0 == strcmp(conf_field, p_find->field))
#endif
)
return p_find;
else;
}
return NULL; // 不支持的变量名
}
#ifdef __CONF_DEBUG__
//----------------------------------------------------------------------------
// Function name : mgs_parse_conf_line
// Description : output message to std device
// Return type : int
// Argument : int type : error type of parse_conf_line
// int line_no: line number
//----------------------------------------------------------------------------
int mgs_parse_conf_line(int type, int line_no)
{
//解释配置文件行;
switch(type)
{
default:
case CONF_LINE_UNKNOW:
printf("line %d: unknow\n", line_no);
break;
case CONF_LINE_EMPTY:
printf("line %d: empty line\n", line_no);
break;
case CONF_LINE_COMMENT:
printf("line %d: comment\n", line_no);
break;
case CONF_LINE_FIELD:
printf("line %d: field change\n", line_no);
break;
case CONF_LINE_CONFIGURE:
printf("line %d: configured\n", line_no);
break;
case CONF_LINE_UNSUPPORT:
printf("line %d: unsupport\n", line_no);
break;
}
return (type);
}
//----------------------------------------------------------------------------
// Function name : mgs_conf_list
// Description : output settings to std device
// Return type : int
// Argument : CONF_ITEM * p_conf_list : variable pointer
//----------------------------------------------------------------------------
int mgs_conf_list(CONF_ITEM * p_conf_list)
{
CONF_ITEM * p_item = NULL;
for(p_item = p_conf_list; NULL != p_item->name; p_item++)
{
if ( NULL != p_item->value )
{
#ifdef __CONF_FIELD_SUPPORT__
printf("%s : %s = %s\n", p_item->field, p_item->name, p_item->value);
#else
printf("%s = %s\n", p_item->name, p_item->value);
#endif
}
else
#ifdef __CONF_FIELD_SUPPORT__
printf("%s : %s = %s\n", p_item->field, p_item->name, p_item->val_default);
#else
printf("%s = %s\n", p_item->name, p_item->val_default);
#endif
}
return 0;
}
#endif // __CONF_DEBUG__
//----------------------------------------------------------------------------
// Function name : parse_conf_line
// Description : To parse one line of configure file, include 3 parts:
// skip comment, variable parse, configure value paese
// DEPENDENCIES : stdlib, string, find_conf_item
// It must be compared with function "free_conf_value" to
// free the memory allocated here
// Return type : int : error code
// CONF_LINE_CONFIGURE // configure ok
// CONF_LINE_DEFAULT // use default configure
// CONF_LINE_COMMENT // comment line
// CONF_LINE_EMPTY // empty line
// CONF_LINE_UNSUPPORT // variable no supported
// CONF_LINE_UNKNOW // line unknow, no '='
// Argument : CONF_ITEM * p_conf_list: configure item struct list
// char * line_str: line char pointer
//----------------------------------------------------------------------------
#ifdef __CONF_FIELD_SUPPORT__
int parse_conf_line(CONF_ITEM * p_conf_list, char * line_str, char * conf_field)
#else
int parse_conf_line(CONF_ITEM * p_conf_list, char * line_str)
#endif
{
CONF_ITEM * p_conf_item;
char * line_pos;
char * conf_name;
char * conf_value;
#ifdef __CONF_FIELD_SUPPORT__
char conf_field_line = 0;
#endif
// skip comment
if ( NULL != (line_pos = strchr(line_str, COMMENT_CHARACTER)) )
*line_pos = 0;
else;
// skip the front whitespace(include ' ', '\t', '\n')
while ( isspace(*line_str) )
line_str++;
if ( '\0' == *line_str )
return CONF_LINE_EMPTY;
else;
#ifdef __CONF_FIELD_SUPPORT__
if ( (SEG_BEGIN_CHARACTER == *line_str ) &&
(NULL != (conf_name = strchr(line_str, SEG_END_CHARACTER))) )
{
*conf_name = '\0'; // 去除段尾
conf_field_line = 1;
line_str++;
// skip the front whitespace(include ' ', '\t', '\n')
while ( isspace(*line_str) )
line_str++;
conf_name = line_str; // 获得段字符串首指针
}
else
#endif
conf_name = line_str; // 获得名字符串首指针
// 查找=, 并将其置为0, 这样简单折分出变量名和变量值来
while ( !( ('\n' == *line_str) || ('\0' == *line_str) ||
(ASSIGN_CHARACTER == *line_str) ) )
{
line_str++;
*line_str = tolower(*line_str); // 顺便把变量名变成小写
}
#ifdef __CONF_FIELD_SUPPORT__
if ( !conf_field_line && (ASSIGN_CHARACTER != *line_str) )// 表示这一行不是[],即不是段行
#else
if ( ASSIGN_CHARACTER != *line_str ) // 表示这一行没有 =
#endif
return CONF_LINE_UNKNOW;
else;
line_pos = line_str + 1; // --临时保存当前下个字符指针
do // 截掉变量名后面空字符(' ', '\t', '\n')
{
line_str--;
} while ( isspace(*line_str) && (conf_name < line_str));
*(line_str + 1) = '\0';
#ifdef __CONF_FIELD_SUPPORT__
if (conf_field_line) // field configure
{
if ('\0' == *conf_name)
C语言实现读取配置文件程序
4星 · 超过85%的资源 需积分: 32 6 浏览量
2013-09-05
12:36:50
上传
评论 6
收藏 6KB RAR 举报
Arthurzss
- 粉丝: 1
- 资源: 2
最新资源
- 机器学习利用SKlearn中决策树和线性回归模型的身高数据集
- LinkedListS.py
- cesiumlab3-3.0.7安装包
- vsftpd 无依赖安装包
- 老照片修复教程(带资料),再也不用去照相馆修复了!-教程网盘链接提取码下载 .txt
- AI认知课+AI工具实战课,理论结合实操一门课全搞定-视频课程资源网盘链接提取码下载 .txt
- IP切片带货5.0,图文玩法,制作更加简单,流量更大,日入1000+-教程网盘链接提取码下载 .txt
- 2服务注册中心Nacos应用实践
- 2024年3月份GESP1~8级编程题题干+数据
- STM32单片机FPGA毕设电路原理论文报告一种建立单片机应用系统菜单的新方法
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈