Debian Codegraph @

third_party/spiro/ppedit/sexp.c

raw symbols SCIP
1 /*
2 ppedit - A pattern plate editor for Spiro splines.
3 Copyright (C) 2007 Raph Levien
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
19
20 */
21 #include <stdio.h>
22 #include <math.h>
23
24 #include "sexp.h"
25
26 /* This is handcoded to avoid locale problems. */
27 static int
28 parse_double(sexp_reader *sr)
29 {
30 double sign = 1.0, val = 0.0;
31 int i;
32 int numstart;
33 const char * const b = sr->tokbuf;
34 int is_double = 1;
35
36 i = 0;
37 if (b[i] == '+') {
38 i++;
39 } else if (b[i] == '-') {
40 sign = -1.0;
41 i++;
42 }
43 numstart = i;
44 while (b[i] >= '0' && b[i] <= '9')
45 val = val * 10.0 + b[i++] - '0';
46 if (b[i] == '.') {
47 double frac = 1.0;
48
49 for (i++; b[i] >= '0' && b[i] <= '9'; i++) {
50 frac *= 0.1;
51 val += (b[i] - '0') * frac;
52 }
53
54 /* A '.' without any digits on either side isn't valid. */
55 if (i == numstart + 1)
56 is_double = 0;
57 }
58 if (b[i] == 'e' || b[i] == 'E') {
59 int expsign = 1, exp = 0;
60 int expstart;
61
62 if (b[i] == '+') {
63 i++;
64 } else if (b[i] == '-') {
65 expsign = -1;
66 i++;
67 }
68 expstart = i;
69 while (b[i] >= '0' && b[i] <= '9')
70 exp = exp * 10 + b[i++] - '0';
71
72 if (i == expstart)
73 is_double = 0;
74 val *= pow(10.0, expsign * exp);
75 }
76 val *= sign;
77 sr->d = val;
78 if (b[i] != 0) is_double = 0;
79 sr->is_double = is_double;
80 return is_double;
81 }
82
83 /* Return values: 0 = EOF, 1 = token but not double, 2 = valid double */
84 int
85 sexp_token(sexp_reader *sr)
86 {
87 int c;
88 int i;
89
90 sr->singlechar = -1;
91 if (sr->f == NULL)
92 return 0;
93
94 for (;;) {
95 c = getc(sr->f);
96 if (c == EOF) {
97 sr->f = NULL;
98 return 0;
99 } else if (c == '#') {
100 do {
101 c = getc(sr->f);
102 } while (c != EOF && c != '\r' && c != '\n');
103 } else if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
104 break;
105 }
106 sr->tokbuf[0] = c;
107 i = 1;
108 if (c != '(' && c != ')') {
109 for (;;) {
110 c = getc(sr->f);
111 if (c == EOF) {
112 sr->f = NULL;
113 break;
114 } else if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
115 break;
116 } else if (c == '(' || c == ')' || c == '#') {
117 ungetc(c, sr->f);
118 break;
119 } else if (i < sizeof(sr->tokbuf) - 1)
120 sr->tokbuf[i++] = c;
121 }
122 }
123 sr->tokbuf[i] = 0;
124 if (i == 1)
125 sr->singlechar = sr->tokbuf[0];
126 return 1 + parse_double(sr);
127 }