|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#include "sflini.h"
int
ini_dyn_save (
SYMTAB *symtab,
const char *filename)
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.
{
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? */
char
*colon, /* Points to ':' in symbol name */
*outchar, /* Output character pointer */
*valchar; /* Symbol value character pointer */
ASSERT (filename);
ASSERT (symtab);
/* Copy ini file header to temporary file */
wrkfile = ftmp open (NULL);
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)
{
ftmp close (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);
}
ftmp 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 symbols formatted as key:name */
colon = strrchr (symbol-> name, ':');
if (colon)
{
memcpy (ini_value, symbol-> name, colon - symbol-> name);
ini_value [colon - symbol-> name] = '\0';
strcpy (ini_keyword, colon + 1);
/* 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);
snprintf (iniline, sizeof (iniline), "[%s]", ini_section);
file write (inifile, "");
file write (inifile, iniline);
}
/* We always put quotes around values when writing */
snprintf (iniline, sizeof (iniline), " %s = \"", ini_keyword);
outchar = iniline + strlen (iniline);
for (valchar = symbol-> value; *valchar; valchar++)
{
/* If line is too long, break it */
if (outchar - iniline > 75)
{
*outchar++ = '-';
*outchar++ = '\0';
file write (inifile, iniline);
strclr (iniline);
outchar = iniline;
continue;
}
/* Escape ", \, ( and newlines. We escape ( so that $(xxx)
* in the value is not replaced on input.
*/
if (*valchar == '\n')
{
*outchar++ = '\\';
*outchar++ = 'n';
}
else
if (*valchar == '"'
|| *valchar == '\\'
|| *valchar == '(')
{
*outchar++ = '\\';
*outchar++ = *valchar;
}
else
*outchar++ = *valchar;
}
*outchar++ = '"';
*outchar++ = '\0';
file write (inifile, iniline);
}
}
file close (inifile);
return (count);
}
| | << | < | > | >> |
|