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

 

ini_dyn_save

#include "sflini.h"
int
ini_dyn_save (
    SYMTAB *symtab,
    const char *filename)

Synopsis

Saves a symbol table to the specified file. The symbol table entries must be formatted as "section:name=value" - see ini dyn load(). Scans the ini file for a line containing only "#*END", then writes the symbol data to the file from that point. Returns the number of symbols saved, or -1 if there was an error. As a side-effect, sorts the table on the symbol name.

Source Code - (sflini.c)

{
    FILE
        *inifile,
        *wrkfile;
    SYMBOL
        *symbol;                        /*  Next symbol in table             */
    Bool
        header_found;                   /*  Did we find a file header?       */
    int
        count;                          /*  How many symbols did we save?    */

    ASSERT (filename);
    ASSERT (symtab);

    /*  Copy ini file header to temporary file                               */
    wrkfile = tmpfile ();
    header_found = FALSE;
    if ((inifile = file open (filename, 'r')) != NULL)
      {
        while (file read (inifile, iniline))
          {
            if (streq (iniline, "#*END"))
              {
                header_found = TRUE;
                break;
              }
            file write (wrkfile, iniline);
          }
        file close (inifile);
      }
    /*  Now rewrite ini file                                                 */
    if ((inifile = file open (filename, 'w')) == NULL)
      {
        fclose (wrkfile);
        return (-1);                    /*  No permission to write file      */
      }
    if (header_found)
      {
        fseek (wrkfile, 0, SEEK_SET);
        while (file read (wrkfile, iniline))
            file write (inifile, iniline);
      }
    file close (wrkfile);               /*  Finished with temporary file     */

    /*  Output ini file values                                               */
    file write (inifile, "#*END");
    strclr (ini_section);               /*  Current section                  */
    count = 0;

    sym sort table (symtab, NULL);      /*  Sort table by symbol name        */
    for (symbol = symtab-> symbols; symbol; symbol = symbol-> next)
      {
        /*  Output only symbols formatted as key:name                        */
        if (sscanf (symbol-> name, "%[^:]:%s", ini_value, ini_keyword) == 2)
          {
            /*  If we start a new section, output the section header         */
            *ini_value   = toupper (*ini_value);
            *ini_keyword = toupper (*ini_keyword);
            if (strneq (ini_section, ini_value))
              {
                strcpy (ini_section, ini_value);
                sprintf (iniline, "[%s]", ini_section);
                file write (inifile, "");
                file write (inifile, iniline);
              }
            if (strnull (symbol-> value))
                sprintf (iniline, "    %s=\"\"", ini_keyword);
            else
            if (strpbrk (symbol-> value, ";#="))
                sprintf (iniline, "    %s=\"%s\"", ini_keyword, symbol-> value);
            else
                sprintf (iniline, "    %s=%s", ini_keyword, symbol-> value);
            file write (inifile, iniline);
          }
      }
    file close (inifile);
    return (count);
}

| << | < | > | >> iMatix Copyright © 1996-98 iMatix