1 /*
2  *  Eukleides version 1.5.4
3  *  Copyright (c) Christian Obrecht 2004-2010
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 3 of the License, or
8  *  (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., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <math.h>
24 #include "core.h"
25 #include "symbol.h"
26 #include "utils.h"
27 #include "error.h"
28 
29 	/* String table */
30 
31 typedef struct {
32     char **first;
33     char **last;
34     char **mark;
35     char **end;
36 } _strtab;
37 
38 _strtab tab;
39 
40 #define STRTAB_INIT_SIZE 64
41 
init_string_table(void)42 void init_string_table(void)
43 {
44     tab.first = (char **)malloc(STRTAB_INIT_SIZE * sizeof(char *));
45     check_mem(tab.first);
46     tab.last = tab.first;
47     tab.end = tab.first + STRTAB_INIT_SIZE;
48 }
49 
resize_string_table(void)50 void resize_string_table(void)
51 {
52     int size = tab.end - tab.first;
53 
54     tab.first = (char **)realloc(tab.first, 2 * size * sizeof(char *));
55     check_mem(tab.first);
56     tab.last = tab.first + size;
57     tab.end = tab.first + 2 * size;
58 }
59 
register_string(char * s)60 void register_string(char *s)
61 {
62     *(tab.last++) = s;
63     if (tab.last == tab.end) resize_string_table();
64 }
65 
reset_string_table(void)66 void reset_string_table(void)
67 {
68     tab.last = tab.first;
69 }
70 
71 	/* String buffer stack */
72 
73 typedef struct {
74     char *buf;
75     char *cur;
76     int size;
77 } _strbuf;
78 
79 typedef struct {
80     _strbuf *first;
81     _strbuf *last;
82     _strbuf *end;
83 } _bufstk;
84 
85 _bufstk stk;
86 
87 #define STRSTK_INIT_SIZE 64
88 
init_buffer_stack(void)89 void init_buffer_stack(void)
90 {
91     stk.first = (_strbuf *)malloc(STRSTK_INIT_SIZE*sizeof(_strbuf));
92     check_mem(stk.first);
93     stk.last = stk.first - 1;
94     stk.end = stk.first + STRSTK_INIT_SIZE;
95 }
96 
resize_buffer_stack(void)97 void resize_buffer_stack(void)
98 {
99     int size = stk.end - stk.first;
100 
101     stk.first = (_strbuf *)realloc(stk.first, 2*size*sizeof(_strbuf));
102     check_mem(stk.first);
103     stk.last = stk.first + size;
104     stk.end = stk.first + 2*size;
105 }
106 
init_string(void)107 void init_string(void)
108 {
109     init_string_table();
110     init_buffer_stack();
111 }
112 
113 	/* String buffer */
114 
115 #define BUF_INIT_SIZE	128
116 
start_buffer(void)117 void start_buffer(void)
118 {
119     stk.last++;
120     stk.last->buf = (char *)malloc(BUF_INIT_SIZE);
121     check_mem(stk.last->buf);
122     stk.last->cur = stk.last->buf;
123     stk.last->size = BUF_INIT_SIZE;
124     if (stk.last == stk.end) resize_buffer_stack();
125 }
126 
resize_buffer(int amount)127 void resize_buffer(int amount)
128 {
129     int pos, size;
130 
131     pos = stk.last->cur - stk.last->buf;
132     size = stk.last->size + amount;
133     while (stk.last->size < size) stk.last->size *= 2;
134     stk.last->buf = (char *)realloc(stk.last->buf, stk.last->size);
135     check_mem(stk.last->buf);
136     stk.last->cur = stk.last->buf + pos;
137 }
138 
add_string(char * s,int l)139 void add_string(char *s, int l)
140 {
141     if (stk.last->cur - stk.last->buf + l > stk.last->size) resize_buffer(l);
142     memcpy((void*)stk.last->cur, (void *)s, l);
143     stk.last->cur += l;
144 }
145 
add_char(char c)146 void add_char(char c)
147 {
148     if (stk.last->cur - stk.last->buf == stk.last->size) resize_buffer(1);
149     *(stk.last->cur++) = c;
150 }
151 
get_special_char(char c)152 char get_special_char(char c)
153 {
154     switch (c) {
155 	case 'n': return '\n';
156 	case 'r': return '\r';
157 	case 't': return '\t';
158     }
159     return c;
160 }
161 
add_special_char(char c)162 void add_special_char(char c)
163 {
164     add_char(get_special_char(c));
165 }
166 
get_buffer(void)167 char *get_buffer(void)
168 {
169     char *val;
170 
171     add_char('\0');
172     val = (char *)realloc(stk.last->buf, stk.last->size);
173     register_string(val);
174     stk.last--;
175     return val;
176 }
177 
178 	/* String functions */
179 
extract_substring(void)180 void extract_substring(void)
181 {
182     char *s;
183     double a, b;
184     int i, j;
185 
186     b = POPn;
187     a = POPn;
188     s = POPs;
189     i = (int)a;
190     j = (int)b;
191     if (a < 0 || b < a || j >= strlen(s)) runtime_error(_("invalid indices"));
192     start_buffer();
193     add_string(s + i, j - i + 1);
194     PSH(get_buffer());
195 }
196 
start_string(void)197 void start_string(void)
198 {
199     start_buffer();
200 }
201 
202 char tmp[31];
203 
cat_num(void)204 void cat_num(void)
205 {
206     double x;
207 
208     x = POPn;
209     add_string(tmp, snprintf(tmp, 14, "%g", fabs(x) < EPSILON ? 0 : x));
210 }
211 
cat_pnt(void)212 void cat_pnt(void)
213 {
214     _point *A;
215 
216     A = POP(_point);
217 #ifdef __euktopst__
218     add_string(tmp, snprintf(tmp, 31, "(%.4f, %.4f)", A->x, A->y));
219 #else
220     add_string(tmp, snprintf(tmp, 28, "%7.4f %7.4f", A->x, A->y));
221 #endif
222 }
223 
cat_set(void)224 void cat_set(void)
225 {
226     _set *s;
227 
228     s = POP(_set);
229 #ifndef __euktopst__
230     if (s != NULL) {
231 	add_string(tmp, snprintf(tmp, 28, "%7.4f %7.4f", s->p->x, s->p->y));
232 	s = s->next;
233     }
234 #endif
235     while (s != NULL) {
236 #ifdef __euktopst__
237 	add_string(tmp, snprintf(tmp, 31, "(%.4f, %.4f)", s->p->x, s->p->y));
238 #else
239 	add_string(tmp, snprintf(tmp, 29, " %7.4f %7.4f", s->p->x, s->p->y));
240 #endif
241 	s = s->next;
242     }
243 }
244 
cat_str(void)245 void cat_str(void)
246 {
247     char *s;
248 
249     s = POPs;
250     add_string(s, strlen(s));
251 }
252 
get_string(void)253 void get_string(void)
254 {
255     PSH(get_buffer());
256 }
257 
str_eq(void)258 void str_eq(void)
259 {
260     PSHn(strcmp(POPs, POPs) == 0);
261 }
262 
str_neq(void)263 void str_neq(void)
264 {
265     PSHn(strcmp(POPs, POPs) != 0);
266 }
267 
str_length(void)268 void str_length(void)
269 {
270     PSHn((double)strlen(POPs));
271 }
272 
get_output_name(char * in,const char * suf)273 char* get_output_name(char* in, const char* suf)
274 {
275     int l;
276     char* out;
277 
278     l = strlen(in);
279     if (l >= 4 && strcmp(in + l - 4, ".euk") == 0) l -= 4;
280     out = (char *)malloc(l + 5);
281     strncpy(out, in, l);
282     strncpy(out + l, suf, 5);
283     return out;
284 }
285