1 /* inih -- simple .INI file parser
3 inih is released under the New BSD license (see LICENSE.txt). Go to the project
4 home page for more info:
6 http://code.google.com/p/inih/
20 #define MAX_SECTION 50
23 /* Strip whitespace chars off end of given string, in place. Return s. */
24 static char* rstrip(char* s
)
26 char* p
= s
+ strlen(s
);
27 while (p
> s
&& isspace((unsigned char)(*--p
)))
32 /* Return pointer to first non-whitespace char in given string. */
33 static char* lskip(const char* s
)
35 while (*s
&& isspace((unsigned char)(*s
)))
40 /* Return pointer to first char c or ';' comment in given string, or pointer to
41 null at end of string if neither found. ';' must be prefixed by a whitespace
42 character to register as a comment. */
43 static char* find_char_or_comment(const char* s
, char c
)
45 int was_whitespace
= 0;
46 while (*s
&& *s
!= c
&& !(was_whitespace
&& *s
== ';')) {
47 was_whitespace
= isspace((unsigned char)(*s
));
53 /* Version of strncpy that ensures dest (size bytes) is null-terminated. */
54 static char* strncpy0(char* dest
, const char* src
, size_t size
)
56 strncpy(dest
, src
, size
);
57 dest
[size
- 1] = '\0';
61 /* See documentation in header file. */
62 int ini_parse_file(FILE* file
,
63 int (*handler
)(void*, const char*, const char*,
67 /* Uses a fair bit of stack (use heap instead if you need to) */
69 char line
[INI_MAX_LINE
];
73 char section
[MAX_SECTION
] = "";
74 char prev_name
[MAX_NAME
] = "";
84 line
= (char*)malloc(INI_MAX_LINE
);
90 /* Scan through file line by line */
91 while (fgets(line
, INI_MAX_LINE
, file
) != NULL
) {
96 if (lineno
== 1 && (unsigned char)start
[0] == 0xEF &&
97 (unsigned char)start
[1] == 0xBB &&
98 (unsigned char)start
[2] == 0xBF) {
102 start
= lskip(rstrip(start
));
104 if (*start
== ';' || *start
== '#') {
105 /* Per Python ConfigParser, allow '#' comments at start of line */
107 #if INI_ALLOW_MULTILINE
108 else if (*prev_name
&& *start
&& start
> line
) {
109 /* Non-black line with leading whitespace, treat as continuation
110 of previous name's value (as per Python ConfigParser). */
111 if (!handler(user
, section
, prev_name
, start
) && !error
)
115 else if (*start
== '[') {
116 /* A "[section]" line */
117 end
= find_char_or_comment(start
+ 1, ']');
120 strncpy0(section
, start
+ 1, sizeof(section
));
124 /* No ']' found on section line */
128 else if (*start
&& *start
!= ';') {
129 /* Not a comment, must be a name[=:]value pair */
130 end
= find_char_or_comment(start
, '=');
132 end
= find_char_or_comment(start
, ':');
134 if (*end
== '=' || *end
== ':') {
136 name
= rstrip(start
);
137 value
= lskip(end
+ 1);
138 end
= find_char_or_comment(value
, '\0');
143 /* Valid name[=:]value pair found, call handler */
144 strncpy0(prev_name
, name
, sizeof(prev_name
));
145 if (!handler(user
, section
, name
, value
) && !error
)
149 /* No '=' or ':' found on name[=:]value line */
154 #if INI_STOP_ON_FIRST_ERROR
167 /* See documentation in header file. */
168 int ini_parse(const char* filename
,
169 int (*handler
)(void*, const char*, const char*, const char*),
175 file
= fopen(filename
, "r");
178 error
= ini_parse_file(file
, handler
, user
);