1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/*
* $Id: vstring.c 558 2007-06-15 19:17:02Z elliotth $
*
* Copyright (c) 1998-2002, Darren Hiebert
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* This module contains functions supporting resizeable strings.
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <limits.h> /* to define INT_MAX */
#include <string.h>
#include <ctype.h>
#include "debug.h"
#include "routines.h"
#include "vstring.h"
/*
* DATA DEFINITIONS
*/
static const size_t vStringInitialSize = 32;
/*
* FUNCTION DEFINITIONS
*/
static void vStringResize (vString *const string, const size_t newSize)
{
char *const newBuffer = xRealloc (string->buffer, newSize, char);
string->size = newSize;
string->buffer = newBuffer;
}
/*
* External interface
*/
extern boolean vStringAutoResize (vString *const string)
{
boolean ok = TRUE;
if (string->size <= INT_MAX / 2)
{
const size_t newSize = string->size * 2;
vStringResize (string, newSize);
}
return ok;
}
extern void vStringClear (vString *const string)
{
string->length = 0;
string->buffer [0] = '\0';
DebugStatement ( memset (string->buffer, 0, string->size); )
}
extern void vStringDelete (vString *const string)
{
if (string != NULL)
{
if (string->buffer != NULL)
eFree (string->buffer);
eFree (string);
}
}
extern vString *vStringNew (void)
{
vString *const string = xMalloc (1, vString);
string->length = 0;
string->size = vStringInitialSize;
string->buffer = xMalloc (string->size, char);
vStringClear (string);
return string;
}
#ifndef VSTRING_PUTC_MACRO
extern void vStringPut (vString *const string, const int c)
{
if (string->length + 1 == string->size) /* check for buffer overflow */
vStringAutoResize (string);
string->buffer [string->length] = c;
if (c != '\0')
string->buffer [++string->length] = '\0';
}
#endif
extern void vStringCatS (vString *const string, const char *const s)
{
#if 1
const size_t len = strlen (s);
while (string->length + len + 1 >= string->size)/* check for buffer overflow */
vStringAutoResize (string);
strcpy (string->buffer + string->length, s);
string->length += len;
#else
const char *p = s;
do
vStringPut (string, *p);
while (*p++ != '\0');
#endif
}
extern vString *vStringNewCopy (const vString *const string)
{
vString *vs = vStringNew ();
vStringCatS (vs, string->buffer);
return vs;
}
extern vString *vStringNewInit (const char *const s)
{
vString *vs = vStringNew ();
vStringCatS (vs, s);
return vs;
}
extern void vStringNCatS (
vString *const string, const char *const s, const size_t length)
{
const char *p = s;
size_t remain = length;
while (*p != '\0' && remain > 0)
{
vStringPut (string, *p);
--remain;
++p;
}
vStringTerminate (string);
}
/* Strip trailing newline from string.
*/
extern void vStringStripNewline (vString *const string)
{
const size_t final = string->length - 1;
if (string->buffer [final] == '\n')
{
string->buffer [final] = '\0';
string->length--;
}
}
/* Strip leading white space from string.
*/
extern void vStringStripLeading (vString *const string)
{
while (isspace ((int) string->buffer [0]) && string->length > 0)
{
size_t i;
for (i = 1 ; i < string->length ; ++i)
string->buffer [i - 1] = string->buffer [i];
--string->length;
string->buffer [string->length] = '\0';
}
}
/* Strip trailing white space from string.
*/
extern void vStringStripTrailing (vString *const string)
{
while (isspace ((int) string->buffer [string->length - 1]) &&
string->length > 0)
{
string->length--;
string->buffer [string->length] = '\0';
}
}
/* Chop last character from string.
*/
extern void vStringChop (vString *const string)
{
if (string->length > 0)
{
--string->length;
string->buffer [string->length] = '\0';
}
}
extern void vStringCopyS (vString *const string, const char *const s)
{
vStringClear (string);
vStringCatS (string, s);
}
extern void vStringNCopyS (
vString *const string, const char *const s, const size_t length)
{
vStringClear (string);
vStringNCatS (string, s, length);
}
extern void vStringCopyToLower (vString *const dest, const vString *const src)
{
const size_t length = src->length;
const char *s = src->buffer;
char *d;
size_t i;
if (dest->size < src->size)
vStringResize (dest, src->size);
d = dest->buffer;
for (i = 0 ; i < length ; ++i)
{
int c = s [i];
d [i] = tolower (c);
}
d [i] = '\0';
}
extern void vStringSetLength (vString *const string)
{
string->length = strlen (string->buffer);
}
/* vi:set tabstop=4 shiftwidth=4: */
|