INI文件解析源码及使用简介

一:环境配置

1、 下载函数库:

链接:https://pan.baidu.com/s/1ZOpa2og_QQy-MnHNQg7N1w 密码:neez

2、解压文件会得到以下文件,其中dictionary.h、iniparser.h 、dictionary.c 、iniparser.c为对应的库文件,ini_test为测试函数,test.ini为需要解析的配置文件。

3、  该代码已经分别在windows系统下VisualStudio 和 Ubuntu系统下eclipse进行了测试,都能成功运行。只需分别将头文件和源文件添加到新建的工程中即可。运行之前先要把Init_test中  iniparser_load("/.../test.ini")函数的路径改成和 配置文件test.ini实际存储位置一致。

二:函数介绍

1、 INI文件格式

INI文件由3个重要的部分组成:段(section) ,参数(parameters),和注释(comments).其格式如下:

段(section)

[section]

参数(parameters)

key  =  value;

注释(comments)

;comments

如解压得到的test.ini文件内容:

2、 常用函数简介:

/*
 * iniparser.h
 *
 *  Created on: Apr 13, 2018
 *      Author: lgh
 */

#ifndef INIPARSER_H_
#define INIPARSER_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dictionary.h"

/*-------------------------------------------------------------------------*/
/**
@brief    Get number of sections in a dictionary
@param    d   Dictionary to examine
@return   int Number of sections found in dictionary

This function returns the number of sections found in a dictionary.
The test to recognize sections is done on the string stored in the
dictionary: a section name is given as "section" whereas a key is
stored as "section:key", thus the test looks for entries that do not
contain a colon.

This clearly fails in the case a section name contains a colon, but
this should simply be avoided.

This function returns -1 in case of error.
*/
/*--------------------------------------------------------------------------*/

int iniparser_getnsec(dictionary * d);

/*-------------------------------------------------------------------------*/
/**
@brief    Get name for section n in a dictionary.
@param    d   Dictionary to examine
@param    n   Section number (from 0 to nsec-1).
@return   Pointer to char string

This function locates the n-th section in a dictionary and returns
its name as a pointer to a string statically allocated inside the
dictionary. Do not free or modify the returned string!

This function returns NULL in case of error.
*/
/*--------------------------------------------------------------------------*/

char * iniparser_getsecname(dictionary * d, int n);

/*-------------------------------------------------------------------------*/
/**
@brief    Save a dictionary to a loadable ini file
@param    d   Dictionary to dump
@param    f   Opened file pointer to dump to
@return   void

This function dumps a given dictionary into a loadable ini file.
It is Ok to specify @c stderr or @c stdout as output files.
*/
/*--------------------------------------------------------------------------*/

void iniparser_dump_ini(dictionary * d, FILE * f);

/*-------------------------------------------------------------------------*/
/**
@brief    Save a dictionary section to a loadable ini file
@param    d   Dictionary to dump
@param    s   Section name of dictionary to dump
@param    f   Opened file pointer to dump to
@return   void

This function dumps a given section of a given dictionary into a loadable ini
file.  It is Ok to specify @c stderr or @c stdout as output files.
*/
/*--------------------------------------------------------------------------*/

void iniparser_dumpsection_ini(dictionary * d, char * s, FILE * f);

/*-------------------------------------------------------------------------*/
/**
@brief    Dump a dictionary to an opened file pointer.
@param    d   Dictionary to dump.
@param    f   Opened file pointer to dump to.
@return   void

This function prints out the contents of a dictionary, one element by
line, onto the provided file pointer. It is OK to specify @c stderr
or @c stdout as output files. This function is meant for debugging
purposes mostly.
*/
/*--------------------------------------------------------------------------*/
void iniparser_dump(dictionary * d, FILE * f);

/*-------------------------------------------------------------------------*/
/**
@brief    Get the number of keys in a section of a dictionary.
@param    d   Dictionary to examine
@param    s   Section name of dictionary to examine
@return   Number of keys in section
*/
/*--------------------------------------------------------------------------*/
int iniparser_getsecnkeys(dictionary * d, char * s);

/*-------------------------------------------------------------------------*/
/**
@brief    Get the number of keys in a section of a dictionary.
@param    d   Dictionary to examine
@param    s   Section name of dictionary to examine
@return   pointer to statically allocated character strings

This function queries a dictionary and finds all keys in a given section.
Each pointer in the returned char pointer-to-pointer is pointing to
a string allocated in the dictionary; do not free or modify them.

This function returns NULL in case of error.
*/
/*--------------------------------------------------------------------------*/
char ** iniparser_getseckeys(dictionary * d, char * s);

/*-------------------------------------------------------------------------*/
/**
@brief    Get the string associated to a key
@param    d       Dictionary to search
@param    key     Key string to look for
@param    def     Default value to return if key not found.
@return   pointer to statically allocated character string

This function queries a dictionary for a key. A key as read from an
ini file is given as "section:key". If the key cannot be found,
the pointer passed as ‘def‘ is returned.
The returned char pointer is pointing to a string allocated in
the dictionary, do not free or modify it.
*/
/*--------------------------------------------------------------------------*/
char * iniparser_getstring(dictionary * d, const char * key, char * def);

/*-------------------------------------------------------------------------*/
/**
@brief    Get the string associated to a key, convert to an int
@param    d Dictionary to search
@param    key Key string to look for
@param    notfound Value to return in case of error
@return   integer

This function queries a dictionary for a key. A key as read from an
ini file is given as "section:key". If the key cannot be found,
the notfound value is returned.

Supported values for integers include the usual C notation
so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
are supported. Examples:

- "42"      ->  42
- "042"     ->  34 (octal -> decimal)
- "0x42"    ->  66 (hexa  -> decimal)

Warning: the conversion may overflow in various ways. Conversion is
totally outsourced to strtol(), see the associated man page for overflow
handling.

Credits: Thanks to A. Becker for suggesting strtol()
*/
/*--------------------------------------------------------------------------*/
int iniparser_getint(dictionary * d, const char * key, int notfound);

/*-------------------------------------------------------------------------*/
/**
@brief    Get the string associated to a key, convert to a double
@param    d Dictionary to search
@param    key Key string to look for
@param    notfound Value to return in case of error
@return   double

This function queries a dictionary for a key. A key as read from an
ini file is given as "section:key". If the key cannot be found,
the notfound value is returned.
*/
/*--------------------------------------------------------------------------*/
double iniparser_getdouble(dictionary * d, const char * key, double notfound);

/*-------------------------------------------------------------------------*/
/**
@brief    Get the string associated to a key, convert to a boolean
@param    d Dictionary to search
@param    key Key string to look for
@param    notfound Value to return in case of error
@return   integer

This function queries a dictionary for a key. A key as read from an
ini file is given as "section:key". If the key cannot be found,
the notfound value is returned.

A true boolean is found if one of the following is matched:

- A string starting with ‘y‘
- A string starting with ‘Y‘
- A string starting with ‘t‘
- A string starting with ‘T‘
- A string starting with ‘1‘

A false boolean is found if one of the following is matched:

- A string starting with ‘n‘
- A string starting with ‘N‘
- A string starting with ‘f‘
- A string starting with ‘F‘
- A string starting with ‘0‘

The notfound value returned if no boolean is identified, does not
necessarily have to be 0 or 1.
*/
/*--------------------------------------------------------------------------*/
int iniparser_getboolean(dictionary * d, const char * key, int notfound);

/*-------------------------------------------------------------------------*/
/**
@brief    Set an entry in a dictionary.
@param    ini     Dictionary to modify.
@param    entry   Entry to modify (entry name)
@param    val     New value to associate to the entry.
@return   int 0 if Ok, -1 otherwise.

If the given entry can be found in the dictionary, it is modified to
contain the provided value. If it cannot be found, -1 is returned.
It is Ok to set val to NULL.
*/
/*--------------------------------------------------------------------------*/
int iniparser_set(dictionary * ini, const char * entry, const char * val);

/*-------------------------------------------------------------------------*/
/**
@brief    Delete an entry in a dictionary
@param    ini     Dictionary to modify
@param    entry   Entry to delete (entry name)
@return   void

If the given entry can be found, it is deleted from the dictionary.
*/
/*--------------------------------------------------------------------------*/
void iniparser_unset(dictionary * ini, const char * entry);

/*-------------------------------------------------------------------------*/
/**
@brief    Finds out if a given entry exists in a dictionary
@param    ini     Dictionary to search
@param    entry   Name of the entry to look for
@return   integer 1 if entry exists, 0 otherwise

Finds out if a given entry exists in the dictionary. Since sections
are stored as keys with NULL associated values, this is the only way
of querying for the presence of sections in a dictionary.
*/
/*--------------------------------------------------------------------------*/
int iniparser_find_entry(dictionary * ini, const char * entry);

/*-------------------------------------------------------------------------*/
/**
@brief    Parse an ini file and return an allocated dictionary object
@param    ininame Name of the ini file to read.
@return   Pointer to newly allocated dictionary

This is the parser for ini files. This function is called, providing
the name of the file to be read. It returns a dictionary object that
should not be accessed directly, but through accessor functions
instead.

The returned dictionary must be freed using iniparser_freedict().
*/
/*--------------------------------------------------------------------------*/
dictionary * iniparser_load(const char * ininame);

/*-------------------------------------------------------------------------*/
/**
@brief    Free all memory associated to an ini dictionary
@param    d Dictionary to free
@return   void

Free all memory associated to an ini dictionary.
It is mandatory to call this function before the dictionary object
gets out of the current context.
*/
/*--------------------------------------------------------------------------*/
void iniparser_freedict(dictionary * d);

#endif /* INIPARSER_H_ */

  

原文地址:https://www.cnblogs.com/GuanghuiLiu/p/8832034.html

时间: 2024-08-05 11:04:29

INI文件解析源码及使用简介的相关文章

FFmpeg中HLS文件解析源码

不少人都在找FFmpeg中是否有hls(m3u8)解析的源码,其实是有的.就是ffmpeg/libavformat/hlsproto.c,它依赖的文件也在那个目录中. /* * Apple HTTP Live Streaming Protocol Handler * Copyright (c) 2010 Martin Storsjo * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute

Django的settings文件部分源码分析

Django的settings文件部分源码分析 在编写Django项目的过程中, 其中一个非常强大的功能就是我们可以在settings文件配置许多选项来完成我们预期的功能, 并且这些配置还必须大写, 否则就不会生效. 此外, Django自身还有一套更详细的配置, 那Django是如何做到用户配置了相关配置就使用用户的配置, 否则就使用自己默认的配置. 带着这样的疑问, 去查看了用户配置项相关的源码部分. 过程分析 首先启动Django项目, 一般Django都是通过python manage.

一个Ini文件解析类CIniFileParse

使用方法非常简单,看看源代码就明白了,该类支持Unicode文本. 一个Ini文件解析类CIniFileParse.rar

Asp.Net Web Api 2 实现多文件打包并下载文件示例源码_转

一篇关于Asp.Net Web Api下载文件的文章,之前我也写过类似的文章,请见:<ASP.NET(C#) Web Api通过文件流下载文件到本地实例>本文以这篇文章的基础,提供了ByteArrayContent的下载以及在下载多个文件时实现在服务器对多文件进行压缩打包后下载的功能.关于本文中实现的在服务器端用.NET压缩打包文件功能的过程中,使用到了一个第方类库:DotNetZip,具体的使用将在正文中涉及.好了,描述了这么多前言,下面我们进入本文示例的正文. 一.创建项目 1.1 首先创

git文件致源码泄露

前言:在一道ctf题的驱动下,我进行了对git和.git文件致源文件泄露的学习. 一.任务 一道ctf题目. 二.确定题目考的点 谷歌关键词:版本管理工具 github ctf 由得到的结果猜测,可能考的点是.git文件导致源文件泄露. 三.对git的粗略学习 其实经常会用到git,例如在用hexo+github pages搭建博客的时候就用到了git,又如在linux下载一些工具的时候,用到的git clone. git:分布式版本控制系统. 首先,何为版本控制系统?以git的诞生故事来解释,

mybatis之XML解析源码分析

一直想知道mybatis是如何解析xml文件的,今天认真看了下源码,这里记录一下 这里是用mybatis-spring的SqlSessionFactoryBean当作的入口,mybatis-spring其实很简单,源码也就几个看看就懂了,代理了一下而已没啥东东. 1.解析spring的配置 不过很多参数都是spring中来处理了,所以mybatis-spring没有先parse而是先加载了配置文件 依次是 typeAliasesPackage typeAliases Plugins typeHa

Appium Android Bootstrap源码分析之简介

在上一个系列中我们分析了UiAutomator的核心源码,对UiAutomator是怎么运行的原理有了根本的了解.今天我们会开始另外一个在安卓平台上基于UiAutomator的新起之秀--Appium的源码分析之旅. 本文在真个系列中会扮演一个简介的角色,不会分析任何的代码,只会先给大家一个基本的印象,方便大家在持有这个印象的基础上往下和本人一块分析. 1. Bootstrap定义及在Appium中扮演的角色 我们先看一下本人大概一个月之前刚接触appium时整理的一个高层架构图 下面一部分就是

Tachyon学习及源码阅读:简介

前言 这个系列是关于Tachyon的,主要基于<Reliable, Memory Speed Storage for Cluster Computing Frameworks >这篇论文以及源码,最近工作比较忙,精力实在有限,可能更新得比较慢 简介 Tachyon是一个分布式文件系统,提供了一种可靠的方式,可以以访问内存的速度在不同的分布式计算框架之间共享数据.Tachyon使用lineage技术实现容错,并通过一种检查点(checkpoint)算法来确保恢复以及资源开销在一定范围之内.据作者

Java注解及其原理以及分析spring注解解析源码

注解的定义 注解是那些插入到源代码中,使用其他工具可以对其进行处理的标签. 注解不会改变程序的编译方式:Java编译器对于包含注解和不包含注解的代码会生成相同的虚拟机指令. 在Java中,注解是被当做一个修饰符来使用的(修饰符:如public.private) 注解的常用用法:1. 附属文件的自动生成,例如bean信息类. 2. 测试.日志.事务等代码的自动生成. 单元测试例子: import org.junit.Test; public class SomeTest { @Test publi