Debian Codegraph @

terps/tads/tads3/vmstrref.h

raw symbols SCIP
1 /* $Header$ */
2
3 /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */
4 /*
5 Name
6 vmstrref.h - reference-counted string class
7 Function
8
9 Notes
10
11 Modified
12 08/11/10 MJRoberts - Creation
13 */
14
15 #ifndef VMSTRREF_H
16 #define VMSTRREF_H
17
18 #include "osifcnet.h"
19
20
21 class StringRef: public CVmRefCntObj
22 {
23 public:
24 /* create by copying a literal string */
25 StringRef() { init(0, 0, 256); }
26 StringRef(long init_alo) { init(0, 0, init_alo); }
27 StringRef(const char *s)
28 {
29 size_t l = strlen(s);
30 init(s, l, l);
31 }
32 StringRef(const char *s, long l)
33 { init(s, l, l); }
34 StringRef(const char *s, long l, long init_alo)
35 { init(s, l, init_alo); }
36
37 /* get the string's contents and length */
38 char *get() const { return str; }
39 char *getend() const { return str + len; }
40 long getlen() const { return len; }
41
42 void setlen(long len) { this->len = len; }
43 void addlen(long len) { this->len += len; }
44
45 /* create from a sprintf-style format */
46 static StringRef *format(const char *fmt, ...)
47 {
48 /* create an empty StringRef to hold the new string */
49 StringRef *s = new StringRef();
50
51 /* format the string into an allocated buffer */
52 va_list argp;
53 va_start(argp, fmt);
54 s->str = t3sprintf_alloc(fmt, argp);
55 va_end(argp);
56
57 /* note the length */
58 s->alo = s->len = strlen(s->str);
59
60 /* return the new string */
61 return s;
62 }
63
64 /* append */
65 void append(const char *s) { append(s, strlen(s)); }
66 void append(const char *s, long l)
67 {
68 /* if necessary, reallocate the buffer */
69 if (len + l > alo)
70 {
71 /*
72 * calculate the expansion size: go to the needed length plus
73 * half again that total for headroom, with minimum headroom of
74 * 256 bytes
75 */
76 alo = len + l + (len+l > 512 ? (len+l)/2 : 256);
77
78 /* allocate the new buffer */
79 str = (char *)t3realloc(str, alo + 1);
80 }
81
82 /* copy the new text */
83 memcpy(str + len, s, l);
84
85 /* null-terminate it */
86 str[len + l] = '\0';
87
88 /* note the new length */
89 len += l;
90 }
91
92 /* append formatted */
93 void appendf(const char *fmt, ...)
94 {
95 /* do the formatting */
96 va_list argp;
97 va_start(argp, fmt);
98 char *s = t3sprintf_alloc(fmt, argp);
99 va_end(argp);
100
101 /* append the result to the string */
102 append(s);
103
104 /* we're done with the formatted result */
105 t3free(s);
106 }
107
108 /* append a utf-8 character */
109 void append_utf8(wchar_t ch)
110 {
111 char buf[3];
112
113 if (ch <= 0x7f)
114 {
115 /* it's in the range 0..7f - encode as one byte */
116 buf[0] = (char)ch;
117 append(buf, 1);
118 }
119 else if (ch <= 0x7ff)
120 {
121 /* it's in the range 80..7ff - encode as two bytes */
122 buf[0] = (char)(0xC0 | ((ch >> 6) & 0x1F));
123 buf[1] = (char)(0x80 | (ch & 0x3F));
124 append(buf, 2);
125 }
126 else
127 {
128 /* it's in the range 800..ffff - encode as three bytes */
129 buf[0] = (char)(0xE0 | ((ch >> 12) & 0x0F));
130 buf[1] = (char)(0x80 | ((ch >> 6) & 0x3F));
131 buf[2] = (char)(0x80 | (ch & 0x3F));
132 append(buf, 3);
133 }
134 }
135
136 /*
137 * ensure there's space for 'n' added bytes
138 */
139 void ensure(long n)
140 {
141 if (n + len > alo)
142 {
143 /*
144 * calculate the expansion size: go to the needed length plus
145 * half again that total for headroom, with minimum headroom of
146 * 256 bytes
147 */
148 alo = len + n + (len+n > 512 ? (len+n)/2 : 256);
149
150 /* allocate the new buffer */
151 str = (char *)t3realloc(str, alo + 1);
152 }
153 }
154
155 /* truncate to the given size */
156 void truncate(long l)
157 {
158 /* if we're actually shrinking the buffer, make the change */
159 if (l < len)
160 {
161 /* set the new length, and null-terminate at the new length */
162 len = l;
163 str[l] = '\0';
164 }
165 }
166
167 protected:
168 ~StringRef()
169 {
170 if (str != 0)
171 t3free(str);
172 }
173
174 void init(const char *s, size_t l, size_t init_alo)
175 {
176 /* make sure the initial allocation is at least 'l' */
177 if (init_alo < l)
178 init_alo = l;
179
180 /* allocate the buffer */
181 alo = init_alo;
182 str = (char *)t3malloc(alo + 1);
183
184 /* copy the string */
185 if (str != 0)
186 memcpy(str, s, l);
187
188 /* null-terminate it */
189 str[l] = '\0';
190
191 /* set the length */
192 len = l;
193 }
194
195 /* the string */
196 char *str;
197
198 /* stored length of the string */
199 long len;
200
201 /* size of the allocated buffer */
202 long alo;
203 };
204
205
206 #endif /* VMSTRREF_H */