pax_global_header00006660000000000000000000000064131135362320014511gustar00rootroot0000000000000052 comment=56edbbbef9ba432521442ee47ba7d1c8de37e63d inih-r40/000077500000000000000000000000001311353623200124075ustar00rootroot00000000000000inih-r40/LICENSE.txt000066400000000000000000000027461311353623200142430ustar00rootroot00000000000000 The "inih" library is distributed under the New BSD license: Copyright (c) 2009, Ben Hoyt All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Ben Hoyt nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY BEN HOYT ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BEN HOYT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. inih-r40/README.md000066400000000000000000000120571311353623200136730ustar00rootroot00000000000000**inih (INI Not Invented Here)** is a simple [.INI file](http://en.wikipedia.org/wiki/INI_file) parser written in C. It's only a couple of pages of code, and it was designed to be _small and simple_, so it's good for embedded systems. It's also more or less compatible with Python's [ConfigParser](http://docs.python.org/library/configparser.html) style of .INI files, including RFC 822-style multi-line syntax and `name: value` entries. To use it, just give `ini_parse()` an INI file, and it will call a callback for every `name=value` pair parsed, giving you strings for the section, name, and value. It's done this way ("SAX style") because it works well on low-memory embedded systems, but also because it makes for a KISS implementation. You can also call `ini_parse_file()` to parse directly from a `FILE*` object, `ini_parse_string()` to parse data from a string, or `ini_parse_stream()` to parse using a custom fgets-style reader function for custom I/O. Download a release, browse the source, or read about [how to use inih in a DRY style](http://blog.brush.co.nz/2009/08/xmacros/) with X-Macros. ## Compile-time options ## * **Multi-line entries:** By default, inih supports multi-line entries in the style of Python's ConfigParser. To disable, add `-DINI_ALLOW_MULTILINE=0`. * **UTF-8 BOM:** By default, inih allows a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of INI files. To disable, add `-DINI_ALLOW_BOM=0`. * **Inline comments:** By default, inih allows inline comments with the `;` character. To disable, add `-DINI_ALLOW_INLINE_COMMENTS=0`. You can also specify which character(s) start an inline comment using `INI_INLINE_COMMENT_PREFIXES`. * **Stack vs heap:** By default, inih allocates its line buffer on the stack. To allocate on the heap using `malloc` instead, specify `-DINI_USE_STACK=0`. * **Stop on first error:** By default, inih keeps parsing the rest of the file after an error. To stop parsing on the first error, add `-DINI_STOP_ON_FIRST_ERROR=1`. * **Maximum line length:** The default maximum line length is 200 bytes. To override this, add something like `-DINI_MAX_LINE=1000`. * **Report line numbers:** By default, the `ini_handler` callback doesn't receive the line number as a parameter. If you need that, add `-DINI_HANDLER_LINENO=1`. ## Simple example in C ## ```c #include #include #include #include "../ini.h" typedef struct { int version; const char* name; const char* email; } configuration; static int handler(void* user, const char* section, const char* name, const char* value) { configuration* pconfig = (configuration*)user; #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0 if (MATCH("protocol", "version")) { pconfig->version = atoi(value); } else if (MATCH("user", "name")) { pconfig->name = strdup(value); } else if (MATCH("user", "email")) { pconfig->email = strdup(value); } else { return 0; /* unknown section/name, error */ } return 1; } int main(int argc, char* argv[]) { configuration config; if (ini_parse("test.ini", handler, &config) < 0) { printf("Can't load 'test.ini'\n"); return 1; } printf("Config loaded from 'test.ini': version=%d, name=%s, email=%s\n", config.version, config.name, config.email); return 0; } ``` ## C++ example ## If you're into C++ and the STL, there is also an easy-to-use [INIReader class](https://github.com/benhoyt/inih/blob/master/cpp/INIReader.h) that stores values in a `map` and lets you `Get()` them: ```cpp #include #include "INIReader.h" int main() { INIReader reader("../examples/test.ini"); if (reader.ParseError() < 0) { std::cout << "Can't load 'test.ini'\n"; return 1; } std::cout << "Config loaded from 'test.ini': version=" << reader.GetInteger("protocol", "version", -1) << ", name=" << reader.Get("user", "name", "UNKNOWN") << ", email=" << reader.Get("user", "email", "UNKNOWN") << ", pi=" << reader.GetReal("user", "pi", -1) << ", active=" << reader.GetBoolean("user", "active", true) << "\n"; return 0; } ``` This simple C++ API works fine, but it's not very fully-fledged. I'm not planning to work more on the C++ API at the moment, so if you want a bit more power (for example `GetSections()` and `GetFields()` functions), see these forks: * https://github.com/Blandinium/inih * https://github.com/OSSystems/inih ## Differences from ConfigParser ## Some differences between inih and Python's [ConfigParser](http://docs.python.org/library/configparser.html) standard library module: * INI name=value pairs given above any section headers are treated as valid items with no section (section name is an empty string). In ConfigParser having no section is an error. * Line continuations are handled with leading whitespace on continued lines (like ConfigParser). However, instead of concatenating continued lines together, they are treated as separate values for the same key (unlike ConfigParser). inih-r40/cpp/000077500000000000000000000000001311353623200131715ustar00rootroot00000000000000inih-r40/cpp/INIReader.cpp000066400000000000000000000050501311353623200154370ustar00rootroot00000000000000// Read an INI file into easy-to-access name/value pairs. // inih and INIReader are released under the New BSD license (see LICENSE.txt). // Go to the project home page for more info: // // https://github.com/benhoyt/inih #include #include #include #include "../ini.h" #include "INIReader.h" using std::string; INIReader::INIReader(const string& filename) { _error = ini_parse(filename.c_str(), ValueHandler, this); } int INIReader::ParseError() const { return _error; } string INIReader::Get(const string& section, const string& name, const string& default_value) const { string key = MakeKey(section, name); // Use _values.find() here instead of _values.at() to support pre C++11 compilers return _values.count(key) ? _values.find(key)->second : default_value; } long INIReader::GetInteger(const string& section, const string& name, long default_value) const { string valstr = Get(section, name, ""); const char* value = valstr.c_str(); char* end; // This parses "1234" (decimal) and also "0x4D2" (hex) long n = strtol(value, &end, 0); return end > value ? n : default_value; } double INIReader::GetReal(const string& section, const string& name, double default_value) const { string valstr = Get(section, name, ""); const char* value = valstr.c_str(); char* end; double n = strtod(value, &end); return end > value ? n : default_value; } bool INIReader::GetBoolean(const string& section, const string& name, bool default_value) const { string valstr = Get(section, name, ""); // Convert to lower case to make string comparisons case-insensitive std::transform(valstr.begin(), valstr.end(), valstr.begin(), ::tolower); if (valstr == "true" || valstr == "yes" || valstr == "on" || valstr == "1") return true; else if (valstr == "false" || valstr == "no" || valstr == "off" || valstr == "0") return false; else return default_value; } string INIReader::MakeKey(const string& section, const string& name) { string key = section + "=" + name; // Convert to lower case to make section/name lookups case-insensitive std::transform(key.begin(), key.end(), key.begin(), ::tolower); return key; } int INIReader::ValueHandler(void* user, const char* section, const char* name, const char* value) { INIReader* reader = (INIReader*)user; string key = MakeKey(section, name); if (reader->_values[key].size() > 0) reader->_values[key] += "\n"; reader->_values[key] += value; return 1; } inih-r40/cpp/INIReader.h000066400000000000000000000042541311353623200151110ustar00rootroot00000000000000// Read an INI file into easy-to-access name/value pairs. // inih and INIReader are released under the New BSD license (see LICENSE.txt). // Go to the project home page for more info: // // https://github.com/benhoyt/inih #ifndef __INIREADER_H__ #define __INIREADER_H__ #include #include // Read an INI file into easy-to-access name/value pairs. (Note that I've gone // for simplicity here rather than speed, but it should be pretty decent.) class INIReader { public: // Construct INIReader and parse given filename. See ini.h for more info // about the parsing. INIReader(const std::string& filename); // Return the result of ini_parse(), i.e., 0 on success, line number of // first error on parse error, or -1 on file open error. int ParseError() const; // Get a string value from INI file, returning default_value if not found. std::string Get(const std::string& section, const std::string& name, const std::string& default_value) const; // Get an integer (long) value from INI file, returning default_value if // not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2"). long GetInteger(const std::string& section, const std::string& name, long default_value) const; // Get a real (floating point double) value from INI file, returning // default_value if not found or not a valid floating point value // according to strtod(). double GetReal(const std::string& section, const std::string& name, double default_value) const; // Get a boolean value from INI file, returning default_value if not found or if // not a valid true/false value. Valid true values are "true", "yes", "on", "1", // and valid false values are "false", "no", "off", "0" (not case sensitive). bool GetBoolean(const std::string& section, const std::string& name, bool default_value) const; private: int _error; std::map _values; static std::string MakeKey(const std::string& section, const std::string& name); static int ValueHandler(void* user, const char* section, const char* name, const char* value); }; #endif // __INIREADER_H__ inih-r40/examples/000077500000000000000000000000001311353623200142255ustar00rootroot00000000000000inih-r40/examples/INIReaderExample.cpp000066400000000000000000000012701311353623200200070ustar00rootroot00000000000000// Example that shows simple usage of the INIReader class #include #include "../cpp/INIReader.h" int main() { INIReader reader("../examples/test.ini"); if (reader.ParseError() < 0) { std::cout << "Can't load 'test.ini'\n"; return 1; } std::cout << "Config loaded from 'test.ini': version=" << reader.GetInteger("protocol", "version", -1) << ", name=" << reader.Get("user", "name", "UNKNOWN") << ", email=" << reader.Get("user", "email", "UNKNOWN") << ", pi=" << reader.GetReal("user", "pi", -1) << ", active=" << reader.GetBoolean("user", "active", true) << "\n"; return 0; } inih-r40/examples/config.def000066400000000000000000000002201311353623200161440ustar00rootroot00000000000000// CFG(section, name, default) CFG(protocol, version, "0") CFG(user, name, "Fatty Lumpkin") CFG(user, email, "fatty@lumpkin.com") #undef CFG inih-r40/examples/ini_dump.c000066400000000000000000000017241311353623200162010ustar00rootroot00000000000000/* ini.h example that simply dumps an INI file without comments */ #include #include #include "../ini.h" static int dumper(void* user, const char* section, const char* name, const char* value) { static char prev_section[50] = ""; if (strcmp(section, prev_section)) { printf("%s[%s]\n", (prev_section[0] ? "\n" : ""), section); strncpy(prev_section, section, sizeof(prev_section)); prev_section[sizeof(prev_section) - 1] = '\0'; } printf("%s = %s\n", name, value); return 1; } int main(int argc, char* argv[]) { int error; if (argc <= 1) { printf("Usage: ini_dump filename.ini\n"); return 1; } error = ini_parse(argv[1], dumper, NULL); if (error < 0) { printf("Can't read '%s'!\n", argv[1]); return 2; } else if (error) { printf("Bad config file (first error on line %d)!\n", error); return 3; } return 0; } inih-r40/examples/ini_example.c000066400000000000000000000021371311353623200166660ustar00rootroot00000000000000/* Example: parse a simple configuration file */ #include #include #include #include "../ini.h" typedef struct { int version; const char* name; const char* email; } configuration; static int handler(void* user, const char* section, const char* name, const char* value) { configuration* pconfig = (configuration*)user; #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0 if (MATCH("protocol", "version")) { pconfig->version = atoi(value); } else if (MATCH("user", "name")) { pconfig->name = strdup(value); } else if (MATCH("user", "email")) { pconfig->email = strdup(value); } else { return 0; /* unknown section/name, error */ } return 1; } int main(int argc, char* argv[]) { configuration config; if (ini_parse("test.ini", handler, &config) < 0) { printf("Can't load 'test.ini'\n"); return 1; } printf("Config loaded from 'test.ini': version=%d, name=%s, email=%s\n", config.version, config.name, config.email); return 0; } inih-r40/examples/ini_xmacros.c000066400000000000000000000022251311353623200167050ustar00rootroot00000000000000/* Parse a configuration file into a struct using X-Macros */ #include #include #include "../ini.h" /* define the config struct type */ typedef struct { #define CFG(s, n, default) char *s##_##n; #include "config.def" } config; /* create one and fill in its default values */ config Config = { #define CFG(s, n, default) default, #include "config.def" }; /* process a line of the INI file, storing valid values into config struct */ int handler(void *user, const char *section, const char *name, const char *value) { config *cfg = (config *)user; if (0) ; #define CFG(s, n, default) else if (strcmp(section, #s)==0 && \ strcmp(name, #n)==0) cfg->s##_##n = strdup(value); #include "config.def" return 1; } /* print all the variables in the config, one per line */ void dump_config(config *cfg) { #define CFG(s, n, default) printf("%s_%s = %s\n", #s, #n, cfg->s##_##n); #include "config.def" } int main(int argc, char* argv[]) { if (ini_parse("test.ini", handler, &Config) < 0) printf("Can't load 'test.ini', using defaults\n"); dump_config(&Config); return 0; } inih-r40/examples/test.ini000066400000000000000000000005421311353623200157060ustar00rootroot00000000000000; Test config file for ini_example.c and INIReaderTest.cpp [protocol] ; Protocol configuration version=6 ; IPv6 [user] name = Bob Smith ; Spaces around '=' are stripped email = bob@smith.com ; And comments (like this) ignored active = true ; Test a boolean pi = 3.14159 ; Test a floating point number inih-r40/extra/000077500000000000000000000000001311353623200135325ustar00rootroot00000000000000inih-r40/extra/Makefile.static000066400000000000000000000005001311353623200164530ustar00rootroot00000000000000# Simple makefile to build inih as a static library using g++ SRC = ../ini.c OBJ = $(SRC:.c=.o) OUT = libinih.a INCLUDES = -I.. CCFLAGS = -g -O2 CC = g++ default: $(OUT) .c.o: $(CC) $(INCLUDES) $(CCFLAGS) $(EXTRACCFLAGS) -c $< -o $@ $(OUT): $(OBJ) ar rcs $(OUT) $(OBJ) $(EXTRAARFLAGS) clean: rm -f $(OBJ) $(OUT) inih-r40/ini.c000066400000000000000000000146021311353623200133350ustar00rootroot00000000000000/* inih -- simple .INI file parser inih is released under the New BSD license (see LICENSE.txt). Go to the project home page for more info: https://github.com/benhoyt/inih */ #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS #endif #include #include #include #include "ini.h" #if !INI_USE_STACK #include #endif #define MAX_SECTION 50 #define MAX_NAME 50 /* Used by ini_parse_string() to keep track of string parsing state. */ typedef struct { const char* ptr; size_t num_left; } ini_parse_string_ctx; /* Strip whitespace chars off end of given string, in place. Return s. */ static char* rstrip(char* s) { char* p = s + strlen(s); while (p > s && isspace((unsigned char)(*--p))) *p = '\0'; return s; } /* Return pointer to first non-whitespace char in given string. */ static char* lskip(const char* s) { while (*s && isspace((unsigned char)(*s))) s++; return (char*)s; } /* Return pointer to first char (of chars) or inline comment in given string, or pointer to null at end of string if neither found. Inline comment must be prefixed by a whitespace character to register as a comment. */ static char* find_chars_or_comment(const char* s, const char* chars) { #if INI_ALLOW_INLINE_COMMENTS int was_space = 0; while (*s && (!chars || !strchr(chars, *s)) && !(was_space && strchr(INI_INLINE_COMMENT_PREFIXES, *s))) { was_space = isspace((unsigned char)(*s)); s++; } #else while (*s && (!chars || !strchr(chars, *s))) { s++; } #endif return (char*)s; } /* Version of strncpy that ensures dest (size bytes) is null-terminated. */ static char* strncpy0(char* dest, const char* src, size_t size) { strncpy(dest, src, size); dest[size - 1] = '\0'; return dest; } /* See documentation in header file. */ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler, void* user) { /* Uses a fair bit of stack (use heap instead if you need to) */ #if INI_USE_STACK char line[INI_MAX_LINE]; #else char* line; #endif char section[MAX_SECTION] = ""; char prev_name[MAX_NAME] = ""; char* start; char* end; char* name; char* value; int lineno = 0; int error = 0; #if !INI_USE_STACK line = (char*)malloc(INI_MAX_LINE); if (!line) { return -2; } #endif #if INI_HANDLER_LINENO #define HANDLER(u, s, n, v) handler(u, s, n, v, lineno) #else #define HANDLER(u, s, n, v) handler(u, s, n, v) #endif /* Scan through stream line by line */ while (reader(line, INI_MAX_LINE, stream) != NULL) { lineno++; start = line; #if INI_ALLOW_BOM if (lineno == 1 && (unsigned char)start[0] == 0xEF && (unsigned char)start[1] == 0xBB && (unsigned char)start[2] == 0xBF) { start += 3; } #endif start = lskip(rstrip(start)); if (*start == ';' || *start == '#') { /* Per Python configparser, allow both ; and # comments at the start of a line */ } #if INI_ALLOW_MULTILINE else if (*prev_name && *start && start > line) { /* Non-blank line with leading whitespace, treat as continuation of previous name's value (as per Python configparser). */ if (!HANDLER(user, section, prev_name, start) && !error) error = lineno; } #endif else if (*start == '[') { /* A "[section]" line */ end = find_chars_or_comment(start + 1, "]"); if (*end == ']') { *end = '\0'; strncpy0(section, start + 1, sizeof(section)); *prev_name = '\0'; } else if (!error) { /* No ']' found on section line */ error = lineno; } } else if (*start) { /* Not a comment, must be a name[=:]value pair */ end = find_chars_or_comment(start, "=:"); if (*end == '=' || *end == ':') { *end = '\0'; name = rstrip(start); value = end + 1; #if INI_ALLOW_INLINE_COMMENTS end = find_chars_or_comment(value, NULL); if (*end) *end = '\0'; #endif value = lskip(value); rstrip(value); /* Valid name[=:]value pair found, call handler */ strncpy0(prev_name, name, sizeof(prev_name)); if (!HANDLER(user, section, name, value) && !error) error = lineno; } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = lineno; } } #if INI_STOP_ON_FIRST_ERROR if (error) break; #endif } #if !INI_USE_STACK free(line); #endif return error; } /* See documentation in header file. */ int ini_parse_file(FILE* file, ini_handler handler, void* user) { return ini_parse_stream((ini_reader)fgets, file, handler, user); } /* See documentation in header file. */ int ini_parse(const char* filename, ini_handler handler, void* user) { FILE* file; int error; file = fopen(filename, "r"); if (!file) return -1; error = ini_parse_file(file, handler, user); fclose(file); return error; } /* An ini_reader function to read the next line from a string buffer. This is the fgets() equivalent used by ini_parse_string(). */ static char* ini_reader_string(char* str, int num, void* stream) { ini_parse_string_ctx* ctx = (ini_parse_string_ctx*)stream; const char* ctx_ptr = ctx->ptr; size_t ctx_num_left = ctx->num_left; char* strp = str; char c; if (ctx_num_left == 0 || num < 2) return NULL; while (num > 1 && ctx_num_left != 0) { c = *ctx_ptr++; ctx_num_left--; *strp++ = c; if (c == '\n') break; num--; } *strp = '\0'; ctx->ptr = ctx_ptr; ctx->num_left = ctx_num_left; return str; } /* See documentation in header file. */ int ini_parse_string(const char* string, ini_handler handler, void* user) { ini_parse_string_ctx ctx; ctx.ptr = string; ctx.num_left = strlen(string); return ini_parse_stream((ini_reader)ini_reader_string, &ctx, handler, user); } inih-r40/ini.h000066400000000000000000000072061311353623200133440ustar00rootroot00000000000000/* inih -- simple .INI file parser inih is released under the New BSD license (see LICENSE.txt). Go to the project home page for more info: https://github.com/benhoyt/inih */ #ifndef __INI_H__ #define __INI_H__ /* Make this header file easier to include in C++ code */ #ifdef __cplusplus extern "C" { #endif #include /* Nonzero if ini_handler callback should accept lineno parameter. */ #ifndef INI_HANDLER_LINENO #define INI_HANDLER_LINENO 0 #endif /* Typedef for prototype of handler function. */ #if INI_HANDLER_LINENO typedef int (*ini_handler)(void* user, const char* section, const char* name, const char* value, int lineno); #else typedef int (*ini_handler)(void* user, const char* section, const char* name, const char* value); #endif /* Typedef for prototype of fgets-style reader function. */ typedef char* (*ini_reader)(char* str, int num, void* stream); /* Parse given INI-style file. May have [section]s, name=value pairs (whitespace stripped), and comments starting with ';' (semicolon). Section is "" if name=value pair parsed before any section heading. name:value pairs are also supported as a concession to Python's configparser. For each name=value pair parsed, call handler function with given user pointer as well as section, name, and value (data only valid for duration of handler call). Handler should return nonzero on success, zero on error. Returns 0 on success, line number of first error on parse error (doesn't stop on first error), -1 on file open error, or -2 on memory allocation error (only when INI_USE_STACK is zero). */ int ini_parse(const char* filename, ini_handler handler, void* user); /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't close the file when it's finished -- the caller must do that. */ int ini_parse_file(FILE* file, ini_handler handler, void* user); /* Same as ini_parse(), but takes an ini_reader function pointer instead of filename. Used for implementing custom or string-based I/O (see also ini_parse_string). */ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler, void* user); /* Same as ini_parse(), but takes a zero-terminated string with the INI data instead of a file. Useful for parsing INI data from a network socket or already in memory. */ int ini_parse_string(const char* string, ini_handler handler, void* user); /* Nonzero to allow multi-line value parsing, in the style of Python's configparser. If allowed, ini_parse() will call the handler with the same name for each subsequent line parsed. */ #ifndef INI_ALLOW_MULTILINE #define INI_ALLOW_MULTILINE 1 #endif /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of the file. See http://code.google.com/p/inih/issues/detail?id=21 */ #ifndef INI_ALLOW_BOM #define INI_ALLOW_BOM 1 #endif /* Nonzero to allow inline comments (with valid inline comment characters specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match Python 3.2+ configparser behaviour. */ #ifndef INI_ALLOW_INLINE_COMMENTS #define INI_ALLOW_INLINE_COMMENTS 1 #endif #ifndef INI_INLINE_COMMENT_PREFIXES #define INI_INLINE_COMMENT_PREFIXES ";" #endif /* Nonzero to use stack, zero to use heap (malloc/free). */ #ifndef INI_USE_STACK #define INI_USE_STACK 1 #endif /* Stop parsing on first error (default is to keep parsing). */ #ifndef INI_STOP_ON_FIRST_ERROR #define INI_STOP_ON_FIRST_ERROR 0 #endif /* Maximum line length for any line in INI file. */ #ifndef INI_MAX_LINE #define INI_MAX_LINE 200 #endif #ifdef __cplusplus } #endif #endif /* __INI_H__ */ inih-r40/tests/000077500000000000000000000000001311353623200135515ustar00rootroot00000000000000inih-r40/tests/bad_comment.ini000066400000000000000000000000211311353623200165130ustar00rootroot00000000000000This is an error inih-r40/tests/bad_multi.ini000066400000000000000000000000131311353623200162040ustar00rootroot00000000000000 indented inih-r40/tests/bad_section.ini000066400000000000000000000001071311353623200165220ustar00rootroot00000000000000[section1] name1=value1 [section2 [section3 ; comment ] name2=value2 inih-r40/tests/baseline_disallow_inline_comments.txt000066400000000000000000000024541311353623200232420ustar00rootroot00000000000000no_file.ini: e=-1 user=0 ... [section1] ... one=This is a test ; name=value comment; ... two=1234; ... [ section 2 ] ... happy=4; ... sad=; ... [comment_test] ... test1=1;2;3 ; only this will be a comment; ... test2=2;3;4;this won't be a comment, needs whitespace before ';'; ... test;3=345 ; key should be "test;3"; ... test4=4#5#6 ; '#' only starts a comment at start of line; ... test7=; blank value, except if inline comments disabled; ... test8=; not a comment, needs whitespace before ';'; ... [colon_tests] ... Content-Type=text/html; ... foo=bar; ... adams=42; ... funny1=with = equals; ... funny2=with : colons; ... funny3=two = equals; ... funny4=two : colons; normal.ini: e=0 user=101 ... [section1] ... name1=value1; ... [section3 ; comment ] ... name2=value2; bad_section.ini: e=3 user=102 bad_comment.ini: e=1 user=102 ... [section] ... a=b; ... user=parse_error; ... c=d; user_error.ini: e=3 user=104 ... [section1] ... single1=abc; ... multi=this is a; ... multi=multi-line value; ... single2=xyz; ... [section2] ... multi=a; ... multi=b; ... multi=c; ... [section3] ... single=ghi; ... multi=the quick; ... multi=brown fox; ... name=bob smith ; comment line 1; multi_line.ini: e=0 user=105 bad_multi.ini: e=1 user=105 ... [bom_section] ... bom_name=bom_value; ... key“=value“; bom.ini: e=0 user=107 inih-r40/tests/baseline_handler_lineno.txt000066400000000000000000000025741311353623200211450ustar00rootroot00000000000000no_file.ini: e=-1 user=0 ... [section1] ... one=This is a test; line 3 ... two=1234; line 4 ... [ section 2 ] ... happy=4; line 8 ... sad=; line 9 ... [comment_test] ... test1=1;2;3; line 15 ... test2=2;3;4;this won't be a comment, needs whitespace before ';'; line 16 ... test;3=345; line 17 ... test4=4#5#6; line 18 ... test7=; line 21 ... test8=; not a comment, needs whitespace before ';'; line 22 ... [colon_tests] ... Content-Type=text/html; line 25 ... foo=bar; line 26 ... adams=42; line 27 ... funny1=with = equals; line 28 ... funny2=with : colons; line 29 ... funny3=two = equals; line 30 ... funny4=two : colons; line 31 normal.ini: e=0 user=101 ... [section1] ... name1=value1; line 2 ... name2=value2; line 5 bad_section.ini: e=3 user=102 bad_comment.ini: e=1 user=102 ... [section] ... a=b; line 2 ... user=parse_error; line 3 ... c=d; line 4 user_error.ini: e=3 user=104 ... [section1] ... single1=abc; line 2 ... multi=this is a; line 3 ... multi=multi-line value; line 4 ... single2=xyz; line 5 ... [section2] ... multi=a; line 7 ... multi=b; line 8 ... multi=c; line 9 ... [section3] ... single=ghi; line 11 ... multi=the quick; line 12 ... multi=brown fox; line 13 ... name=bob smith; line 14 multi_line.ini: e=0 user=105 bad_multi.ini: e=1 user=105 ... [bom_section] ... bom_name=bom_value; line 2 ... key“=value“; line 3 bom.ini: e=0 user=107 inih-r40/tests/baseline_multi.txt000066400000000000000000000021231311353623200173040ustar00rootroot00000000000000no_file.ini: e=-1 user=0 ... [section1] ... one=This is a test; ... two=1234; ... [ section 2 ] ... happy=4; ... sad=; ... [comment_test] ... test1=1;2;3; ... test2=2;3;4;this won't be a comment, needs whitespace before ';'; ... test;3=345; ... test4=4#5#6; ... test7=; ... test8=; not a comment, needs whitespace before ';'; ... [colon_tests] ... Content-Type=text/html; ... foo=bar; ... adams=42; ... funny1=with = equals; ... funny2=with : colons; ... funny3=two = equals; ... funny4=two : colons; normal.ini: e=0 user=101 ... [section1] ... name1=value1; ... name2=value2; bad_section.ini: e=3 user=102 bad_comment.ini: e=1 user=102 ... [section] ... a=b; ... user=parse_error; ... c=d; user_error.ini: e=3 user=104 ... [section1] ... single1=abc; ... multi=this is a; ... multi=multi-line value; ... single2=xyz; ... [section2] ... multi=a; ... multi=b; ... multi=c; ... [section3] ... single=ghi; ... multi=the quick; ... multi=brown fox; ... name=bob smith; multi_line.ini: e=0 user=105 bad_multi.ini: e=1 user=105 ... [bom_section] ... bom_name=bom_value; ... key“=value“; bom.ini: e=0 user=107 inih-r40/tests/baseline_single.txt000066400000000000000000000020101311353623200174260ustar00rootroot00000000000000no_file.ini: e=-1 user=0 ... [section1] ... one=This is a test; ... two=1234; ... [ section 2 ] ... happy=4; ... sad=; ... [comment_test] ... test1=1;2;3; ... test2=2;3;4;this won't be a comment, needs whitespace before ';'; ... test;3=345; ... test4=4#5#6; ... test7=; ... test8=; not a comment, needs whitespace before ';'; ... [colon_tests] ... Content-Type=text/html; ... foo=bar; ... adams=42; ... funny1=with = equals; ... funny2=with : colons; ... funny3=two = equals; ... funny4=two : colons; normal.ini: e=0 user=101 ... [section1] ... name1=value1; ... name2=value2; bad_section.ini: e=3 user=102 bad_comment.ini: e=1 user=102 ... [section] ... a=b; ... user=parse_error; ... c=d; user_error.ini: e=3 user=104 ... [section1] ... single1=abc; ... multi=this is a; ... single2=xyz; ... [section2] ... multi=a; ... [section3] ... single=ghi; ... multi=the quick; ... name=bob smith; multi_line.ini: e=4 user=105 bad_multi.ini: e=1 user=105 ... [bom_section] ... bom_name=bom_value; ... key“=value“; bom.ini: e=0 user=107 inih-r40/tests/baseline_stop_on_first_error.txt000066400000000000000000000020701311353623200222540ustar00rootroot00000000000000no_file.ini: e=-1 user=0 ... [section1] ... one=This is a test; ... two=1234; ... [ section 2 ] ... happy=4; ... sad=; ... [comment_test] ... test1=1;2;3; ... test2=2;3;4;this won't be a comment, needs whitespace before ';'; ... test;3=345; ... test4=4#5#6; ... test7=; ... test8=; not a comment, needs whitespace before ';'; ... [colon_tests] ... Content-Type=text/html; ... foo=bar; ... adams=42; ... funny1=with = equals; ... funny2=with : colons; ... funny3=two = equals; ... funny4=two : colons; normal.ini: e=0 user=101 ... [section1] ... name1=value1; bad_section.ini: e=3 user=102 bad_comment.ini: e=1 user=102 ... [section] ... a=b; ... user=parse_error; user_error.ini: e=3 user=104 ... [section1] ... single1=abc; ... multi=this is a; ... multi=multi-line value; ... single2=xyz; ... [section2] ... multi=a; ... multi=b; ... multi=c; ... [section3] ... single=ghi; ... multi=the quick; ... multi=brown fox; ... name=bob smith; multi_line.ini: e=0 user=105 bad_multi.ini: e=1 user=105 ... [bom_section] ... bom_name=bom_value; ... key“=value“; bom.ini: e=0 user=107 inih-r40/tests/baseline_string.txt000066400000000000000000000005411311353623200174620ustar00rootroot00000000000000empty string: e=0 user=0 ... [section] ... foo=bar; ... bazz=buzz quxx; basic: e=0 user=101 ... [section] ... hello=world; ... forty_two=42; crlf: e=0 user=102 ... [sec] ... foo=0123456789012; ... bar=4321; long line: e=3 user=103 ... [sec] ... foo=0123456789012; ... bix=1234; long continued: e=0 user=104 ... [s] ... a=1; ... c=3; error: e=3 user=105 inih-r40/tests/bom.ini000066400000000000000000000000661311353623200150310ustar00rootroot00000000000000[bom_section] bom_name=bom_value key“ = value“ inih-r40/tests/multi_line.ini000066400000000000000000000003721311353623200164150ustar00rootroot00000000000000[section1] single1 = abc multi = this is a multi-line value single2 = xyz [section2] multi = a b c [section3] single: ghi multi: the quick brown fox name = bob smith ; comment line 1 ; comment line 2 inih-r40/tests/normal.ini000066400000000000000000000013551311353623200155460ustar00rootroot00000000000000; This is an INI file [section1] ; section comment one=This is a test ; name=value comment two = 1234 ; x=y [ section 2 ] happy = 4 sad = [empty] ; do nothing [comment_test] test1 = 1;2;3 ; only this will be a comment test2 = 2;3;4;this won't be a comment, needs whitespace before ';' test;3 = 345 ; key should be "test;3" test4 = 4#5#6 ; '#' only starts a comment at start of line #test5 = 567 ; entire line commented # test6 = 678 ; entire line commented, except in MULTILINE mode test7 = ; blank value, except if inline comments disabled test8 =; not a comment, needs whitespace before ';' [colon_tests] Content-Type: text/html foo:bar adams : 42 funny1 : with = equals funny2 = with : colons funny3 = two = equals funny4 : two : colons inih-r40/tests/unittest.bat000066400000000000000000000007121311353623200161200ustar00rootroot00000000000000@call tcc ..\ini.c -I..\ -run unittest.c > baseline_multi.txt @call tcc ..\ini.c -I..\ -DINI_ALLOW_MULTILINE=0 -run unittest.c > baseline_single.txt @call tcc ..\ini.c -I..\ -DINI_ALLOW_INLINE_COMMENTS=0 -run unittest.c > baseline_disallow_inline_comments.txt @call tcc ..\ini.c -I..\ -DINI_STOP_ON_FIRST_ERROR=1 -run unittest.c > baseline_stop_on_first_error.txt @call tcc ..\ini.c -I..\ -DINI_HANDLER_LINENO=1 -run unittest.c > baseline_handler_lineno.txt inih-r40/tests/unittest.c000066400000000000000000000031741311353623200156010ustar00rootroot00000000000000/* inih -- unit tests This works simply by dumping a bunch of info to standard output, which is redirected to an output file (baseline_*.txt) and checked into the Subversion repository. This baseline file is the test output, so the idea is to check it once, and if it changes -- look at the diff and see which tests failed. See unittest.bat and unittest.sh for how to run this (with tcc and gcc, respectively). */ #include #include #include #include "../ini.h" int User; char Prev_section[50]; #if INI_HANDLER_LINENO int dumper(void* user, const char* section, const char* name, const char* value, int lineno) #else int dumper(void* user, const char* section, const char* name, const char* value) #endif { User = *((int*)user); if (strcmp(section, Prev_section)) { printf("... [%s]\n", section); strncpy(Prev_section, section, sizeof(Prev_section)); Prev_section[sizeof(Prev_section) - 1] = '\0'; } #if INI_HANDLER_LINENO printf("... %s=%s; line %d\n", name, value, lineno); #else printf("... %s=%s;\n", name, value); #endif return strcmp(name, "user")==0 && strcmp(value, "parse_error")==0 ? 0 : 1; } void parse(const char* fname) { static int u = 100; int e; *Prev_section = '\0'; e = ini_parse(fname, dumper, &u); printf("%s: e=%d user=%d\n", fname, e, User); u++; } int main(void) { parse("no_file.ini"); parse("normal.ini"); parse("bad_section.ini"); parse("bad_comment.ini"); parse("user_error.ini"); parse("multi_line.ini"); parse("bad_multi.ini"); parse("bom.ini"); return 0; } inih-r40/tests/unittest.sh000077500000000000000000000016521311353623200157730ustar00rootroot00000000000000#!/usr/bin/env bash gcc ../ini.c unittest.c -o unittest_multi ./unittest_multi > baseline_multi.txt rm -f unittest_multi gcc ../ini.c -DINI_ALLOW_MULTILINE=0 unittest.c -o unittest_single ./unittest_single > baseline_single.txt rm -f unittest_single gcc ../ini.c -DINI_ALLOW_INLINE_COMMENTS=0 unittest.c -o unittest_disallow_inline_comments ./unittest_disallow_inline_comments > baseline_disallow_inline_comments.txt rm -f unittest_disallow_inline_comments gcc ../ini.c -DINI_STOP_ON_FIRST_ERROR=1 unittest.c -o unittest_stop_on_first_error ./unittest_stop_on_first_error > baseline_stop_on_first_error.txt rm -f unittest_stop_on_first_error gcc ../ini.c -DINI_HANDLER_LINENO=1 unittest.c -o unittest_handler_lineno ./unittest_handler_lineno > baseline_handler_lineno.txt rm -f unittest_handler_lineno gcc ../ini.c -DINI_MAX_LINE=20 unittest_string.c -o unittest_string ./unittest_string > baseline_string.txt rm -f unittest_string inih-r40/tests/unittest_string.c000066400000000000000000000021671311353623200171700ustar00rootroot00000000000000/* inih -- unit tests for ini_parse_string() */ #include #include #include #include "../ini.h" int User; char Prev_section[50]; int dumper(void* user, const char* section, const char* name, const char* value) { User = *((int*)user); if (strcmp(section, Prev_section)) { printf("... [%s]\n", section); strncpy(Prev_section, section, sizeof(Prev_section)); Prev_section[sizeof(Prev_section) - 1] = '\0'; } printf("... %s=%s;\n", name, value); return 1; } void parse(const char* name, const char* string) { static int u = 100; int e; *Prev_section = '\0'; e = ini_parse_string(string, dumper, &u); printf("%s: e=%d user=%d\n", name, e, User); u++; } int main(void) { parse("empty string", ""); parse("basic", "[section]\nfoo = bar\nbazz = buzz quxx"); parse("crlf", "[section]\r\nhello = world\r\nforty_two = 42\r\n"); parse("long line", "[sec]\nfoo = 01234567890123456789\nbar=4321\n"); parse("long continued", "[sec]\nfoo = 0123456789012bix=1234\n"); parse("error", "[s]\na=1\nb\nc=3"); return 0; } inih-r40/tests/user_error.ini000066400000000000000000000000511311353623200164350ustar00rootroot00000000000000[section] a = b user = parse_error c = d