Debian Codegraph @

plugins/symbolbrowser/symbols/parsers/vstring.cpp

raw symbols SCIP
1 /***************************************************************************
2 A SymbolBrowser plugin for JuffEd editor.
3
4 Copyright: 2010 Aleksander Sokoloff <[email protected]>
5
6 Some of the code based on a ctags project http://ctags.sourceforge.net/
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License.
10 version 2 as published by the Free Software Foundation.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 ***************************************************************************/
21
22 /*
23 * INCLUDE FILES
24 */
25
26 #include <limits.h> /* to define INT_MAX */
27 #include <string.h>
28 #include <ctype.h>
29
30 #include "vstring.h"
31 #include <stdlib.h> /* to declare malloc (), realloc () */
32
33 #include <QtGlobal>
34
35
36
37
38
39 /*
40 * Memory allocation functions
41 */
42
43 /*****************************************************************************
44 *****************************************************************************/
45 extern void *eMalloc (const size_t size)
46 {
47 void *buffer = malloc (size);
48
49 if (buffer == NULL) {
50 qFatal("out of memory");
51 exit(1);
52 }
53
54 return buffer;
55 }
56
57
58 /*****************************************************************************
59 *****************************************************************************/
60 extern void *eCalloc (const size_t count, const size_t size)
61 {
62 void *buffer = calloc (count, size);
63
64 if (buffer == NULL) {
65 qFatal("out of memory");
66 exit(1);
67 }
68
69 return buffer;
70 }
71
72
73 /*****************************************************************************
74 *****************************************************************************/
75 extern void *eRealloc (void *const ptr, const size_t size)
76 {
77 void *buffer;
78 if (ptr == NULL)
79 buffer = eMalloc (size);
80 else
81 {
82 buffer = realloc (ptr, size);
83 if (buffer == NULL) {
84 qFatal("out of memory");
85 exit(1);
86 }
87 }
88 return buffer;
89 }
90
91
92 /*****************************************************************************
93 *****************************************************************************/
94 extern void eFree (void *const ptr)
95 {
96 Assert (ptr != NULL);
97 free(ptr);
98 }
99
100
101 /*
102 * MACROS
103 */
104 #define xMalloc(n,Type) (Type *)eMalloc((size_t)(n) * sizeof (Type))
105 #define xCalloc(n,Type) (Type *)eCalloc((size_t)(n), sizeof (Type))
106 #define xRealloc(p,n,Type) (Type *)eRealloc((p), (n) * sizeof (Type))
107
108
109 /*
110 * DATA DEFINITIONS
111 */
112 static const size_t vStringInitialSize = 32;
113
114 /*
115 * FUNCTION DEFINITIONS
116 */
117
118
119 /*****************************************************************************
120 *****************************************************************************/
121 static void vStringResize (vString *const string, const size_t newSize)
122 {
123 char *const newBuffer = xRealloc (string->buffer, newSize, char);
124
125 string->size = newSize;
126 string->buffer = newBuffer;
127 }
128
129 /*
130 * External interface
131 */
132
133
134 /*****************************************************************************
135 *****************************************************************************/
136 extern boolean vStringAutoResize (vString *const string)
137 {
138 boolean ok = true;
139
140 if (string->size <= INT_MAX / 2)
141 {
142 const size_t newSize = string->size * 2;
143
144 vStringResize (string, newSize);
145 }
146 return ok;
147 }
148
149
150 /*****************************************************************************
151 *****************************************************************************/
152 extern void vStringClear (vString *const string)
153 {
154 string->length = 0;
155 string->buffer [0] = '\0';
156 DebugStatement ( memset (string->buffer, 0, string->size); )
157 }
158
159
160 /*****************************************************************************
161 *****************************************************************************/
162 extern void vStringDelete (vString *const string)
163 {
164 if (string != NULL)
165 {
166 if (string->buffer != NULL)
167 eFree (string->buffer);
168 eFree (string);
169 }
170 }
171
172
173 /*****************************************************************************
174 *****************************************************************************/
175 extern vString *vStringNew (void)
176 {
177 vString *const string = xMalloc (1, vString);
178
179 string->length = 0;
180 string->size = vStringInitialSize;
181 string->buffer = xMalloc (string->size, char);
182
183 vStringClear (string);
184
185 return string;
186 }
187
188
189 /*****************************************************************************
190 *****************************************************************************/
191 #ifndef VSTRING_PUTC_MACRO
192 extern void vStringPut (vString *const string, const int c)
193 {
194 if (string->length + 1 == string->size) /* check for buffer overflow */
195 vStringAutoResize (string);
196
197 string->buffer [string->length] = c;
198 if (c != '\0')
199 string->buffer [++string->length] = '\0';
200 }
201 #endif
202
203
204 /*****************************************************************************
205 *****************************************************************************/
206 extern void vStringCatS (vString *const string, const char *const s)
207 {
208 #if 1
209 const size_t len = strlen (s);
210 while (string->length + len + 1 >= string->size)/* check for buffer overflow */
211 vStringAutoResize (string);
212 strcpy (string->buffer + string->length, s);
213 string->length += len;
214 #else
215 const char *p = s;
216 do
217 vStringPut (string, *p);
218 while (*p++ != '\0');
219 #endif
220 }
221
222
223 /*****************************************************************************
224 *****************************************************************************/
225 extern vString *vStringNewCopy (const vString *const string)
226 {
227 vString *vs = vStringNew ();
228 vStringCatS (vs, string->buffer);
229 return vs;
230 }
231
232
233 /*****************************************************************************
234 *****************************************************************************/
235 extern vString *vStringNewInit (const char *const s)
236 {
237 vString *vs = vStringNew ();
238 vStringCatS (vs, s);
239 return vs;
240 }
241
242
243 /*****************************************************************************
244 *****************************************************************************/
245 extern void vStringNCatS (
246 vString *const string, const char *const s, const size_t length)
247 {
248 const char *p = s;
249 size_t remain = length;
250
251 while (*p != '\0' && remain > 0)
252 {
253 vStringPut (string, *p);
254 --remain;
255 ++p;
256 }
257 vStringTerminate (string);
258 }
259
260
261 /*****************************************************************************
262 Strip trailing newline from string.
263 *****************************************************************************/
264 extern void vStringStripNewline (vString *const string)
265 {
266 const size_t final = string->length - 1;
267 if (string->buffer [final] == '\n')
268 {
269 string->buffer [final] = '\0';
270 string->length--;
271 }
272 }
273
274
275 /*****************************************************************************
276 Strip leading white space from string.
277 *****************************************************************************/
278 extern void vStringStripLeading (vString *const string)
279 {
280 while (isspace ((int) string->buffer [0]) && string->length > 0)
281 {
282 size_t i;
283 for (i = 1 ; i < string->length ; ++i)
284 string->buffer [i - 1] = string->buffer [i];
285 --string->length;
286 string->buffer [string->length] = '\0';
287 }
288 }
289
290 /*****************************************************************************
291 Strip trailing white space from string.
292 *****************************************************************************/
293 extern void vStringStripTrailing (vString *const string)
294 {
295 while (isspace ((int) string->buffer [string->length - 1]) &&
296 string->length > 0)
297 {
298 string->length--;
299 string->buffer [string->length] = '\0';
300 }
301 }
302
303
304 /*****************************************************************************
305 Chop last character from string.
306 *****************************************************************************/
307 extern void vStringChop (vString *const string)
308 {
309 if (string->length > 0)
310 {
311 --string->length;
312 string->buffer [string->length] = '\0';
313 }
314 }
315
316
317 /*****************************************************************************
318 *****************************************************************************/
319 extern void vStringCopyS (vString *const string, const char *const s)
320 {
321 vStringClear (string);
322 vStringCatS (string, s);
323 }
324
325
326 /*****************************************************************************
327 *****************************************************************************/
328 extern void vStringNCopyS (
329 vString *const string, const char *const s, const size_t length)
330 {
331 vStringClear (string);
332 vStringNCatS (string, s, length);
333 }
334
335
336 /*****************************************************************************
337 *****************************************************************************/
338 extern void vStringCopyToLower (vString *const dest, const vString *const src)
339 {
340 const size_t length = src->length;
341 const char *s = src->buffer;
342 char *d;
343 size_t i;
344
345 if (dest->size < src->size)
346 vStringResize (dest, src->size);
347 d = dest->buffer;
348 for (i = 0 ; i < length ; ++i)
349 {
350 int c = s [i];
351
352 d [i] = tolower (c);
353 }
354 d [i] = '\0';
355 }
356
357
358 /*****************************************************************************
359 *****************************************************************************/
360 extern void vStringSetLength (vString *const string)
361 {
362 string->length = strlen (string->buffer);
363 }
364
365
366 /*****************************************************************************
367 *****************************************************************************/
368 extern QString vStringToQString(const vString *const string) {
369 char * buf = vStringValue(string);
370 return QString::fromUtf8(buf);
371 }
372
373
374 /*****************************************************************************
375 *****************************************************************************/
376 QDebug& operator<< (QDebug &debug, const vString *v) {
377 return debug << vStringToQString(v);
378 }