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.
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 @@
[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 @@
}
/* 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 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 @@
}
}
- 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 @@
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 @@
#include <stdio.h>
#include <stdlib.h>
-
+#include <string.h>
#include "../ini.h"
int User;