⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sflini.c

📁 短小精悍的C语言标准函数库。提供450个以上的可移植的算法和工具代码。
💻 C
📖 第 1 页 / 共 2 页
字号:
        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);
}


/*  ---------------------------------------------------------------------[<]-
    Function: ini_dyn_loade

    Synopsis: Loads the contents of an .ini file into a symbol table, as
    for ini_dyn_load(), but requires that the .ini file exists.  Returns
    a loaded symbol table if the file was present, and NULL otherwise.
    ---------------------------------------------------------------------[>]-*/

SYMTAB *
ini_dyn_loade (
    SYMTAB *load_symtab,
    const char *filename)
{
    ASSERT (filename);
    if (file_locate ("PATH", filename, NULL))
        return (ini_dyn_load (load_symtab, filename));
    else
        return (NULL);
}


/*  ---------------------------------------------------------------------[<]-
    Function: ini_dyn_save

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

int
ini_dyn_save (
    SYMTAB *symtab,
    const char *filename)
{
    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);
}


/*  ---------------------------------------------------------------------[<]-
    Function: ini_dyn_changed

    Synopsis: Returns TRUE if the ini file loaded into the specified table
    has in the meantime been changed.  Returns FALSE if not.
    ---------------------------------------------------------------------[>]-*/

Bool
ini_dyn_changed (
    SYMTAB *symtab)
{
    char
        *filename;

    ASSERT (symtab);

    /*  Date, time, and name of original ini file are in the table           */
    filename = sym_get_value (symtab, "filename", NULL);
    if (filename
    &&  file_has_changed (filename,
                          sym_get_number (symtab, "filedate", 0),
                          sym_get_number (symtab, "filetime", 0)))
        return (TRUE);
    else
        return (FALSE);
}


/*  ---------------------------------------------------------------------[<]-
    Function: ini_dyn_refresh

    Synopsis: Refreshes a symbol table created by ini_dyn_load().  If the
    original file (as specified by the 'filename' symbol) has been modified,
    reloads the whole ini file.  You would typically call this function at
    regular intervals to permit automatic reloading of an ini file in an
    application.  Returns TRUE if the ini file was actually reloaded, or
    FALSE if the file had not changed or could not be accessed, or if the
    symbol table was incorrectly created.  If the symbol table is reloaded
    from the ini file, all previous symbols are deleted.
    ---------------------------------------------------------------------[>]-*/

Bool
ini_dyn_refresh (
    SYMTAB *symtab)
{
    char
        *filename;

    ASSERT (symtab);
    if (ini_dyn_changed (symtab))
      {
        filename = mem_strdup (sym_get_value (symtab, "filename", NULL));
        sym_empty_table (symtab);       /*  Delete previous table contents   */
        ini_dyn_load (symtab, filename);
        mem_free (filename);
        return (TRUE);
      }
    return (FALSE);
}


/*  ---------------------------------------------------------------------[<]-
    Function: ini_dyn_value

    Synopsis: Finds a section:keyword in the symbol table and returns a
    pointer to its value.  Returns the default value if the symbol is not
    defined in the table.  The default value may be NULL.  The specified
    section and keyword can be in any case; they are converted internally
    to lowercase to match the symbol table.  If the keyword is empty or
    NULL, no ':keyword' is appended to the section name.
    ---------------------------------------------------------------------[>]-*/

char *
ini_dyn_value (
    SYMTAB *symtab,
    const char *section,
    const char *keyword,
    const char *default_value)
{
    ASSERT (section);
    if (keyword && *keyword)
        snprintf (ini_keyword, sizeof (ini_keyword), "%s:%s", section, keyword);
    else
        strncpy  (ini_keyword, section, sizeof (ini_keyword));

    strlwc (ini_keyword);
    return (sym_get_value (symtab, ini_keyword, default_value));
}


/*  ---------------------------------------------------------------------[<]-
    Function: ini_dyn_values

    Synopsis: Finds a section:keyword in the symbol table and returns a
    pointer to a string table containing the values, delimited by commas.
    When finished with the string table you should call tok_free() to free
    the memory allocated for it.  The default value may not be NULL.
    Returns a pointer to a table of string tokens (see tok_split()), or
    NULL if there was insufficient memory.  The specified section and keyword
    can be in any case; they are converted internally to lowercase to match
    the symbol table.  If the keyword is empty or NULL, no ':keyword' is
    appended to the section name.
    ---------------------------------------------------------------------[>]-*/

char **
ini_dyn_values (
    SYMTAB *symtab,
    const char *section,
    const char *keyword,
    const char *default_value)
{
    ASSERT (section);
    ASSERT (default_value);

    if (keyword && *keyword)
        snprintf (ini_keyword, sizeof (ini_keyword), "%s:%s", section, keyword);
    else
        strncpy  (ini_keyword, section, sizeof (ini_keyword));

    strlwc (ini_keyword);
    strcpy (iniline, sym_get_value (symtab, ini_keyword, default_value));
    strconvch (iniline, ',', ' ');
    return (tok_split (iniline));
}


/*  ---------------------------------------------------------------------[<]-
    Function: ini_dyn_default

    Synopsis: As ini_dyn_value, but creates an entry with the default value
    if none already exists.
    ---------------------------------------------------------------------[>]-*/

char *
ini_dyn_default (
    SYMTAB *symtab,
    const char *section,
    const char *keyword,
    const char *default_value)
{
    ASSERT (section);
    if (keyword && *keyword)
        snprintf (ini_keyword, sizeof (ini_keyword), "%s:%s", section, keyword);
    else
        strncpy  (ini_keyword, section, sizeof (ini_keyword));

    strlwc (ini_keyword);
    if (!sym_lookup_symbol (symtab, ini_keyword))
        sym_assume_symbol (symtab, ini_keyword, default_value);
    
    return (sym_get_value (symtab, ini_keyword, default_value));
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -