GRASS 8 Programmer's Manual 8.6.0dev(2026)-c83afef6d3
Loading...
Searching...
No Matches
value.c
Go to the documentation of this file.
1/*!
2 \file lib/db/dbmi_base/value.c
3
4 \brief DBMI Library (base) - value management
5
6 SPDX-FileCopyrightText: 1999-2009, 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Joel Jones (CERL/UIUC), Radim Blazek
10 \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
11 */
12
13#include <stdlib.h>
14#include <grass/dbmi.h>
15
16/*!
17 \brief Check of value is null
18
19 \param value pointer to dbValue
20
21 \return non-zero is null
22 \return zero is not null
23 */
25{
26 return (value->isNull != 0);
27}
28
29/*!
30 \brief Get integer value
31
32 \param value pointer to dbValue
33
34 \return value
35 */
37{
38 return (value->i);
39}
40
41/*!
42 \brief Get double precision value
43
44 \param value pointer to dbValue
45
46 \return value
47 */
49{
50 return (value->d);
51}
52
53/*!
54 \brief Get value as double
55
56 For given value and C type of value returns double representation.
57
58 \param value pointer to dbValue
59 \param ctype C data type
60
61 \return value
62 */
63double db_get_value_as_double(dbValue *value, int ctype)
64{
65 double val;
66
67 switch (ctype) {
68 case (DB_C_TYPE_INT):
69 val = (double)db_get_value_int(value);
70 break;
71 case (DB_C_TYPE_STRING):
72 val = atof(db_get_value_string(value));
73 break;
74 case (DB_C_TYPE_DOUBLE):
75 val = db_get_value_double(value);
76 break;
77 default:
78 val = 0;
79 }
80 return val;
81}
82
83/*!
84 \brief Get string value
85
86 \param value pointer to dbValue
87
88 \return value
89 */
90const char *db_get_value_string(dbValue *value)
91{
92 return (db_get_string(&value->s));
93}
94
95/*!
96 \brief Get year value
97
98 \param value pointer to dbValue
99
100 \return value
101 */
103{
104 return (value->t.year);
105}
106
107/*!
108 \brief Get month value
109
110 \param value pointer to dbValue
111
112 \return value
113 */
115{
116 return (value->t.month);
117}
118
119/*!
120 \brief Get day value
121
122 \param value pointer to dbValue
123
124 \return value
125 */
127{
128 return (value->t.day);
129}
130
131/*!
132 \brief Get hour value
133
134 \param value pointer to dbValue
135
136 \return value
137 */
139{
140 return (value->t.hour);
141}
142
143/*!
144 \brief Get minute value
145
146 \param value pointer to dbValue
147
148 \return value
149 */
151{
152 return (value->t.minute);
153}
154
155/*!
156 \brief Get seconds value
157
158 \param value pointer to dbValue
159
160 \return value
161 */
163{
164 return (value->t.seconds);
165}
166
167/*!
168 \brief Set value to null
169
170 \param value pointer to dbValue
171 */
173{
174 value->isNull = 1;
175}
176
177/*!
178 \brief Set value to not null
179
180 \param value pointer to dbValue
181 */
183{
184 value->isNull = 0;
185}
186
187/*!
188 \brief Set integer value
189
190 \param value pointer to dbValue
191 \param i integer value
192 */
193void db_set_value_int(dbValue *value, int i)
194{
195 value->i = i;
197}
198
199/*!
200 \brief Set double precision value
201
202 \param value pointer to dbValue
203 \param d double value
204 */
205void db_set_value_double(dbValue *value, double d)
206{
207 value->d = d;
209}
210
211/*!
212 \brief Set string value
213
214 \param value pointer to dbValue
215 \param s string value
216 */
217int db_set_value_string(dbValue *value, const char *s)
218{
220 return db_set_string(&value->s, s);
221}
222
223/*!
224 \brief Set year value
225
226 \param value pointer to dbValue
227 \param year year value
228 */
229void db_set_value_year(dbValue *value, int year)
230{
231 value->t.year = year;
233}
234
235/*!
236 \brief Set month value
237
238 \param value pointer to dbValue
239 \param month month value
240 */
241void db_set_value_month(dbValue *value, int month)
242{
243 value->t.month = month;
245}
246
247/*!
248 \brief Set day value
249
250 \param value pointer to dbValue
251 \param day day value
252 */
253void db_set_value_day(dbValue *value, int day)
254{
255 value->t.day = day;
257}
258
259/*!
260 \brief Set hour value
261
262 \param value pointer to dbValue
263 \param hour hour value
264 */
265void db_set_value_hour(dbValue *value, int hour)
266{
267 value->t.hour = hour;
269}
270
271/*!
272 \brief Set minute value
273
274 \param value pointer to dbValue
275 \param minute minute value
276 */
277void db_set_value_minute(dbValue *value, int minute)
278{
279 value->t.minute = minute;
281}
282
283/*!
284 \brief Set seconds value
285
286 \param value pointer to dbValue
287 \param seconds seconds value
288 */
289void db_set_value_seconds(dbValue *value, double seconds)
290{
291 value->t.seconds = seconds;
293}
294
295/*!
296 \brief Check if datatime is current
297
298 \param value pointer to dbValue
299
300 \return non-zero for true
301 \return zero for false
302 */
304{
305 return (value->t.current != 0);
306}
307
308/*!
309 \brief Set datetime to current
310
311 \param value pointer to dbValue
312 */
314{
315 value->t.current = 1;
317}
318
319/*!
320 \brief Set value to non-current
321
322 \param value pointer to dbValue
323 */
325{
326 value->t.current = 0;
328}
329
330/*!
331 \brief Copy value
332
333 Copy value from src to destination
334
335 \param dst destination dbValue
336 \param src source dbValue
337 */
339{
340 dst->isNull = src->isNull;
341 dst->i = src->i;
342 dst->d = src->d;
343 if (src->s.nalloc > 0)
344 db_copy_string(&(dst->s), &(src->s));
345 dst->t.current = src->t.current;
346 dst->t.year = src->t.year;
347 dst->t.month = src->t.month;
348 dst->t.day = src->t.day;
349 dst->t.hour = src->t.hour;
350 dst->t.minute = src->t.minute;
351 dst->t.seconds = src->t.seconds;
352}
353
354/*!
355 \brief Initialize dbCatValArray
356
357 \param arr pointer to dbCatValArray to be initialized
358 */
360{
361 arr->n_values = 0;
362 arr->alloc = 0;
363 arr->value = NULL;
364}
365
366/*!
367 \brief Free allocated dbCatValArray
368
369 \param arr pointer to dbCatValArray
370 */
372{
373 if (arr->ctype == DB_C_TYPE_STRING || arr->ctype == DB_C_TYPE_DATETIME) {
374 int i;
375
376 for (i = 0; i < arr->n_values; i++) {
377 if (arr->ctype == DB_C_TYPE_STRING && arr->value[i].val.s) {
378 db_free_string(arr->value[i].val.s);
379 }
380 if (arr->ctype == DB_C_TYPE_DATETIME && arr->value[i].val.t) {
381 db_free(arr->value[i].val.t);
382 }
383 }
384 }
385
386 G_free(arr->value);
387}
388
389/*!
390 \brief Allocate dbCatValArray
391
392 \todo return type void?
393
394 \param arr pointer to dbCatValArray
395 \param n number of items
396
397 \return DB_OK
398 */
400{
401 arr->value = (dbCatVal *)G_calloc(n, sizeof(dbCatVal));
402
403 arr->alloc = n;
404
405 return DB_OK;
406}
407
408/*!
409 \brief Realloc dbCatValArray
410
411 \todo return code void?
412
413 \param arr pointer to dbCatValArray
414 \param n number of items
415
416 \return DB_OK
417 */
419{
420 arr->value = (dbCatVal *)G_realloc(arr->value, n * sizeof(dbCatVal));
421
422 arr->alloc = n;
423
424 return DB_OK;
425}
#define NULL
Definition ccmath.h:32
Main header of GRASS DataBase Management Interface.
#define DB_C_TYPE_INT
Definition dbmi.h:106
#define DB_C_TYPE_STRING
Definition dbmi.h:105
#define DB_C_TYPE_DOUBLE
Definition dbmi.h:107
#define DB_OK
Definition dbmi.h:69
#define DB_C_TYPE_DATETIME
Definition dbmi.h:108
int db_copy_string(dbString *, const dbString *)
Copy dbString.
Definition string.c:228
void db_free_string(dbString *)
Free allocated space for dbString.
Definition string.c:148
char * db_get_string(const dbString *)
Get string.
Definition string.c:138
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition string.c:39
void db_free(void *)
Free allocated memory.
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_realloc(p, n)
Definition defs/gis.h:138
#define G_calloc(m, n)
Definition defs/gis.h:137
int minute
Definition dbmi.h:181
double seconds
Definition dbmi.h:182
int month
Definition dbmi.h:178
char current
Definition dbmi.h:176
int nalloc
Definition dbmi.h:147
dbString s
Definition dbmi.h:189
dbDateTime t
Definition dbmi.h:190
char isNull
Definition dbmi.h:186
double d
Definition dbmi.h:188
int i
Definition dbmi.h:187
int db_set_value_string(dbValue *value, const char *s)
Set string value.
Definition value.c:217
int db_get_value_month(dbValue *value)
Get month value.
Definition value.c:114
int db_get_value_year(dbValue *value)
Get year value.
Definition value.c:102
int db_get_value_int(dbValue *value)
Get integer value.
Definition value.c:36
void db_set_value_day(dbValue *value, int day)
Set day value.
Definition value.c:253
void db_set_value_minute(dbValue *value, int minute)
Set minute value.
Definition value.c:277
void db_set_value_int(dbValue *value, int i)
Set integer value.
Definition value.c:193
void db_CatValArray_init(dbCatValArray *arr)
Initialize dbCatValArray.
Definition value.c:359
int db_test_value_datetime_current(dbValue *value)
Check if datatime is current.
Definition value.c:303
void db_set_value_month(dbValue *value, int month)
Set month value.
Definition value.c:241
int db_get_value_hour(dbValue *value)
Get hour value.
Definition value.c:138
void db_set_value_double(dbValue *value, double d)
Set double precision value.
Definition value.c:205
void db_set_value_datetime_not_current(dbValue *value)
Set value to non-current.
Definition value.c:324
int db_CatValArray_realloc(dbCatValArray *arr, int n)
Realloc dbCatValArray.
Definition value.c:418
void db_set_value_datetime_current(dbValue *value)
Set datetime to current.
Definition value.c:313
void db_set_value_year(dbValue *value, int year)
Set year value.
Definition value.c:229
int db_CatValArray_alloc(dbCatValArray *arr, int n)
Allocate dbCatValArray.
Definition value.c:399
void db_set_value_not_null(dbValue *value)
Set value to not null.
Definition value.c:182
void db_set_value_hour(dbValue *value, int hour)
Set hour value.
Definition value.c:265
void db_set_value_null(dbValue *value)
Set value to null.
Definition value.c:172
void db_copy_value(dbValue *dst, dbValue *src)
Copy value.
Definition value.c:338
const char * db_get_value_string(dbValue *value)
Get string value.
Definition value.c:90
int db_get_value_day(dbValue *value)
Get day value.
Definition value.c:126
double db_get_value_double(dbValue *value)
Get double precision value.
Definition value.c:48
int db_get_value_minute(dbValue *value)
Get minute value.
Definition value.c:150
double db_get_value_seconds(dbValue *value)
Get seconds value.
Definition value.c:162
void db_set_value_seconds(dbValue *value, double seconds)
Set seconds value.
Definition value.c:289
int db_test_value_isnull(dbValue *value)
Check of value is null.
Definition value.c:24
void db_CatValArray_free(dbCatValArray *arr)
Free allocated dbCatValArray.
Definition value.c:371
double db_get_value_as_double(dbValue *value, int ctype)
Get value as double.
Definition value.c:63