Introduction (中文)
cini is a lightweight ini configuration file parsing library designed for convenient reading and modification of ini format configuration files in C language projects, avoiding the complexity of manual parsing.
cini is suitable for C language applications that require frequent configuration modifications and dynamic parameter adjustments, eliminating the hassle of recompiling the code. It can be used to modify configuration files for programs such as games and servers.
cini is designed with a simple C language interface, requiring only a few lines of code to integrate into a project, greatly improving configuration management efficiency.
- Retrieve key values from a specified group, with support for default values
- Set key values in a specified group
- Remove key values from a specified group
- Check if a specified group contains a key
Using cini mainly includes the following steps:
- Include cini.h header file
- Create cini config object, set ini file path
cini_t config;
cini_path_set(&config, "/path/to/config.ini");
- Open a group (case sensitive)
cini_group_begin(&config, "group1");
- Read/write keys
// Read key with default value
char value[128] = {0};
cini_read(&config, "key1", "default", value, 128);
// Write key
cini_write(&config, "key2", "value");
- Close current group
cini_group_end(&config);
See example directory for complete examples.
Main API functions:
cini_path_set()
: Set file pathcini_group_begin()
: Open groupcini_group_end()
: Close groupcini_value_get()
: Get key valuecini_value_set()
: Set key valuecini_value_remove()
: Remove keycini_value_contains()
: Check if key exists
The implementation of cini mainly consists of two parts:
- Configuration file path and group management
cini uses the cini_t object to store the path of the ini file and information about the current group, including its name, starting line, and ending line. With this information, it is possible to quickly locate the position of the current group and perform read and write operations.
- Read and write operations for key-value pairs
Read and write operations are implemented by accessing the configuration file. The file is opened, and each line is scanned to locate the specified key name and parse it based on the key=value format in the line.
For writing, a temporary file is created, and the unchanged content is copied over. The modified content is inserted at specific positions to generate a new file.
cini uses CMake as build system.
In project root, run commands:
mkdir build
cd build
cmake ..
make
Alternative:
cmake -S . -B build
cmake --build build
Will generate libini.a/libini.so library, cini example program and test_cini test program in build/bin directory.
The test program is called test_cini and is used for automated testing of the cini library.
To run the test_cini program without any parameters, execute the following command:
./test_cini
To view the usage help information, use the -h or --help option:
./test_cini --help
To view the version information, use the -v or --version option:
./test_cini --version
To list all available test cases, use the -l or --list option:
./test_cini --list
To run a specific test case, use the -t or --target option followed by the name of the test case:
./test_cini --target testcase_name
Replace testcase_name
with the name of the test case you want to run.
Some test cases accept additional parameters. See each test case definition for parameter usage details.
After running each test case, the program will print the result in the following format:
- PASS: Indicates that the test case passed
- FAIL: Indicates that the test case failed
Contributions via issues and pull requests are welcome!
cini uses LGPL-3.0 license. See LICENSE.