
/*-------------------------------------------------------------------------*/
/**
   @file	iniparser.c
   @author	N. Devillard
   @date	Mar 2000
   @version	$Revision: 1.1.1.1 $
   @brief	Parser for ini files.
*/
/*--------------------------------------------------------------------------*/

/*
	$Id: iniparser.c,v 1.1.1.1 2006/03/07 08:56:41 rosenauer Exp $
	$Author: rosenauer $
	$Date: 2006/03/07 08:56:41 $
	$Revision: 1.1.1.1 $
*/

/*---------------------------------------------------------------------------
   								Includes
 ---------------------------------------------------------------------------*/

#include "iniparser.h"
#include "strlib.h"

#ifdef _ECLIPSE_
#include "static_sz.h"
#else
#define ASCIILINESZ	1024
#endif


/*---------------------------------------------------------------------------
						Private to this module
 ---------------------------------------------------------------------------*/

/* Private: add an entry to the dictionary */
static void iniparser_add_entry(
	dictionary * d,
	char * sec,
	char * key,
	char * val)
{
	char		longkey[2*ASCIILINESZ+1];

	/* Make a key as section:keyword */
	if (key!=NULL) {
		sprintf(longkey, "%s:%s", sec, key);
	} else {
		strcpy(longkey, sec);
	}

	/* Add (key,val) to dictionary */
	dictionary_set(d, longkey, val);
	return ;
}


/*-------------------------------------------------------------------------*/
/**
  @brief	Get number of sections in a dictionary
  @param    d   Dictionary to examine
  @return   int Number of sections found in dictionary

  This function returns the number of sections found in a dictionary.
  The test to recognize sections is done on the string stored in the
  dictionary: a section name is given as "section" whereas a key is
  stored as "section:key", thus the test looks for entries that do not
  contain a colon.

  This clearly fails in the case a section name contains a colon, but
  this should simply be avoided.

  This function returns -1 in case of error.
 */
/*--------------------------------------------------------------------------*/

int iniparser_getnsec(dictionary * d)
{
    int i ;
    int nsec ;

    if (d==NULL) return -1 ;
    nsec=0 ;
    for (i=0 ; i<d->n ; i++) {
        if (strchr(d->key[i], ':')==NULL) {
            nsec ++ ;
        }
    }
    return nsec ;
}


/*-------------------------------------------------------------------------*/
/**
  @brief	Get name for section n in a dictionary.
  @param    d   Dictionary to examine
  @param    n   Section number (from 0 to nsec-1).
  @return   Pointer to char string

  This function locates the n-th section in a dictionary and returns
  its name as a pointer to a string statically allocated inside the
  dictionary. Do not free or modify the returned string!

  This function returns NULL in case of error.
 */
/*--------------------------------------------------------------------------*/

char * iniparser_getsecname(dictionary * d, int n)
{
    int i ;
    int foundsec ;

    if (d==NULL || n<0) return NULL ;
    foundsec=0 ;
    for (i=0 ; i<d->n ; i++) {
        if (strchr(d->key[i], ':')==NULL) {
            foundsec++ ;
            if (foundsec>n)
                break ;
        }
    }
    if (foundsec<=n) {
        return NULL ;
    }
    return d->key[i] ;
}


/*-------------------------------------------------------------------------*/
/**
  @brief	Dump a dictionary to an opened file pointer.
  @param	d	Dictionary to dump.
  @param	f	Opened file pointer to dump to.
  @return	void

  This function prints out the contents of a dictionary, one element by
  line, onto the provided file pointer. It is OK to specify @c stderr
  or @c stdout as output files. This function is meant for debugging
  purposes mostly.
 */
/*--------------------------------------------------------------------------*/
void iniparser_dump(dictionary * d, FILE * f)
{
	int		i ;

	if (d==NULL || f==NULL) return ;
	for (i=0 ; i<d->n ; i++) {
		if (d->val[i]!=NULL) {
			fprintf(f, "[%s]=[%s]\n", d->key[i], d->val[i]);
		} else {
			fprintf(f, "[%s]=UNDEF\n", d->key[i]);
		}
	}
	return ;
}


/*-------------------------------------------------------------------------*/
/**
  @brief	Dump a dictionary to an opened file pointer.
  @param	d	Dictionary to dump.
  @param	f	Opened file pointer to dump to.
  @return	void

  This function prints out the contents of a dictionary, one element by
  line, onto the provided file pointer. It is OK to specify @c stderr or
  @c stdout as output files.

  The output syntax is fully compatible with lua tables.
 */
/*--------------------------------------------------------------------------*/
void iniparser_dump_lua(dictionary * d, FILE * f)
{
	int		i ;
	char	lua_s[ASCIILINESZ+1];
	int		j ;

	if (d==NULL || f==NULL) return ;
	fprintf(f, "ini={}\n");
	for (i=0 ; i<d->n ; i++) {
		memset(lua_s, 0, ASCIILINESZ+1);
		strcpy(lua_s, d->key[i]);
		j=0 ;
		while (lua_s[j]) {
			if (lua_s[j]==':' || lua_s[j]=='/')
				lua_s[j] = '.' ;
			j++ ;
		}
		if (d->val[i]!=NULL) {
			fprintf(f, "ini.%s='%s'\n", lua_s, d->val[i]);
		} else {
			fprintf(f, "ini.%s={}\n", lua_s);
		}
	}
	return ;
}



/*-------------------------------------------------------------------------*/
/**
  @brief	Save a dictionary to a loadable ini file
  @param    d   Dictionary to dump
  @param    f   Opened file pointer to dump to
  @return   void

  This function dumps a given dictionary into a loadable ini file.
  It is Ok to specify @c stderr or @c stdout as output files.
 */
/*--------------------------------------------------------------------------*/

void iniparser_dump_ini(dictionary * d, FILE * f)
{
    int     i, j ;
    char    keym[ASCIILINESZ+1];
    int     nsec ;
    char *  secname ;
    int     seclen ;

    if (d==NULL || f==NULL) return ;

    nsec = iniparser_getnsec(d);
    if (nsec<1) {
        /* No section in file: dump all keys as they are */
        for (i=0 ; i<d->n ; i++) {
            fprintf(f, "%s = %s\n", d->key[i], d->val[i]);
        }
        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->n ; j++) {
            if (!strncmp(d->key[j], keym, seclen+1)) {
                fprintf(f, "%-30s = %s\n", d->key[j]+seclen+1, d->val[j]);
            }
        }
    }
    fprintf(f, "\n");
    return ;
}



/*-------------------------------------------------------------------------*/
/**
  @brief	Get the string associated to a key
  @param	d Dictionary to search
  @param	key Key string to look for
  @return	pointer to statically allocated character string

  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, a
  NULL value is returned. The returned char pointer is pointing to a
  statically allocated string in this module.
 */
/*--------------------------------------------------------------------------*/


char * iniparser_getstr(dictionary * d, char * key)
{
	return dictionary_get(d, strlwc(key));
}



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


int iniparser_getint(dictionary * d, char * key, int notfound)
{
	char	*	str ;

	str = iniparser_getstr(d, key);
	if (str==NULL) return notfound ;
	return atoi(str);
}


/*-------------------------------------------------------------------------*/
/**
  @brief	Get the string associated to a key, convert to a double
  @param	d Dictionary to search
  @param	key Key string to look for
  @param	notFound Value to return in case of error
  @return	double

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


double iniparser_getdouble(dictionary * d, char * key, double notfound)
{
	char	*	str ;

	str = iniparser_getstr(d, key);
	if (str==NULL) return notfound ;
	return atof(str);
}



/*-------------------------------------------------------------------------*/
/**
  @brief	Get the string associated to a key, convert to a boolean
  @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.

  A true boolean is found if one of the following is matched:

  - A string starting with 'y'
  - A string starting with 'Y'
  - A string starting with 't'
  - A string starting with 'T'
  - A string starting with '1'

  A false boolean is found if one of the following is matched:

  - A string starting with 'n'
  - A string starting with 'N'
  - A string starting with 'f'
  - A string starting with 'F'
  - A string starting with '0'

  The notFound value returned if no boolean is identified, does not
  necessarily have to be 0 or 1.
 */
/*--------------------------------------------------------------------------*/


int iniparser_getboolean(dictionary * d, char * key, int notfound)
{
	char	*	c ;
	int			ret ;

	c = iniparser_getstr(d, key);
	if (c==NULL) 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') {
        ret = 0 ;
    } else {
        ret = notfound ;
    }
    return ret;
}


/*-------------------------------------------------------------------------*/
/**
  @brief	Finds out if a given entry exists in a dictionary
  @param	ini 	Dictionary to search
  @param	entry 	Name of the entry to look for
  @return	integer 1 if entry exists, 0 otherwise

  Finds out if a given entry exists in the dictionary. Since sections
  are stored as keys with NULL associated values, this is the only way
  of querying for the presence of sections in a dictionary.
 */
/*--------------------------------------------------------------------------*/

int iniparser_find_entry(
	dictionary	*	ini,
	char		*	entry
)
{
	int			i ;
	unsigned	hash ;
	int			found ;
    char    *   lc_entry ;

	found = 0 ;
    lc_entry = strlwc(entry);
	hash = dictionary_hash(lc_entry);
	for (i=0 ; i<ini->n ; i++) {
		if (hash==ini->hash[i]) {
			if (!strcmp(lc_entry, ini->key[i])) {
				found = 1 ;
				break ;
			}
		}
	}
	return found ;
}



/*-------------------------------------------------------------------------*/
/**
  @brief	Set an entry in a dictionary.
  @param	ini		Dictionary to modify.
  @param	entry	Entry to modify (entry name)
  @param	val		New value to associate to the entry.
  @return	int 0 if Ok, -1 otherwise.

  If the given entry can be found in the dictionary, it is modified to
  contain the provided value. If it cannot be found, -1 is returned.
  It is Ok to set val to NULL.
 */
/*--------------------------------------------------------------------------*/

int iniparser_setstr(
	dictionary	*	ini,
	char		*	entry,
	char		*	val
)
{
	dictionary_set(ini, strlwc(entry), val);
	return 0 ;
}


/*-------------------------------------------------------------------------*/
/**
  @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(char * ininame)
{
	dictionary	*	d ;
	char		lin[ASCIILINESZ+1];
	char		sec[ASCIILINESZ+1];
	char		key[ASCIILINESZ+1];
	char		val[ASCIILINESZ+1];
	char	*	where ;
	FILE	*	ini ;
	int			lineno ;

	if ((ini=fopen(ininame, "r"))==NULL) {
		return NULL ;
	}

    sec[0]=0;

	/*
	 * Initialize a new dictionary entry
	 */
	d = dictionary_new(0);
	lineno = 0 ;
	while (fgets(lin, ASCIILINESZ, ini)!=NULL) {
		lineno++ ;
		where = strskp(lin); /* Skip leading spaces */
		if (*where==';' || *where=='#' || *where==0)
			continue ; /* Comment lines */
		else {
			if (sscanf(where, "[%[^]]", sec)==1) {
				/* Valid section name */
				strcpy(sec, strlwc(sec));
				iniparser_add_entry(d, sec, NULL, NULL);
			} else if (sscanf (where, "%[^=] = \"%[^\"]\"", key, val) == 2
				   ||  sscanf (where, "%[^=] = '%[^\']'",   key, val) == 2
				   ||  sscanf (where, "%[^=] = %[^;#]",     key, val) == 2) {
				strcpy(key, strlwc(strcrop(key)));
				/*
				 * sscanf cannot handle "" or '' as empty value,
				 * this is done here
				 */
				if (!strcmp(val, "\"\"") || !strcmp(val, "''")) {
					val[0] = (char)0;
				} else {
					strcpy(val, strcrop(val));
				}
				iniparser_add_entry(d, sec, key, val);
			}
		}
	}
	fclose(ini);
	return d ;
}



/*-------------------------------------------------------------------------*/
/**
  @brief	Free all memory associated to an ini dictionary
  @param	d Dictionary to free
  @return	void

  Free all memory associated to an ini dictionary.
  It is mandatory to call this function before the dictionary object
  gets out of the current context.
 */
/*--------------------------------------------------------------------------*/

void iniparser_freedict(dictionary * d)
{
	dictionary_del(d);
}

/* vim: set ts=4 et sw=4 tw=75 */
