cnet's support functions

NAME
Lexical support functions.

SYNOPSIS
#include <cnet.h>
#include <cnetsupport.h>

#define  CHAR_COMMENT    '#'
#define  CHAR_DQUOTE     '"'

extern  char  **tokenize(char *line, int *ntokens, const char *separators);
extern  void  free_tokens(char **tokens);
extern  void  remove_comment(char *line);
extern  void  remove_nl(char *line);

DESCRIPTION
While attributes to define cnet's simulations are provided in topology files, we often need to define the required execution of our own protocols as well. Command-line parameters may be passed to each node's EV_REBOOT event handler, but providing many parameters this way can be cumbersome. Instead, we often prefer to specify the parameters of a large protocol in an external configuration file that is initially read by each node. These lexical support functions assist with the basic parsing of such files.

tokenize accepts a line of characters and breaks that string into a number of tokens that were separated by any of a set of indicated characters. Any initial separators, at the beginning of the line, are first skipped. A token appearing within CHAR_QDUOTE characters may contain any of the separators.

tokenize returns a NULL-terminated vector of strings (tokens), with the number of tokens reported in ntokens. The result returned by tokenize should eventually be passed to free_tokens to deallocate memory.

The functions remove_comment and remove_nl simply replace the first instance of CHAR_COMMENT, or \n or \r, with the NUL byte.

EXAMPLE
A typical use of these functions is:

    FILE  *fp = fopen(filename, "r");

    if(fp) {
        char  line[BUFSIZE], **tokens;
        int   ntokens;

        while(fgets(line, sizeof(line), fp) != NULL) {
            remove_comment(line);
            tokens  = tokenize(line, &ntokens, " \t");
            for(int n=0 ; n<ntokens ; ++n) {
                // access or copy tokens[n] ...
            }
            free_tokens(tokens);
        }
        fclose(fp);
    }


 cnet v3.3.4, written by Chris.McDonald@uwa.edu.au
 Last modified: Tue Mar 1 7:43AM 2016