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

 

symb2strt

#include "sflsymb.h"
char **
symb2strt (
    const SYMTAB *symtab)               /*  Symbol table to export           */

Synopsis

Exports the symbol table as an array of strings of the format "name=value". Returns a pointer to the array. The array is allocated dynamically. The array ends with a NULL string. To free the table, call strtfree(). If there was not enough memory to allocate the table, returns NULL. See also symb2env().

Source Code - (sflsymb.c)

{
    SYMBOL
        *symbol;                        /*  Pointer to symbol                */
    char
        **strings,                      /*  Returned string array            */
        *name_and_value;                /*  Name=value string                */
    int
        string_nbr;                     /*  Index into symbol_array          */

    if (!symtab)
        return (NULL);                  /*  Return NULL if argument is null  */

    /*  Allocate the array of pointers with one slot for the final NULL      */
    strings = mem_alloc (sizeof (char *) * (symtab-> size + 1));
    if (strings)
      {
        string_nbr = 0;
        for (symbol = symtab-> symbols; symbol; symbol = symbol-> next)
          {
            /*  Allocate space for "name=value" plus final null char         */
            name_and_value = mem_alloc (strlen (symbol-> name)
                                      + strlen (symbol-> value) + 2);
            if (name_and_value)         /*  Fail-safe if no memory left      */
                sprintf (name_and_value, "%s=%s", symbol-> name,
                                                  symbol-> value);
            strings [string_nbr++] = name_and_value;
          }
        strings [string_nbr] = NULL;    /*  Store final null pointer         */
      }
    return (strings);
}

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