From b1170c9568313dc829b9a98d79d21dc7b894aec4 Mon Sep 17 00:00:00 2001 From: "benhoyt@gmail.com" Date: Fri, 24 Jun 2011 18:48:58 +0000 Subject: [PATCH] Issue 7: Factored out ini_parse_file() so users can parse INI file with just a FILE* instead of a filename, and use fmemopen. Forgot to commit examples/test.ini in previous commit. Fix strncmp redefine warning when compiling tests/unittest.c. --- examples/test.ini | 3 ++- ini.c | 30 ++++++++++++++++++++---------- ini.h | 7 +++++++ tests/unittest.c | 2 +- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/examples/test.ini b/examples/test.ini index 216ea0a..e40dc3e 100644 --- a/examples/test.ini +++ b/examples/test.ini @@ -1,4 +1,4 @@ -; Test config file for ini_test.c +; Test config file for ini_example.c and INIReaderTest.cpp [Protocol] ; Protocol configuration Version=6 ; IPv6 @@ -6,3 +6,4 @@ 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 diff --git a/ini.c b/ini.c index a1ef32a..a4bade8 100644 --- a/ini.c +++ b/ini.c @@ -56,16 +56,16 @@ static char* strncpy0(char* dest, const char* src, size_t size) } /* See documentation in header file. */ -int ini_parse(const char* filename, - int (*handler)(void*, const char*, const char*, const char*), - void* user) +int ini_parse_file(FILE* file, + int (*handler)(void*, const char*, const char*, + const char*), + void* user) { /* Uses a fair bit of stack (use heap instead if you need to) */ char line[MAX_LINE]; char section[MAX_SECTION] = ""; char prev_name[MAX_NAME] = ""; - FILE* file; char* start; char* end; char* name; @@ -73,10 +73,6 @@ int ini_parse(const char* filename, int lineno = 0; int error = 0; - file = fopen(filename, "r"); - if (!file) - return -1; - /* Scan through file line by line */ while (fgets(line, sizeof(line), file) != NULL) { lineno++; @@ -134,7 +130,21 @@ int ini_parse(const char* filename, } } - fclose(file); - + return error; +} + +/* See documentation in header file. */ +int ini_parse(const char* filename, + int (*handler)(void*, const char*, const char*, const char*), + 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; } diff --git a/ini.h b/ini.h index 0bfccd6..d6869d4 100644 --- a/ini.h +++ b/ini.h @@ -32,6 +32,13 @@ int ini_parse(const char* filename, const char* name, const char* value), 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, + int (*handler)(void* user, const char* section, + const char* name, const char* value), + 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. */ diff --git a/tests/unittest.c b/tests/unittest.c index 6d5d4ee..4859ab0 100644 --- a/tests/unittest.c +++ b/tests/unittest.c @@ -14,7 +14,7 @@ tcc -DINI_ALLOW_MULTILINE=0 ../ini.c -run unittest.c > baseline_single.txt #include #include - +#include #include "../ini.h" int User;