index.html 9.3 KB
Newer Older
N
ndevilla 已提交
1 2 3 4 5 6 7 8 9 10 11
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>iniparser: iniparser documentation</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.3 -->
<div class="contents">
E
Emmanuel Leblond 已提交
12
<h1>iniparser documentation </h1><h3 class="version">4.0 </h3><h2><a class="anchor" id="welcome">
N
ndevilla 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
Introduction</a></h2>
<p>iniParser 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 on most platforms without difficulty.</p>
<h2><a class="anchor" id="inidef">
What is an ini file?</a></h2>
<p>An 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>
<p>An ini file is segmented into Sections, declared by the following syntax:</p>
<div class="fragment"><pre class="fragment">
    [Section Name]
	</pre></div><p>i.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.</p>
<p>In any section are zero or more variables, declared with the following syntax:</p>
<div class="fragment"><pre class="fragment">
    Key = value ; comment
	</pre></div><p>The 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 before the comment. The following declarations are identical:</p>
<div class="fragment"><pre class="fragment">
    Hello = "this is a long string value" ; comment
    Hello = this is a long string value ; comment
	</pre></div><p>The 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>
<p>Multi-line values can be provided by ending the line with a backslash (\).</p>
<div class="fragment"><pre class="fragment">
    Multiple = Line 1 \
    Line 2 \
    Line 3 \
    Line 4 ; comment
    </pre></div><p>This would yield: "multiple" &lt;- "Line1 Line2 Line3 Line4"</p>
<p>Comments in an ini file are:</p>
<ul>
<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>
<h2><a class="anchor" id="install">
Compiling/installing the library</a></h2>
<p>Edit 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 ar program on your machine to build a library (.a) from a set of object (.o) files.</p>
<p>Defaults are set for the gcc compiler and the standard ar library builder.</p>
<p>Type 'make', that should do it.</p>
<p>To use the library in your programs, add the following line on top of your module:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">    #include &quot;<a class="code" href="iniparser_8h.html" title="Parser for ini files.">iniparser.h</a>&quot;</span>
</pre></div><p>And link your program with the iniparser library by adding <code>-liniparser.a</code> to the compile line.</p>
<p>See the file test/initest.c for an example.</p>
<p>iniparser is an ANSI C library. If you want to compile it with a C++ compiler you will likely run into compatibility issues. Headers probably have to include the extern "C" hack and function prototypes will want to add some const here and there to keep the compiler happy. This job is left to the reader as there are too many C++ compilers around, each with its own requirements as to what represents acceptable C code in a C++ environment. You have been warned.</p>
<h2><a class="anchor" id="reference">
Library reference</a></h2>
<p>The library is completely documented in its header file. On-line documentation has been generated and can be consulted here:</p>
<ul>
<li><a class="el" href="iniparser_8h.html" title="Parser for ini files.">iniparser.h</a></li>
</ul>
<h2><a class="anchor" id="usage">
Using the parser</a></h2>
<p>Comments 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>
<div class="fragment"><pre class="fragment">
    [Section]
    Keyword = value ; comment
	</pre></div><p>is converted to the following key pair:</p>
<div class="fragment"><pre class="fragment">
    ("section:keyword", "value")
	</pre></div><p>This 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>
<p>Section 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#a3d67c98bbc0cb5239f024ad54bdc63f1" title="Finds out if a given entry exists in a dictionary.">iniparser_find_entry()</a>.</p>
<p>To launch the parser, use the function called <a class="el" href="iniparser_8h.html#a45d87791d4d2593781bfdfe2991c3a2d" title="Parse an ini file and return an allocated dictionary object.">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>
<ul>
<li><a class="el" href="iniparser_8h.html#aec2e5539bc2be063d1323cdf65f162a3" title="Get the string associated to a key.">iniparser_getstring()</a></li>
<li><a class="el" href="iniparser_8h.html#ab813340fa9c9a7fcfe6775d6e5e458c2" title="Get the string associated to a key, convert to an int.">iniparser_getint()</a></li>
<li><a class="el" href="iniparser_8h.html#a480d35322f1252344cf2246ac21ee559" title="Get the string associated to a key, convert to a double.">iniparser_getdouble()</a></li>
<li><a class="el" href="iniparser_8h.html#aa2ea2b34f6f4b3cf93c9d4f8f992811f" title="Get the string associated to a key, convert to a boolean.">iniparser_getboolean()</a></li>
</ul>
<p>Finally, discard this structure using <a class="el" href="iniparser_8h.html#a90549ee518523921886b74454ff872eb" title="Free all memory associated to an ini dictionary.">iniparser_freedict()</a>.</p>
<p>All values parsed from the ini file are stored as strings. The 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 string accessor.</p>
<p>Notice that <a class="el" href="iniparser_8h.html#aa2ea2b34f6f4b3cf93c9d4f8f992811f" title="Get the string associated to a key, convert to a boolean.">iniparser_getboolean()</a> 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 some flexibility in handling of boolean answers.</p>
<p>If 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#aa5d5787f96d6982a937edb2fd499ba60" title="Set an entry in a dictionary.">iniparser_set()</a> to insert a string.</p>
<p>If you want to add a section to the structure, add a key with a NULL value. Example: </p>
<div class="fragment"><pre class="fragment">
    iniparser_set(ini, "section", NULL);
    iniparser_set(ini, "section:key1", NULL);
    iniparser_set(ini, "section:key2", NULL);
    </pre></div><h2><a class="anchor" id="implementation">
A word about the implementation</a></h2>
<p>The 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>
<h2><a class="anchor" id="defects">
Known defects</a></h2>
<p>The dictionary structure is extremely unefficient for searching as keys are sorted in the same order as they are read from the ini file, which is convenient when dumping back to a file. The simplistic first-approach linear search implemented there can become a bottleneck if you have a very large number of keys.</p>
<p>People who need to load large amounts of data from an ini file should definitely turn to more appropriate solutions: sqlite3 or similar. There are otherwise many other dictionary implementations available on the net to replace this one.</p>
<h2><a class="anchor" id="authors">
Authors</a></h2>
<p>Nicolas Devillard (ndevilla AT free DOT fr). </p>
</div>
E
Emmanuel Leblond 已提交
97
<hr class="footer"/><address style="text-align: right;"><small>Generated on Sun Jun 12 19:07:18 2016 for iniparser by&nbsp;
N
ndevilla 已提交
98
<a href="http://www.doxygen.org/index.html">
E
Emmanuel Leblond 已提交
99
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </small></address>
N
ndevilla 已提交
100 101
</body>
</html>