配置文件解析函数(C语言)
来源:互联网 发布:日本直播软件排名 编辑:程序博客网 时间:2024/05/01 01:08
配置文件解析函数(C语言)
// config.h
struct conf_info
{
const char *name;
void *object;
};
typedef struct conf_info Cconf_info;
/*
* the function of removing the free space.
*/
extern void trim(char *);
extern struct conf_info *lookup_keyword(char *);
extern void apply_command(Cconf_info *, char *);
extern void parse(FILE *);
以下是调用例子:
// config.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
char *server_root;
char *db_addr;
char *db_user;
char *db_passwd;
struct conf_info clist[] = {
{"serverName", &server_root},
{"dbAddr", &db_addr},
{"dbUser", &db_user},
{"dbPasswd", &db_passwd},
};
void trim(char *s)
{
char *c = s + strlen(s) - 1;
while (isspace(*c) && c > s) {
*c = '\0';
--c;
}
if(c == s) //此处
*c = '\0';
}
struct conf_info *lookup_keyword(char *c)
{
struct conf_info *p;
for (p = clist; p < clist + (sizeof (clist) / sizeof (struct conf_info)); p++)
{
if (strcasecmp(c, p->name) == 0)
return p;
}
return NULL;
}
static void apply_command(Cconf_info * p, char *args)
{
if (p->object) {
if (*(char **) p->object != NULL)
free(*(char **) p->object);
*(char **) p->object = strdup(args);
}
}
/* parse configure file */
void parse(FILE * fp)
{
Cconf_info *p;
char buf[1024], *c;
int line = 0;
while (fgets(buf, 1024, fp) != NULL)
{
++line;
if (buf[0] == '\0' || buf[0] == '#' || buf[0] == '\n')
{
memset(buf, 0, 1024);
continue;
}
trim(buf);
if (buf[0] == '\0')
continue;
c = buf;
while (!isspace(*c))
++c;
if (*c == '\0') {
c = NULL;
}
else {
*c = '\0';
++c;
}
p = lookup_keyword(buf);
if(p != NULL)
apply_command(p, c);
memset(buf, 0, 1024);
}
}
//main.c
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
extern char *server_root;
extern char *db_addr;
extern char *db_user;
extern char *db_passwd;
int main(void)
{
FILE *fp;
if ((fp = fopen("test.conf", "r")) == NULL)
{
fprintf(stderr, "Can't open conf file %s .\n", "test.conf");
exit(1);
}
parse(fp);
printf("ServerRoot is : %s\n", server_root);
printf("DataBase address is : %s\n", db_addr);
printf("DataBase user is : %s\n", db_user);
printf("DataBase user password is : %s\n", db_passwd);
return 0;
}
此是Makefie文件:
LIBS= -lm
CFLAGS= -Wall -O2 -g
OBJS= main.o config.o
config: $(OBJS)
gcc -o $@ $(OBJS) $(LIBS)
clean:
rm -f config $(OBJS)
下面是test.conf配置文件:
#######配置文件test.conf######
#
serverName 10.10.206.30
dbAddr 10.10.206.32
dbUser root
dbPasswd 123456
####
##
值得注意的是:
1、windows下的回车/换行,\r\n 两个分开的,在Linux下,\n作用可以等同于\r\n,
2、windows下Enter键 是\r\n 而Linux下Enter键 是\n
- 配置文件解析函数(C语言)
- 配置文件解析函数(C语言)
- C语言--配置文件解析
- LUA解析配置文件-C语言调用
- 在C语言中解析json配置文件
- 在C语言中解析JSON配置文件
- C语言中配置文件解析案例
- C语言字符串函数解析
- C语言时间函数解析
- C语言中变参函数解析
- C语言realloc函数解析
- 【c基础练习】c语言实现配置文件解析
- 用C语言实现解析简单配置文件的小工具
- C语言面向对象编程(六):配置文件解析
- C语言配置文件解析库——iniparser
- C语言ini形式配置文件解析库——iniparser
- C语言配置文件解析库——iniparser
- C语言面向对象编程之六:配置文件解析
- FreeMaker FTL Tag
- onNewIntent()用法
- 适用于云环境的 JSF 2.0,第 2 部分
- 如何养狗完整版 !-为了迎接我的大宝做好准备
- USACO The Tamworth Two
- 配置文件解析函数(C语言)
- 董的博客
- 人性的弱点全集的评论
- asp.net mvc SelectList 的selected 失效及解决方法
- spring加载hibernate映射文件的几种方式
- 单例模式以及双检锁DCL
- 以前项目中的js写法2
- 修改Win7菜单栏无法隐藏
- 读《企业应用架构模式》8 在一起