benhoyt/inih logo

benhoyt/inih

Free

Simple .INI file parser in C, good for embedded systems

FreeFree tier
Type
Open Source

About benhoyt/inih

inih (INI Not Invented Here) is a simple .INI file parser written in C, designed to be small and simple for embedded systems. It consists of only a couple of pages of code and is compatible with Python's ConfigParser style of .INI files, including RFC 822-style multi-line syntax and name: value entries. The parser uses a callback-based (SAX style) approach, calling a handler function for each name=value pair. It supports parsing from files, strings, custom streams, or using a fgets-style reader. A wide range of compile-time options allow developers to control syntax features (multi-line, UTF-8 BOM, inline comments), parsing behavior (stop on first error, line numbers, callback on new sections), and memory allocation (stack vs heap, maximum line length). inih is distributed as open source under a permissive license.

Key Features

Minimal C codebase (only a couple of pages) for easy integration
Callback-based (SAX style) parsing for low-memory environments
Supports parsing from FILE*, string, or custom stream via ini_parse() family
Compile-time options to enable/disable multi-line entries, inline comments, UTF-8 BOM
Configurable comment prefix characters and start-of-line comment characters
Option to allow keys with no values (value set to NULL)
Can stop parsing on first error or continue
Option to include line numbers in callback or get callbacks for new sections
Choice of stack-based or heap-allocated line buffer with configurable max line length
Compatible with Python's ConfigParser and RFC 822-style INI files

Pros & Cons

Pros
  • Extremely small and lightweight, suitable for embedded targets
  • Highly configurable via preprocessor defines without runtime overhead
  • Permissive license (BSD-style) allows use in proprietary projects
  • Well-tested with a history of use in other open source projects (e.g., uhub)
  • Easy to integrate – just copy ini.c and ini.h into your project
  • Supports multiple input sources (file, string, stream)
Cons
  • Read-only parser: does not generate or modify INI files
  • Callback-based API may be less intuitive than a DOM-style approach
  • Configuration via preprocessor defines can be inconvenient for some project setups
  • Only supports INI format, not JSON, YAML, or other structured config formats

Best For

Embedded systems with limited memory and storageConfiguration file parsing in C/C++ applicationsResource-constrained environments where a full parser is too largeProjects needing a simple, dependency-free INI parser

FAQ

What is inih?
inih (INI Not Invented Here) is a simple, small .INI file parser written in C, designed for embedded systems and resource-constrained environments.
How do I parse an INI file with inih?
Call ini_parse() with the filename and a callback function. For each name=value pair, the callback is invoked with the section, name, and value strings. Alternative functions ini_parse_file(), ini_parse_string(), and ini_parse_stream() are also available.
Can I handle multi-line values?
Yes, by default inih supports multi-line entries in the style of Python's ConfigParser. You can disable this with -DINI_ALLOW_MULTILINE=0.
How do I disable inline comments?
Add -DINI_ALLOW_INLINE_COMMENTS=0 at compile time. You can also change which characters start an inline comment using INI_INLINE_COMMENT_PREFIXES.
Does inih support using heap memory?
Yes. By default it uses a fixed-size buffer on the stack. To use heap allocation instead, define -DINI_USE_STACK=0.