|
| iMatix home page | << | < | > | >> |
SFLVersion 2.10 |
#include "sfltok.h"
char **
tok_push (
char **token_list,
const char *string)
Prepends a string to the specified token table. Reallocates the table as required to make room for the new string. Returns the new token table -- the supplied token table is automatically freed by a call to tok free().
{
char
*new_buffer, /* Newly-allocated buffer */
**new_list, /* Returned token list */
*new_bufptr; /* Pointer into new buffer */
int
word_count, /* Number of words in string */
word_nbr;
size_t
buffer_size;
buffer_size = tok text size (token_list);
word_count = tok size (token_list);
/* New list has one entry for each in old list, plus header, plus null
* entry at end, plus one for the new string -- makes 3 more.
*/
new_list = mem_alloc (sizeof (char *) * (word_count + 3));
new_buffer = mem_alloc (buffer_size + strlen (string));
if (new_list == NULL || new_buffer == NULL)
return (NULL);
word_count++; /* We add one word */
strcpy (new_buffer, string);
memcpy (new_buffer + strlen (string) + 1, token_list [-1], buffer_size);
new_list [0] = new_buffer; /* Store buffer address */
new_list++; /* and bump starting address */
new_bufptr = new_buffer;
for (word_nbr = 0; word_nbr < word_count; word_nbr++)
{
new_list [word_nbr] = new_bufptr;
new_bufptr += strlen (new_bufptr) + 1;
}
new_list [word_count] = NULL; /* Store final null pointer */
tok free (token_list); /* Free old list */
return (new_list);
}
| | << | < | > | >> |
|