1<html> 2<head> 3 <meta name="author" content="ndevilla@free.fr"> 4 <meta name="keywords" content="ini file, config file, parser, C library"> 5 <link href="doxygen.css" rel="stylesheet" type="text/css"> 6<title>iniparser 2.x</title> 7</head> 8 9<body text="#000000" bgcolor="#ffffff"> 10 11 12 13<!-- Generated by Doxygen 1.4.4 --> 14<h1>iniparser documentation </h1> 15<p> 16<h3 align="center">2.x </h3><hr> 17<h2><a class="anchor" name="welcome"> 18Introduction</a></h2> 19iniParser is a simple C library offering ini file parsing services. The library is pretty small (less than 1500 lines of C) and robust, and does not depend on any other external library to compile. It is written in ANSI C and should compile anywhere without difficulty.<p> 20<hr> 21<h2><a class="anchor" name="inidef"> 22What is an ini file?</a></h2> 23An ini file is an ASCII file describing simple parameters (character strings, integers, floating-point values or booleans) in an explicit format, easy to use and modify for users.<p> 24An ini file is segmented into Sections, declared by the following syntax:<p> 25<div class="fragment"><pre class="fragment"> [Section Name] 26 </pre></div><p> 27i.e. the section name enclosed in square brackets, alone on a line. Sections names are allowed to contain any character but square brackets or linefeeds. Slashes (/) are also reserved for hierarchical sections (see below).<p> 28In any section are zero or more variables, declared with the following syntax:<p> 29<div class="fragment"><pre class="fragment"> Key = value ; comment 30 </pre></div><p> 31The key is any string (possibly containing blanks). The value is any character on the right side of the equal sign. Values can be given enclosed with quotes. If no quotes are present, the value is understood as containing all characters between the first and the last non-blank characters. The following declarations are identical:<p> 32<div class="fragment"><pre class="fragment"> Hello = "this is a long string value" ; comment 33 Hello = this is a long string value ; comment 34 </pre></div><p> 35The semicolon and comment at the end of the line are optional. If there is a comment, it starts from the first character after the semicolon up to the end of the line.<p> 36Comments in an ini file are:<p> 37<ul> 38<li>Lines starting with a hash sign</li><li>Blank lines (only blanks or tabs)</li><li>Comments given on value lines after the semicolon (if present)</li></ul> 39<p> 40<hr> 41<h2><a class="anchor" name="install"> 42Compiling/installing the library</a></h2> 43Edit the Makefile to indicate the C compiler you want to use, the options to provide to compile ANSI C, and possibly the options to pass to the <code>ar</code> program on your machine to build a library (.a) from a set of object (.o) files.<p> 44Defaults are set for the gcc compiler and the standard ar library builder.<p> 45Type 'make', that should do it.<p> 46To use the library in your programs, add the following line on top of your module:<p> 47<div class="fragment"><pre class="fragment"><span class="preprocessor"> #include "<a class="code" href="iniparser_8h.html">iniparser.h</a>"</span> 48</pre></div><p> 49And link your program with the iniparser library by adding <code>-liniparser</code>.a to the compile line.<p> 50See the file test/initest.c for an example.<p> 51<hr> 52<h2><a class="anchor" name="reference"> 53Library reference</a></h2> 54The library is completely documented in its header file. On-line documentation has been generated and can be consulted here:<p> 55<ul> 56<li><a class="el" href="iniparser_8h.html">iniparser.h</a></li></ul> 57<p> 58<hr> 59<h2><a class="anchor" name="usage"> 60Using the parser</a></h2> 61Comments are discarded by the parser. Then sections are identified, and in each section a new entry is created for every keyword found. The keywords are stored with the following syntax:<p> 62<div class="fragment"><pre class="fragment"> [Section] 63 Keyword = value ; comment 64 </pre></div><p> 65is converted to the following key pair:<p> 66<div class="fragment"><pre class="fragment"> ("section:keyword", "value") 67 </pre></div><p> 68This means that if you want to retrieve the value that was stored in the section called <code>Pizza</code>, in the keyword <code>Cheese</code>, you would make a request to the dictionary for <code>"pizza:cheese"</code>. All section and keyword names are converted to lowercase before storage in the structure. The value side is conserved as it has been parsed, though.<p> 69Section names are also stored in the structure. They are stored using as key the section name, and a NULL associated value. They can be queried through <a class="el" href="iniparser_8h.html#a11">iniparser_find_entry()</a>.<p> 70To launch the parser, simply use the function called <a class="el" href="iniparser_8h.html#a12">iniparser_load()</a>, which takes an input file name and returns a newly allocated <em>dictionary</em> structure. This latter object should remain opaque to the user and only accessed through the following accessor functions:<p> 71<ul> 72<li><a class="el" href="iniparser_8h.html#a4">iniparser_getstr()</a></li><li><a class="el" href="iniparser_8h.html#a6">iniparser_getint()</a></li><li><a class="el" href="iniparser_8h.html#a7">iniparser_getdouble()</a></li><li><a class="el" href="iniparser_8h.html#a8">iniparser_getboolean()</a></li></ul> 73<p> 74Finally, discard this structure using <a class="el" href="iniparser_8h.html#a13">iniparser_freedict()</a>.<p> 75All values parsed from the ini file are stored as strings. The getint, getdouble and getboolean accessors are just converting these strings to the requested type on the fly, but you could basically perform this conversion by yourself after having called the getstr accessor.<p> 76Notice that the <a class="el" href="iniparser_8h.html#a8">iniparser_getboolean()</a> function will return an integer (0 or 1), trying to make sense of what was found in the file. Strings starting with "y", "Y", "t", "T" or "1" are considered true values (return 1), strings starting with "n", "N", "f", "F", "0" are considered false (return 0). This allows flexible handling of boolean answers.<p> 77If you want to add extra information into the structure that was not present in the ini file, you can use <a class="el" href="iniparser_8h.html#a9">iniparser_setstr()</a> to insert a string.<p> 78<hr> 79<h2><a class="anchor" name="implementation"> 80A word about the implementation</a></h2> 81The dictionary structure is a pretty simple dictionary implementation which might find some uses in other applications. If you are curious, look into the source.<p> 82<hr> 83<h2><a class="anchor" name="hierarchical"> 84Hierarchical ini files</a></h2> 85ini files are nice to present informations to the user in a readable format, but lack a very useful feature: the possibility of organizing data in a hierarchical (tree-like) fashion. The following convention can be used to make ini files obtain this second dimension:<p> 86A section depends on another section if it contains its name as a prefix, separated by slashes (/). For example: we have 2 main sections in the ini file. The first one is called <code>Pizza</code> and has two child subsections called <code>Cheese</code> and <code>Ham</code>. The second main section in the ini file is called <code>Wine</code> and has two child subsections called <code>Year</code> and <code>Grape</code>. As a tree, this could appear as:<p> 87<div class="fragment"><pre class="fragment"> | 88 +-- Pizza 89 | +-- Cheese 90 | +-- Ham 91 +-- Wine 92 +--- Year 93 +--- Grape 94 </pre></div><p> 95In an ini file, that would be converted to:<p> 96<div class="fragment"><pre class="fragment"> [Pizza] 97 98 [Pizza/Cheese] 99 Name = Gorgonzola ; 100 Origin = Italy ; 101 102 [Pizza/Ham] 103 Name = Parma ; 104 Origin = Italy ; 105 106 [Wine] 107 108 [Wine/Year] 109 Value = 1998 ; 110 111 [Wine/Grape] 112 Name = Cabernet Sauvignon ; 113 Origin = Chile ; 114 </pre></div><p> 115This proposal is actually more related to the way people write ini files, more than the parser presented here. But it is certainly a useful way of making tree-like data declarations without going through painful formats like XML.<p> 116Accessing the above tree would give something like (error checking removed for clarity sake):<p> 117<div class="fragment"><pre class="fragment"> dictionary * d ; 118 119 d = <a class="code" href="iniparser_8h.html#a12">iniparser_load</a>(<span class="stringliteral">"example.ini"</span>); 120 121 printf(<span class="stringliteral">"cheese name is %s\n"</span>, <a class="code" href="iniparser_8h.html#a4">iniparser_getstr</a>(d, <span class="stringliteral">"pizza/cheese:name"</span>)); 122 printf(<span class="stringliteral">"grape name is %s\n"</span>, <a class="code" href="iniparser_8h.html#a4">iniparser_getstr</a>(d, <span class="stringliteral">"wine/grape:name"</span>)); 123 124 <a class="code" href="iniparser_8h.html#a13">iniparser_freedict</a>(d); 125</pre></div><p> 126The whole ini file above is represented in the dictionary as the following list of pairs:<p> 127<div class="fragment"><pre class="fragment"> key value 128 129 "pizza" NULL 130 "pizza/cheese" NULL 131 "pizza/cheese:name" "Gorgonzola" 132 "pizza/cheese:origin" "Italy" 133 "pizza/ham" NULL 134 "pizza/ham:name" "Parma" 135 "pizza/ham:origin" "Italy" 136 "wine" NULL 137 "wine/year" NULL 138 "wine/year:value" "1998" 139 "wine/grape" NULL 140 "wine/grape:name" "Cabernet Sauvignon" 141 "wine/grape:origin" "Chile" 142 </pre></div><p> 143<hr> 144<h2><a class="anchor" name="authors"> 145Authors</a></h2> 146Nicolas Devillard (ndevilla AT free DOT fr). 147</body> 148</html> 149