C Token Library

To make simple token list manipulation easier.

Conventions

any name starting with a _ should be considered like C++'s private. If you have a CSS broswer actual code usable in tokens.h will have a black background, and samples will have a white background. Everything else will have a silver background.

Includes

The only include is #include <sys/types.h>, and that is for the size_t type.

Structures

One creates a tokens structure using C code like: tokens foo; tokInit(&foo); and frees it up for reuse with tokFree(&foo);.

token

This structure should never be destroyed or created but the user of this this library. It is, however, ok for to reference elements in it. Every instance of tokens will have one of these structures for every token in the list.

typedef struct token_s {
struct token_s* next;
Points to the next token in the token list.
char* tok;
The actual token ("" is legal but NULL is not).
size_t len;
This holds the length of tok, this is the length not including the terminating NULL character.
} _token;

tokens

typedef struct tokens_s {
struct token_s* first;
Pointer to the first token on the list. NULL if no tokens are defined.
struct token_s* last;
Pointer to the last token on the list. It equals first if there is only token on the list.
size_t count;
The number of tokens in this list.
  char *_string; /* place to hold string to print. */
} tokens;

Functions

Please note, I would of written this in C++ but I needed it in C. So you might notice some C++ish conventions.

tokens* tokInit(tokens* this);
Initializes token string. Must be called after a new tokens is declared.
tokens* tokFree(tokens* this);
Deletes all tokens and free all memory used by these tokens. It returns tokInit(this). Any return values from tok2string will is also made free.
tokens* tokFree_first(tokens* this);
Deletes and frees up memory of the first element in the token list. It returns this.
int tokFree_one(tokens* this, const char* remove);
Deletes first token that matches remove. It returns 0 if it didn't remove anything, something else if it did. If you are not sure if there is a token matching remove just use tokFree_one(&path, "."); instead of if (tokMatch(&path, ".")) { tokFree_one(&path, "."); }. The later sample does the same query twice.
tokens* tokAdd(tokens* this, const char* list, const char* delim);
Not Implemented. Adds a new set of tokens to the end of this. The value list is a string of tokens delineated by delim characters. If called tokAdd(&path, "/usr/local/bin", ""); the list gets put in as one token.
tokens* tokAdd_unique(tokens* this, const char* list, const char* delim);
Like tokAdd but only adds the a token if it is unique.
int tokMatch(tokens* this, const char* find);
Returns 0 if find isn't alread in this, else it returns non-zero.
const char* tok2string(tokens* this, const char* delim);
returns a delim separated token list of all the elements in this. If called a second time the first return value will be made invalid.

Copyright © 1998 Sven Heinicke <sven@zen.org>
[Made with
CSS]