Make ndblib not reopen and reread files all the time.
[akaros.git] / user / iplib / tokenize.c
1 /* 
2  * This file is part of the UCB release of Plan 9. It is subject to the license
3  * terms in the LICENSE file found in the top-level directory of this
4  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
5  * part of the UCB release of Plan 9, including this file, may be copied,
6  * modified, propagated, or distributed except according to the terms contained
7  * in the LICENSE file.
8  */
9 /*
10
11  * Copyright 2013 Google Inc.
12  * Copyright (c) 1989-2003 by Lucent Technologies, Bell Laboratories.
13  */
14
15 #include <string.h>
16 static char qsep[] = " \t\r\n";
17
18 int sep(char *s)
19 {
20         if (strchr(qsep, *s))
21                 return 1;
22         return 0;
23 }
24 /* s is output string, t is input string.
25  * warning: modifies data in place.
26  */
27 static char*
28 qtoken(char *s, char *sep)
29 {
30         int quoting;
31         char *t;
32
33         quoting = 0;
34         t = s;
35         while(*t!='\0' && (quoting || (strchr(sep,*t)==NULL))){
36                 if(*t != '\''){
37                         *s++ = *t++;
38                         continue;
39                 }
40                 /* *t is a quote */
41                 if(!quoting){
42                         quoting = 1;
43                         t++;
44                         continue;
45                 }
46                 /* quoting and we're on a quote */
47                 if(t[1] != '\''){
48                         /* end of quoted section; absorb closing quote */
49                         t++;
50                         quoting = 0;
51                         continue;
52                 }
53                 /* doubled quote; fold one quote into two */
54                 t++;
55                 *s++ = *t++;
56         }
57         if(*s != '\0'){
58                 *s = '\0';
59                 if(t == s)
60                         t++;
61         }
62         return t;
63 }
64
65 static char*
66 etoken(char *t, char *sep)
67 {
68         int quoting;
69
70         /* move to end of next token */
71         quoting = 0;
72         while(*t!='\0' && (quoting || (strchr(sep,*t) == NULL))){
73                 if(*t != '\''){
74                         t++;
75                         continue;
76                 }
77                 /* *t is a quote */
78                 if(!quoting){
79                         quoting = 1;
80                         t++;
81                         continue;
82                 }
83                 /* quoting and we're on a quote */
84                 if(t[1] != '\''){
85                         /* end of quoted section; absorb closing quote */
86                         t++;
87                         quoting = 0;
88                         continue;
89                 }
90                 /* doubled quote; fold one quote into two */
91                 t += 2;
92         }
93         return t;
94 }
95
96 int
97 gettokens(char *s, char **args, int maxargs, char *sep)
98 {
99         int nargs;
100
101         for(nargs=0; nargs<maxargs; nargs++){
102                 while((*s!='\0') && (strchr(sep, *s)!=NULL))
103                         *s++ = '\0';
104                 if(*s == '\0')
105                         break;
106                 args[nargs] = s;
107                 s = etoken(s, sep);
108         }
109
110         return nargs;
111 }
112
113 int
114 tokenize(char *s, char **args, int maxargs)
115 {
116         int nargs;
117
118         for(nargs=0; nargs<maxargs; nargs++){
119                 while((*s!='\0') && sep(s))
120                         s++;
121                 if(*s == '\0')
122                         break;
123                 args[nargs] = s;
124                 s = qtoken(s, qsep);
125         }
126
127         return nargs;
128 }