diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2009-12-09 03:52:08 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2009-12-09 03:52:08 +0000 |
commit | 481a3f5110e03c2301e53837da2666114b5efa5c (patch) | |
tree | 77e1d1272c4252785583794324e5e7007da1d60e /lib/util/token.c | |
parent | ca1c1c7c4ca76728b3134cf685e8b8a6142e663b (diff) | |
download | netpbm-mirror-481a3f5110e03c2301e53837da2666114b5efa5c.tar.gz netpbm-mirror-481a3f5110e03c2301e53837da2666114b5efa5c.tar.xz netpbm-mirror-481a3f5110e03c2301e53837da2666114b5efa5c.zip |
new facilities needed by recently committed Pnmconvol updates: string list option type, token parsing, file line I/O
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1031 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/util/token.c')
-rw-r--r-- | lib/util/token.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/util/token.c b/lib/util/token.c new file mode 100644 index 00000000..c4fe0aed --- /dev/null +++ b/lib/util/token.c @@ -0,0 +1,79 @@ +#include <stdlib.h> +#include <assert.h> + +#include "nstring.h" + +#include "token.h" + + +void +pm_gettoken(const char * const tokenStart, + char const delimiter, + const char ** const tokenP, + const char ** const nextP, + const char ** const errorP) { +/*---------------------------------------------------------------------------- + Find the token starting at 'tokenStart' up to but not including + the first 'delimiter' character or end of string. Return it in newly + malloced memory as *tokenP, NUL-terminated. + + Make *nextP point just past the token, i.e. to the delimiter or + end of string NUL character. + + Note that if the string is empty, or starts with the delimiter, + we return an empty string and *nextP == tokenStart, i.e. *nextP + doesn't necessarily advance. +-----------------------------------------------------------------------------*/ + char * token; + const char * cursor; + unsigned int charCount; + + /* Run through the token, counting characters */ + + charCount = 0; /* initial value */ + cursor = tokenStart; /* initial value */ + *errorP = NULL; /* initial value */ + + while (*cursor != delimiter && *cursor != '\0' && !*errorP) { + if (*cursor == '\\') { + ++cursor; + if (*cursor == '\0') + asprintfN(errorP, "string ends with an escape character (\\)"); + } else { + ++cursor; + ++charCount; + } + } + if (!*errorP) { + token = malloc(charCount + 1); + if (token == NULL) + asprintfN(errorP, "Could not allocate %u bytes of memory " + "to parse a string", + charCount + 1); + else { + /* Go back and do it again, this time copying the characters */ + charCount = 0; + cursor = tokenStart; + + while (*cursor != delimiter && *cursor != '\0') { + if (*cursor == '\\') + ++cursor; + + assert(*cursor != '\0'); + + token[charCount++] = *cursor++; + } + token[charCount] = '\0'; + + *tokenP = token; + *nextP = cursor; + } + } +} + + + + + + + |