9 #ifndef INI_COMMENT_CHAR
10 #define INI_COMMENT_CHAR ('#')
16 #ifndef INI_ALLOW_MULTILINE
17 #define INI_ALLOW_MULTILINE 1
23 #define INI_ALLOW_BOM 1
28 #define INI_USE_STACK 0
32 #ifndef INI_STOP_ON_FIRST_ERROR
33 #define INI_STOP_ON_FIRST_ERROR 0
38 #define INI_MAX_LINE 1024
41 #define MAX_SECTION 50
51 static char* rstrip(
char* s)
53 char* p = s + strlen(s);
54 while (p > s && isspace((
unsigned char)(*--p)))
60 static char* lskip(
const char* s)
62 while (*s && isspace((
unsigned char)(*s)))
70 static char* find_char_or_comment(
const char* s,
char c)
72 int was_whitespace = 0;
73 while (*s && *s !=
c && !(was_whitespace && *s == INI_COMMENT_CHAR)) {
74 was_whitespace = isspace((
unsigned char)(*s));
81 static char* strncpy0(
char* dest,
const char* src,
size_t size)
83 strncpy(dest, src, size);
84 dest[size - 1] =
'\0';
89 int (*handler)(
void*,
const char*,
const char*,
const char*),
95 char line[INI_MAX_LINE];
99 char section[MAX_SECTION] =
"";
100 char prev_name[MAX_NAME] =
"";
110 line = (
char*)malloc(INI_MAX_LINE);
117 while (fgets(line, INI_MAX_LINE, file) != NULL) {
122 if (lineno == 1 && (
unsigned char)start[0] == 0xEF &&
123 (
unsigned char)start[1] == 0xBB &&
124 (
unsigned char)start[2] == 0xBF) {
128 start = lskip(rstrip(start));
130 if (*start == INI_COMMENT_CHAR || *start ==
'#') {
133 #if INI_ALLOW_MULTILINE
134 else if (*prev_name && *start && start > line) {
137 if (!handler(user, section, prev_name, start) && !error)
141 else if (*start ==
'[') {
143 end = find_char_or_comment(start + 1,
']');
146 strncpy0(section, start + 1,
sizeof(section));
154 else if (*start && *start != INI_COMMENT_CHAR) {
156 end = find_char_or_comment(start,
'=');
158 end = find_char_or_comment(start,
':');
160 if (*end ==
'=' || *end ==
':') {
162 name = rstrip(start);
163 value = lskip(end + 1);
164 end = find_char_or_comment(value,
'\0');
165 if (*end == INI_COMMENT_CHAR)
170 strncpy0(prev_name, name,
sizeof(prev_name));
171 if (!handler(user, section, name, value) && !error)
180 #if INI_STOP_ON_FIRST_ERROR
194 int (*handler)(
void*,
const char*,
const char*,
const char*),
201 file = fopen(filename,
"r");
constexpr units::realT c()
The speed of light.
int ini_parse_file(FILE *file, int(*handler)(void *, const char *, const char *, const char *), void *user)
Parse ini file.
int ini_parse(const char *filename, int(*handler)(void *, const char *, const char *, const char *), void *user)
Parse given INI-style file.
The inih ini-style, file parser modified for mxlib.