|
|
1.1 root 1: /*ident "@(#)ctrans:demangler/String.c 1.2"*/
2: /*
3: * C++ Demangler Source Code
4: * @(#)master 1.5
5: * 7/27/88 13:54:37
6: */
7: #include "String.h"
8: #include <stdio.h>
9: #include <assert.h>
10: extern char *malloc();
11: /* This code emulates the C++ String package
12: * in a crude way.
13: */
14:
15: /* This function will expand the space
16: * availabe to a String so that more data
17: * can be appended to it
18: */
19: static String *
20: grow(s)
21: String *s;
22: {
23: int sz = s->sg.max * 2;
24: assert(sz > 0);
25: s = (String *)realloc(s,sz + sizeof(StringGuts)+1);
26: assert(s != 0);
27: s->sg.max = sz;
28: return s;
29: }
30:
31: /* This function will expand the space
32: * available to a String so that more data
33: * can be prepended to it.
34: */
35: static String *
36: ror(s,n)
37: String *s;
38: int n;
39: {
40: int i;
41: assert(s != 0);
42: while(s->sg.end + n > s->sg.max)
43: s = grow(s);
44: for(i = s->sg.end - 1;i >= s->sg.start;i--)
45: s->data[i+n] = s->data[i];
46: s->sg.end += n;
47: s->sg.start += n;
48: s->data[s->sg.end] = 0;
49: return s;
50: }
51:
52: /* This function will prepend c
53: * to s
54: */
55: String *
56: prep_String(c,s)
57: char *c;
58: String *s;
59: {
60: int n = strlen(c);
61: return nprep_String(c,s,n);
62: }
63:
64: /* This function will prepend the
65: * first n characters of c to s
66: */
67: String *
68: nprep_String(c,s,n)
69: char *c;
70: String *s;
71: int n;
72: {
73: int len = strlen(c);
74: assert(s != 0);
75: if(len > n)
76: len = n;
77: if(len >= s->sg.start)
78: s = ror(s,len);
79: while(len--)
80: s->data[--(s->sg.start)] = c[len];
81: s->data[s->sg.end] = '\0';
82: return s;
83: }
84:
85: /* This function will append
86: * c to s.
87: */
88: String *
89: app_String(s,c)
90: String *s;
91: char *c;
92: {
93: int n = strlen(c);
94: s = napp_String(s,c,n);
95: return s;
96: }
97:
98: /* This function will append the
99: * first n characters of c to s
100: */
101: String *
102: napp_String(s,c,n)
103: String *s;
104: char *c;
105: {
106: int len = strlen(c);
107: int catlen;
108: assert(s != 0);
109: if(n < len)
110: len = n;
111: catlen = s->sg.end + len;
112: while(catlen > s->sg.max)
113: s = grow(s);
114: while(len--)
115: s->data[s->sg.end++] = *c++;
116: s->data[s->sg.end] = '\0';
117: assert(s->sg.end == catlen);
118: return s;
119: }
120:
121: /* This function initializes a
122: * String. It returns
123: * its argument is non-zero.
124: * This prevents the same string
125: * from being re-initialized.
126: */
127: String *
128: mk_String(s)
129: String *s;
130: {
131: if(s)
132: return s;
133: s = (String *)malloc(STRING_START + sizeof(StringGuts)+1);
134: assert(s != 0);
135: s->sg.start = s->sg.end = STRING_START/2;
136: s->sg.max = STRING_START;
137: s->data[s->sg.end] = '\0';
138: return s;
139: }
140:
141: void
142: free_String(s)
143: String *s;
144: {
145: if(s) free(s);
146: }
147:
148: /* This function copies
149: * c into s
150: */
151: String *
152: set_String(s,c)
153: String *s;
154: char *c;
155: {
156: int len = strlen(c)*2;
157: while(len > s->sg.max)
158: s = grow(s);
159: s->sg.start = s->sg.end = s->sg.max / 2;
160: s = app_String(s,c);
161: return s;
162: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.