| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

ini_dyn_load

#include "sflini.h"
SYMTAB *
ini_dyn_load (
    SYMTAB *load_symtab,
    const char *filename)

Synopsis

Loads the contents of an .ini file into a symbol table. If no symbol table is specified, creates a new symbol table. The ini file data is loaded as a set of symbols and values, where the symbol name is built from the section name and keyword like this: "section:keyword". The symbol name is always stored in lowercase, with no trailing spaces. If the same keyword occurs several times in a section, earlier symbols are overwritten. Ignores all comments and blank lines. Returns NULL if there is not enough memory. Stores these control variables in the symbol table if the table was freshly created or the file was loaded:
filename Name of input file
filetime Time of input file, as 8-digit string "HHMMSSCC"
filedate Date of input file, as 8-digit string "YYYYMMDD"
Also creates a symbol for each section, with name equal to the section name, and value equal to an empty string. Looks for the .ini file on the current PATH. The table is sorted after loading.

Source Code - (sflini.c)

{
    FILE
        *inifile;
    SYMTAB
        *symtab,                        /*  Symbol table to populate         */
        *envtab;                        /*  Environment, as symbol table     */
    char
        *section = NULL,                /*  Filled as we scan through        */
        *keyword = NULL,                /*    the ini file                   */
        *value   = NULL,
        *fromptr,
        *toptr,
        *section_end;                   /*  Null byte at end of section      */

    ASSERT (filename);
    inifile = file locate ("PATH", filename, NULL);

    if (load_symtab)                    /*  Use specified symbol table       */
        symtab = load_symtab;           /*    or create a new one            */
    else
      {
        symtab = sym create table ();
        if (symtab == NULL)
            return (NULL);              /*  Quit if insufficient memory      */
      }
    /*  Store control variables in symbol table                              */
    if (inifile || load_symtab == NULL)
      {
        sym assume symbol (symtab, "filename", filename);
        snprintf (iniline, sizeof (iniline), "%ld",
	 	           timer to date (get file time (filename)));
        sym assume symbol (symtab, "filedate", iniline);
        snprintf (iniline, sizeof (iniline), "%ld",
		           timer to time (get file time (filename)));
        sym assume symbol (symtab, "filetime", iniline);
      }
    if (!inifile)
        return (symtab);                /*  File not found; empty table      */

    /*  Now load the ini file, starting from the beginning                   */
    envtab = env2symb ();
    fseek (inifile, 0, SEEK_SET);
    FOREVER
      {
        if (ini scan section (inifile, &keyword, &value))
          {
            if (section)
              {
                section_end = strchr (section, '\0');
                ASSERT (section_end);
                xstrcat (section, ":", keyword, NULL);
                value = tok subst (value, envtab);

                /*  Handle value in quotes                                   */
                if (*value == '"')
                  {
                    /*  Unescape value if necessary                          */
                    if (strchr (value, '\\'))
                      {
                        toptr = value;
                        for (fromptr = value; *fromptr; fromptr++)
                          {
                            if (*fromptr == '\\')
                              {
                                fromptr++;
                                if (*fromptr == 'n')
                                    *toptr++ = '\n';
                                else
                                    *toptr++ = *fromptr;
                              }
                            else
                                *toptr++ = *fromptr;
                          }
                        *toptr = '\0';
                      }
                    strlast (value) = '\0';
                    sym assume symbol (symtab, section, value + 1);
                  }
                else
                    sym assume symbol (symtab, section, value);

                mem_strfree (&value);
                *section_end = '\0';
              }
          }
        else
        if (keyword)                    /*  Found new section                */
          {
            section = keyword;
            sym assume symbol (symtab, section, "");
          }
        else
            break;
      }
    file close (inifile);
    sym delete table (envtab);
    sym sort table (symtab, NULL);      /*  Sort table by symbol name        */
    return (symtab);
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation