GRASS Programmer's Manual
6.4.2(2012)
Main Page
Related Pages
Namespaces
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
string.c
Go to the documentation of this file.
1
#include <string.h>
2
#include <stdlib.h>
3
#include <grass/dbmi.h>
4
11
void
db_init_string
(dbString * x)
12
{
13
x->string =
""
;
14
x->nalloc = 0;
15
}
16
17
18
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
30
*/
31
static
int
set_string(dbString * x,
char
*
s
,
int
copy);
32
33
int
db_set_string
(dbString * x,
const
char
*
s
)
34
{
35
return
set_string(x, (
char
*)s, 1);
36
}
37
44
int
db_set_string_no_copy
(dbString * x,
char
*
s
)
45
{
46
return
set_string(x, s, 0);
47
}
48
55
unsigned
int
db_sizeof_string
(dbString * x)
56
{
57
if
(x->nalloc < 0)
58
return
0;
59
return
(
unsigned
int
)x->nalloc;
60
}
61
68
void
db_zero_string
(dbString * x)
69
{
70
db_zero
((
void
*)
db_get_string
(x),
db_sizeof_string
(x));
71
}
72
79
static
int
set_string(dbString * x,
char
*
s
,
int
copy)
80
{
81
int
len;
82
int
stat;
83
84
if
(s ==
NULL
) {
85
s =
""
;
86
copy = 1;
87
}
88
89
len = strlen(s) + 1;
90
91
if
(copy) {
92
stat =
db_enlarge_string
(x, len);
93
if
(stat != DB_OK)
94
return
stat;
95
strcpy(x->string, s);
96
}
97
else
{
98
db_free_string
(x);
99
x->string =
s
;
100
x->nalloc = -1;
101
}
102
return
DB_OK;
103
}
104
111
int
db_enlarge_string
(dbString * x,
int
len)
112
{
113
if
(x->nalloc < len) {
114
if
(x->nalloc <= 0)
115
x->string =
db_store
(
""
);
116
x->string =
db_realloc
((
void
*)x->string, len);
117
if
(x->string ==
NULL
)
118
return
DB_MEMORY_ERR;
119
x->nalloc = len;
120
}
121
return
DB_OK;
122
}
123
131
char
*
db_get_string
(dbString * x)
132
{
133
return
x->string;
134
}
135
142
void
db_free_string
(dbString * x)
143
{
144
if
(x->nalloc > 0)
145
db_free
(x->string);
146
db_init_string
(x);
147
}
148
155
void
db_free_string_array
(dbString * a,
int
n)
156
{
157
int
i;
158
159
if
(a) {
160
for
(i = 0; i < n; i++)
161
db_free_string
(&a[i]);
162
db_free
(a);
163
}
164
}
165
172
dbString *
db_alloc_string_array
(
int
count
)
173
{
174
int
i;
175
dbString *a;
176
177
if
(count < 0)
178
count = 0;
179
a = (dbString *)
db_calloc
(count,
sizeof
(dbString));
180
if
(a) {
181
for
(i = 0; i <
count
; i++)
182
db_init_string
(&a[i]);
183
}
184
return
a;
185
}
186
193
int
db_append_string
(dbString * x,
const
char
*s)
194
{
195
int
len;
196
int
stat;
197
198
len = strlen(
db_get_string
(x)) + strlen(s) + 1;
199
stat =
db_enlarge_string
(x, len);
200
if
(stat != DB_OK)
201
return
stat;
202
strcat(
db_get_string
(x), s);
203
return
DB_OK;
204
}
205
212
int
db_copy_string
(dbString * dst, dbString * src)
213
{
214
return
db_set_string
(dst,
db_get_string
(src));
215
}
216
223
void
db_double_quote_string
(dbString * src)
224
{
225
char
*ptra, *ptrb, buf[2];
226
dbString tmp;
227
228
db_init_string
(&tmp);
229
buf[1] = 0;
230
231
ptrb =
db_get_string
(src);
232
while
((ptra = strchr(ptrb,
'\''
)) !=
NULL
) {
233
for
(; ptrb <= ptra; ptrb++) {
234
buf[0] = ptrb[0];
235
db_append_string
(&tmp, buf);
236
}
237
db_append_string
(&tmp,
"'"
);
238
}
239
db_append_string
(&tmp, ptrb);
240
db_set_string
(src,
db_get_string
(&tmp));
241
db_free_string
(&tmp);
242
}
lib
db
dbmi_base
string.c
Generated on Sun Mar 16 2014 05:07:48 for GRASS Programmer's Manual by
1.8.1.2