sc/ini/README.md

106 lines
2.1 KiB
Markdown
Raw Normal View History

2021-02-07 22:31:04 +03:00
### INI parser
2020-12-27 16:02:35 +03:00
### Overview
2021-02-03 08:09:50 +03:00
- Ini file parser
2020-12-27 16:02:35 +03:00
2021-02-07 22:31:04 +03:00
### Features
2020-12-27 16:02:35 +03:00
2021-02-07 22:31:04 +03:00
### Comment example
2020-12-27 16:02:35 +03:00
```ini
#Comment
;Another comment
[Network]
#This is comment
hostname = github.com #Line comments start with space. Either " ;" or " #"
```
2021-02-07 22:31:04 +03:00
### No section
2020-12-27 16:02:35 +03:00
Possible to use without sections
```ini
key1 = value1 ;Comment x
key2 = value2 ;Comment y
key3 = value3 #Comment z
```
```
- Item 1 : ""(Section), "key1"(Key), "value1"(Value)
- Item 2 : ""(Section), "key2"(Key), "value2"(Value)
- Item 3 : ""(Section), "key3"(Key), "value3"(Value)
```
2021-02-07 22:31:04 +03:00
### Multi-value
2020-12-27 16:02:35 +03:00
Values without keys in the next line will be reported as if belongs to previous
key. Those values should be indented at least with a single space character.
```ini
#Comment
;Another comment
[Network]
#This is comment
hostname = github.com
github.io
github.org
```
```
- Item 1 : "Network"(Section), "hostname"(Key), "github.com"(Value)
- Item 2 : "Network"(Section), "hostname"(Key), "github.io"(Value)
- Item 3 : "Network"(Section), "hostname"(Key), "github.org"(Value)
```
2021-02-07 22:31:04 +03:00
### Usage
2020-12-27 16:02:35 +03:00
```c
#include "sc_ini.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
const char *example_ini = "# My configuration"
"[Network] \n"
"hostname = github.com \n"
"port = 443 \n"
"protocol = https \n"
"repo = any";
int callback(void *arg, int line, const char *section, const char *key,
const char *value)
{
2021-02-06 21:36:18 +03:00
printf("Line(%d), Section(%s), Key(%s), Value(%s) \n", line, section, key,
value);
2020-12-27 16:02:35 +03:00
return 0;
}
void file_example(void)
{
int rc;
2021-02-06 21:36:18 +03:00
// Create example file.
2020-12-27 16:02:35 +03:00
FILE *fp = fopen("my_config.ini", "w+");
fwrite(example_ini, 1, strlen(example_ini), fp);
fclose(fp);
2021-02-06 21:36:18 +03:00
// Parse file
2020-12-27 16:02:35 +03:00
rc = sc_ini_parse_file(NULL, callback, "my_config.ini");
assert(rc == 0);
}
void string_example(void)
{
int rc;
2021-02-06 21:36:18 +03:00
2020-12-27 16:02:35 +03:00
rc = sc_ini_parse_string(NULL, callback, example_ini);
assert(rc == 0);
}
int main(int argc, char *argv[])
{
string_example();
file_example();
}
```