Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/agcore/stacks.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children | 57b2cc9b87f7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, A System for Syntax Directed Programming | |
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved. | |
4 * See the file COPYING for license and usage terms. | |
5 * | |
6 * stacks.cpp | |
7 */ | |
8 | |
9 #include <stdarg.h> | |
10 #include <stdio.h> | |
11 #include <string.h> | |
12 #include "port.h" | |
13 | |
14 #ifdef VACLGUI | |
15 #include <icritsec.hpp> | |
16 #include "resource.h" | |
17 #endif | |
18 | |
19 #include "agstring.h" | |
20 #include "assert.h" | |
21 #include "dict.h" | |
22 #include "myalloc.h" | |
23 #include "stacks.h" | |
24 | |
25 //#define INCLUDE_LOGGING | |
26 #include "log.h" | |
27 | |
28 | |
29 #define cs char_strings_base | |
30 #define ws word_strings_base | |
31 #define ls long_strings_base | |
32 #define is int_stack_base | |
33 #define ni int_stack_top | |
34 | |
35 static char *cs; | |
36 static int *ws; | |
37 static int *is; | |
38 static unsigned lcs,lws, lis; | |
39 unsigned ni; | |
40 unsigned nc; | |
41 unsigned nw; | |
42 static unsigned lcx; | |
43 static unsigned lwx; | |
44 static unsigned llx; | |
45 char *string_base; | |
46 int *list_base; | |
47 | |
48 #ifdef VACLGUI | |
49 static AgResource charStackResource; | |
50 #endif | |
51 | |
52 | |
53 void init_stk(void) { | |
54 LOGSECTION("init_stk"); | |
55 lcs = 100; | |
56 lws = 100; | |
57 lis = 20; | |
58 cs = (char *) myalloc(lcs); | |
59 ws = (int *) myalloc(lws * sizeof(*ws)); | |
60 is = (int *) myalloc((lis+1) * sizeof(*is)); | |
61 nc = nw = ni = lcx = lwx = llx = 0; | |
62 } | |
63 | |
64 /* | |
65 void logStringStack() { | |
66 //LOGSECTION("Stack status"); | |
67 log.line() << nc << nw << ni; | |
68 log.line() << lcs << lws << lis; | |
69 } | |
70 */ | |
71 /* | |
72 void delete_stack(void) { | |
73 DEALLOCATE(cs); | |
74 DEALLOCATE(ws); | |
75 DEALLOCATE(is); | |
76 } | |
77 */ | |
78 /* | |
79 void slide_stack(void) { | |
80 cs = (char *) myrealloc(cs, lcs, __FILE__, __LINE__); | |
81 ws = (int *) myrealloc(ws, lws * sizeof(*ws), __FILE__, __LINE__); | |
82 is = (int *) myrealloc(is, (lis+1) * sizeof(*is), __FILE__, __LINE__); | |
83 } | |
84 */ | |
85 void reset_stk(void) { | |
86 nc = nw = ni = lcx = lwx = llx = 0; | |
87 } | |
88 | |
89 /* | |
90 void slns(const char *s) { / * extract line from string * / | |
91 int n; | |
92 const char *sp; | |
93 | |
94 ics(); | |
95 n = 0; | |
96 if (s) for (sp = s; *sp && *sp != '\n'; sp++) { | |
97 if (*sp == '\t') do acs(' '); while (++n % tab_spacing); | |
98 else acs(*sp), n++; | |
99 } | |
100 cs[nc] = 0; | |
101 } | |
102 */ | |
103 | |
104 char *string_space(int n) { | |
105 if (nc + n >= lcs) { | |
106 #ifdef VACLGUI | |
107 AgLock lock(charStackResource); | |
108 #endif | |
109 LOGSECTION("string_space::resize"); | |
110 LOGV(lcs) LCV(nc) LCV(n); | |
111 | |
112 unsigned k = lcs + (lcs >> 1) + n + 1; | |
113 if (k > MAX_BYTES) k = (unsigned) MAX_BYTES; | |
114 lcs = k - 1; | |
115 LOGV(lcs) LCV(nc) LCV(n); | |
116 assert(lcs >= nc+n); | |
117 cs = reallocate(cs,k,char); | |
118 string_base = &cs[lcx]; | |
119 } | |
120 return &cs[nc]; | |
121 } | |
122 | |
123 int *list_space(int n) { | |
124 if (nw + n >= lws) { | |
125 #ifdef VACLGUI | |
126 AgLock lock(charStackResource); | |
127 #endif | |
128 unsigned k = lws + (lws >> 1) + n + 1; | |
129 if (k > MAX_INTS) { | |
130 k = (unsigned) MAX_INTS; | |
131 } | |
132 lws = k - 1; | |
133 LOGSECTION("list_space::resize"); | |
134 LOGV(lws) LCV(nw+n); | |
135 assert(lws >= nw+n); | |
136 REALLOCATE_ST(ws,k); | |
137 list_base = &ws[lwx]; | |
138 } | |
139 return &ws[nw]; | |
140 } | |
141 | |
142 void sss(const char *s) { | |
143 #ifdef VACLGUI | |
144 AgLock lock(charStackResource); | |
145 #endif | |
146 int n; | |
147 | |
148 assert(s != NULL); | |
149 n = strlen(s); | |
150 if (ni + 1 >= lis) { | |
151 unsigned k = lis + 3 + (lis >> 1); | |
152 if (k > MAX_INTS) { | |
153 k = (unsigned) MAX_INTS; | |
154 } | |
155 lis = k-1; | |
156 LOGSECTION("sss::resize"); | |
157 LOGV(lis) LCV(ni+2); | |
158 assert(lis >= ni+2); | |
159 REALLOCATE_ST(is,k); | |
160 } | |
161 is[ni++] = lcx; | |
162 if (nc + n >= lcs) { | |
163 unsigned k = lcs + (lcs >> 1) + n + 1; | |
164 if (k > MAX_BYTES) { | |
165 k = (unsigned) MAX_BYTES; | |
166 } | |
167 lcs = k - 1; | |
168 LOGSECTION("sss::resize"); | |
169 LOGV(lcs) LCV(nc+n); | |
170 assert(lcs >= nc + n); | |
171 cs = reallocate(cs,k,char); | |
172 } | |
173 lcx = nc; | |
174 string_base = &cs[lcx]; | |
175 strncpy(string_base,s,n); | |
176 nc += is[ni++] = n; | |
177 cs[nc] = 0; | |
178 } | |
179 | |
180 void ass(const char *s) { | |
181 #ifdef VACLGUI | |
182 AgLock lock(charStackResource); | |
183 #endif | |
184 int n; | |
185 | |
186 assert(s !=NULL); | |
187 assert(ni>=2); | |
188 n = strlen(s); | |
189 if (nc + n >= lcs) { | |
190 unsigned k = lcs + (lcs >> 1) + n + 1; | |
191 if (k > MAX_BYTES) { | |
192 k = (unsigned) MAX_BYTES; | |
193 } | |
194 lcs = k-1; | |
195 LOGSECTION("ass::resize"); | |
196 LOGV(lcs) LCV(nc+n); | |
197 assert(lcs >= nc + n); | |
198 cs = reallocate(cs,k,char); | |
199 string_base = &cs[lcx]; | |
200 } | |
201 strncpy(&cs[nc], s, n); | |
202 nc += n; | |
203 is[ni-1] += n; | |
204 cs[nc] = 0; | |
205 } | |
206 | |
207 /* | |
208 static void extend_string(int n) { | |
209 LOGSECTION("extend_string"); | |
210 LOGV(nc) LCV(n); | |
211 is[ni-1] += n; | |
212 nc += n; | |
213 assert(nc < lcs); | |
214 } | |
215 */ | |
216 | |
217 int ssprintf(const char *fs, ...) { | |
218 #ifdef VACLGUI | |
219 AgLock lock(charStackResource); | |
220 #endif | |
221 va_list ap; | |
222 int n; | |
223 | |
224 check_stack; | |
225 if (ni + 1 >= lis) { | |
226 unsigned k = lis + 3 + (lis >> 1); | |
227 if (k > MAX_INTS) { | |
228 k = (unsigned) MAX_INTS; | |
229 } | |
230 lis = k - 1; | |
231 LOGSECTION("ssprintf::resize"); | |
232 LOGV(lis) LCV(ni+2); | |
233 assert(lis >= ni+2); | |
234 REALLOCATE_ST(is,k); | |
235 } | |
236 is[ni++] = lcx; | |
237 string_space(500); | |
238 lcx = nc; | |
239 string_base = &cs[lcx]; | |
240 va_start(ap,fs); | |
241 n = is[ni++] = vsprintf(&cs[nc],fs,ap); | |
242 nc += n; | |
243 assert(nc < lcs); | |
244 va_end(ap); | |
245 return n; | |
246 } | |
247 | |
248 int apprintf(const char *fs, ...) { | |
249 #ifdef VACLGUI | |
250 AgLock lock(charStackResource); | |
251 #endif | |
252 va_list ap; | |
253 int n; | |
254 | |
255 assert(ni>=2); | |
256 string_space(500); | |
257 va_start(ap,fs); | |
258 is[ni-1] += (n = vsprintf(&cs[nc],fs,ap)); | |
259 nc += n; | |
260 assert(nc < lcs); | |
261 va_end(ap); | |
262 return n; | |
263 } | |
264 | |
265 void tss(void) { | |
266 cs[nc] = 0; | |
267 } | |
268 | |
269 void ics(void) { | |
270 #ifdef VACLGUI | |
271 AgLock lock(charStackResource); | |
272 #endif | |
273 //check_stack; | |
274 if (ni + 2 > lis) { | |
275 unsigned k = lis + 3 + (lis >> 1); | |
276 if (k > MAX_INTS) { | |
277 k = (unsigned) MAX_INTS; | |
278 } | |
279 lis = k - 1; | |
280 LOGSECTION("ics::resize"); | |
281 LOGV(lis) LCV(ni+2); | |
282 assert(lis >= ni+2); | |
283 REALLOCATE_ST(is,k); | |
284 } | |
285 is[ni++] = lcx; | |
286 lcx = nc; | |
287 string_base = &cs[lcx]; | |
288 is[ni++] = 0; | |
289 cs[nc] = 0; | |
290 } | |
291 | |
292 void scs(int c) { | |
293 #ifdef VACLGUI | |
294 AgLock lock(charStackResource); | |
295 #endif | |
296 //check_stack; | |
297 if (ni + 1 >= lis) { | |
298 unsigned k = lis + 3 + (lis >> 1); | |
299 if (k > MAX_INTS) { | |
300 k = (unsigned) MAX_INTS; | |
301 } | |
302 lis = k - 1; | |
303 LOGSECTION("scs::resize"); | |
304 LOGV(lis) LCV(ni+2); | |
305 assert(lis >= ni+2); | |
306 REALLOCATE_ST(is,k); | |
307 } | |
308 is[ni++] = lcx; | |
309 if (nc + 1 >= lcs) { | |
310 unsigned k = lcs + 2 + (lcs >> 1); | |
311 if (k > MAX_BYTES) { | |
312 k = (unsigned) MAX_BYTES; | |
313 } | |
314 lcs = k - 1; | |
315 LOGSECTION("scs::resize"); | |
316 LOGV(lcs) LCV(nc+1); | |
317 assert(lcs >= nc+1); | |
318 cs = reallocate(cs,k,char); | |
319 } | |
320 lcx = nc; | |
321 string_base = &cs[lcx]; | |
322 cs[nc++] = (char) c; | |
323 is[ni++] = 1; | |
324 cs[nc] = 0; | |
325 } | |
326 | |
327 #if 0 /* NOTUSED */ | |
328 void ist(void) { /* init string table */ | |
329 check_stack; | |
330 if (ni + 4 >= lis) { | |
331 unsigned k = lis + 5 + (lis >> 1); | |
332 if (k > MAX_INTS) { | |
333 k = (unsigned) MAX_INTS; | |
334 } | |
335 lis = k - 1; | |
336 assert(lis >= ni+4); | |
337 REALLOCATE_ST(is,k); | |
338 } | |
339 is[ni++] = lwx; | |
340 is[ni++] = lcx; | |
341 lwx = nw; | |
342 list_base = &ws[lwx]; | |
343 is[ni++] = nc; | |
344 lcx = nc; | |
345 string_base = &cs[lcx]; | |
346 is[ni++] = 0; | |
347 ws[nw++] = 0; | |
348 } | |
349 #endif /* 0 - NOTUSED */ | |
350 | |
351 void iws(void) { /* init word stack */ | |
352 #ifdef VACLGUI | |
353 AgLock lock(charStackResource); | |
354 #endif | |
355 //check_stack; | |
356 if (ni + 2 >= lis) { | |
357 unsigned k = lis + 3 + (lis >> 1); | |
358 if (k > MAX_INTS) { | |
359 k = (unsigned) MAX_INTS; | |
360 } | |
361 lis = k - 1; | |
362 LOGSECTION("iws::resize"); | |
363 LOGV(lis) LCV(ni+2); | |
364 assert(lis >= ni+2); | |
365 REALLOCATE_ST(is,k); | |
366 } | |
367 is[ni++] = lwx; | |
368 lwx = nw; | |
369 list_base = &ws[lwx]; | |
370 is[ni++] = 0; | |
371 } | |
372 | |
373 void sws(int c) { /* store word on stack */ | |
374 #ifdef VACLGUI | |
375 AgLock lock(charStackResource); | |
376 #endif | |
377 //check_stack; | |
378 if (ni + 1 >= lis) { | |
379 unsigned k = lis + 3 + (lis >> 1); | |
380 if (k > MAX_INTS) { | |
381 k = (unsigned) MAX_INTS; | |
382 } | |
383 lis = k - 1; | |
384 LOGSECTION("sws::resize"); | |
385 LOGV(lis) LCV(ni+2); | |
386 assert(lis >= ni+2); | |
387 REALLOCATE_ST(is,k); | |
388 } | |
389 is[ni++] = lwx; | |
390 if (nw >= lws) { | |
391 unsigned k = lws + 2 + (lws >> 1); | |
392 if (k > MAX_INTS) { | |
393 k = (unsigned) MAX_INTS; | |
394 } | |
395 lws = k - 1; | |
396 LOGSECTION("sws::resize"); | |
397 LOGV(lws) LCV(nw+2); | |
398 assert(lws >= nw+1); | |
399 REALLOCATE_ST(ws,k); | |
400 } | |
401 lwx = nw; | |
402 list_base = &ws[lwx]; | |
403 ws[nw++] = c; | |
404 is[ni++] = 1; | |
405 } | |
406 | |
407 int rcs(void) { | |
408 #ifdef VACLGUI | |
409 AgLock lock(charStackResource); | |
410 #endif | |
411 //LOGSECTION("rcs"); | |
412 int n; | |
413 | |
414 assert(ni >= 2); | |
415 n = is[--ni]; | |
416 LOGV(nc) LCV(lcx) LCV(n); | |
417 assert(nc == lcx + n); | |
418 cs[nc] = 0; | |
419 nc = lcx; | |
420 lcx = is[--ni]; | |
421 string_base = &cs[lcx]; | |
422 return n; | |
423 } | |
424 | |
425 int fps(FILE *f) { /* put string to file */ | |
426 int n; | |
427 | |
428 char *sb; | |
429 sb = string_base; | |
430 n = rcs(); | |
431 fputs(sb,f); | |
432 return n; | |
433 } | |
434 | |
435 int rps(void) { | |
436 #ifdef VACLGUI | |
437 AgLock lock(charStackResource); | |
438 #endif | |
439 int n; | |
440 | |
441 assert(ni >= 2); | |
442 n = 2*is[--ni]; | |
443 assert(nw == lwx + n); | |
444 nw = lwx; | |
445 lwx = is[--ni]; | |
446 list_base = &ws[lwx]; | |
447 return n; | |
448 } | |
449 | |
450 int rws(void) { | |
451 #ifdef VACLGUI | |
452 AgLock lock(charStackResource); | |
453 #endif | |
454 int n; | |
455 | |
456 assert(ni >= 2); | |
457 n = is[--ni]; | |
458 assert(nw == lwx + n); | |
459 nw = lwx; | |
460 lwx = is[--ni]; | |
461 list_base = &ws[lwx]; | |
462 return n; | |
463 } | |
464 | |
465 | |
466 void sis(int i) { | |
467 #ifdef VACLGUI | |
468 AgLock lock(charStackResource); | |
469 #endif | |
470 //check_stack; | |
471 if (ni >= lis) { | |
472 unsigned k = lis + 2 + (lis >> 1); | |
473 if (k > MAX_INTS) { | |
474 k = (unsigned) MAX_INTS; | |
475 } | |
476 lis = k - 1; | |
477 LOGSECTION("sis::resize"); | |
478 LOGV(lis) LCV(ni+2); | |
479 assert (lis >= ni+1); | |
480 REALLOCATE_ST(is,k); | |
481 } | |
482 is[ni++] = i; | |
483 } | |
484 | |
485 | |
486 int tis(void) { | |
487 assert(ni >= 1); | |
488 return is[ni-1]; | |
489 } | |
490 | |
491 int fis(void) { | |
492 #ifdef VACLGUI | |
493 AgLock lock(charStackResource); | |
494 #endif | |
495 assert(ni >= 1); | |
496 return is[--ni]; | |
497 } | |
498 | |
499 void concat_string(void) { | |
500 #ifdef VACLGUI | |
501 AgLock lock(charStackResource); | |
502 #endif | |
503 // check_stack; | |
504 assert(ni >= 4); | |
505 ni -= 2; | |
506 lcx = is[ni]; | |
507 string_base = &cs[lcx]; | |
508 is[ni-1] += is[ni+1]; | |
509 } | |
510 | |
511 void concat_list(void) { | |
512 #ifdef VACLGUI | |
513 AgLock lock(charStackResource); | |
514 #endif | |
515 //check_stack; | |
516 assert(ni >= 2); | |
517 ni -= 2; | |
518 lwx = is[ni]; | |
519 list_base = &ws[lwx]; | |
520 is[ni-1] += is[ni+1]; | |
521 } | |
522 | |
523 /* | |
524 void its(char c,int n) { | |
525 assert(ni>=2); | |
526 while (n > is[ni-1]) acs(' '); | |
527 if (n == is[ni-1]) {acs(c); return;} | |
528 if (nc + 1 >= lcs) { | |
529 unsigned k = lcs + 1 + (lcs >> 1); | |
530 if (k > MAX_BYTES) { | |
531 k = (unsigned) MAX_BYTES; | |
532 } | |
533 lcs = k - 1; | |
534 assert(lcs > nc); | |
535 cs = reallocate(cs,k,char); | |
536 string_base = &cs[lcx]; | |
537 } | |
538 memmove(&cs[lcx+n+1],&cs[lcx+n],sizeof(*cs)*(is[ni-1]-n)); | |
539 cs[lcx+n] = c; | |
540 (is[ni-1])++; | |
541 cs[++nc] = 0; | |
542 } | |
543 */ | |
544 | |
545 void acs(int c) { | |
546 #ifdef VACLGUI | |
547 AgLock lock(charStackResource); | |
548 #endif | |
549 assert(ni>=2); | |
550 if (nc + 1 >= lcs) { | |
551 unsigned k = lcs + 2 + (lcs >> 1); | |
552 if (k > MAX_BYTES) { | |
553 k = (unsigned) MAX_BYTES; | |
554 } | |
555 lcs = k - 1; | |
556 LOGSECTION("acs::resize"); | |
557 LOGV(lcs) LCV(nc); | |
558 assert(lcs > nc); | |
559 cs = reallocate(cs,k,char); | |
560 string_base = &cs[lcx]; | |
561 } | |
562 cs[nc++] = (char) c; | |
563 cs[nc] = 0; | |
564 (is[ni-1])++; | |
565 } | |
566 | |
567 int isws(int c) { | |
568 #ifdef VACLGUI | |
569 AgLock lock(charStackResource); | |
570 #endif | |
571 int bx, tx; | |
572 unsigned nx; | |
573 check_stack; | |
574 assert(ni>=2); | |
575 if (nw >= lws) { | |
576 unsigned k = lws + 2 + (lws >> 1); | |
577 if (k > MAX_INTS) { | |
578 k = (unsigned) MAX_INTS; | |
579 } | |
580 lws = k-1; | |
581 LOGSECTION("isws::resize"); | |
582 LOGV(lws) LCV(nw); | |
583 assert(lws > nw); | |
584 REALLOCATE_ST(ws,k); | |
585 list_base = &ws[lwx]; | |
586 } | |
587 if (nw == lwx) { | |
588 ws[nw++] = c; | |
589 is[ni-1]++; | |
590 return 0; | |
591 } | |
592 bx = lwx; | |
593 tx = nw-1; | |
594 while (bx <= tx) { | |
595 nx = (bx+tx)/2; | |
596 if (ws[nx] > c) { | |
597 tx = nx-1; | |
598 } | |
599 else if (ws[nx] < c) { | |
600 bx = nx + 1; | |
601 } | |
602 else { | |
603 return 1; | |
604 } | |
605 } | |
606 if (nw > (unsigned) bx) { | |
607 memmove(ws+bx+1,ws+bx, (nw-bx)*sizeof(*ws)); | |
608 } | |
609 ws[bx] = c; | |
610 nw++; | |
611 (is[ni-1])++; | |
612 return 0; | |
613 } | |
614 | |
615 int xws(int c) { | |
616 #ifdef VACLGUI | |
617 AgLock lock(charStackResource); | |
618 #endif | |
619 int k; | |
620 int *p; | |
621 | |
622 assert(ni>=2); | |
623 if (nw >= lws) { | |
624 unsigned k = lws + 2 + (lws >> 1); | |
625 if (k > MAX_INTS) { | |
626 k = (unsigned) MAX_INTS; | |
627 } | |
628 lws = k - 1; | |
629 LOGSECTION("xws::resize"); | |
630 LOGV(lws) LCV(nw); | |
631 assert(lws > nw); | |
632 REALLOCATE_ST(ws,k); | |
633 list_base = &ws[lwx]; | |
634 } | |
635 k = nw - lwx; | |
636 p = list_base; | |
637 while (k--) { | |
638 if (c == *p++) { | |
639 return 1; | |
640 } | |
641 } | |
642 ws[nw++] = c; | |
643 (is[ni-1])++; | |
644 return 0; | |
645 } | |
646 | |
647 int xps(int c1,int c2) { | |
648 #ifdef VACLGUI | |
649 AgLock lock(charStackResource); | |
650 #endif | |
651 unsigned i; | |
652 int *p; | |
653 int flag; | |
654 | |
655 // some compilers fail to be able to unravel the loop below to show | |
656 // that this isn't necessary | |
657 flag = -1; | |
658 | |
659 assert(ni>=2); | |
660 if (nw + 2 >= lws) { | |
661 unsigned k = lws + 3 + (lws >> 1); | |
662 if (k > MAX_INTS) { | |
663 k = (unsigned) MAX_INTS; | |
664 } | |
665 lws = k - 1; | |
666 LOGSECTION("xps::resize"); | |
667 LOGV(lws) LCV(nw + 2); | |
668 assert(lws >= nw + 2); | |
669 REALLOCATE_ST(ws,k); | |
670 list_base = &ws[lwx]; | |
671 } | |
672 for (p = list_base, i = lwx; | |
673 i < nw && ((flag = p[0] - c1) < 0 || | |
674 (flag == 0 && (flag = p[1] - c2) < 0)); | |
675 p += 2, i += 2); | |
676 if (i < nw && flag == 0) { | |
677 return 1; | |
678 } | |
679 memmove(p+2, p, (nw - i)* sizeof(*p)); | |
680 *p++ = c1; *p = c2; | |
681 nw += 2; | |
682 (is[ni-1])++; | |
683 return 0; | |
684 } | |
685 | |
686 void aws(int c) { | |
687 #ifdef VACLGUI | |
688 AgLock lock(charStackResource); | |
689 #endif | |
690 assert(ni>=2); | |
691 if (nw >= lws) { | |
692 unsigned k = lws + 2 + (lws >> 1); | |
693 if (k > MAX_INTS) { | |
694 k = (unsigned) MAX_INTS; | |
695 } | |
696 lws = k - 1; | |
697 LOGSECTION("aws::resize"); | |
698 LOGV(lws) LCV(nw); | |
699 assert(lws > nw); | |
700 REALLOCATE_ST(ws,k); | |
701 list_base = &ws[lwx]; | |
702 } | |
703 ws[nw++] = c; | |
704 (is[ni-1])++; | |
705 } | |
706 | |
707 char *build_string(void) { | |
708 #ifdef VACLGUI | |
709 AgLock lock(charStackResource); | |
710 #endif | |
711 char *h; | |
712 unsigned n; | |
713 | |
714 check_stack; | |
715 assert(ni >= 2); | |
716 n = is[--ni]; | |
717 lcx = is[--ni]; | |
718 string_base = &cs[lcx]; | |
719 assert(nc >= n); | |
720 nc -= n; | |
721 h = ALLOCATE(n+1, char); | |
722 memmove(h, cs + nc, n*sizeof(*h)); | |
723 h[n] = 0; | |
724 return h; | |
725 } | |
726 | |
727 AgString buildAgString(void) { | |
728 char *sb; | |
729 sb = string_base; | |
730 return AgString(sb,rcs()); | |
731 } | |
732 | |
733 AgArray<int> buildStaticList(void) { | |
734 #ifdef VACLGUI | |
735 AgLock lock(charStackResource); | |
736 #endif | |
737 unsigned n; | |
738 | |
739 //check_stack; | |
740 assert(ni >= 1); | |
741 n = is[--ni]; | |
742 lwx = is[--ni]; | |
743 list_base = &ws[lwx]; | |
744 assert(nw >= n); | |
745 nw -= n; | |
746 return AgArray<int>(ws+nw, n); | |
747 } | |
748 | |
749 int *build_list(void) { | |
750 #ifdef VACLGUI | |
751 AgLock lock(charStackResource); | |
752 #endif | |
753 int *h; | |
754 unsigned n; | |
755 | |
756 //check_stack; | |
757 assert(ni >= 1); | |
758 n = is[--ni]; | |
759 lwx = is[ni-1]; | |
760 list_base = &ws[lwx]; | |
761 assert(nw >= n); | |
762 nw -= n; | |
763 is[ni-1] = n; | |
764 if (n == 0) h = NULL; | |
765 else ALLOCATE_ST(h, n); | |
766 memmove(h,ws + nw, n*sizeof(*ws)); | |
767 return h; | |
768 } | |
769 /* | |
770 static void rtbc(void) { | |
771 assert(ni>=2); | |
772 if (cs[nc-1] != ' ') return; | |
773 nc--; | |
774 is[ni-1]--; | |
775 cs[nc] = 0; | |
776 } | |
777 */ | |
778 | |
779 int idl(list_dict *d) { | |
780 #ifdef VACLGUI | |
781 AgLock lock(charStackResource); | |
782 #endif | |
783 int k,nsx; | |
784 int *lb; | |
785 | |
786 check_stack; | |
787 assert(ni>=2); | |
788 nsx = d->nsx; | |
789 lb = list_base; | |
790 k = add_list_dict(lb, rws(), d); | |
791 sis(k); | |
792 return k == nsx; | |
793 } | |
794 | |
795 int fws(void) { | |
796 #ifdef VACLGUI | |
797 AgLock lock(charStackResource); | |
798 #endif | |
799 assert(nw > 0); | |
800 is[ni-1]--; | |
801 return(ws[--nw]); | |
802 } | |
803 | |
804 void fdl(list_dict *d,unsigned k) { | |
805 #ifdef VACLGUI | |
806 AgLock lock(charStackResource); | |
807 #endif | |
808 int n, *l; | |
809 | |
810 assert(k < d->nsx); | |
811 iws(); | |
812 l = d->text + d->ndx[k]; | |
813 n = *l++ - 1; | |
814 if (nw + n >= lws) { | |
815 unsigned k = lws + n + 1 + (lws >> 1); | |
816 if (k > MAX_INTS) { | |
817 k = (unsigned) MAX_INTS; | |
818 } | |
819 lws = k - 1; | |
820 LOGSECTION("fdl::resize"); | |
821 LOGV(lws) LCV(nw + n); | |
822 assert(lws >= nw+n); | |
823 REALLOCATE_ST(ws,k); | |
824 list_base = &ws[lwx]; | |
825 } | |
826 memmove(list_base, l, n*sizeof(*list_base)); | |
827 nw += n; | |
828 is[ni-1] = n; | |
829 } | |
830 | |
831 //int idsx(string_dict *); | |
832 /* | |
833 int ids(string_dict *d) { | |
834 rtbc(); | |
835 return idsx(d); | |
836 } | |
837 | |
838 static int idsx(string_dict *d) { | |
839 int k,nsx; | |
840 char *sb; | |
841 | |
842 check_stack; | |
843 assert(ni>=2); | |
844 cs[nc] = 0; | |
845 sb = string_base; | |
846 rcs(); | |
847 nsx = d->nsx; | |
848 k = add_string_dict(sb, d); | |
849 is[ni++] = k; | |
850 cs[nc] = 0; | |
851 return k == nsx; | |
852 } | |
853 */ |