Making "inline" behave like an attribute. Fixes #1
[arduino-ctags.git] / vstring.c
blob7ab52cc85f8f892ece7020f795e0294698c9f6e4
1 /*
2 * $Id: vstring.c 558 2007-06-15 19:17:02Z elliotth $
4 * Copyright (c) 1998-2002, Darren Hiebert
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions supporting resizeable strings.
13 * INCLUDE FILES
15 #include "general.h" /* must always come first */
17 #include <limits.h> /* to define INT_MAX */
18 #include <string.h>
19 #include <ctype.h>
21 #include "debug.h"
22 #include "routines.h"
23 #include "vstring.h"
26 * DATA DEFINITIONS
28 static const size_t vStringInitialSize = 32;
31 * FUNCTION DEFINITIONS
34 static void vStringResize (vString *const string, const size_t newSize)
36 char *const newBuffer = xRealloc (string->buffer, newSize, char);
38 string->size = newSize;
39 string->buffer = newBuffer;
43 * External interface
46 extern boolean vStringAutoResize (vString *const string)
48 boolean ok = TRUE;
50 if (string->size <= INT_MAX / 2)
52 const size_t newSize = string->size * 2;
54 vStringResize (string, newSize);
56 return ok;
59 extern void vStringClear (vString *const string)
61 string->length = 0;
62 string->buffer [0] = '\0';
63 DebugStatement ( memset (string->buffer, 0, string->size); )
66 extern void vStringDelete (vString *const string)
68 if (string != NULL)
70 if (string->buffer != NULL)
71 eFree (string->buffer);
72 eFree (string);
76 extern vString *vStringNew (void)
78 vString *const string = xMalloc (1, vString);
80 string->length = 0;
81 string->size = vStringInitialSize;
82 string->buffer = xMalloc (string->size, char);
84 vStringClear (string);
86 return string;
89 #ifndef VSTRING_PUTC_MACRO
90 extern void vStringPut (vString *const string, const int c)
92 if (string->length + 1 == string->size) /* check for buffer overflow */
93 vStringAutoResize (string);
95 string->buffer [string->length] = c;
96 if (c != '\0')
97 string->buffer [++string->length] = '\0';
99 #endif
101 extern void vStringCatS (vString *const string, const char *const s)
103 #if 1
104 const size_t len = strlen (s);
105 while (string->length + len + 1 >= string->size)/* check for buffer overflow */
106 vStringAutoResize (string);
107 strcpy (string->buffer + string->length, s);
108 string->length += len;
109 #else
110 const char *p = s;
112 vStringPut (string, *p);
113 while (*p++ != '\0');
114 #endif
117 extern vString *vStringNewCopy (const vString *const string)
119 vString *vs = vStringNew ();
120 vStringCatS (vs, string->buffer);
121 return vs;
124 extern vString *vStringNewInit (const char *const s)
126 vString *vs = vStringNew ();
127 vStringCatS (vs, s);
128 return vs;
131 extern void vStringNCatS (
132 vString *const string, const char *const s, const size_t length)
134 const char *p = s;
135 size_t remain = length;
137 while (*p != '\0' && remain > 0)
139 vStringPut (string, *p);
140 --remain;
141 ++p;
143 vStringTerminate (string);
146 /* Strip trailing newline from string.
148 extern void vStringStripNewline (vString *const string)
150 const size_t final = string->length - 1;
151 if (string->buffer [final] == '\n')
153 string->buffer [final] = '\0';
154 string->length--;
158 /* Strip leading white space from string.
160 extern void vStringStripLeading (vString *const string)
162 while (isspace ((int) string->buffer [0]) && string->length > 0)
164 size_t i;
165 for (i = 1 ; i < string->length ; ++i)
166 string->buffer [i - 1] = string->buffer [i];
167 --string->length;
168 string->buffer [string->length] = '\0';
172 /* Strip trailing white space from string.
174 extern void vStringStripTrailing (vString *const string)
176 while (isspace ((int) string->buffer [string->length - 1]) &&
177 string->length > 0)
179 string->length--;
180 string->buffer [string->length] = '\0';
184 /* Chop last character from string.
186 extern void vStringChop (vString *const string)
188 if (string->length > 0)
190 --string->length;
191 string->buffer [string->length] = '\0';
195 extern void vStringCopyS (vString *const string, const char *const s)
197 vStringClear (string);
198 vStringCatS (string, s);
201 extern void vStringNCopyS (
202 vString *const string, const char *const s, const size_t length)
204 vStringClear (string);
205 vStringNCatS (string, s, length);
208 extern void vStringCopyToLower (vString *const dest, const vString *const src)
210 const size_t length = src->length;
211 const char *s = src->buffer;
212 char *d;
213 size_t i;
215 if (dest->size < src->size)
216 vStringResize (dest, src->size);
217 d = dest->buffer;
218 for (i = 0 ; i < length ; ++i)
220 int c = s [i];
222 d [i] = tolower (c);
224 d [i] = '\0';
227 extern void vStringSetLength (vString *const string)
229 string->length = strlen (string->buffer);
232 /* vi:set tabstop=4 shiftwidth=4: */