chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / parse.c
1 /*
2  * Copyright (C) 2007 Kim Woelders
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "parse.h"
28
29 int
30 parse(char *buf, const char *fmt, ...)
31 {
32    int                 nitems;
33    char                chi, chf, chq;
34    char               *p, **ps;
35    va_list             args;
36
37    va_start(args, fmt);
38
39    for (nitems = 0;;)
40      {
41         chf = *fmt++;
42         if (chf != '%')
43            break;
44         chf = *fmt++;
45         if (chf == '\0')
46            break;
47         /* Strip leading whitespace */
48         while (isspace(*buf))
49            buf++;
50         if (!*buf)
51            break;
52         switch (chf)
53           {
54           case 'S':             /* Return pointer to string */
55           case 'T':             /* As S, convert "NULL" to NULL pointer */
56              chi = *buf;
57              chq = (chi == '\'' || chi == '"') ? chi : '\0';
58              if (chq)
59                {
60                   /* Token is quoted */
61                   buf++;
62                   for (p = buf;; p++)
63                     {
64                        p = strchr(p, chq);
65                        if (p)
66                          {
67                             if (p[1] && !isspace(p[1]))
68                                continue;
69                             *p++ = '\0';        /* Terminate at quote */
70                          }
71                        else
72                          {
73                             p = buf + strlen(buf);      /* Missing end quote */
74                          }
75                        break;
76                     }
77                }
78              else
79                {
80                   /* Token is unquoted */
81                   p = buf + 1;
82                   while (*p && !isspace(*p))
83                      p++;
84                   if (*p)
85                      *p++ = '\0';
86                }
87              ps = va_arg(args, char **);
88
89              if (chf == 'T' && (!buf[0] || !strcmp(buf, "NULL")))
90                 *ps = NULL;
91              else
92                 *ps = buf;
93              nitems++;
94              buf = p;
95           }
96      }
97
98    va_end(args);
99
100    return nitems;
101 }