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.
15 #include "general.h" /* must always come first */
17 #include <limits.h> /* to define INT_MAX */
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
;
46 extern boolean
vStringAutoResize (vString
*const string
)
50 if (string
->size
<= INT_MAX
/ 2)
52 const size_t newSize
= string
->size
* 2;
54 vStringResize (string
, newSize
);
59 extern void vStringClear (vString
*const string
)
62 string
->buffer
[0] = '\0';
63 DebugStatement ( memset (string
->buffer
, 0, string
->size
); )
66 extern void vStringDelete (vString
*const string
)
70 if (string
->buffer
!= NULL
)
71 eFree (string
->buffer
);
76 extern vString
*vStringNew (void)
78 vString
*const string
= xMalloc (1, vString
);
81 string
->size
= vStringInitialSize
;
82 string
->buffer
= xMalloc (string
->size
, char);
84 vStringClear (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
;
97 string
->buffer
[++string
->length
] = '\0';
101 extern void vStringCatS (vString
*const string
, const char *const s
)
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
;
112 vStringPut (string
, *p
);
113 while (*p
++ != '\0');
117 extern vString
*vStringNewCopy (const vString
*const string
)
119 vString
*vs
= vStringNew ();
120 vStringCatS (vs
, string
->buffer
);
124 extern vString
*vStringNewInit (const char *const s
)
126 vString
*vs
= vStringNew ();
131 extern void vStringNCatS (
132 vString
*const string
, const char *const s
, const size_t length
)
135 size_t remain
= length
;
137 while (*p
!= '\0' && remain
> 0)
139 vStringPut (string
, *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';
158 /* Strip leading white space from string.
160 extern void vStringStripLeading (vString
*const string
)
162 while (isspace ((int) string
->buffer
[0]) && string
->length
> 0)
165 for (i
= 1 ; i
< string
->length
; ++i
)
166 string
->buffer
[i
- 1] = string
->buffer
[i
];
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]) &&
180 string
->buffer
[string
->length
] = '\0';
184 /* Chop last character from string.
186 extern void vStringChop (vString
*const string
)
188 if (string
->length
> 0)
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
;
215 if (dest
->size
< src
->size
)
216 vStringResize (dest
, src
->size
);
218 for (i
= 0 ; i
< length
; ++i
)
227 extern void vStringSetLength (vString
*const string
)
229 string
->length
= strlen (string
->buffer
);
232 /* vi:set tabstop=4 shiftwidth=4: */