~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/db/dbmi_base/string.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
  \file db/dbmi_base/string.c
 
3
  
 
4
  \brief DBMI Library (base) - string management
 
5
  
 
6
  (C) 1999-2009 by the GRASS Development Team
 
7
  
 
8
  This program is free software under the GNU General Public License
 
9
  (>=v2). Read the file COPYING that comes with GRASS for details.
 
10
  
 
11
  \author Joel Jones (CERL/UIUC)
 
12
  \author Upgraded to GRASS 5.7 by Radim Blazek
 
13
*/
 
14
 
1
15
#include <string.h>
2
16
#include <stdlib.h>
 
17
#include <grass/gis.h>
3
18
#include <grass/dbmi.h>
4
19
 
5
20
/*!
6
 
   \fn 
7
 
   \brief 
8
 
   \return 
9
 
   \param 
 
21
  \brief Initialize dbString 
 
22
  
 
23
  \param[out] x pointer to dbString
10
24
 */
11
 
void db_init_string(dbString * x)
 
25
void db_init_string(dbString *x)
12
26
{
13
 
    x->string = "";
14
 
    x->nalloc = 0;
 
27
    G_zero(x, sizeof(dbString));
15
28
}
16
29
 
17
 
 
18
 
 
19
 
/*!
20
 
   \fn 
21
 
   \brief 
22
 
   \return 
23
 
   \param 
24
 
 */
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
30
static int set_string(dbString * x, char *s, int copy);
32
31
 
 
32
/*!
 
33
  \brief Inserts string to dbString (enlarge string)
 
34
  
 
35
  \param[in,out] x pointer to dbString
 
36
  \param s string to be inserted
 
37
 
 
38
  \return DB_OK on success
 
39
  \return DB_MEMORY_ERR on error
 
40
 */
33
41
int db_set_string(dbString * x, const char *s)
34
42
{
35
43
    return set_string(x, (char *)s, 1);
36
44
}
37
45
 
38
46
/*!
39
 
   \fn 
40
 
   \brief 
41
 
   \return 
42
 
   \param 
 
47
  \brief Inserts string to dbString (overwrite current value)
 
48
  
 
49
  \param[in,out] x pointer to dbString
 
50
  \param s string to be inserted
 
51
 
 
52
  \return DB_OK on success
 
53
  \return DB_MEMORY_ERR on error
43
54
 */
44
55
int db_set_string_no_copy(dbString * x, char *s)
45
56
{
47
58
}
48
59
 
49
60
/*!
50
 
   \fn 
51
 
   \brief 
52
 
   \return 
53
 
   \param 
 
61
  \brief Get string size
 
62
 
 
63
  \param x pointer to dbString
 
64
 
 
65
  \return string size
54
66
 */
55
 
unsigned int db_sizeof_string(dbString * x)
 
67
unsigned int db_sizeof_string(const dbString * x)
56
68
{
57
69
    if (x->nalloc < 0)
58
70
        return 0;
60
72
}
61
73
 
62
74
/*!
63
 
   \fn 
64
 
   \brief 
65
 
   \return 
66
 
   \param 
 
75
  \brief Zero string
 
76
 
 
77
  \param x pointer to dbString
67
78
 */
68
79
void db_zero_string(dbString * x)
69
80
{
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);
71
83
}
72
84
 
73
 
/*!
74
 
   \fn 
75
 
   \brief 
76
 
   \return 
77
 
   \param 
78
 
 */
79
85
static int set_string(dbString * x, char *s, int copy)
80
86
{
81
87
    int len;
103
109
}
104
110
 
105
111
/*!
106
 
   \fn 
107
 
   \brief 
108
 
   \return 
109
 
   \param 
 
112
   \brief Enlarge dbString
 
113
 
 
114
   \param x pointer to dbString
 
115
   \param len requested string size
 
116
 
 
117
   \return DB_OK on success
 
118
   \return DB_MEMORY_ERR on error
110
119
 */
111
120
int db_enlarge_string(dbString * x, int len)
112
121
{
113
122
    if (x->nalloc < len) {
114
 
        if (x->nalloc <= 0)
115
 
            x->string = db_store("");
 
123
        if (x->nalloc < 0)
 
124
            x->string = NULL;
116
125
        x->string = db_realloc((void *)x->string, len);
117
126
        if (x->string == NULL)
118
127
            return DB_MEMORY_ERR;
122
131
}
123
132
 
124
133
/*!
125
 
   \fn
126
 
   \brief
127
 
   \return
128
 
   \param
129
 
 */
130
 
 
131
 
char *db_get_string(dbString * x)
 
134
  \brief Get string
 
135
 
 
136
  \param x pointer to dbString
 
137
 
 
138
  \return pointer to buffer containg string
 
139
*/
 
140
char *db_get_string(const dbString * x)
132
141
{
133
142
    return x->string;
134
143
}
135
144
 
136
145
/*!
137
 
   \fn 
138
 
   \brief 
139
 
   \return 
140
 
   \param 
141
 
 */
142
 
void db_free_string(dbString * x)
 
146
  \brief Free allocated space for dbString
 
147
 
 
148
  \param x pointer to dbString
 
149
*/
 
150
void db_free_string(dbString *x)
143
151
{
144
152
    if (x->nalloc > 0)
145
153
        db_free(x->string);
147
155
}
148
156
 
149
157
/*!
150
 
   \fn 
151
 
   \brief 
152
 
   \return 
153
 
   \param 
 
158
  \brief Free allocated dbString array
 
159
 
 
160
  \param a pointer to 1st dbString in the array
 
161
  \param n number of items in array
154
162
 */
155
 
void db_free_string_array(dbString * a, int n)
 
163
void db_free_string_array(dbString *a, int n)
156
164
{
157
165
    int i;
158
166
 
164
172
}
165
173
 
166
174
/*!
167
 
   \fn 
168
 
   \brief 
169
 
   \return 
170
 
   \param 
171
 
 */
 
175
  \brief Allocate dbString array
 
176
 
 
177
  \param count number of items to be allocated
 
178
 
 
179
  \return pointer to 1st dbString in the array
 
180
*/
172
181
dbString *db_alloc_string_array(int count)
173
182
{
174
183
    int i;
185
194
}
186
195
 
187
196
/*!
188
 
   \fn 
189
 
   \brief 
190
 
   \return 
191
 
   \param 
 
197
  \brief Append string to dbString
 
198
 
 
199
  \param x pointer to dbString
 
200
  \param s string to be appended
 
201
 
 
202
  \return DB_OK on success
 
203
  \return otherwise error code is returned
192
204
 */
193
205
int db_append_string(dbString * x, const char *s)
194
206
{
195
207
    int len;
196
208
    int stat;
197
209
 
 
210
    if (!db_get_string(x))
 
211
        return db_set_string(x, s);
 
212
 
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)
204
219
}
205
220
 
206
221
/*!
207
 
   \fn 
208
 
   \brief 
209
 
   \return 
210
 
   \param 
 
222
  \brief Copy dbString
 
223
 
 
224
  \param dst destination dbString
 
225
  \param src source dbString
 
226
 
 
227
  \return DB_OK on success
 
228
  \return DB_ERR code on error
211
229
 */
212
 
int db_copy_string(dbString * dst, dbString * src)
 
230
int db_copy_string(dbString * dst, const dbString * src)
213
231
{
214
232
    return db_set_string(dst, db_get_string(src));
215
233
}
216
234
 
217
235
/*!
218
 
   \fn 
219
 
   \brief each ' is replaced by ''
220
 
   \return 
221
 
   \param 
222
 
 */
 
236
  \brief Replace each ' is replaced by ''
 
237
 
 
238
  \param src pointer to dbString
 
239
*/
223
240
void db_double_quote_string(dbString * src)
224
241
{
225
242
    char *ptra, *ptrb, buf[2];