File: string.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (270 lines) | stat: -rw-r--r-- 3,522 bytes parent folder | download | duplicates (2)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <string.h>
#include <stdlib.h>
#include "dbmi.h"

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
void
db_init_string (x)
    dbString *x;
{
    x->string = "";
    x->nalloc = 0;
}



/*!
 \fn 
 \brief 
 \return 
 \param 
*/
/* db_set_string(dbString *x, char *s, int copy)
 *  inserts 's' into 'x'
 *   if 'copy' is true, then memory is allocated to copy into
 *   else 'x' is made to point to 's'
 * returns DB_OK or DB_MEMORY_ERR
 */
static int set_string();

int
db_set_string (x, s)
    dbString *x;
    char *s;
{
    return set_string (x, s, 1);
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
int 
db_set_string_no_copy (x, s)
    dbString *x;
    char *s;
{
    return set_string (x, s, 0);
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
unsigned int
db_sizeof_string (x)
    dbString *x;
{
    if (x->nalloc < 0) return 0;
    return (unsigned int) x->nalloc;
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
void
db_zero_string (x)
    dbString *x;
{
    db_zero ((void *)db_get_string(x), db_sizeof_string(x));
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
static int
set_string (x, s, copy)
    dbString *x;
    char *s;
{
    int len;
    int stat;

    if (s == NULL)
    {
	s = "";
	copy = 1;
    }

    len = strlen(s)+1;

    if (copy)
    {
	stat = db_enlarge_string (x, len);
	if (stat != DB_OK)
	    return stat;
	strcpy (x->string, s);
    }
    else
    {
	db_free_string(x);
	x->string = s;
	x->nalloc = -1;
    }
    return DB_OK;
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
int
db_enlarge_string (x, len)
    dbString *x;
    int len;
{
    if (x->nalloc < len)
    {
	if (x->nalloc <= 0)
	    x->string = db_store("");
	x->string = db_realloc ((void *)x->string, len);
	if (x->string == NULL)
	    return DB_MEMORY_ERR;
	x->nalloc = len;
    }
    return DB_OK;
}

char *
db_get_string(x)
    dbString *x;
{
    return x->string;
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
void
db_free_string(x)
    dbString *x;
{	
    if (x->nalloc > 0)
	free(x->string);
    db_init_string (x);
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
void
db_free_string_array (a, n)
    dbString *a;
{
    int i;

    if (a)
    {
	for (i = 0; i<n; i++)
	    db_free_string(&a[i]);
	free (a);
    }
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
dbString *
db_alloc_string_array (count)
    int count;
{
    int i;
    dbString *a;

    if (count < 0) count = 0;
    a = (dbString *) db_calloc (count, sizeof(dbString));
    if (a)
    {
	for (i = 0; i < count; i++)
	    db_init_string(&a[i]);
    }
    return a;
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
int
db_append_string (x, s)
    dbString *x;
    char *s;
{
    int len;
    int stat;

    len = strlen (db_get_string(x)) + strlen(s) + 1;
    stat = db_enlarge_string (x, len);
    if (stat != DB_OK)
	return stat;
    strcat (db_get_string(x), s);
    return DB_OK;
}

/*!
 \fn 
 \brief 
 \return 
 \param 
*/
int
db_copy_string (dst, src)
    dbString *dst, *src;
{
    return db_set_string (dst, db_get_string(src));
}

/*!
 \fn 
 \brief each ' is replaced by ''
 \return 
 \param 
*/
void
db_double_quote_string (src)
    dbString *src;
{
    char *ptra, *ptrb, buf[2];
    dbString tmp;
    
    db_init_string (&tmp);
    buf[1] = 0;
    
    ptrb = db_get_string(src);
    while ( (ptra = strchr( ptrb, '\'') ) != NULL ) {
	for ( ; ptrb <= ptra; ptrb++ ) { buf[0] = ptrb[0]; db_append_string (&tmp, buf); }
        db_append_string (&tmp, "'");
    }
    db_append_string (&tmp, ptrb );
    db_set_string ( src, db_get_string(&tmp));
    db_free_string( &tmp );
}