GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
value.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <grass/dbmi.h>
3 
11 {
12  return (value->isNull != 0);
13 }
14 
21 int db_get_value_int(dbValue * value)
22 {
23  return (value->i);
24 }
25 
32 double db_get_value_double(dbValue * value)
33 {
34  return (value->d);
35 }
36 
43 /* for given value and C type of value returns double representation */
44 double db_get_value_as_double(dbValue * value, int ctype)
45 {
46  double val;
47 
48  switch (ctype) {
49  case (DB_C_TYPE_INT):
50  val = (double)db_get_value_int(value);
51  break;
52  case (DB_C_TYPE_STRING):
53  val = atof(db_get_value_string(value));
54  break;
55  case (DB_C_TYPE_DOUBLE):
56  val = db_get_value_double(value);
57  break;
58  default:
59  val = 0;
60  }
61  return val;
62 }
63 
70 const char *db_get_value_string(dbValue * value)
71 {
72  return (db_get_string(&value->s));
73 }
74 
81 int db_get_value_year(dbValue * value)
82 {
83  return (value->t.year);
84 }
85 
92 int db_get_value_month(dbValue * value)
93 {
94  return (value->t.month);
95 }
96 
103 int db_get_value_day(dbValue * value)
104 {
105  return (value->t.day);
106 }
107 
114 int db_get_value_hour(dbValue * value)
115 {
116  return (value->t.hour);
117 }
118 
126 {
127  return (value->t.minute);
128 }
129 
136 double db_get_value_seconds(dbValue * value)
137 {
138  return (value->t.seconds);
139 }
140 
147 void db_set_value_null(dbValue * value)
148 {
149  value->isNull = 1;
150 }
151 
159 {
160  value->isNull = 0;
161 }
162 
169 void db_set_value_int(dbValue * value, int i)
170 {
171  value->i = i;
172  db_set_value_not_null(value);
173 }
174 
181 void db_set_value_double(dbValue * value, double d)
182 {
183  value->d = d;
184  db_set_value_not_null(value);
185 }
186 
193 int db_set_value_string(dbValue * value, const char *s)
194 {
195  db_set_value_not_null(value);
196  return db_set_string(&value->s, s);
197 }
198 
205 void db_set_value_year(dbValue * value, int year)
206 {
207  value->t.year = year;
209 }
210 
217 void db_set_value_month(dbValue * value, int month)
218 {
219  value->t.month = month;
221 }
222 
229 void db_set_value_day(dbValue * value, int day)
230 {
231  value->t.day = day;
233 }
234 
241 void db_set_value_hour(dbValue * value, int hour)
242 {
243  value->t.hour = hour;
245 }
246 
253 void db_set_value_minute(dbValue * value, int minute)
254 {
255  value->t.minute = minute;
257 }
258 
265 void db_set_value_seconds(dbValue * value, double seconds)
266 {
267  value->t.seconds = seconds;
269 }
270 
278 {
279  return (value->t.current != 0);
280 }
281 
289 {
290  value->t.current = 1;
291  db_set_value_not_null(value);
292 }
293 
301 {
302  value->t.current = 0;
303  db_set_value_not_null(value);
304 }
305 
312 /* copy value from src to destination */
313 void db_copy_value(dbValue * dst, dbValue * src)
314 {
315  dst->isNull = src->isNull;
316  dst->i = src->i;
317  dst->d = src->d;
318  if (src->s.nalloc > 0)
319  db_copy_string(&(dst->s), &(src->s));
320  dst->t.current = src->t.current;
321  dst->t.year = src->t.year;
322  dst->t.month = src->t.month;
323  dst->t.day = src->t.day;
324  dst->t.hour = src->t.hour;
325  dst->t.minute = src->t.minute;
326  dst->t.seconds = src->t.seconds;
327 }
328 
335 void db_CatValArray_init(dbCatValArray * arr)
336 {
337  arr->n_values = 0;
338  arr->alloc = 0;
339  arr->value = NULL;
340 }
341 
348 void db_CatValArray_free(dbCatValArray * arr)
349 {
350  if (arr->ctype == DB_C_TYPE_STRING || arr->ctype == DB_C_TYPE_DATETIME) {
351  int i;
352 
353  for (i = 0; i < arr->n_values; i++) {
354  if (arr->ctype == DB_C_TYPE_STRING && arr->value[i].val.s) {
355  db_free_string(arr->value[i].val.s);
356  }
357  if (arr->ctype == DB_C_TYPE_DATETIME && arr->value[i].val.t) {
358  db_free(arr->value[i].val.t);
359  }
360  }
361  }
362 
363  G_free(arr->value);
364 }
365 
372 int db_CatValArray_alloc(dbCatValArray * arr, int n)
373 {
374  arr->value = (dbCatVal *) G_calloc(n, sizeof(dbCatVal));
375 
376  arr->alloc = n;
377 
378  return DB_OK;
379 }
380 
387 int db_CatValArray_realloc(dbCatValArray * arr, int n)
388 {
389  arr->value = (dbCatVal *) G_realloc(arr->value, n * sizeof(dbCatVal));
390 
391  arr->alloc = n;
392 
393  return DB_OK;
394 }
int db_test_value_isnull(dbValue *value)
Definition: value.c:10
int db_CatValArray_realloc(dbCatValArray *arr, int n)
Definition: value.c:387
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
void db_set_value_year(dbValue *value, int year)
Definition: value.c:205
int db_get_value_day(dbValue *value)
Definition: value.c:103
void db_set_value_int(dbValue *value, int i)
Definition: value.c:169
int db_get_value_month(dbValue *value)
Definition: value.c:92
void db_set_value_null(dbValue *value)
Definition: value.c:147
void db_set_value_month(dbValue *value, int month)
Definition: value.c:217
int db_test_value_datetime_current(dbValue *value)
Definition: value.c:277
int db_copy_string(dbString *dst, dbString *src)
Definition: string.c:212
int db_get_value_minute(dbValue *value)
Definition: value.c:125
void db_set_value_seconds(dbValue *value, double seconds)
Definition: value.c:265
void db_CatValArray_free(dbCatValArray *arr)
Definition: value.c:348
int db_CatValArray_alloc(dbCatValArray *arr, int n)
Definition: value.c:372
void db_set_value_double(dbValue *value, double d)
Definition: value.c:181
void db_set_value_day(dbValue *value, int day)
Definition: value.c:229
double db_get_value_double(dbValue *value)
Definition: value.c:32
double db_get_value_seconds(dbValue *value)
Definition: value.c:136
void db_set_value_minute(dbValue *value, int minute)
Definition: value.c:253
char * value
Definition: env.c:30
void db_set_value_not_null(dbValue *value)
Definition: value.c:158
void db_set_value_datetime_not_current(dbValue *value)
Definition: value.c:300
int db_get_value_hour(dbValue *value)
Definition: value.c:114
void db_CatValArray_init(dbCatValArray *arr)
Definition: value.c:335
int db_get_value_year(dbValue *value)
Definition: value.c:81
return NULL
Definition: dbfopen.c:1394
char * db_get_string(dbString *x)
Definition: string.c:131
int db_set_value_string(dbValue *value, const char *s)
Definition: value.c:193
int db_set_string(dbString *x, const char *s)
Definition: string.c:33
int db_get_value_int(dbValue *value)
Definition: value.c:21
double db_get_value_as_double(dbValue *value, int ctype)
Definition: value.c:44
void db_free_string(dbString *x)
Definition: string.c:142
void db_set_value_hour(dbValue *value, int hour)
Definition: value.c:241
void db_set_value_datetime_current(dbValue *value)
Definition: value.c:288
int n
Definition: dataquad.c:291
const char * db_get_value_string(dbValue *value)
Definition: value.c:70
void db_free(void *s)
void db_copy_value(dbValue *dst, dbValue *src)
Definition: value.c:313