432c4e18 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: qdparse.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ |
432c4e18 |
4 | * |
5 | * Quick-and-dirty parser |
6 | * |
7 | * (c) 2004 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
432c4e18 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <ctype.h> |
33 | #include <string.h> |
34 | |
35 | #include "qdparse.h" |
36 | |
37 | /*----- Main code ---------------------------------------------------------*/ |
38 | |
39 | /* --- @qd_skipspc@ --- * |
40 | * |
41 | * Arguments: @qd_parse *qd@ = context |
42 | * |
43 | * Returns: --- |
44 | * |
45 | * Use: Skips spaces in the string. No errors. |
46 | */ |
47 | |
48 | void qd_skipspc(qd_parse *qd) |
49 | { |
50 | while (isspace((unsigned char)*qd->p)) |
51 | qd->p++; |
52 | } |
53 | |
54 | /* --- @qd_delim@ --- * |
55 | * |
56 | * Arguments: @qd_parse *qd@ = context |
57 | * @int ch@ = character to compare with |
58 | * |
59 | * Returns: Nonzero if it was, zero if it wasn't. |
60 | * |
61 | * Use: Checks the next (non-whitespace) character is what we |
62 | * expect. If it is, the character is eaten; otherwise it's no |
63 | * big deal. |
64 | */ |
65 | |
66 | int qd_delim(qd_parse *qd, int ch) |
67 | { |
68 | qd_skipspc(qd); |
69 | if (*qd->p != ch) |
70 | return (0); |
71 | qd->p++; |
72 | return (1); |
73 | } |
74 | |
75 | /* --- @qd_enum@ --- * |
76 | * |
77 | * Arguments: @qd_parse *qd@ = context |
78 | * @const char *e@ = list of enum strings, space separated |
79 | * |
80 | * Returns: Index of the string matched, or @-1@. |
81 | * |
82 | * Use: Matches a keyword. |
83 | */ |
84 | |
85 | int qd_enum(qd_parse *qd, const char *e) |
86 | { |
87 | size_t n; |
88 | int i = 0; |
89 | |
90 | qd_skipspc(qd); |
91 | for (;;) { |
92 | e += strspn(e, ", "); |
93 | if (!*e) break; |
94 | n = strcspn(e, ", "); |
95 | if (strncmp(qd->p, e, n) == 0 && !isalnum((unsigned char)qd->p[n])) { |
96 | qd->p += n; |
97 | return (i); |
98 | } |
99 | i++; e += n; |
100 | } |
101 | qd->e = "unrecognized keyword"; |
102 | return (-1); |
103 | } |
104 | |
105 | /* --- @qd_getmp@ --- * |
106 | * |
107 | * Arguments: @qd_parse *qd@ = context |
108 | * |
109 | * Returns: The integer extracted, or null. |
110 | * |
111 | * Use: Parses a multiprecision integer from a string. |
112 | */ |
113 | |
114 | mp *qd_getmp(qd_parse *qd) |
115 | { |
116 | char *q; |
117 | mp *m; |
118 | |
119 | qd_skipspc(qd); |
120 | m = mp_readstring(MP_NEW, qd->p, &q, 0); |
121 | if (m && !isalnum((unsigned char)*q)) |
122 | qd->p = q; |
123 | else { |
124 | mp_drop(m); |
125 | qd->e = "bad number"; |
126 | } |
127 | return (m); |
128 | } |
129 | |
130 | /* --- @qd_eofp@ --- * |
131 | * |
132 | * Arguments: @qd_parse *qd@ = context |
133 | * |
134 | * Returns: Nonzero if at EOF, zero otherwise. |
135 | */ |
136 | |
137 | int qd_eofp(qd_parse *qd) |
138 | { |
139 | qd_skipspc(qd); |
140 | return (!*qd->p); |
141 | } |
142 | |
143 | /*----- That's all, folks -------------------------------------------------*/ |