-
/*-------------------------------------------------------------------------*/
/**
@file iniparser.c
@version 3.0
@brief Parser for ini files.
*/
-/*--------------------------------------------------------------------------*/
-/*
- $Id: iniparser.c,v 2.19 2011-03-02 20:15:13 ndevilla Exp $
- $Revision: 2.19 $
- $Date: 2011-03-02 20:15:13 $
-*/
+
/*---------------------------- Includes ------------------------------------*/
#include <ctype.h>
+
#include <atalk/iniparser.h>
+#include <atalk/logger.h>
/*---------------------------- Defines -------------------------------------*/
#define ASCIILINESZ (1024)
/*-------------------------------------------------------------------------*/
/**
- @brief Remove blanks at the beginning and the end of a string.
- @param s String to parse.
- @return ptr to statically allocated string.
+ @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
static char * strstrip(char * s)
{
static char l[ASCIILINESZ+1];
- char * last ;
-
+ 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 ;
+
+ 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 ;
}
/*-------------------------------------------------------------------------*/
This function returns -1 in case of error.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getnsec(dictionary * d)
+int iniparser_getnsec(const dictionary * d)
{
int i ;
int nsec ;
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 ;
int foundsec ;
purposes mostly.
*/
/*--------------------------------------------------------------------------*/
-void iniparser_dump(dictionary * d, FILE * f)
+void iniparser_dump(const dictionary * d, FILE * f)
{
int i ;
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 ;
+ const char * secname ;
int seclen ;
if (d==NULL || f==NULL) return ;
the dictionary, do not free or modify it.
*/
/*--------------------------------------------------------------------------*/
-char * iniparser_getstring(dictionary * d, char *section, char * key, char * def)
+const char * iniparser_getstring(const dictionary * d, const char *section, const char * key, const char * def)
{
- char * sval ;
+ const char * sval ;
if (d==NULL || key==NULL)
return def ;
The returned char pointer a strdup'ed allocated string, so the caller must free it.
*/
/*--------------------------------------------------------------------------*/
-char * iniparser_getstringdup(dictionary * d, char *section, char * key, char * def)
+char * iniparser_getstrdup(const dictionary * d, const char *section, const char * key, const char * def)
{
- char * sval ;
+ const char * sval ;
if (d==NULL || key==NULL)
- return def ;
+ return NULL;
if ((sval = dictionary_get(d, section, key, def)))
return strdup(sval);
Credits: Thanks to A. Becker for suggesting strtol()
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getint(dictionary * d, char *section, char * key, int notfound)
+int iniparser_getint(const dictionary * d, const char *section, const char * key, int notfound)
{
- char * str ;
+ const char * str ;
str = iniparser_getstring(d, section, key, INI_INVALID_KEY);
if (str==INI_INVALID_KEY) return notfound ;
the notfound value is returned.
*/
/*--------------------------------------------------------------------------*/
-double iniparser_getdouble(dictionary * d, char *section, char * key, double notfound)
+double iniparser_getdouble(const dictionary * d, const char *section, const char * key, double notfound)
{
- char * str ;
+ const char * str ;
str = iniparser_getstring(d, section, key, INI_INVALID_KEY);
if (str==INI_INVALID_KEY) return notfound ;
necessarily have to be 0 or 1.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getboolean(dictionary * d, char *section, char * key, int notfound)
+int iniparser_getboolean(const dictionary * d, const char *section, const char * key, int notfound)
{
- char * c ;
+ const char * c ;
int ret ;
c = iniparser_getstring(d, section, key, INI_INVALID_KEY);
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, NULL, INI_INVALID_KEY)!=INI_INVALID_KEY) {
/*-------------------------------------------------------------------------*/
/**
- @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
char * section,
char * key,
char * value)
-{
+{
line_status sta ;
char line[ASCIILINESZ+1];
int len ;
sta = LINE_EMPTY ;
} else if (line[0]=='#' || line[0]==';') {
/* Comment line */
- sta = LINE_COMMENT ;
+ sta = LINE_COMMENT ;
} else if (line[0]=='[' && line[len-1]==']') {
/* Section name */
sscanf(line, "[%[^]]", section);
The returned dictionary must be freed using iniparser_freedict().
*/
/*--------------------------------------------------------------------------*/
-dictionary * iniparser_load(char * ininame)
+dictionary * iniparser_load(const char * ininame)
{
- FILE * in ;
+ FILE *in, *include = NULL, *inifile;
char line [ASCIILINESZ+1] ;
char section [ASCIILINESZ+1] ;
dictionary * dict ;
- if ((in=fopen(ininame, "r"))==NULL) {
- fprintf(stderr, "iniparser: cannot open %s\n", ininame);
+ if ((inifile=fopen(ininame, "r"))==NULL) {
+ LOG(log_error, logtype_default, "iniparser: cannot open \"%s\"", ininame);
return NULL ;
}
dict = dictionary_new(0) ;
if (!dict) {
- fclose(in);
+ fclose(inifile);
return NULL ;
}
memset(val, 0, ASCIILINESZ);
last=0 ;
- while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
+ in = inifile;
+ while (1) {
+ if (fgets(line+last, ASCIILINESZ-last, in) == NULL) {
+ if (include) {
+ fclose(include);
+ include = NULL;
+ in = inifile;
+ continue;
+ }
+ break;
+ }
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);
+ LOG(log_error, logtype_default, "iniparser: input line too long in \"%s\" (lineno: %d)",
+ ininame, lineno);
dictionary_del(dict);
fclose(in);
return NULL ;
last=0 ;
}
switch (iniparser_line(line, section, key, val)) {
- case LINE_EMPTY:
- case LINE_COMMENT:
+ case LINE_EMPTY:
+ case LINE_COMMENT:
break ;
-
- case LINE_SECTION:
- errs = dictionary_set(dict, section, NULL, NULL);
+ case LINE_SECTION:
+ errs = dictionary_set(dict, section, NULL, NULL);
break ;
-
- case LINE_VALUE:
+ case LINE_VALUE:
+ if (strcmp(key, "include") == 0) {
+ if ((include = fopen(val, "r")) == NULL) {
+ LOG(log_error, logtype_default, "iniparser: cannot open \"%s\"", val);
+ continue;
+ }
+ in = include;
+ continue;
+ }
errs = dictionary_set(dict, section, key, val) ;
break ;
-
- case LINE_ERROR:
- fprintf(stderr, "iniparser: syntax error in %s (%d):\n",
- ininame,
- lineno);
- fprintf(stderr, "-> %s\n", line);
+ case LINE_ERROR:
+ LOG(log_error, logtype_default, "iniparser: syntax error in %s (lineno: %d): %s",
+ ininame, lineno, line);
errs++ ;
break;
-
- default:
+ default:
break ;
}
memset(line, 0, ASCIILINESZ);
last=0;
if (errs<0) {
- fprintf(stderr, "iniparser: memory allocation failure\n");
+ LOG(log_error, logtype_default, "iniparser: memory allocation failure");
break ;
}
}