diff options
author | axel <axel@liljencrantz.se> | 2005-09-20 23:26:39 +1000 |
---|---|---|
committer | axel <axel@liljencrantz.se> | 2005-09-20 23:26:39 +1000 |
commit | 149594f974350bb364a76c73b91b1d5ffddaa1fa (patch) | |
tree | 95650e9982d5fabe4bd805d94c5d700cbbc1ca7f /tokenize.c |
Initial revision
darcs-hash:20050920132639-ac50b-fa3b476891e1f5f67207cf4cc7bf623834cc5edc.gz
Diffstat (limited to 'tokenize.c')
-rw-r--r-- | tokenize.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/tokenize.c b/tokenize.c new file mode 100644 index 00000000..bad79a9f --- /dev/null +++ b/tokenize.c @@ -0,0 +1,138 @@ +/** file tokenize.c + Small utility command for tokenizing an argument. + \c tokenize is used for splitting a text string into separate parts (i.e. tokenizing) with a user supplied delimiter character. +*/ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> + +#include "config.h" + +#ifdef HAVE_GETOPT_H +#include <getopt.h> +#endif + +/** + Print help message +*/ +void print_help(); + +/** + Main program +*/ +int main( int argc, char **argv ) +{ + char *delim = " \t"; + int empty_ok = 0; + int i; + extern int optind; + + while( 1 ) + { +#ifdef __GLIBC__ + static struct option + long_options[] = + { + { + "with-empty", no_argument, 0, 'e' + } + , + { + "no-empty", no_argument, 0, 'n' + } + , + { + "delimiter", required_argument, 0, 'd' + } + , + { + "help", no_argument, 0, 'h' + } + , + { + "version", no_argument, 0, 'v' + } + , + { + 0, 0, 0, 0 + } + } + ; + + int opt_index = 0; + + int opt = getopt_long( argc, + argv, + "end:hv", + long_options, + &opt_index ); +#else + int opt = getopt( argc, + argv, + "end:hv" ); +#endif + if( opt == -1 ) + break; + + switch( opt ) + { + case 0: + break; + + case 'e': + empty_ok = 1; + break; + + case 'n': + empty_ok = 0; + break; + + case 'd': + delim = optarg; + break; + case 'h': + print_help(); + exit(0); + + case 'v': + printf( "tokenize, version %s\n", PACKAGE_VERSION ); + exit( 0 ); + + case '?': + return 1; + + } + + } + + for( i=optind; i<argc; i++ ) + { + char *curr; + int printed=0; + for( curr = argv[i]; *curr; curr++ ) + { + if( strchr( delim, *curr )==0 ) + { + printed = 1; + putchar( *curr ); + } + else + { + if( empty_ok || printed ) + putchar( '\n' ); + printed=0; + } + } + if( printed ) + putchar( '\n' ); + } + +} + + + + + |