1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 /* Lefteris Koutsofios - AT&T Labs Research */
15 
16 #include "common.h"
17 #include "code.h"
18 #include "mem.h"
19 
20 Code_t *cbufp;
21 int cbufn, cbufi;
22 #define CBUFINCR 1000
23 #define CBUFSIZE sizeof (Code_t)
24 
25 static int Cstringoffset;
26 
Cinit(void)27 void Cinit (void) {
28     Code_t c;
29 
30     cbufp = Marrayalloc ((long) CBUFINCR * CBUFSIZE);
31     cbufn = CBUFINCR;
32     cbufi = 0;
33     Cstringoffset = (char *) &c.u.s[0] - (char *) &c + 1;
34     /* the + 1 above accounts for the null character */
35 }
36 
Cterm(void)37 void Cterm (void) {
38     Marrayfree (cbufp);
39     cbufp = NULL;
40     cbufn = cbufi = 0;
41 }
42 
Creset(void)43 void Creset (void) {
44     cbufi = 0;
45 }
46 
Cnew(int ctype)47 int Cnew (int ctype) {
48     int i;
49 
50     if (cbufi >= cbufn) {
51         cbufp = Marraygrow (cbufp, (long) (cbufn + CBUFINCR) * CBUFSIZE);
52         cbufn += CBUFINCR;
53     }
54     i = cbufi++;
55     cbufp[i].ctype = ctype;
56     cbufp[i].next = C_NULL;
57     return i;
58 }
59 
Cinteger(long i)60 int Cinteger (long i) {
61     int j;
62 
63     if (cbufi >= cbufn) {
64         cbufp = Marraygrow (cbufp, (long) (cbufn + CBUFINCR) * CBUFSIZE);
65         cbufn += CBUFINCR;
66     }
67     j = cbufi++;
68     cbufp[j].ctype = C_INTEGER;
69     cbufp[j].u.i = i;
70     cbufp[j].next = C_NULL;
71     return j;
72 }
73 
Creal(double d)74 int Creal (double d) {
75     int i;
76 
77     if (cbufi >= cbufn) {
78         cbufp = Marraygrow (cbufp, (long) (cbufn + CBUFINCR) * CBUFSIZE);
79         cbufn += CBUFINCR;
80     }
81     i = cbufi++;
82     cbufp[i].ctype = C_REAL;
83     cbufp[i].u.d = d;
84     cbufp[i].next = C_NULL;
85     return i;
86 }
87 
Cstring(char * s)88 int Cstring (char *s) {
89     int i, size, incr;
90 
91     size = (strlen (s) + Cstringoffset + CBUFSIZE - 1) / CBUFSIZE;
92     if (cbufi + size > cbufn) {
93         incr = size > CBUFINCR ? size : CBUFINCR;
94         cbufp = Marraygrow (cbufp, (long) (cbufn + incr) * CBUFSIZE);
95         cbufn += incr;
96     }
97     i = cbufi, cbufi += size;
98     cbufp[i].ctype = C_STRING;
99     strcpy ((char *) &cbufp[i].u.s[0], s);
100     cbufp[i].next = C_NULL;
101     return i;
102 }
103