* Replacement for strtok() (a.k.a. poor man's flex) * * Splits a string into tokens, returning one token per call, then NULL * when no more tokens exist in the given string. * * The calling convention is similar to that of strtok, but with more * frammishes. * * s - string to parse, if NULL continue parsing the last string * whitespace - set of whitespace characters that separate tokens
| 49 | * since you might lose data. |
| 50 | */ |
| 51 | char * |
| 52 | strtokx(const char *s, |
| 53 | const char *whitespace, |
| 54 | const char *delim, |
| 55 | const char *quote, |
| 56 | char escape, |
| 57 | bool e_strings, |
| 58 | bool del_quotes, |
| 59 | int encoding) |
| 60 | { |
| 61 | static char *storage = NULL; /* store the local copy of the users |
| 62 | * string here */ |
| 63 | static char *string = NULL; /* pointer into storage where to continue on |
| 64 | * next call */ |
| 65 | |
| 66 | /* variously abused variables: */ |
| 67 | unsigned int offset; |
| 68 | char *start; |
| 69 | char *p; |
| 70 | |
| 71 | if (s) |
| 72 | { |
| 73 | free(storage); |
| 74 | |
| 75 | /* |
| 76 | * We may need extra space to insert delimiter nulls for adjacent |
| 77 | * tokens. 2X the space is a gross overestimate, but it's unlikely |
| 78 | * that this code will be used on huge strings anyway. |
| 79 | */ |
| 80 | storage = pg_malloc(2 * strlen(s) + 1); |
| 81 | strcpy(storage, s); |
| 82 | string = storage; |
| 83 | } |
| 84 | |
| 85 | if (!storage) |
| 86 | return NULL; |
| 87 | |
| 88 | /* skip leading whitespace */ |
| 89 | offset = strspn(string, whitespace); |
| 90 | start = &string[offset]; |
| 91 | |
| 92 | /* end of string reached? */ |
| 93 | if (*start == '\0') |
| 94 | { |
| 95 | /* technically we don't need to free here, but we're nice */ |
| 96 | free(storage); |
| 97 | storage = NULL; |
| 98 | string = NULL; |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | /* test if delimiter character */ |
| 103 | if (delim && strchr(delim, *start)) |
| 104 | { |
| 105 | /* |
| 106 | * If not at end of string, we need to insert a null to terminate the |
| 107 | * returned token. We can just overwrite the next character if it |
| 108 | * happens to be in the whitespace set ... otherwise move over the |
no test coverage detected