引用别人的博文: http://www.open-open.com/lib/view/open1402278076447.html

可以解析 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 <stdio.h>
#include <string.h>
#include "tag_value.h" static struct tag_value_pair * parse_line(char *line, int len)
{
struct tag_value_pair * pair = ;
int count = ;
char * p = line;
char * end = ;
char * start = line;
if(!p) return ;
while(*p == ' ') p++; /*blank line*/
if(p - line == len ||
*p == '\r' ||
*p == '\n' ||
*p == '\0') return ; /*do not support group*/
if(*p == '[') return ;
/*comments*/
if(*p == '#') return ; /* extract key */
start = p;
end = line + len;
while(*p != '=' && p!= end) p++;
if(p == end)
{
/* none '=' , invalid line */
return ;
}
end = p - ;
while(*end == ' ') end--; /* skip blank at the end */
count = end - start + ; pair = new_tag_value_pair();
pair->szTag = malloc(count + );
strncpy(pair->szTag, start, count);
pair->szTag[count] = ; /* 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 ;
}
start = p;
end--; /* to the last char */
if(*end == '\n') { *end = ; end--; }
if(*end == '\r') { *end = ; end--; }
count = end - start + ;
if(count > )
{
pair->szValue = malloc(count + );
strncpy(pair->szValue, start, count);
pair->szValue[count] = ;
} /* release empty key-value pair */
if(!pair->szValue)
{
delete_tag_value_pair(pair);
return ;
} 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 = ;
char buf[] = {};
while(fgets(buf, , fp))
{
pair = parse_line(buf, strlen(buf));
if(pair)
{
ini->keyvalues->add(ini->keyvalues, pair);
}
}
fclose(fp);
return ini->keyvalues->size;
}
return -;
} static int _parse_text(struct ini_parser * ini, const char * text){
char *p = text;
char * start = ;
struct tag_value_pair * pair = ;
if(!text) return -; while()
{
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 ;
} 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->keyvalues->remove(ini->keyvalues, pair);
} static void write_keyvalue(struct tag_value_pair * pair, FILE *fp)
{
fputs(pair->szTag, fp);
fputc('=', fp);
fputs(pair->szValue, fp);
fputc('\n', fp);
} static int _save_to_file(struct ini_parser * ini, const char * file){
if(ini->keyvalues->size > )
{
FILE * fp = fopen(file, "w");
if(fp)
{
struct tag_value_pair * pair = ini->keyvalues->head;
while(pair != ini->keyvalues->tail)
{
write_keyvalue(pair, fp);
pair = pair->next;
} if(pair)write_keyvalue(pair, fp); fclose(fp);
return ;
}
}
return -;
} struct ini_parser * new_ini_parser(){
struct ini_parser * ini = (struct ini_parser*)malloc(sizeof(struct ini_parser));
ini->keyvalues = new_tag_value_list();
ini->parse_file = _parse_file;
ini->parse_string = _parse_text;
ini->value = _value;
ini->set_value = _set_value;
ini->remove = _remove;
ini->save_to_file = _save_to_file;
return ini;
} void delete_ini_parser(struct ini_parser *ini){
if(ini)
{
delete_tag_value_list(ini->keyvalues);
free(ini);
}
}
测试代码:
#include "util/ini_parser.h"
#include "ini_test.h"
#include <stdio.h>
#include <assert.h> static char * g_szIniString = "#abc\nfirst=2\nsecond\nname=charli zhang \n"; static void ini_parser_test_string()
{
struct ini_parser * ini = new_ini_parser();
int size = ini->parse_string(ini, g_szIniString); assert( size > );
assert( ini->value(ini, "second") == );
assert( ini->value(ini, "abc") == );
assert( ini->value(ini, "name") != NULL );
assert( ini->value(ini, "first") != NULL); printf("ini string: %s\n", g_szIniString);
printf("key-value pairs count = %d\n", size);
printf("key \'name\'', value = %s\n", ini->value(ini, "name"));
printf("key \'first\'', value = %s\n", ini->value(ini, "first")); ini->set_value(ini, "baidu", "hahaha");
ini->save_to_file(ini, "write.conf"); ini->remove(ini, "first");
ini->save_to_file(ini, "write2.conf"); delete_ini_parser(ini);
} static void ini_parser_test_file()
{
struct ini_parser * ini = new_ini_parser();
int size = ini->parse_file(ini, "test.conf"); assert( size > );
assert( ini->value(ini, "second") == );
assert( ini->value(ini, "abc") == );
assert( ini->value(ini, "name") != NULL );
assert( ini->value(ini, "first") != NULL); printf("ini string: %s\n", g_szIniString);
printf("key-value pairs count = %d\n", size);
printf("key \'name\'', value = %s\n", ini->value(ini, "name"));
printf("key \'first\'', value = %s\n", ini->value(ini, "first"));
printf("key \'baidu\'', value = %s\n", ini->value(ini, "baidu")); delete_ini_parser(ini);
} void ini_parser_test()
{
ini_parser_test_string();
ini_parser_test_file();
}

C语言解析Ini格式文件的更多相关文章

  1. shell解析ini格式文件

    功能 本脚本实现了ini文件中的查询修改指定value 百度云连接地址 链接:https://pan.baidu.com/s/12_T5yST7Y3L1H4_MkVEcvA 密码:fo5p 解压后先看 ...

  2. C语言解析WAV音频文件

    C语言解析WAV音频文件 代码地址: Github : https://github.com/CasterWx/c-wave-master 目录 前言 了解WAV音频文件 什么是二进制文件 WAV的二 ...

  3. SHELL读取 ini 格式文件做配置文件

    ini文件格式一般都是由节.键.值三部分组成 格式: [第一节 ] 第一个键 = 值 第二个键 = 第二个值 [第二节 ] 第一个键 = val1,val2,val3 例子: [COM] KINGGO ...

  4. 利用 nodejs 解析 m3u8 格式文件,并下 ts 合并为 mp4

    利用 nodejs 解析 m3u8 格式文件,并下 ts 合并为 mp4 以前看视频的时候,直接找到 video标签,查看视频地址,然后下载下来.. 后来发现,好多 video 标签打开元素审查,如下 ...

  5. WP8解析XML格式文件

    DOTA2 WebAPI请求返回的格式有两种,一种是XML,一种是JSON,默认是返回JSON格式,如果要返回XML格式的话,需要在加上format=xml. 这里举一个简单的解析XML格式的例子(更 ...

  6. dom4解析xml格式文件实例

    以下给4种常见的xml文件的解析方式的分析对比: DOM DOM4J JDOM SAX 解析XML文件的几种方式和区别答: Dom解析 在内存中创建一个DOM树,该结构通常需要加载整个文档然后才能做工 ...

  7. dom4j解析xml格式文件实例

    以下给4种常见的xml文件的解析方式的分析对比: DOM  DOM4J  JDOM  SAX Dom解析    在内存中创建一个DOM树,该结构通常需要加载整个文档然后才能做工作.由于它是基于信息层次 ...

  8. Linux C语言解析.bmp格式图片并显示汉字

    bmp.h 文件 #ifndef __BMP_H__ #define __BMP_H__ #include <unistd.h> #include <stdio.h> #inc ...

  9. XmlDocument解析Soap格式文件案例:

    private static string Analysis(string strResult) { var doc = new System.Xml.XmlDocument(); //加载soap文 ...

随机推荐

  1. petapoco存储过程

    db.ExecuteScalar<string>("exec P_GetCode @0,@1,@2,@3,@4,@5",); using (var db = new D ...

  2. Oracle数据库监听服务无法启动

    (1) 安装好Oracle后,启动Net Manager,测试orcl失败,报错“ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务”,需要修改监听文件.修改前: # list ...

  3. C# 多线程线程池( 一 )

    我们将在这里进一步讨论一些.NET类,以及他们在多线程编程中扮演的角色和怎么编程.它们是: System.Threading.ThreadPool 类 System.Threading.Timer 类 ...

  4. xampp3.2下mysql中文乱码终极解决方案

    xmapp3.2.2中mysql已经被替换成了Mariadb,网上那些显示char语句已经失灵. 另外本文主要介绍的是手动在mysql中写入中文乱码问题 那么我们将采用如下三个步骤解决乱码问题 1.打 ...

  5. ckeditor插件

    插件下载地址:http://ckeditor.com/download 1.CKeditor配置 在html页面的<head>标签中引入核心文件 ckeditor.js <scrip ...

  6. 我刚知道的WAP app中meta的属性

    之前我一直做的都是WEB前端开发,来北京以后面试了一个移动前端开发,WAP前端开发. 其实在原来公司的时候也做过这方面的开发,可面试的时候面试官问我,要想强制让文档与设备的宽度保持1:1,mate标签 ...

  7. Eclipse使用tomcat的原理

    1. 当我们使用Eclipse将项目部署到Tomcat的时,我们发现,在Tomcat下的webapps目录下并没有我们创建好的项目,但是当通过Eclipse启动服务器后,项目却真的可以访问到,这是为什 ...

  8. dedecms后台验证码显示不正常的四种处理办法

    验证码不正确解决方法 分为两类解决方法 第一类:取消掉验证码,直接登录 第二类:修复验证码,回复验证码功能 四种常见的处理办法如下: 第一种:取消掉验证码具体方法如下 实现的方法一共分为两步来进行: ...

  9. pycharm 注册码

    43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...

  10. Python自动获取数据库表结构

    Sandman https://sandman.readthedocs.io/en/latest/#