aboutsummaryrefslogtreecommitdiff
path: root/lib/libiniparser.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libiniparser.c')
-rw-r--r--lib/libiniparser.c502
1 files changed, 355 insertions, 147 deletions
diff --git a/lib/libiniparser.c b/lib/libiniparser.c
index a6ddcc7..4b21b34 100644
--- a/lib/libiniparser.c
+++ b/lib/libiniparser.c
@@ -3,19 +3,16 @@
/**
@file iniparser.c
@author N. Devillard
- @date Sep 2007
- @version 3.0
@brief Parser for ini files.
*/
/*--------------------------------------------------------------------------*/
-/*
- $Id: iniparser.c,v 2.18 2008-01-03 18:35:39 ndevilla Exp $
- $Revision: 2.18 $
- $Date: 2008-01-03 18:35:39 $
-*/
/*---------------------------- Includes ------------------------------------*/
#include <ctype.h>
-#include <libiniparser.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include "libiniparser.h"
/*---------------------------- Defines -------------------------------------*/
#define ASCIILINESZ (1024)
@@ -38,68 +35,101 @@ typedef enum _line_status_ {
/*-------------------------------------------------------------------------*/
/**
- @brief Convert a string to lowercase.
- @param s String to convert.
- @return ptr to statically allocated string.
-
- This function returns a pointer to a statically allocated string
- containing a lowercased version of the input string. Do not free
- or modify the returned string! Since the returned string is statically
- allocated, it will be modified at each function call (not re-entrant).
+ @brief Convert a string to lowercase.
+ @param in String to convert.
+ @param out Output buffer.
+ @param len Size of the out buffer.
+ @return ptr to the out buffer or NULL if an error occured.
+
+ This function convert a string into lowercase.
+ At most len - 1 elements of the input string will be converted.
*/
/*--------------------------------------------------------------------------*/
-static char * strlwc(const char * s)
+static const char * strlwc(const char * in, char *out, unsigned len)
{
- static char l[ASCIILINESZ+1];
- int i ;
+ unsigned i ;
- if (s==NULL) return NULL ;
- memset(l, 0, ASCIILINESZ+1);
+ if (in==NULL || out == NULL || len==0) return NULL ;
i=0 ;
- while (s[i] && i<ASCIILINESZ) {
- l[i] = (char)tolower((int)s[i]);
+ while (in[i] != '\0' && i < len-1) {
+ out[i] = (char)tolower((int)in[i]);
i++ ;
}
- l[ASCIILINESZ]=(char)0;
- return l ;
+ out[i] = '\0';
+ return out ;
+}
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Duplicate a string
+ @param s String to duplicate
+ @return Pointer to a newly allocated string, to be freed with free()
+
+ This is a replacement for strdup(). This implementation is provided
+ for systems that do not have it.
+ */
+/*--------------------------------------------------------------------------*/
+static char * xstrdup(const char * s)
+{
+ char * t ;
+ size_t len ;
+ if (!s)
+ return NULL ;
+
+ len = strlen(s) + 1 ;
+ t = (char*) malloc(len) ;
+ if (t) {
+ memcpy(t, s, len) ;
+ }
+ return t ;
}
/*-------------------------------------------------------------------------*/
/**
- @brief Remove blanks at the beginning and the end of a string.
- @param s String to parse.
- @return ptr to statically allocated string.
-
- This function returns a pointer to a statically allocated string,
- which is identical to the input string, except that all blank
- characters at the end and the beg. of the string have been removed.
- Do not free or modify the returned string! Since the returned string
- is statically allocated, it will be modified at each function call
- (not re-entrant).
+ @brief Remove blanks at the beginning and the end of a string.
+ @param str String to parse and alter.
+ @return unsigned New size of the string.
*/
/*--------------------------------------------------------------------------*/
-static char * strstrip(char * s)
+static unsigned strstrip(char * s)
{
- static char l[ASCIILINESZ+1];
- char * last ;
-
- if (s==NULL) return NULL ;
-
- while (isspace((int)*s) && *s) s++;
- memset(l, 0, ASCIILINESZ+1);
- strcpy(l, s);
- last = l + strlen(l);
- while (last > l) {
- if (!isspace((int)*(last-1)))
- break ;
- last -- ;
- }
- *last = (char)0;
- return (char*)l ;
+ char *last = NULL ;
+ char *dest = s;
+
+ if (s==NULL) return 0;
+
+ last = s + strlen(s);
+ while (isspace((int)*s) && *s) s++;
+ while (last > s) {
+ if (!isspace((int)*(last-1)))
+ break ;
+ last -- ;
+ }
+ *last = (char)0;
+
+ memmove(dest,s,last - s + 1);
+ return last - s;
}
/*-------------------------------------------------------------------------*/
/**
+ @brief Default error callback for iniparser: wraps `fprintf(stderr, ...)`.
+ */
+/*--------------------------------------------------------------------------*/
+static int default_error_callback(const char *format, ...)
+{
+ int ret;
+ va_list argptr;
+ va_start(argptr, format);
+ ret = vfprintf(stderr, format, argptr);
+ va_end(argptr);
+ return ret;
+}
+
+static int (*iniparser_error_callback)(const char*, ...) = default_error_callback;
+
+/*-------------------------------------------------------------------------*/
+/**
@brief Get number of sections in a dictionary
@param d Dictionary to examine
@return int Number of sections found in dictionary
@@ -116,9 +146,9 @@ static char * strstrip(char * s)
This function returns -1 in case of error.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getnsec(dictionary * d)
+int iniparser_getnsec(const dictionary * d)
{
- int i ;
+ size_t i ;
int nsec ;
if (d==NULL) return -1 ;
@@ -147,9 +177,9 @@ int iniparser_getnsec(dictionary * d)
This function returns NULL in case of error.
*/
/*--------------------------------------------------------------------------*/
-char * iniparser_getsecname(dictionary * d, int n)
+const char * iniparser_getsecname(const dictionary * d, int n)
{
- int i ;
+ size_t i ;
int foundsec ;
if (d==NULL || n<0) return NULL ;
@@ -182,9 +212,9 @@ char * iniparser_getsecname(dictionary * d, int n)
purposes mostly.
*/
/*--------------------------------------------------------------------------*/
-void iniparser_dump(dictionary * d, FILE * f)
+void iniparser_dump(const dictionary * d, FILE * f)
{
- int i ;
+ size_t i ;
if (d==NULL || f==NULL) return ;
for (i=0 ; i<d->size ; i++) {
@@ -199,6 +229,26 @@ void iniparser_dump(dictionary * d, FILE * f)
return ;
}
+static void escape_value(char *escaped, char *value) {
+ char c;
+ int v = 0;
+ int e = 0;
+
+ if(!escaped || !value)
+ return;
+
+ while((c = value[v]) != '\0') {
+ if(c == '\\' || c == '"') {
+ escaped[e] = '\\';
+ e++;
+ }
+ escaped[e] = c;
+ v++;
+ e++;
+ }
+ escaped[e] = '\0';
+}
+
/*-------------------------------------------------------------------------*/
/**
@brief Save a dictionary to a loadable ini file
@@ -210,13 +260,12 @@ void iniparser_dump(dictionary * d, FILE * f)
It is Ok to specify @c stderr or @c stdout as output files.
*/
/*--------------------------------------------------------------------------*/
-void iniparser_dump_ini(dictionary * d, FILE * f)
+void iniparser_dump_ini(const dictionary * d, FILE * f)
{
- int i, j ;
- char keym[ASCIILINESZ+1];
- int nsec ;
- char * secname ;
- int seclen ;
+ size_t i ;
+ size_t nsec ;
+ const char * secname ;
+ char escaped[ASCIILINESZ+1] = "";
if (d==NULL || f==NULL) return ;
@@ -226,24 +275,50 @@ void iniparser_dump_ini(dictionary * d, FILE * f)
for (i=0 ; i<d->size ; i++) {
if (d->key[i]==NULL)
continue ;
- fprintf(f, "%s = %s\n", d->key[i], d->val[i]);
+ escape_value(escaped, d->val[i]);
+ fprintf(f, "%s = \"%s\"\n", d->key[i], escaped);
}
return ;
}
for (i=0 ; i<nsec ; i++) {
secname = iniparser_getsecname(d, i) ;
- seclen = (int)strlen(secname);
- fprintf(f, "\n[%s]\n", secname);
- sprintf(keym, "%s:", secname);
- for (j=0 ; j<d->size ; j++) {
- if (d->key[j]==NULL)
- continue ;
- if (!strncmp(d->key[j], keym, seclen+1)) {
- fprintf(f,
- "%-30s = %s\n",
- d->key[j]+seclen+1,
- d->val[j] ? d->val[j] : "");
- }
+ iniparser_dumpsection_ini(d, secname, f);
+ }
+ fprintf(f, "\n");
+ return ;
+}
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Save a dictionary section to a loadable ini file
+ @param d Dictionary to dump
+ @param s Section name of dictionary to dump
+ @param f Opened file pointer to dump to
+ @return void
+
+ This function dumps a given section of a given dictionary into a loadable ini
+ file. It is Ok to specify @c stderr or @c stdout as output files.
+ */
+/*--------------------------------------------------------------------------*/
+void iniparser_dumpsection_ini(const dictionary * d, const char * s, FILE * f)
+{
+ size_t j ;
+ char keym[ASCIILINESZ+1];
+ int seclen ;
+ char escaped[ASCIILINESZ+1] = "";
+
+ if (d==NULL || f==NULL) return ;
+ if (! iniparser_find_entry(d, s)) return ;
+
+ seclen = (int)strlen(s);
+ fprintf(f, "\n[%s]\n", s);
+ sprintf(keym, "%s:", s);
+ for (j=0 ; j<d->size ; j++) {
+ if (d->key[j]==NULL)
+ continue ;
+ if (!strncmp(d->key[j], keym, seclen+1)) {
+ escape_value(escaped, d->val[j]);
+ fprintf(f, "%-30s = \"%s\"\n", d->key[j]+seclen+1, escaped);
}
}
fprintf(f, "\n");
@@ -265,26 +340,27 @@ void iniparser_dump_ini(dictionary * d, FILE * f)
the dictionary, do not free or modify it.
*/
/*--------------------------------------------------------------------------*/
-char * iniparser_getstring(dictionary * d, const char * key, char * def)
+const char * iniparser_getstring(const dictionary * d, const char * key, const char * def)
{
- char * lc_key ;
- char * sval ;
+ const char * lc_key ;
+ const char * sval ;
+ char tmp_str[ASCIILINESZ+1];
if (d==NULL || key==NULL)
return def ;
- lc_key = strlwc(key);
+ lc_key = strlwc(key, tmp_str, sizeof(tmp_str));
sval = dictionary_get(d, lc_key, def);
return sval ;
}
/*-------------------------------------------------------------------------*/
/**
- @brief Get the string associated to a key, convert to an int
+ @brief Get the string associated to a key, convert to an long int
@param d Dictionary to search
@param key Key string to look for
@param notfound Value to return in case of error
- @return integer
+ @return long integer
This function queries a dictionary for a key. A key as read from an
ini file is given as "section:key". If the key cannot be found,
@@ -305,13 +381,65 @@ char * iniparser_getstring(dictionary * d, const char * key, char * def)
Credits: Thanks to A. Becker for suggesting strtol()
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getint(dictionary * d, const char * key, int notfound)
+long int iniparser_getlongint(const dictionary * d, const char * key, long int notfound)
{
- char * str ;
+ const char * str ;
str = iniparser_getstring(d, key, INI_INVALID_KEY);
- if (str==INI_INVALID_KEY) return notfound ;
- return (int)strtol(str, NULL, 0);
+ if (str==NULL || str==INI_INVALID_KEY) return notfound ;
+ return strtol(str, NULL, 0);
+}
+
+int64_t iniparser_getint64(const dictionary * d, const char * key, int64_t notfound)
+{
+ const char * str ;
+
+ str = iniparser_getstring(d, key, INI_INVALID_KEY);
+ if (str==NULL || str==INI_INVALID_KEY) return notfound ;
+ return strtoimax(str, NULL, 0);
+}
+
+uint64_t iniparser_getuint64(const dictionary * d, const char * key, uint64_t notfound)
+{
+ const char * str ;
+
+ str = iniparser_getstring(d, key, INI_INVALID_KEY);
+ if (str==NULL || str==INI_INVALID_KEY) return notfound ;
+ return strtoumax(str, NULL, 0);
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key, convert to an int
+ @param d Dictionary to search
+ @param key Key string to look for
+ @param notfound Value to return in case of error
+ @return integer
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ the notfound value is returned.
+
+ Supported values for integers include the usual C notation
+ so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
+ are supported. Examples:
+
+ "42" -> 42
+ "042" -> 34 (octal -> decimal)
+ "0x42" -> 66 (hexa -> decimal)
+
+ Warning: the conversion may overflow in various ways. Conversion is
+ totally outsourced to strtol(), see the associated man page for overflow
+ handling.
+
+ Credits: Thanks to A. Becker for suggesting strtol()
+ */
+/*--------------------------------------------------------------------------*/
+int iniparser_getint(const dictionary * d, const char * key, int notfound)
+{
+ return (int)iniparser_getlongint(d, key, notfound);
}
/*-------------------------------------------------------------------------*/
@@ -346,13 +474,13 @@ int iniparser_getint(dictionary * d, const char * key, int notfound)
necessarily have to be 0 or 1.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getboolean(dictionary * d, const char * key, int notfound)
+int iniparser_getboolean(const dictionary * d, const char * key, int notfound)
{
- char * c ;
- int ret ;
+ int ret ;
+ const char * c ;
c = iniparser_getstring(d, key, INI_INVALID_KEY);
- if (c==INI_INVALID_KEY) return notfound ;
+ if (c==NULL || c==INI_INVALID_KEY) return notfound ;
if (c[0]=='y' || c[0]=='Y' || c[0]=='1' || c[0]=='t' || c[0]=='T') {
ret = 1 ;
} else if (c[0]=='n' || c[0]=='N' || c[0]=='0' || c[0]=='f' || c[0]=='F') {
@@ -375,10 +503,7 @@ int iniparser_getboolean(dictionary * d, const char * key, int notfound)
of querying for the presence of sections in a dictionary.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_find_entry(
- dictionary * ini,
- char * entry
-)
+int iniparser_find_entry(const dictionary * ini, const char * entry)
{
int found=0 ;
if (iniparser_getstring(ini, entry, INI_INVALID_KEY)!=INI_INVALID_KEY) {
@@ -397,14 +522,53 @@ int iniparser_find_entry(
If the given entry can be found, it is deleted from the dictionary.
*/
/*--------------------------------------------------------------------------*/
-void iniparser_unset(dictionary * ini, char * entry)
+void iniparser_unset(dictionary * ini, const char * entry)
{
- dictionary_unset(ini, strlwc(entry));
+ char tmp_str[ASCIILINESZ+1];
+ dictionary_unset(ini, strlwc(entry, tmp_str, sizeof(tmp_str)));
+}
+
+static void parse_quoted_value(char *value, char quote) {
+ char c;
+ char *quoted;
+ int q = 0, v = 0;
+ int esc = 0;
+
+ if(!value)
+ return;
+
+ quoted = xstrdup(value);
+
+ if(!quoted) {
+ iniparser_error_callback("iniparser: memory allocation failure\n");
+ goto end_of_value;
+ }
+
+ while((c = quoted[q]) != '\0') {
+ if(!esc) {
+ if(c == '\\') {
+ esc = 1;
+ q++;
+ continue;
+ }
+
+ if(c == quote) {
+ goto end_of_value;
+ }
+ }
+ esc = 0;
+ value[v] = c;
+ v++;
+ q++;
+ }
+end_of_value:
+ value[v] = '\0';
+ free(quoted);
}
/*-------------------------------------------------------------------------*/
/**
- @brief Load a single line from an INI file
+ @brief Load a single line from an INI file
@param input_line Input line, may be concatenated multi-line input
@param section Output space to store section
@param key Output space to store key
@@ -413,38 +577,54 @@ void iniparser_unset(dictionary * ini, char * entry)
*/
/*--------------------------------------------------------------------------*/
static line_status iniparser_line(
- char * input_line,
+ const char * input_line,
char * section,
char * key,
char * value)
{
line_status sta ;
- char line[ASCIILINESZ+1];
- int len ;
+ char * line = NULL;
+ size_t len ;
+ int d_quote;
- strcpy(line, strstrip(input_line));
- len = (int)strlen(line);
+ line = xstrdup(input_line);
+ len = strstrip(line);
sta = LINE_UNPROCESSED ;
if (len<1) {
/* Empty line */
sta = LINE_EMPTY ;
- } else if (line[0]=='#') {
+ } else if (line[0]=='#' || line[0]==';') {
/* Comment line */
sta = LINE_COMMENT ;
} else if (line[0]=='[' && line[len-1]==']') {
- /* Section name */
- sscanf(line, "[%[^]]", section);
- strcpy(section, strstrip(section));
- strcpy(section, strlwc(section));
+ /* Section name without opening square bracket */
+ sscanf(line, "[%[^\n]", section);
+ len = strlen(section);
+ /* Section name without closing square bracket */
+ if(section[len-1] == ']')
+ {
+ section[len-1] = '\0';
+ }
+ strstrip(section);
+ strlwc(section, section, len);
sta = LINE_SECTION ;
- } else if (sscanf (line, "%[^=] = \"%[^\"]\"", key, value) == 2
- || sscanf (line, "%[^=] = '%[^\']'", key, value) == 2
- || sscanf (line, "%[^=] = %[^;#]", key, value) == 2) {
- /* Usual key=value, with or without comments */
- strcpy(key, strstrip(key));
- strcpy(key, strlwc(key));
- strcpy(value, strstrip(value));
+ } else if ((d_quote = sscanf (line, "%[^=] = \"%[^\n]\"", key, value)) == 2
+ || sscanf (line, "%[^=] = '%[^\n]'", key, value) == 2) {
+ /* Usual key=value with quotes, with or without comments */
+ strstrip(key);
+ strlwc(key, key, len);
+ if(d_quote == 2)
+ parse_quoted_value(value, '"');
+ else
+ parse_quoted_value(value, '\'');
+ /* Don't strip spaces from values surrounded with quotes */
+ sta = LINE_VALUE ;
+ } else if (sscanf (line, "%[^=] = %[^;#]", key, value) == 2) {
+ /* Usual key=value without quotes, with or without comments */
+ strstrip(key);
+ strlwc(key, key, len);
+ strstrip(value);
/*
* sscanf cannot handle '' or "" as empty values
* this is done here
@@ -461,56 +641,51 @@ static line_status iniparser_line(
* key=;
* key=#
*/
- strcpy(key, strstrip(key));
- strcpy(key, strlwc(key));
+ strstrip(key);
+ strlwc(key, key, len);
value[0]=0 ;
sta = LINE_VALUE ;
} else {
/* Generate syntax error */
sta = LINE_ERROR ;
}
+
+ free(line);
return sta ;
}
/*-------------------------------------------------------------------------*/
/**
@brief Parse an ini file and return an allocated dictionary object
- @param ininame Name of the ini file to read.
+ @param in File to read.
+ @param ininame Name of the ini file to read (only used for nicer error messages)
@return Pointer to newly allocated dictionary
This is the parser for ini files. This function is called, providing
- the name of the file to be read. It returns a dictionary object that
- should not be accessed directly, but through accessor functions
- instead.
+ the file to be read. It returns a dictionary object that should not
+ be accessed directly, but through accessor functions instead.
The returned dictionary must be freed using iniparser_freedict().
*/
/*--------------------------------------------------------------------------*/
-dictionary * iniparser_load(const char * ininame)
+dictionary * iniparser_load_file(FILE * in, const char * ininame)
{
- FILE * in ;
-
char line [ASCIILINESZ+1] ;
char section [ASCIILINESZ+1] ;
char key [ASCIILINESZ+1] ;
- char tmp [ASCIILINESZ+1] ;
+ char tmp [(ASCIILINESZ * 2) + 2] ;
char val [ASCIILINESZ+1] ;
int last=0 ;
int len ;
int lineno=0 ;
int errs=0;
+ int mem_err=0;
dictionary * dict ;
- if ((in=fopen(ininame, "r"))==NULL) {
- fprintf(stderr, "iniparser: cannot open %s\n", ininame);
- return NULL ;
- }
-
dict = dictionary_new(0) ;
if (!dict) {
- fclose(in);
return NULL ;
}
@@ -523,14 +698,15 @@ dictionary * iniparser_load(const char * ininame)
while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
lineno++ ;
len = (int)strlen(line)-1;
+ if (len<=0)
+ continue;
/* Safety check against buffer overflows */
- if (line[len]!='\n') {
- fprintf(stderr,
- "iniparser: input line too long in %s (%d)\n",
- ininame,
- lineno);
+ if (line[len]!='\n' && !feof(in)) {
+ iniparser_error_callback(
+ "iniparser: input line too long in %s (%d)\n",
+ ininame,
+ lineno);
dictionary_del(dict);
- fclose(in);
return NULL ;
}
/* Get rid of \n and spaces at end of line */
@@ -539,6 +715,9 @@ dictionary * iniparser_load(const char * ininame)
line[len]=0 ;
len-- ;
}
+ if (len < 0) { /* Line was entirely \n and/or spaces */
+ len = 0;
+ }
/* Detect multi-line */
if (line[len]=='\\') {
/* Multi-line value */
@@ -553,19 +732,20 @@ dictionary * iniparser_load(const char * ininame)
break ;
case LINE_SECTION:
- errs = dictionary_set(dict, section, NULL);
+ mem_err = dictionary_set(dict, section, NULL);
break ;
case LINE_VALUE:
sprintf(tmp, "%s:%s", section, key);
- errs = dictionary_set(dict, tmp, val) ;
+ mem_err = dictionary_set(dict, tmp, val);
break ;
case LINE_ERROR:
- fprintf(stderr, "iniparser: syntax error in %s (%d):\n",
- ininame,
- lineno);
- fprintf(stderr, "-> %s\n", line);
+ iniparser_error_callback(
+ "iniparser: syntax error in %s (%d):\n-> %s\n",
+ ininame,
+ lineno,
+ line);
errs++ ;
break;
@@ -574,8 +754,8 @@ dictionary * iniparser_load(const char * ininame)
}
memset(line, 0, ASCIILINESZ);
last=0;
- if (errs<0) {
- fprintf(stderr, "iniparser: memory allocation failure\n");
+ if (mem_err<0) {
+ iniparser_error_callback("iniparser: memory allocation failure\n");
break ;
}
}
@@ -583,10 +763,40 @@ dictionary * iniparser_load(const char * ininame)
dictionary_del(dict);
dict = NULL ;
}
+ return dict ;
+}
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Parse an ini file and return an allocated dictionary object
+ @param ininame Name of the ini file to read.
+ @return Pointer to newly allocated dictionary
+
+ This is the parser for ini files. This function is called, providing
+ the name of the file to be read. It returns a dictionary object that
+ should not be accessed directly, but through accessor functions
+ instead.
+
+ The returned dictionary must be freed using iniparser_freedict().
+ */
+/*--------------------------------------------------------------------------*/
+dictionary * iniparser_load(const char * ininame)
+{
+ FILE * in ;
+ dictionary * dict ;
+
+ if ((in=fopen(ininame, "r"))==NULL) {
+ iniparser_error_callback("iniparser: cannot open %s\n", ininame);
+ return NULL ;
+ }
+
+ dict = iniparser_load_file(in, ininame);
fclose(in);
+
return dict ;
}
+
/*-------------------------------------------------------------------------*/
/**
@brief Free all memory associated to an ini dictionary
@@ -602,5 +812,3 @@ void iniparser_freedict(dictionary * d)
{
dictionary_del(d);
}
-
-/* vim: set ts=4 et sw=4 tw=75 */