GRASS 8 Programmer's Manual 8.6.0dev(2026)-745b61fbf6
Loading...
Searching...
No Matches
string.c
Go to the documentation of this file.
1/*!
2 \file db/dbmi_base/string.c
3
4 \brief DBMI Library (base) - string management
5
6 SPDX-FileCopyrightText: 1999-2009 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Joel Jones (CERL/UIUC)
10 \author Upgraded to GRASS 5.7 by Radim Blazek
11 */
12
13#include <string.h>
14#include <stdlib.h>
15#include <grass/gis.h>
16#include <grass/dbmi.h>
17
18/*!
19 \brief Initialize dbString
20
21 \param[out] x pointer to dbString
22 */
24{
25 G_zero(x, sizeof(dbString));
26}
27
28static int set_string(dbString *x, char *s, int copy);
29
30/*!
31 \brief Inserts string to dbString (enlarge string)
32
33 \param[in,out] x pointer to dbString
34 \param s string to be inserted
35
36 \return DB_OK on success
37 \return DB_MEMORY_ERR on error
38 */
39int db_set_string(dbString *x, const char *s)
40{
41 return set_string(x, (char *)s, 1);
42}
43
44/*!
45 \brief Inserts string to dbString (overwrite current value)
46
47 \param[in,out] x pointer to dbString
48 \param s string to be inserted
49
50 \return DB_OK on success
51 \return DB_MEMORY_ERR on error
52 */
54{
55 return set_string(x, s, 0);
56}
57
58/*!
59 \brief Get string size
60
61 \param x pointer to dbString
62
63 \return string size
64 */
65unsigned int db_sizeof_string(const dbString *x)
66{
67 if (x->nalloc < 0)
68 return 0;
69 return (unsigned int)x->nalloc;
70}
71
72/*!
73 \brief Zero string
74
75 \param x pointer to dbString
76 */
78{
79 if (db_get_string(x) && x->nalloc > 0)
80 db_zero((void *)db_get_string(x), x->nalloc);
81}
82
83static int set_string(dbString *x, char *s, int copy)
84{
85 int len;
86 int stat;
87
88 if (s == NULL) {
89 s = "";
90 copy = 1;
91 }
92
93 len = strlen(s) + 1;
94
95 if (copy) {
96 stat = db_enlarge_string(x, len);
97 if (stat != DB_OK)
98 return stat;
99 strcpy(x->string, s);
100 }
101 else {
103 x->string = s;
104 x->nalloc = -1;
105 }
106 return DB_OK;
107}
108
109/*!
110 \brief Enlarge dbString
111
112 \param x pointer to dbString
113 \param len requested string size
114
115 \return DB_OK on success
116 \return DB_MEMORY_ERR on error
117 */
119{
120 if (x->nalloc < len) {
121 if (x->nalloc < 0)
122 x->string = NULL;
123 x->string = db_realloc((void *)x->string, len);
124 if (x->string == NULL)
125 return DB_MEMORY_ERR;
126 x->nalloc = len;
127 }
128 return DB_OK;
129}
130
131/*!
132 \brief Get string
133
134 \param x pointer to dbString
135
136 \return pointer to buffer containing string
137 */
138char *db_get_string(const dbString *x)
139{
140 return x->string;
141}
142
143/*!
144 \brief Free allocated space for dbString
145
146 \param x pointer to dbString
147 */
149{
150 if (x->nalloc > 0)
151 db_free(x->string);
153}
154
155/*!
156 \brief Free allocated dbString array
157
158 \param a pointer to 1st dbString in the array
159 \param n number of items in array
160 */
162{
163 int i;
164
165 if (a) {
166 for (i = 0; i < n; i++)
167 db_free_string(&a[i]);
168 db_free(a);
169 }
170}
171
172/*!
173 \brief Allocate dbString array
174
175 \param count number of items to be allocated
176
177 \return pointer to 1st dbString in the array
178 */
180{
181 int i;
182 dbString *a;
183
184 if (count < 0)
185 count = 0;
186 a = (dbString *)db_calloc(count, sizeof(dbString));
187 if (a) {
188 for (i = 0; i < count; i++)
189 db_init_string(&a[i]);
190 }
191 return a;
192}
193
194/*!
195 \brief Append string to dbString
196
197 \param x pointer to dbString
198 \param s string to be appended
199
200 \return DB_OK on success
201 \return otherwise error code is returned
202 */
203int db_append_string(dbString *x, const char *s)
204{
205 int len;
206 int stat;
207
208 if (!db_get_string(x))
209 return db_set_string(x, s);
210
211 len = strlen(db_get_string(x)) + strlen(s) + 1;
212 stat = db_enlarge_string(x, len);
213 if (stat != DB_OK)
214 return stat;
216 return DB_OK;
217}
218
219/*!
220 \brief Copy dbString
221
222 \param dst destination dbString
223 \param src source dbString
224
225 \return DB_OK on success
226 \return DB_ERR code on error
227 */
228int db_copy_string(dbString *dst, const dbString *src)
229{
230 return db_set_string(dst, db_get_string(src));
231}
232
233/*!
234 \brief Replace each ' is replaced by ''
235
236 \param src pointer to dbString
237 */
239{
240 char *ptra, *ptrb, buf[2];
241 dbString tmp;
242
243 db_init_string(&tmp);
244 buf[1] = 0;
245
246 ptrb = db_get_string(src);
247 while ((ptra = strchr(ptrb, '\'')) != NULL) {
248 for (; ptrb <= ptra; ptrb++) {
249 buf[0] = ptrb[0];
250 db_append_string(&tmp, buf);
251 }
252 db_append_string(&tmp, "'");
253 }
254 db_append_string(&tmp, ptrb);
255 db_set_string(src, db_get_string(&tmp));
256 db_free_string(&tmp);
257}
#define NULL
Definition ccmath.h:32
Main header of GRASS DataBase Management Interface.
#define DB_OK
Definition dbmi.h:69
#define DB_MEMORY_ERR
Definition dbmi.h:72
void * db_realloc(void *, int)
Reallocate memory.
void * db_calloc(int, int)
Allocate memory.
void db_free(void *)
Free allocated memory.
void db_zero(void *, int)
Zero allocated space.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:21
int count
#define strcpy
Definition parson.c:66
dbString * db_alloc_string_array(int count)
Allocate dbString array.
Definition string.c:179
void db_zero_string(dbString *x)
Zero string.
Definition string.c:77
void db_init_string(dbString *x)
Initialize dbString.
Definition string.c:23
void db_free_string_array(dbString *a, int n)
Free allocated dbString array.
Definition string.c:161
int db_enlarge_string(dbString *x, int len)
Enlarge dbString.
Definition string.c:118
unsigned int db_sizeof_string(const dbString *x)
Get string size.
Definition string.c:65
int db_copy_string(dbString *dst, const dbString *src)
Copy dbString.
Definition string.c:228
int db_append_string(dbString *x, const char *s)
Append string to dbString.
Definition string.c:203
char * db_get_string(const dbString *x)
Get string.
Definition string.c:138
void db_free_string(dbString *x)
Free allocated space for dbString.
Definition string.c:148
void db_double_quote_string(dbString *src)
Replace each ' is replaced by ''.
Definition string.c:238
int db_set_string_no_copy(dbString *x, char *s)
Inserts string to dbString (overwrite current value)
Definition string.c:53
int db_set_string(dbString *x, const char *s)
Inserts string to dbString (enlarge string)
Definition string.c:39
#define x