设为首页 加入收藏

TOP

纯C语言INI文件解析(一)
2014-11-23 20:25:07 来源: 作者: 【 】 浏览:8
Tags:语言 INI 文件 解析

在一个跨平台( Android 、Windows、Linux )项目中配置文件用 INI 格式,自己写了个解析库,纯C语言的,简单好用。

可以解析 INI 格式的字符串、解析文件、保存到文件。

下面是头文件:

#ifndef INI_PARSER_H
#define INI_PARSER_H
#ifdef __cplusplus
extern "C" {
#endif
struct tag_value_list;

struct ini_parser {
    struct tag_value_list * keyvalues;
    int (*parse_file)(struct ini_parser *, const char * file);
    int (*parse_string)(struct ini_parser *, const char *text);
    char * (*value)(struct ini_parser *, const char * key);
    void (*set_value)(struct ini_parser *, const char * key, const char * value);
    void (*remove)(struct ini_parser *, const char *key);
    int (*save_to_file)(struct ini_parser *, const char * file);
};

struct ini_parser * new_ini_parser();
void delete_ini_parser(struct ini_parser *);

#ifdef __cplusplus
}
#endif
#endif // INI_PARSER_H

下面是源文件:

#include "ini_parser.h"
#include 
  
   
#include 
   
     #include "tag_value.h" static struct tag_value_pair * parse_line(char *line, int len) { struct tag_value_pair * pair = 0; int count = 0; char * p = line; char * end = 0; char * start = line; if(!p) return 0; while(*p == ' ') p++; /*blank line*/ if(p - line == len || *p == '\r' || *p == '\n' || *p == '\0') return 0; /*do not support group*/ if(*p == '[') return 0; /*comments*/ if(*p == '#') return 0; /* extract key */ start = p; end = line + len; while(*p != '=' && p!= end) p++; if(p == end) { /* none '=' , invalid line */ return 0; } end = p - 1; while(*end == ' ') end--; /* skip blank at the end */ count = end - start + 1; pair = new_tag_value_pair(); pair->szTag = malloc(count + 1); strncpy(pair->szTag, start, count); pair->szTag[count] = 0; /* extract value */ p++; end = line + len; /* next pos of the last char */ while( *p == ' ' && p != end) p++; if(p == end) { delete_tag_value_pair(pair); return 0; } start = p; end--; /* to the last char */ if(*end == '\n') { *end = 0; end--; } if(*end == '\r') { *end = 0; end--; } count = end - start + 1; if(count > 0) { pair->szValue = malloc(count + 1); strncpy(pair->szValue, start, count); pair->szValue[count] = 0; } /* release empty key-value pair */ if(!pair->szValue) { delete_tag_value_pair(pair); return 0; } return pair; } static int _parse_file(struct ini_parser * ini, const char *file){ FILE * fp = fopen(file, "r"); if(fp) { struct tag_value_pair * pair = 0; char buf[1024] = {0}; while(fgets(buf, 1024, fp)) { pair = parse_line(buf, strlen(buf)); if(pair) { ini->keyvalues->add(ini->keyvalues, pair); } } fclose(fp); return ini->keyvalues->size; } return -1; } static int _parse_text(struct ini_parser * ini, const char * text){ char *p = text; char * start = 0; struct tag_value_pair * pair = 0; if(!text) return -1; while(1) { start = p; while(*p != '\n' && *p != '\0' )p++; if(*p == '\0') break; pair = parse_line(start, p - start); if(pair) ini->keyvalues->add(ini->keyvalues, pair); p++; } return ini->keyvalues->size; } static char * _value(struct ini_parser * ini, const char * key){ struct tag_value_pair * pair = ini->keyvalues->find_by_tag(ini->keyvalues, key); if(pair) return pair->szValue; return 0; } static void _set_value(struct ini_parser * ini, const char * key, const char *value){ struct tag_value_pair * pair = ini->keyvalues->find_by_tag(ini->keyvalues, key); if(pair) { if(pair->szValue) free(pair->szValue); pair->szValue = strdup(value); } else { ini->keyvalues->add(ini->keyvalues, make_tag_value_pair(key, value)); } } static void _remove(struct ini_parser * ini, const char * key){ struct tag_value_pair * pair = ini->keyvalues->find_by_tag(ini->keyvalues, key); if(pair)ini->keyv
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇纯C语言:检索与周游广度遍历源码 下一篇纯C语言:检索与周游广度深度遍历..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: