C language Configuration File Parsing Library-iniparser

Source: Internet
Author: User

C language Configuration File Parsing Library-iniparser
C language Configuration File Parsing Library-iniparser

Preface:During project optimization, I found that Linux does not have a configuration file function specifically for the C language, so I found the iniparser library, which can be like those object-oriented languages, use the INI file to configure parameters.

Introduction

Iniparser is a parser for INI files. The INI file is the configuration file of some systems or software.

The iniparser library has three download methods:
1. Official Website
2. My github
3. My online storage
4. source code tree

Basic syntax

The API of the Iniparser library can parse, set, and delete INI files.

The most basic unit of an INI file is the key or property. Each key has a name and a value ):

name=value 

Many keys can be grouped into a group, namely, section. The group name must be defined as an independent row and enclosed in brackets:

[section]name=value

The keys declared in the section are associated with the section. The scope of a section ends at the place where the next section is declared. If there is no declaration for the next section, the end of the section is the end of the file. Section cannot be nested.

When you locate a key, it is represented by section: key. Therefore, the key names under different sections can be the same.

When the iniparser library processes names, the names are changed to lowercase letters, so the names of section and property are case-insensitive.

The comment must start with a semicolon:

 ;comment
API

Iniparser. h:

Int iniparser_getnsec (dictionary * d); // gets the number of sections of a dictionary object. char * iniparser_getsecname (dictionary * d, int n ); // obtain the name of section n of the dictionary object void iniparser_dump_ini (dictionary * d, FILE * f); // Save the dictionary object to filevoid iniparser_dumpse_ini (dictionary * d, char * s, FILE * f); // save a section of the dictionary object to filevoid iniparser_dump (dictionary * d, FILE * f ); // Save the dictionary object to fileint iniparser_getsecnkeys (dictionary * d, char * s); // obtain the number of keys in a section of the dictionary object. char ** iniparser_getseckeys (dictionary * d, char * s); // obtain all keychar * iniparser_getstring (dictionary * d, const char * key, char * def) under a section of the dictionary object ); // return the section of the dictionary object: string value int iniparser_getint (dictionary * d, const char * key, int notfound) corresponding to the key; // return the section of the idictionary object: double iniparser_getdouble (dictionary * d, const char * key, double notfound); // return the section of the dictionary object: int iniparser_getboolean (dictionary * d, const char * key, int notfound); // return the section of the dictionary object: boolean int iniparser_set (dictionary * ini, const char * entry, const char * val) corresponding to the key; // set a section of the dictionary object: key Value void iniparser_unset (dictionary * ini, const char * entry); // delete a section in the dictionary object: keyint iniparser_find_entry (dictionary * ini, const char * entry ); // determine whether a section in the dictionary object exists: keydictionary * iniparser_load (const char * inininame); // parse the dictionary object and return (allocate memory) dictionary object void iniparser_freedict (dictionary * d); // release the dictionary object (memory) unsigned dictionary_hash (const char * key ); // calculate the hash value of the keyword dictionary * dictionary_new (int size); // create the dictionary object void dictionary_del (dictionary * vd ); // Delete the dictionary object char * dictionary_get (dictionary * d, const char * key, char * def); // obtain the key value of the dictionary object int dictionary_set (dictionary * vd, const char * key, const char * val); // you can specify void dictionary_unset (dictionary * d, const char * key) for the key of a dictionary object ); // Delete the key value void dictionary_dump (dictionary * d, FILE * out) of the dictionary object; // Save the dictionary object
Example

First, decompress the downloaded library file:

tar -zxvf iniparser-3.1.tar.gz

Compile:

cd iniparser-3.1/make

As you can see, six files are generated under the src directory, where dictionary. h declares Some APIs that directly parse ini file. iniparser. h declares Some APIs that provide user operations. The API in iniparser. h is a re-encapsulation of the API in dictionary. h to provide user friendliness.

Copy the header files dictionary. h and iniparser. h under src and the static library libiniparser. a and dynamic library libiniparser. so.0 under the compressed package directory to the corresponding directory of the target file system.

Compile the INI file:

#ini file for example[tcp];for tcp communicationport = 8000;ip = 127.0.0.1;family = AF_INET;[serial port];for serial port communicationspeed = 9600;

Test file:

/*************************************** * *********************************> File Name: example. c> Author: AnSwEr> Mail: [email protected]> Created Time: thursday, October 22, 2015, 10 seconds ******************************** **************************************** /# include
  
   
# Include
   
    
# Include iniparser. hint main (void) {dictionary * ini; int n = 0; char * str; ini = iniparser_load (example. ini); // parser the file if (ini = NULL) {fprintf (stderr, can not open % s, example. ini); exit (EXIT_FAILURE);} printf (dictionary obj :); iniparser_dump (ini, stderr); // save ini to stderr printf (% s:, iniparser_getsecname (ini, 0); // get section name n = iniparser_getint (ini, tcp: port,-1); printf (port: % d, n); str = iniparser_getstring (ini, TCP/IP, null); printf (ip: % s, str); str = iniparser_getstring (ini, tcp: family, null); printf (family: % s, str ); printf (% s:, iniparser_getsecname (ini, 1); n = iniparser_getint (ini, serial port: speed,-1); printf (speed: % d, n ); iniparser_freedict (ini); // free dirctionary obj return 0 ;}
   
  

Run:

gcc example.c -o example -L. -liniparser./example

Result:

dictionary obj:[tcp]=UNDEF[tcp:port]=[8000][tcp:ip]=[127.0.0.1][tcp:family]=[AF_INET][serial port]=UNDEF[serial port:speed]=[9600]tcp:port : 8000ip : 127.0.0.1family : AF_INETserial port:speed : 9600
Summary

This library is very convenient to manage configuration files, and I hope it will help you.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.