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

📄 sflsymb.c

📁 短小精悍的C语言标准函数库。提供450个以上的可移植的算法和工具代码。
💻 C
📖 第 1 页 / 共 3 页
字号:
    SYMTAB *table,                      /*  Symbol table to search           */
    const char *name,                   /*  Name of symbol to find/create    */
    const char *value,                  /*  Value of symbol to create        */
    const char *filename,               /*  Name of source file making call  */
    size_t      lineno)                 /*  Line number in calling source    */
{
    SYMBOL
        *symbol;                        /*  Allocated or found symbol        */

    ASSERT (table);

    symbol = sym_lookup_symbol (table, name);
    if (symbol)
      {
        /*  Update the symbol value, if it has changed                       */
        if (symbol-> value && strneq (symbol-> value, value))
            sym_set_value (symbol, value);
      }
    else
        symbol = sym_create_symbol_ (table, name, value, filename, lineno);

    return (symbol);
}


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

    Synopsis: Removes the specified symbol from the symbol table and looks
    through the table for another with the same name.  Returns a pointer to
    the next symbol, or NULL if no further symbols were found with the same
    name.  Deallocates the symbol value, if not NULL.  NOTE: The SYMBOL
    must be part of the symbol table that is being modified.
    ---------------------------------------------------------------------[>]-*/

SYMBOL *
sym_delete_symbol (
    SYMTAB *table,                      /*  Symbol table to search           */
    SYMBOL *symbol)                     /*  Symbol to delete                 */
{
    SYMBOL
        *next;                          /*  Next symbol with same name       */

    ASSERT (table);
    ASSERT (symbol);

    /*  Find a symbol with the same name, or NULL if none found              */
    next = symbol;
    for (next = symbol-> h_next; next; next = next-> h_next)
        if (streq (next-> name, symbol-> name))
            break;

    /*  Fix up the pointers and remove the original symbol                   */
    if (symbol-> prev)
        symbol-> prev-> next = symbol-> next;
    else
        table-> symbols = symbol-> next;

    if (symbol-> next)
        symbol-> next-> prev = symbol-> prev;

    if (symbol-> h_prev)
        symbol-> h_prev-> h_next = symbol-> h_next;
    else
        table-> hash [symbol-> hash] = symbol-> h_next;

    if (symbol-> h_next)
        symbol-> h_next-> h_prev = symbol-> h_prev;

    table-> size--;
    mem_free (symbol-> value);
    mem_free (symbol);

    return (next);
}

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

    Synopsis: Traverses the symbol table, executing the specified function
    for every symbol in the table.  The function receives one or more
    arguments: the first argument is a SYMBOL pointer to the symbol, and
    following arguments as supplied by the caller.  Continues so long as the
    function returns TRUE; halts when every symbol has been processed, or
    when the function returns FALSE.  Returns the number of symbols processed,
    if all symbols were processed; or negative the number of symbols processed
    if processing stopped early due to the function returning FALSE or other
    errors.  The symbols are processed in reverse creation order; the newest
    symbol is processed first.

    Examples:
    static Bool
    dump_symbol (SYMBOL *symbol, ...)
    {
        printf ("%s = %s\n", symbol-> name, symbol-> value);
        return (TRUE);
    }

    SYMTAB
        *table;

    table = sym_create_table ();
    sym_create_symbol (table, "Symbol 1", "value 1");
    sym_create_symbol (table, "Symbol 2", "value 2");
    sym_exec_all (table, dump_symbol);
    sym_delete_table (table);
    ---------------------------------------------------------------------[>]-*/

int
sym_exec_all (
    const SYMTAB *table,                /*  Symbol table to process          */
    symfunc the_function, ...           /*  Function to call                 */
)
{
    SYMBOL
        *symbol;                        /*  Next symbol in table             */
    va_list
        argptr;                         /*  Argument list pointer            */
    int
        count = 0;                      /*  Number of symbols processed ok   */
    Bool
        alldone = TRUE;                 /*  Assume all symbols will be done  */
        
    ASSERT (table);

    va_start (argptr, the_function);    /*  Start variable args processing   */
    for (symbol = table-> symbols; symbol; symbol = symbol-> next)
      {
        if ((*the_function) (symbol, argptr))
            count++;
        else
          {
            alldone = FALSE;
            break;
	  }  
      }
    va_end (argptr);                    /*  End variable args processing     */

    if (alldone)
        return (count);                 /*  All symbols processed -> positive*/
    else
        return (-(count));              /*  Stopped early -> negative count  */
}


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

    Synopsis: Computes the hash value for a null-delimited string.  The
    algorithm used is a simple 8-bit checksum of the characters in the
    string.  The hash is within the range 0 .. SYM_HASH_SIZE - 1.
    ---------------------------------------------------------------------[>]-*/

int
sym_hash (
    const char *name)
{
    int
        hash;                           /*  Computed hash value              */

    for (hash = 0; *name; name++)
        hash += *name;

    return (hash & (SYM_HASH_SIZE - 1));
}


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

    Synopsis: Returns the name of a particular symbol.  This can be used
    with the result of sym_lookup_symbol(), when you don't know the name
    of the symbol that you have got.
    ---------------------------------------------------------------------[>]-*/

const char *
sym_get_name (const SYMBOL *symbol)
{
    ASSERT (symbol);
    return (symbol-> name);
}


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

    Synopsis: Returns value for specified symbol, if defined in table, or
    a default value otherwise.  You can use this in situations where a symbol
    does not need to exist, and where frequent look-up by name is required.
    The symbol table must exist and be populated beforehand as appropriate.
    Returns a pointer to the value; you should never write to this string
    since it may exist as a string constant, not writable memory.

    Examples:
    value = sym_get_value (env, "PATH", NULL);
    if (!value)
        puts ("PATH not defined");
    ---------------------------------------------------------------------[>]-*/

char *
sym_get_value (
    const SYMTAB *table,                /*  Symbol table to process          */
    const char *name,                   /*  Name of symbol to look for       */
    const char *default_value)          /*  Value to return if not defined   */
{
    SYMBOL
        *symbol;                        /*  Search through symbol table      */

    ASSERT (table);
    symbol = sym_lookup_symbol (table, name);
    if (symbol)
        return (symbol-> value);
    else
        return ((char *) default_value);
}


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

    Synopsis: Returns value for specified symbol, as a long value.  If the
    symbol is not defined in the table, returns a default value.

    Examples:
    value = sym_get_number (env, "MAX_USERS", 10);
    ---------------------------------------------------------------------[>]-*/

long
sym_get_number (
    const SYMTAB *table,                /*  Symbol table to process          */
    const char *name,                   /*  Name of symbol to look for       */
    const long default_value)           /*  Value to return if not defined   */
{
    char
        *value;

    value = sym_get_value (table, name, NULL);
    return (value? atol (value): default_value);
}


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

    Synopsis: Returns value for specified symbol, as TRUE or FALSE.  If the
    symbol is not defined in the table, returns a default value.

    Examples:
    value = sym_get_boolean (env, "CONNECTS_ENABLED", TRUE);
    ---------------------------------------------------------------------[>]-*/

Bool
sym_get_boolean (
    const SYMTAB *table,                /*  Symbol table to process          */
    const char *name,                   /*  Name of symbol to look for       */
    const Bool default_value)           /*  Value to return if not defined   */
{
    char
        *value;

    value = sym_get_value (table, name, NULL);
    return (value? (conv_str_bool (value) != 0): default_value);
}


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

    Synopsis: Assigns a new value for the symbol; this frees any previously
    assigned value and duplicates the supplied value, which must be a null
    terminated string.  If you want to assign binary values, you can use the
    symbol's data block.  If the value is NULL, any existing value is freed
    and the symbol value pointer is set to NULL.
    ---------------------------------------------------------------------[>]-*/

void
sym_set_value (
    SYMBOL *symbol,                     /*  Symbol to change                 */

⌨️ 快捷键说明

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