Added better MySQL detection, workaround for compiling safely under fedora or redhat
[odkda2.git] / src / value.c
blobe12e63e6f488cf76950d304ad8b5881d1410fb65
1 /*
2 ODKDA: Data Access Library
3 Value Object
5 Copyright (C) 2006 Carlos Daniel Ruvalcaba Valenzuela
6 Contact me: <clsdaniel@gmail.com>
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <malloc.h>
26 #include <string.h>
28 #include <odkutils.h>
29 #include <odkutils_xml.h>
31 #include <odkda/common.h>
32 #include <odkda/date.h>
33 #include <odkda/time.h>
34 #include <odkda/datetime.h>
35 #include <odkda/value.h>
37 void
38 odk_value_delete (odkValue * v)
40 switch (v->type) {
41 case ODKDA_TEXT:
42 free (v->vString);
43 break;
44 case ODKDA_BLOB:
45 free (v->vBlob);
46 break;
47 case ODKDA_DATE:
48 free (v->vDate);
49 break;
50 case ODKDA_TIME:
51 free (v->vTime);
52 break;
53 case ODKDA_DATETIME:
54 free (v->vDateTime);
55 break;
56 default:
57 break;
61 void
62 odk_value_init (odkValue * v)
64 v->changed = 0;
65 switch (v->type) {
66 case ODKDA_TEXT:
67 v->vString = NULL;
68 break;
69 case ODKDA_BLOB:
70 v->vBlob = NULL;
71 break;
72 case ODKDA_DATE:
73 v->vDate = NULL;
74 break;
75 case ODKDA_TIME:
76 v->vTime = NULL;
77 break;
78 case ODKDA_DATETIME:
79 v->vDateTime = NULL;
80 break;
81 case ODKDA_BOOL:
82 v->vBool = false;
83 break;
84 case ODKDA_CHAR:
85 v->vChar = 0;
86 break;
87 case ODKDA_SHORT:
88 v->vShort = 0;
89 break;
90 case ODKDA_INT:
91 v->vInt = 0;
92 break;
93 case ODKDA_LONG:
94 v->vLong = 0;
95 break;
96 case ODKDA_FLOAT:
97 v->vFloat = 0.0;
98 break;
99 case ODKDA_DOUBLE:
100 v->vDouble = 0.0;
101 break;
102 default:
103 break;
107 odkValue *
108 odk_value_new (char type)
110 odkValue *v = (odkValue *) malloc (sizeof (odkValue));
112 object_register (v, "odkValue");
113 object_append_destructor (v, (FREE_FUNC)&odk_value_delete);
114 v->type = type;
115 v->size = 0;
117 if (type != 0)
118 odk_value_init (v);
119 return v;
122 odkValue *
123 odk_value_new_shared (char type, odkMemPool * m)
125 odkValue *v = (odkValue *) odk_mempool_alloc (m, sizeof (odkValue));
127 object_register (v, "odkValue");
128 object_append_destructor (v, (FREE_FUNC)&odk_value_delete);
129 v->type = type;
130 v->size = 0;
132 if (type != 0)
133 odk_value_init (v);
134 return v;
138 odk_value_set (odkValue * v, char *val)
140 if (v == NULL)
141 return 0;
143 v->changed = 1;
144 v->vString = strcopy (val);
145 v->size = strlen(val);
149 odk_value_set_bool (odkValue * v, bool val)
151 if (v == NULL)
152 return 0;
154 v->changed = 1;
155 v->vBool = val;
159 odk_value_set_char (odkValue * v, char val)
161 if (v == NULL)
162 return 0;
164 v->changed = 1;
165 v->vChar = val;
169 odk_value_set_short (odkValue * v, short val)
171 if (v == NULL)
172 return 0;
174 v->changed = 1;
175 v->vShort = val;
179 odk_value_set_int (odkValue * v, int val)
181 if (v == NULL)
182 return 0;
184 v->changed = 1;
185 v->vInt = val;
189 odk_value_set_long (odkValue * v, long val)
191 if (v == NULL)
192 return 0;
194 v->changed = 1;
195 v->vLong = val;
199 odk_value_set_float (odkValue * v, float val)
201 if (v == NULL)
202 return 0;
204 v->changed = 1;
205 v->vFloat = val;
209 odk_value_set_double (odkValue * v, double val)
211 if (v == NULL)
212 return 0;
214 v->changed = 1;
215 v->vDouble = val;
219 odk_value_set_blob (odkValue * v, int size, void *val)
221 if (v == NULL)
222 return 0;
224 v->changed = 1;
225 v->vBlob = val;
226 v->size = size;
230 odk_value_set_date (odkValue * v, odkDate * val)
232 if (v == NULL)
233 return 0;
235 v->changed = 1;
236 v->vDate = val;
240 odk_value_set_time (odkValue * v, odkTime * val)
242 if (v == NULL)
243 return 0;
245 v->changed = 1;
246 v->vTime = val;
250 odk_value_set_datetime (odkValue * v, odkDateTime * val)
252 if (v == NULL)
253 return 0;
255 v->changed = 1;
256 v->vDateTime = val;
259 char *
260 odk_value_get (odkValue * v)
262 char *ret = NULL;
264 switch (v->type) {
265 case ODKDA_BOOL:
266 if (v->vBool) {
267 ret = strcopy ("True");
268 } else {
269 ret = strcopy ("False");
271 break;
272 case ODKDA_CHAR:
273 ret = (char *) malloc (sizeof (char) * 4);
274 sprintf (ret, "%i", v->vChar);
275 break;
276 case ODKDA_SHORT:
277 ret = (char *) malloc (sizeof (char) * 6);
278 sprintf (ret, "%i", v->vShort);
279 break;
280 case ODKDA_INT:
281 ret = (char *) malloc (sizeof (char) * 10);
282 sprintf (ret, "%i", v->vInt);
283 break;
284 case ODKDA_LONG:
285 ret = (char *) malloc (sizeof (char) * 16);
286 sprintf (ret, "%i", v->vLong);
287 break;
288 case ODKDA_FLOAT:
289 ret = (char *) malloc (sizeof (char) * 20);
290 sprintf (ret, "%f", v->vFloat);
291 break;
292 case ODKDA_DOUBLE:
293 ret = (char *) malloc (sizeof (char) * 25);
294 sprintf (ret, "%f", (float)v->vDouble);
295 break;
296 case ODKDA_TEXT:
297 ret = strcopy (v->vString);
298 break;
299 case ODKDA_BLOB:
300 ret = (char *) v->vBlob;
301 break;
302 default:
303 ret = NULL;
304 break;
307 return ret;
310 bool
311 odk_value_get_bool (odkValue * v)
313 if (v == NULL)
314 return 0;
316 return v->vBool;
319 char
320 odk_value_get_char (odkValue * v)
322 if (v == NULL)
323 return 0;
325 switch (v->type) {
326 case ODKDA_BOOL:
327 return (char) v->vBool;
328 case ODKDA_CHAR:
329 return v->vChar;
330 default:
331 break;
334 return 0;
337 short
338 odk_value_get_short (odkValue * v)
340 if (v == NULL)
341 return 0;
343 switch (v->type) {
344 case ODKDA_BOOL:
345 return (short) v->vBool;
346 case ODKDA_CHAR:
347 return (short) v->vChar;
348 case ODKDA_SHORT:
349 return v->vShort;
350 default:
351 break;
354 return 0;
358 odk_value_get_int (odkValue * v)
360 if (v == NULL)
361 return 0;
363 switch (v->type) {
364 case ODKDA_BOOL:
365 return (int) v->vBool;
366 case ODKDA_CHAR:
367 return (int) v->vChar;
368 case ODKDA_SHORT:
369 return (int) v->vShort;
370 case ODKDA_INT:
371 return v->vInt;
372 default:
373 break;
376 return 0;
379 long
380 odk_value_get_long (odkValue * v)
382 if (v == NULL)
383 return 0;
385 switch (v->type) {
386 case ODKDA_BOOL:
387 return (long) v->vBool;
388 case ODKDA_CHAR:
389 return (long) v->vChar;
390 case ODKDA_SHORT:
391 return (long) v->vShort;
392 case ODKDA_INT:
393 return (long) v->vInt;
394 case ODKDA_LONG:
395 return v->vLong;
396 default:
397 break;
400 return 0;
403 float
404 odk_value_get_float (odkValue * v)
406 if (v == NULL)
407 return 0;
409 switch (v->type) {
410 case ODKDA_BOOL:
411 return (float) v->vBool;
412 case ODKDA_CHAR:
413 return (float) v->vChar;
414 case ODKDA_SHORT:
415 return (float) v->vShort;
416 case ODKDA_INT:
417 return (float) v->vInt;
418 case ODKDA_LONG:
419 return (float) v->vLong;
420 case ODKDA_FLOAT:
421 return v->vFloat;
422 case ODKDA_DOUBLE:
423 return (float) v->vDouble;
424 default:
425 break;
428 return 0.0f;
431 double
432 odk_value_get_double (odkValue * v)
434 if (v == NULL)
435 return 0;
437 switch (v->type) {
438 case ODKDA_BOOL:
439 return (double) v->vBool;
440 case ODKDA_CHAR:
441 return (double) v->vChar;
442 case ODKDA_SHORT:
443 return (double) v->vShort;
444 case ODKDA_INT:
445 return (double) v->vInt;
446 case ODKDA_LONG:
447 return (double) v->vLong;
448 case ODKDA_FLOAT:
449 return (double) v->vFloat;
450 case ODKDA_DOUBLE:
451 return v->vDouble;
452 default:
453 break;
455 return 0;
458 void *
459 odk_value_get_blob (odkValue * v)
461 if (v == NULL)
462 return NULL;
463 return v->vBlob;
466 odkDate *
467 odk_value_get_date (odkValue * v)
469 if (v == NULL)
470 return NULL;
472 return v->vDate;
475 odkTime *
476 odk_value_get_time (odkValue * v)
478 if (v == NULL)
479 return NULL;
481 return v->vTime;
484 odkDateTime *
485 odk_value_get_datetime (odkValue * v)
487 if (v == NULL)
488 return NULL;
490 return v->vDateTime;
494 int odk_value_get_type(odkValue *v){
495 if (v == NULL)
496 return 0;
498 return v->type;
501 int odk_value_get_size(odkValue *v){
502 if (v == NULL)
503 return 0;
505 return v->size;
508 bool odk_value_is_changed(odkValue *v){
509 if (v == NULL)
510 return 0;
512 return v->changed;