2
\file db/dbmi_base/string.c
4
\brief DBMI Library (base) - string management
6
(C) 1999-2009 by the GRASS Development Team
8
This program is free software under the GNU General Public License
9
(>=v2). Read the file COPYING that comes with GRASS for details.
11
\author Joel Jones (CERL/UIUC)
12
\author Upgraded to GRASS 5.7 by Radim Blazek
17
#include <grass/gis.h>
3
18
#include <grass/dbmi.h>
21
\brief Initialize dbString
23
\param[out] x pointer to dbString
11
void db_init_string(dbString * x)
25
void db_init_string(dbString *x)
27
G_zero(x, sizeof(dbString));
25
/* db_set_string(dbString *x, char *s, int copy)
26
* inserts 's' into 'x'
27
* if 'copy' is true, then memory is allocated to copy into
28
* else 'x' is made to point to 's'
29
* returns DB_OK or DB_MEMORY_ERR
31
30
static int set_string(dbString * x, char *s, int copy);
33
\brief Inserts string to dbString (enlarge string)
35
\param[in,out] x pointer to dbString
36
\param s string to be inserted
38
\return DB_OK on success
39
\return DB_MEMORY_ERR on error
33
41
int db_set_string(dbString * x, const char *s)
35
43
return set_string(x, (char *)s, 1);
47
\brief Inserts string to dbString (overwrite current value)
49
\param[in,out] x pointer to dbString
50
\param s string to be inserted
52
\return DB_OK on success
53
\return DB_MEMORY_ERR on error
44
55
int db_set_string_no_copy(dbString * x, char *s)
77
\param x pointer to dbString
68
79
void db_zero_string(dbString * x)
70
db_zero((void *)db_get_string(x), db_sizeof_string(x));
81
if (db_get_string(x) && x->nalloc > 0)
82
db_zero((void *)db_get_string(x), x->nalloc);
79
85
static int set_string(dbString * x, char *s, int copy)
112
\brief Enlarge dbString
114
\param x pointer to dbString
115
\param len requested string size
117
\return DB_OK on success
118
\return DB_MEMORY_ERR on error
111
120
int db_enlarge_string(dbString * x, int len)
113
122
if (x->nalloc < len) {
115
x->string = db_store("");
116
125
x->string = db_realloc((void *)x->string, len);
117
126
if (x->string == NULL)
118
127
return DB_MEMORY_ERR;
131
char *db_get_string(dbString * x)
136
\param x pointer to dbString
138
\return pointer to buffer containg string
140
char *db_get_string(const dbString * x)
133
142
return x->string;
142
void db_free_string(dbString * x)
146
\brief Free allocated space for dbString
148
\param x pointer to dbString
150
void db_free_string(dbString *x)
144
152
if (x->nalloc > 0)
145
153
db_free(x->string);
158
\brief Free allocated dbString array
160
\param a pointer to 1st dbString in the array
161
\param n number of items in array
155
void db_free_string_array(dbString * a, int n)
163
void db_free_string_array(dbString *a, int n)
197
\brief Append string to dbString
199
\param x pointer to dbString
200
\param s string to be appended
202
\return DB_OK on success
203
\return otherwise error code is returned
193
205
int db_append_string(dbString * x, const char *s)
210
if (!db_get_string(x))
211
return db_set_string(x, s);
198
213
len = strlen(db_get_string(x)) + strlen(s) + 1;
199
214
stat = db_enlarge_string(x, len);
200
215
if (stat != DB_OK)
224
\param dst destination dbString
225
\param src source dbString
227
\return DB_OK on success
228
\return DB_ERR code on error
212
int db_copy_string(dbString * dst, dbString * src)
230
int db_copy_string(dbString * dst, const dbString * src)
214
232
return db_set_string(dst, db_get_string(src));
219
\brief each ' is replaced by ''
236
\brief Replace each ' is replaced by ''
238
\param src pointer to dbString
223
240
void db_double_quote_string(dbString * src)
225
242
char *ptra, *ptrb, buf[2];