Enable SplitWindow build on Windows again
[geany-mirror.git] / tagmanager / vstring.c
blob23b6549514b68c17030d45921c8a7cb9fbbef284
1 /*
3 * Copyright (c) 1998-2002, Darren Hiebert
5 * This source code is released for free distribution under the terms of the
6 * GNU General Public License.
8 * This module contains functions supporting resizeable strings.
9 */
12 * INCLUDE FILES
14 #include "general.h" /* must always come first */
16 #include <limits.h> /* to define INT_MAX */
17 #include <string.h>
18 #include <ctype.h>
20 #include "main.h"
21 #include "vstring.h"
24 * DATA DEFINITIONS
26 static const size_t vStringInitialSize = 32;
29 * FUNCTION DEFINITIONS
32 static void vStringResize (vString *const string, const size_t newSize)
34 char *const newBuffer = xRealloc (string->buffer, newSize, char);
36 string->size = newSize;
37 string->buffer = newBuffer;
41 * External interface
44 extern boolean vStringAutoResize (vString *const string)
46 boolean ok = TRUE;
48 if (string->size <= INT_MAX / 2)
50 const size_t newSize = string->size * 2;
52 vStringResize (string, newSize);
54 return ok;
57 extern void vStringClear (vString *const string)
59 string->length = 0;
60 string->buffer [0] = '\0';
61 DebugStatement ( memset (string->buffer, 0, string->size); )
64 extern void vStringDelete (vString *const string)
66 if (string != NULL)
68 if (string->buffer != NULL)
69 eFree (string->buffer);
70 eFree (string);
74 extern vString *vStringNew (void)
76 vString *const string = xMalloc (1, vString);
78 string->length = 0;
79 string->size = vStringInitialSize;
80 string->buffer = xMalloc (string->size, char);
82 vStringClear (string);
84 return string;
87 #ifndef VSTRING_PUTC_MACRO
88 extern void vStringPut (vString *const string, const int c)
90 if (string->length + 1 == string->size) /* check for buffer overflow */
91 vStringAutoResize (string);
93 string->buffer [string->length] = c;
94 if (c != '\0')
95 string->buffer [++string->length] = '\0';
97 #endif
99 extern void vStringCatS (vString *const string, const char *const s)
101 #if 1
102 const size_t len = strlen (s);
103 while (string->length + len + 1 >= string->size)/* check for buffer overflow */
104 vStringAutoResize (string);
105 strcpy (string->buffer + string->length, s);
106 string->length += len;
107 #else
108 const char *p = s;
110 vStringPut (string, *p);
111 while (*p++ != '\0');
112 #endif
115 extern vString *vStringNewCopy (const vString *const string)
117 vString *vs = vStringNew ();
118 vStringCatS (vs, string->buffer);
119 return vs;
122 extern vString *vStringNewInit (const char *const s)
124 vString *vs = vStringNew ();
125 vStringCatS (vs, s);
126 return vs;
129 extern void vStringNCatS (
130 vString *const string, const char *const s, const size_t length)
132 const char *p = s;
133 size_t remain = length;
135 while (*p != '\0' && remain > 0)
137 vStringPut (string, *p);
138 --remain;
139 ++p;
141 vStringTerminate (string);
144 /* Strip trailing newline from string.
146 extern void vStringStripNewline (vString *const string)
148 const size_t final = string->length - 1;
149 if (string->buffer [final] == '\n')
151 string->buffer [final] = '\0';
152 string->length--;
156 /* Strip leading white space from string.
158 extern void vStringStripLeading (vString *const string)
160 while (isspace ((int) string->buffer [0]) && string->length > 0)
162 size_t i;
163 for (i = 1 ; i < string->length ; ++i)
164 string->buffer [i - 1] = string->buffer [i];
165 --string->length;
166 string->buffer [string->length] = '\0';
170 /* Strip trailing white space from string.
172 extern void vStringStripTrailing (vString *const string)
174 while (string->length > 0 &&
175 isspace ((int) string->buffer [string->length - 1]))
177 string->length--;
178 string->buffer [string->length] = '\0';
182 /* Chop last character from string.
184 extern void vStringChop (vString *const string)
186 if (string->length > 0)
188 --string->length;
189 string->buffer [string->length] = '\0';
193 extern void vStringCopyS (vString *const string, const char *const s)
195 vStringClear (string);
196 vStringCatS (string, s);
199 extern void vStringNCopyS (
200 vString *const string, const char *const s, const size_t length)
202 vStringClear (string);
203 vStringNCatS (string, s, length);
206 extern void vStringCopyToLower (vString *const dest, const vString *const src)
208 const size_t length = src->length;
209 const char *s = src->buffer;
210 char *d;
211 size_t i;
213 if (dest->size < src->size)
214 vStringResize (dest, src->size);
215 d = dest->buffer;
216 for (i = 0 ; i < length ; ++i)
218 int c = s [i];
220 d [i] = tolower (c);
222 d [i] = '\0';
225 extern void vStringSetLength (vString *const string)
227 string->length = strlen (string->buffer);
230 /* vi:set tabstop=4 shiftwidth=4: */