]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/v/getfields.c
Import sources from 2011-03-30 iso image
[plan9front.git] / sys / src / ape / lib / v / getfields.c
1 #define _RESEARCH_SOURCE
2 #include <libv.h>
3 #include <string.h>
4
5 static char is_sep[256] = {
6         0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7         1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14 };
15 static char is_field[256] = {
16         0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
19         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
20         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
21         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
22         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
23         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
24 };
25 static char last_sep[256];
26
27 char *
28 setfields(char *arg)
29 {
30         register unsigned char *s;
31         register i;
32
33         for(i = 1, s = (unsigned char *)last_sep; i < 256; i++)
34                 if(is_sep[i])
35                         *s++ = i;
36         *s = 0;
37         memset(is_sep, 0, sizeof is_sep);
38         memset(is_field, 1, sizeof is_field);
39         for(s = (unsigned char *)arg; *s;){
40                 is_sep[*s] = 1;
41                 is_field[*s++] = 0;
42         }
43         is_field[0] = 0;
44         return(last_sep);
45 }
46
47 int
48 getfields(char *ss, char **sp, int nptrs)
49 {
50         register unsigned char *s = (unsigned char *)ss;
51         register unsigned char **p = (unsigned char **)sp;
52         register unsigned c;
53
54         for(;;){
55                 if(--nptrs < 0) break;
56                 *p++ = s;
57                 while(is_field[c = *s++])
58                         ;
59                 if(c == 0) break;
60                 s[-1] = 0;
61         }
62         if(nptrs > 0)
63                 *p = 0;
64         else if(--s >= (unsigned char *)ss)
65                 *s = c;
66         return(p - (unsigned char **)sp);
67 }
68
69 int
70 getmfields(char *ss, char **sp, int nptrs)
71 {
72         register unsigned char *s = (unsigned char *)ss;
73         register unsigned char **p = (unsigned char **)sp;
74         register unsigned c;
75
76         if(nptrs <= 0)
77                 return(0);
78         goto flushdelim;
79         for(;;){
80                 *p++ = s;
81                 if(--nptrs == 0) break;
82                 while(is_field[c = *s++])
83                         ;
84                 /*
85                  *  s is now pointing 1 past the delimiter of the last field
86                  *  c is the delimiter
87                  */
88                 if(c == 0) break;
89                 s[-1] = 0;
90         flushdelim:
91                 while(is_sep[c = *s++])
92                         ;
93                 /*
94                  *  s is now pointing 1 past the beginning of the next field
95                  *  c is the first letter of the field
96                  */
97                 if(c == 0) break;
98                 s--;
99                 /*
100                  *  s is now pointing to the beginning of the next field
101                  *  c is the first letter of the field
102                  */
103         }
104         if(nptrs > 0)
105                 *p = 0;
106         return(p - (unsigned char **)sp);
107 }
108
109 #ifdef  MAIN
110 #include        <fio.h>
111
112 main()
113 {
114         char *fields[256];
115         char *s;
116         int n, i;
117         char buf[1024];
118
119         print("go:\n");
120         while(s = Frdline(0)){
121                 strcpy(buf, s);
122                 Fprint(1, "getf:");
123                 n = getfields(s, fields, 4);
124                 for(i = 0; i < n; i++)
125                         Fprint(1, " >%s<", fields[i]);
126                 Fputc(1, '\n');
127                 Fprint(1, "getmf:");
128                 n = getmfields(buf, fields, 4);
129                 for(i = 0; i < n; i++)
130                         Fprint(1, " >%s<", fields[i]);
131                 Fputc(1, '\n');
132                 Fflush(1);
133         }
134         exit(0);
135 }
136 #endif