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