~ubuntu-branches/ubuntu/gutsy/geany/gutsy

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