|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#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,
string_size,
new_buffer_size;
buffer_size = tok text size (token_list);
word_count = tok size (token_list);
string_size = strlen (string);
new_buffer_size = buffer_size + string_size + 1;
/* 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 (new_buffer_size);
if (new_list == NULL || new_buffer == NULL)
{
mem_free (new_list);
mem_free (new_buffer);
return (NULL);
}
word_count++; /* We add one word */
strncpy (new_buffer, string, new_buffer_size);
memcpy (new_buffer + string_size + 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);
}
| | << | < | > | >> |
|