aboutsummaryrefslogtreecommitdiff

parcini: a PARser in C for INI files

parcini is a simple parser for INI files somewhat inspired by inih. I made it because I wanted a somewhat different API from that of inih, and because I was half-way done when I found about inih.

It was mainly made to serve my needs, so I intend on keeping it super minimal. Most probably inih or a different library might of more interest/use to you, but feel free to use my implementation if it fits your use-case as it fits mine.

License

parcini is released under the terms of the GNU Lesser General Public License, version 2.1 only. For more information check the LICENSE file.

Compiling

Just copy the header include/parcini.h and source src/parcini.c files to your project. My main goal is to statically link it, so there's no "support" to use it as a dynamically linked, but feel free to add it yourself.

A compiler with C99 support and a libc with POSIX.1-2008 are required.

The Makefile provided is just for testing purposes. The test(s) can be run simply by executing make from the command-line.

Compile-time options

Currently only the following compile-time option is available:

  • PARCINI_COMMENT_CHAR: should be set to the desired character for start of comment. Default is '#'.

INI file format

There is no one standard INI file spec, but this implementation does differ some from most other implementations out there.

  • It expects only three possible value types: integer, boolean and string.
    • Integers can be signed and should be withing the range of a long int. They are parsed with strtol() so more information can be gathered from the respective manual page.
    • Booleans can be represented as yes/no or true/false.
    • Strings should be surrounded by " quote characters, and can contain quote characters without escaping, since the last quote character is considered the closing one.
  • = is the delimiter sign for keys and values, e.g. key = value
  • Any white space (except newline) surrounding the = delimiter is ignored.
  • Any white space after the end of a value, or the ] of a section is ignored. Other characters are not ignored and invalidate the line, unless they are preceded by the comment character.
  • A new line means a new key-value, section, or comment/empty line.
  • In the default compilation comments start with # and can be on their own line or at the end of key-value and section lines.

Example INI file

int=1995
string = "hello world"
bool = yes

# a comment
[section]
key="value" # another comment

Example program

Check out example/example.c for an example of a program using parcini. You can compile it by running make example, it will be compiled to build/example/example.

Questions

I don't expect this piece of code to be used by a lot of people (if any), but if you have any questions, or maybe even patches, send me an email. You can find my address and my PGP key on my homepage www.yaroslavps.com.