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

 

sym_sort_table

#include "sflsymb.h"
void
sym_sort_table (SYMTAB *table, symsort sort_function)

Synopsis

Sorts a symbol table using the qsort library function. To access the symbol table, use the next and prev symbol pointers. If the sort_function is NULL, sorts on the symbol name.

Examples

    int compare (const void *sym1, const void *sym2)
    {
        return (strcmp (*(SYMBOL **) sym1)-> value,
                        *(SYMBOL **) sym2)-> value));
    }
    SYMTAB
        *table;
    SYMBOL
        *symbol;

    table = sym_create_table ();
    sym_create_symbol (table, "Symbol 1", "A");
    sym_create_symbol (table, "Symbol 2", "D");
    sym_create_symbol (table, "Symbol 4", "B");
    sym_create_symbol (table, "Symbol 3", "C");
    sym_sort_table (table, compare);
    for (symbol = symtab-> symbols; symbol; symbol = symbol-> next)
        printf ("Symbol %s = %s\n", symbol-> name, symbol-> value);
    sym_delete_table (table);

Source Code - (sflsymb.c)

{
    SYMBOL
        *symbol,
        **array;
    int
        index;

    ASSERT (table);

    if (table-> size == 0)
        return;

    array = mem_alloc (table-> size * sizeof (SYMBOL *));
    if (array == NULL)
        return;                         /*  Not enough memory                */

    /*  Build sorting array                                                  */
    for (symbol = table-> symbols, index = 0;
         symbol && index < table-> size;
         symbol = symbol-> next, index++)
        array [index] = symbol;

    if (sort_function == NULL)
        sort_function = sym_sort_by_name;

    qsort ((void *) array, table-> size, sizeof (SYMBOL *), sort_function);

    /*  Re-order symbol table links                                          */
    table-> symbols = array [0];
    for (index = 0; index < table-> size; index++)
      {
        symbol = array [index];
        symbol-> prev = (index > 0)? array [index -1]: NULL;
        symbol-> next = (index < table-> size - 1)? array [index + 1]: NULL;
      }
    mem_free (array);
}

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