Standard curves and curve checking.
[u/mdw/catacomb] / qdparse.c
1 /* -*-c-*-
2 *
3 * $Id: qdparse.c,v 1.1 2004/03/27 17:54:12 mdw Exp $
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
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: qdparse.c,v $
33 * Revision 1.1 2004/03/27 17:54:12 mdw
34 * Standard curves and curve checking.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <ctype.h>
41 #include <string.h>
42
43 #include "qdparse.h"
44
45 /*----- Main code ---------------------------------------------------------*/
46
47 /* --- @qd_skipspc@ --- *
48 *
49 * Arguments: @qd_parse *qd@ = context
50 *
51 * Returns: ---
52 *
53 * Use: Skips spaces in the string. No errors.
54 */
55
56 void qd_skipspc(qd_parse *qd)
57 {
58 while (isspace((unsigned char)*qd->p))
59 qd->p++;
60 }
61
62 /* --- @qd_delim@ --- *
63 *
64 * Arguments: @qd_parse *qd@ = context
65 * @int ch@ = character to compare with
66 *
67 * Returns: Nonzero if it was, zero if it wasn't.
68 *
69 * Use: Checks the next (non-whitespace) character is what we
70 * expect. If it is, the character is eaten; otherwise it's no
71 * big deal.
72 */
73
74 int qd_delim(qd_parse *qd, int ch)
75 {
76 qd_skipspc(qd);
77 if (*qd->p != ch)
78 return (0);
79 qd->p++;
80 return (1);
81 }
82
83 /* --- @qd_enum@ --- *
84 *
85 * Arguments: @qd_parse *qd@ = context
86 * @const char *e@ = list of enum strings, space separated
87 *
88 * Returns: Index of the string matched, or @-1@.
89 *
90 * Use: Matches a keyword.
91 */
92
93 int qd_enum(qd_parse *qd, const char *e)
94 {
95 size_t n;
96 int i = 0;
97
98 qd_skipspc(qd);
99 for (;;) {
100 e += strspn(e, ", ");
101 if (!*e) break;
102 n = strcspn(e, ", ");
103 if (strncmp(qd->p, e, n) == 0 && !isalnum((unsigned char)qd->p[n])) {
104 qd->p += n;
105 return (i);
106 }
107 i++; e += n;
108 }
109 qd->e = "unrecognized keyword";
110 return (-1);
111 }
112
113 /* --- @qd_getmp@ --- *
114 *
115 * Arguments: @qd_parse *qd@ = context
116 *
117 * Returns: The integer extracted, or null.
118 *
119 * Use: Parses a multiprecision integer from a string.
120 */
121
122 mp *qd_getmp(qd_parse *qd)
123 {
124 char *q;
125 mp *m;
126
127 qd_skipspc(qd);
128 m = mp_readstring(MP_NEW, qd->p, &q, 0);
129 if (m && !isalnum((unsigned char)*q))
130 qd->p = q;
131 else {
132 mp_drop(m);
133 qd->e = "bad number";
134 }
135 return (m);
136 }
137
138 /* --- @qd_eofp@ --- *
139 *
140 * Arguments: @qd_parse *qd@ = context
141 *
142 * Returns: Nonzero if at EOF, zero otherwise.
143 */
144
145 int qd_eofp(qd_parse *qd)
146 {
147 qd_skipspc(qd);
148 return (!*qd->p);
149 }
150
151 /*----- That's all, folks -------------------------------------------------*/