]> git.sur5r.net Git - cc65/blob - libsrc/common/strqtok.c
"Inverted" time_t value handling.
[cc65] / libsrc / common / strqtok.c
1 /*
2 ** strqtok() is like strtok():  It finds pieces of text, in a string, that are
3 ** surrounded by given delimiter characters.  It returns each piece, in turn,
4 ** as a string, until every piece has been found.  Then, it returns NULL.  But,
5 ** strqtok() recognizes quotation marks.  A mark makes delimiters look ordinary
6 ** until another quotation mark is seen.  That allows us to include delimiters
7 ** in tokens.  (This version doesn't allow escaped quotation marks.)
8 **
9 ** 2014-04-19, Daniel Serpell
10 ** 2014-04-21, Paul Foerster
11 ** 2014-04-25, Greg King
12 */
13
14
15 #include <string.h>
16
17
18 char* __fastcall__ strqtok (register char* s1, const char* s2)
19 {
20     static char  c;
21     static char* start;
22     static char* next = "";
23
24     if (s1 == NULL) {
25         s1 = next;
26         if (c == '\"') {
27             goto inQuote;
28         }
29     }
30
31     /* Search for the start of a token. */
32     while (strchr (s2, c = *s1)) {
33         if (c == '\0') {
34             /* No more tokens. */
35             return NULL;
36         }
37         ++s1;
38     }
39     if (c == '\"') {
40         goto skipQuote;
41     }
42
43     /* Save the start of the token. */
44     start = s1;
45
46     /* Search for the end of a non-quoted token. */
47     while ((c = *s1) != '\"' && !strchr (s2, c)) {
48         ++s1;
49     }
50     if (c == '\0') {
51         /* The end of the last token is the end of the token list;
52         ** don't go beyond it.
53         */
54         goto found;
55     }
56
57     /* (A possible begin-quote mark will be rememberred.) */
58     goto terminate;
59
60   skipQuote:
61     ++s1;
62
63   inQuote:
64     /* Don't let a quote mark be rememberred. */
65     c = '\0';
66
67     /* Save the start of the token. */
68     start = s1;
69
70     /* Search for the end of a quoted token. */
71     if ((s1 = strchr (s1, '\"')) == NULL) {
72         /* The quoted token ended with '\0'; therefore, point to a '\0',
73         ** so that the next call will return NULL.
74         */
75         next = "";
76         return start;
77     }
78
79   terminate:
80     *s1 = '\0';
81     ++s1;
82
83   found:
84     next = s1;
85     return start;
86 }