1
0
mirror of https://github.com/benhoyt/inih.git synced 2025-01-28 22:52:54 +08:00

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.
This commit is contained in:
benhoyt@gmail.com 2011-06-24 18:48:58 +00:00
parent 9ec69b2a1d
commit b1170c9568
4 changed files with 30 additions and 12 deletions

View File

@ -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

30
ini.c
View File

@ -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;
}

7
ini.h
View File

@ -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. */

View File

@ -14,7 +14,7 @@ tcc -DINI_ALLOW_MULTILINE=0 ../ini.c -run unittest.c > baseline_single.txt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../ini.h"
int User;