Debian Codegraph @

xbmc/dbwrappers/qry_dat.cpp

raw
1 /*
2 * Copyright (C) 2004, Leo Seib, Hannover
3 *
4 * Project: C++ Dynamic Library
5 * Module: FieldValue class realisation file
6 * Author: Leo Seib E-Mail: [email protected]
7 * Begin: 5/04/2002
8 *
9 * SPDX-License-Identifier: MIT
10 * See LICENSES/README.md for more information.
11 */
12
13 /**********************************************************************
14 * 2005-03-29 - Minor modifications to allow get_asBool to function on
15 * on string values that are 1 or 0
16 **********************************************************************/
17
18 #include "qry_dat.h"
19
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #ifndef __GNUC__
25 #pragma warning(disable : 4800)
26 #pragma warning(disable : 4715)
27 #endif
28
29 namespace dbiplus
30 {
31
32 //Constructors
33 field_value::field_value()
34 {
35 field_type = ft_String;
36 is_null = false;
37 }
38
39 field_value::field_value(const char* s) : str_value(s)
40 {
41 field_type = ft_String;
42 is_null = false;
43 }
44
45 field_value::field_value(const bool b)
46 {
47 bool_value = b;
48 field_type = ft_Boolean;
49 is_null = false;
50 }
51
52 field_value::field_value(const char c)
53 {
54 char_value = c;
55 field_type = ft_Char;
56 is_null = false;
57 }
58
59 field_value::field_value(const short s)
60 {
61 short_value = s;
62 field_type = ft_Short;
63 is_null = false;
64 }
65
66 field_value::field_value(const unsigned short us)
67 {
68 ushort_value = us;
69 field_type = ft_UShort;
70 is_null = false;
71 }
72
73 field_value::field_value(const int i)
74 {
75 int_value = i;
76 field_type = ft_Int;
77 is_null = false;
78 }
79
80 field_value::field_value(const unsigned int ui)
81 {
82 uint_value = ui;
83 field_type = ft_UInt;
84 is_null = false;
85 }
86
87 field_value::field_value(const float f)
88 {
89 float_value = f;
90 field_type = ft_Float;
91 is_null = false;
92 }
93
94 field_value::field_value(const double d)
95 {
96 double_value = d;
97 field_type = ft_Double;
98 is_null = false;
99 }
100
101 field_value::field_value(const int64_t i)
102 {
103 int64_value = i;
104 field_type = ft_Int64;
105 is_null = false;
106 }
107
108 field_value::field_value(const char* s, std::size_t len)
109 : field_type(ft_String), str_value(s, len), is_null(false)
110 {
111 }
112
113 field_value::field_value(const field_value& fv)
114 {
115 switch (fv.get_fType())
116 {
117 case ft_String:
118 {
119 set_asString(fv.get_asString());
120 break;
121 }
122 case ft_Boolean:
123 {
124 set_asBool(fv.get_asBool());
125 break;
126 }
127 case ft_Char:
128 {
129 set_asChar(fv.get_asChar());
130 break;
131 }
132 case ft_Short:
133 {
134 set_asShort(fv.get_asShort());
135 break;
136 }
137 case ft_UShort:
138 {
139 set_asUShort(fv.get_asUShort());
140 break;
141 }
142 case ft_Int:
143 {
144 set_asInt(fv.get_asInt());
145 break;
146 }
147 case ft_UInt:
148 {
149 set_asUInt(fv.get_asUInt());
150 break;
151 }
152 case ft_Float:
153 {
154 set_asFloat(fv.get_asFloat());
155 break;
156 }
157 case ft_Double:
158 {
159 set_asDouble(fv.get_asDouble());
160 break;
161 }
162 case ft_Int64:
163 {
164 set_asInt64(fv.get_asInt64());
165 break;
166 }
167 default:
168 break;
169 }
170 is_null = fv.get_isNull();
171 }
172
173 field_value::field_value(field_value&& fv) noexcept
174 {
175 *this = std::move(fv);
176 }
177
178 //empty destructor
179 field_value::~field_value() = default;
180
181 //Conversations functions
182 std::string field_value::get_asString() const&
183 {
184 switch (field_type)
185 {
186 case ft_String:
187 {
188 return str_value;
189 }
190 case ft_Boolean:
191 {
192 if (bool_value)
193 return "True";
194 else
195 return "False";
196 }
197 case ft_Char:
198 {
199 return {char_value};
200 }
201 case ft_Short:
202 {
203 char t[10];
204 snprintf(t, sizeof(t), "%i", short_value);
205 return t;
206 }
207 case ft_UShort:
208 {
209 char t[10];
210 snprintf(t, sizeof(t), "%i", ushort_value);
211 return t;
212 }
213 case ft_Int:
214 {
215 char t[12];
216 snprintf(t, sizeof(t), "%d", int_value);
217 return t;
218 }
219 case ft_UInt:
220 {
221 char t[12];
222 snprintf(t, sizeof(t), "%u", uint_value);
223 return t;
224 }
225 case ft_Float:
226 {
227 char t[16];
228 snprintf(t, sizeof(t), "%f", static_cast<double>(float_value));
229 return t;
230 }
231 case ft_Double:
232 {
233 char t[32];
234 snprintf(t, sizeof(t), "%f", double_value);
235 return t;
236 }
237 case ft_Int64:
238 {
239 char t[23];
240 snprintf(t, sizeof(t), "%" PRId64, int64_value);
241 return t;
242 }
243 default:
244 return "";
245 }
246 }
247
248 std::string field_value::get_asString() &&
249 {
250 switch (field_type)
251 {
252 case ft_String:
253 return std::move(str_value);
254 default:
255 return get_asString();
256 }
257 }
258
259 bool field_value::get_asBool() const
260 {
261 switch (field_type)
262 {
263 case ft_String:
264 {
265 if (str_value == "True" || str_value == "true" || str_value == "1")
266 return true;
267 else
268 return false;
269 }
270 case ft_Boolean:
271 {
272 return bool_value;
273 }
274 case ft_Char:
275 {
276 if (char_value == 'T' || char_value == 't')
277 return true;
278 else
279 return false;
280 }
281 case ft_Short:
282 {
283 return (bool)short_value;
284 }
285 case ft_UShort:
286 {
287 return (bool)ushort_value;
288 }
289 case ft_Int:
290 {
291 return (bool)int_value;
292 }
293 case ft_UInt:
294 {
295 return (bool)uint_value;
296 }
297 case ft_Float:
298 {
299 return (bool)float_value;
300 }
301 case ft_Double:
302 {
303 return (bool)double_value;
304 }
305 case ft_Int64:
306 {
307 return (bool)int64_value;
308 }
309 default:
310 return false;
311 }
312 }
313
314 char field_value::get_asChar() const
315 {
316 switch (field_type)
317 {
318 case ft_String:
319 {
320 return str_value[0];
321 }
322 case ft_Boolean:
323 {
324 if (bool_value)
325 return 'T';
326 else
327 return 'F';
328 }
329 case ft_Char:
330 {
331 return char_value;
332 }
333 case ft_Short:
334 {
335 char t[10];
336 snprintf(t, sizeof(t), "%i", short_value);
337 return t[0];
338 }
339 case ft_UShort:
340 {
341 char t[10];
342 snprintf(t, sizeof(t), "%i", ushort_value);
343 return t[0];
344 }
345 case ft_Int:
346 {
347 char t[12];
348 snprintf(t, sizeof(t), "%d", int_value);
349 return t[0];
350 }
351 case ft_UInt:
352 {
353 char t[12];
354 snprintf(t, sizeof(t), "%u", uint_value);
355 return t[0];
356 }
357 case ft_Float:
358 {
359 char t[16];
360 snprintf(t, sizeof(t), "%f", static_cast<double>(float_value));
361 return t[0];
362 }
363 case ft_Double:
364 {
365 char t[32];
366 snprintf(t, sizeof(t), "%f", double_value);
367 return t[0];
368 }
369 case ft_Int64:
370 {
371 char t[24];
372 snprintf(t, sizeof(t), "%" PRId64, int64_value);
373 return t[0];
374 }
375 default:
376 return '\0';
377 }
378 }
379
380 short field_value::get_asShort() const
381 {
382 switch (field_type)
383 {
384 case ft_String:
385 {
386 return (short)atoi(str_value.c_str());
387 }
388 case ft_Boolean:
389 {
390 return (short)bool_value;
391 }
392 case ft_Char:
393 {
394 return (short)char_value;
395 }
396 case ft_Short:
397 {
398 return short_value;
399 }
400 case ft_UShort:
401 {
402 return (short)ushort_value;
403 }
404 case ft_Int:
405 {
406 return (short)int_value;
407 }
408 case ft_UInt:
409 {
410 return (short)uint_value;
411 }
412 case ft_Float:
413 {
414 return (short)float_value;
415 }
416 case ft_Double:
417 {
418 return (short)double_value;
419 }
420 case ft_Int64:
421 {
422 return (short)int64_value;
423 }
424 default:
425 return 0;
426 }
427 }
428
429 unsigned short field_value::get_asUShort() const
430 {
431 switch (field_type)
432 {
433 case ft_String:
434 {
435 return (unsigned short)atoi(str_value.c_str());
436 }
437 case ft_Boolean:
438 {
439 return (unsigned short)bool_value;
440 }
441 case ft_Char:
442 {
443 return (unsigned short)char_value;
444 }
445 case ft_Short:
446 {
447 return (unsigned short)short_value;
448 }
449 case ft_UShort:
450 {
451 return ushort_value;
452 }
453 case ft_Int:
454 {
455 return (unsigned short)int_value;
456 }
457 case ft_UInt:
458 {
459 return (unsigned short)uint_value;
460 }
461 case ft_Float:
462 {
463 return (unsigned short)float_value;
464 }
465 case ft_Double:
466 {
467 return (unsigned short)double_value;
468 }
469 case ft_Int64:
470 {
471 return (unsigned short)int64_value;
472 }
473 default:
474 return 0;
475 }
476 }
477
478 int field_value::get_asInt() const
479 {
480 switch (field_type)
481 {
482 case ft_String:
483 {
484 return atoi(str_value.c_str());
485 }
486 case ft_Boolean:
487 {
488 return (int)bool_value;
489 }
490 case ft_Char:
491 {
492 return (int)char_value;
493 }
494 case ft_Short:
495 {
496 return (int)short_value;
497 }
498 case ft_UShort:
499 {
500 return (int)ushort_value;
501 }
502 case ft_Int:
503 {
504 return int_value;
505 }
506 case ft_UInt:
507 {
508 return (int)uint_value;
509 }
510 case ft_Float:
511 {
512 return (int)float_value;
513 }
514 case ft_Double:
515 {
516 return (int)double_value;
517 }
518 case ft_Int64:
519 {
520 return (int)int64_value;
521 }
522 default:
523 return 0;
524 }
525 }
526
527 unsigned int field_value::get_asUInt() const
528 {
529 switch (field_type)
530 {
531 case ft_String:
532 {
533 return (unsigned int)atoi(str_value.c_str());
534 }
535 case ft_Boolean:
536 {
537 return (unsigned int)bool_value;
538 }
539 case ft_Char:
540 {
541 return (unsigned int)char_value;
542 }
543 case ft_Short:
544 {
545 return (unsigned int)short_value;
546 }
547 case ft_UShort:
548 {
549 return (unsigned int)ushort_value;
550 }
551 case ft_Int:
552 {
553 return (unsigned int)int_value;
554 }
555 case ft_UInt:
556 {
557 return uint_value;
558 }
559 case ft_Float:
560 {
561 return (unsigned int)float_value;
562 }
563 case ft_Double:
564 {
565 return (unsigned int)double_value;
566 }
567 case ft_Int64:
568 {
569 return (unsigned int)int64_value;
570 }
571 default:
572 return 0;
573 }
574 }
575
576 float field_value::get_asFloat() const
577 {
578 switch (field_type)
579 {
580 case ft_String:
581 {
582 return (float)atof(str_value.c_str());
583 }
584 case ft_Boolean:
585 {
586 return (float)bool_value;
587 }
588 case ft_Char:
589 {
590 return (float)char_value;
591 }
592 case ft_Short:
593 {
594 return (float)short_value;
595 }
596 case ft_UShort:
597 {
598 return (float)ushort_value;
599 }
600 case ft_Int:
601 {
602 return (float)int_value;
603 }
604 case ft_UInt:
605 {
606 return (float)uint_value;
607 }
608 case ft_Float:
609 {
610 return float_value;
611 }
612 case ft_Double:
613 {
614 return (float)double_value;
615 }
616 case ft_Int64:
617 {
618 return (float)int64_value;
619 }
620 default:
621 return 0.0;
622 }
623 }
624
625 double field_value::get_asDouble() const
626 {
627 switch (field_type)
628 {
629 case ft_String:
630 {
631 return atof(str_value.c_str());
632 }
633 case ft_Boolean:
634 {
635 return (double)bool_value;
636 }
637 case ft_Char:
638 {
639 return (double)char_value;
640 }
641 case ft_Short:
642 {
643 return (double)short_value;
644 }
645 case ft_UShort:
646 {
647 return (double)ushort_value;
648 }
649 case ft_Int:
650 {
651 return (double)int_value;
652 }
653 case ft_UInt:
654 {
655 return (double)uint_value;
656 }
657 case ft_Float:
658 {
659 return (double)float_value;
660 }
661 case ft_Double:
662 {
663 return (double)double_value;
664 }
665 case ft_Int64:
666 {
667 return (double)int64_value;
668 }
669 default:
670 return 0.0;
671 }
672 }
673
674 int64_t field_value::get_asInt64() const
675 {
676 switch (field_type)
677 {
678 case ft_String:
679 {
680 return std::atoll(str_value.c_str());
681 }
682 case ft_Boolean:
683 {
684 return (int64_t)bool_value;
685 }
686 case ft_Char:
687 {
688 return (int64_t)char_value;
689 }
690 case ft_Short:
691 {
692 return (int64_t)short_value;
693 }
694 case ft_UShort:
695 {
696 return (int64_t)ushort_value;
697 }
698 case ft_Int:
699 {
700 return (int64_t)int_value;
701 }
702 case ft_UInt:
703 {
704 return (int64_t)uint_value;
705 }
706 case ft_Float:
707 {
708 return (int64_t)float_value;
709 }
710 case ft_Double:
711 {
712 return (int64_t)double_value;
713 }
714 case ft_Int64:
715 {
716 return int64_value;
717 }
718 default:
719 return 0;
720 }
721 }
722
723 field_value& field_value::operator=(const field_value& fv)
724 {
725 if (this == &fv)
726 return *this;
727
728 is_null = fv.get_isNull();
729
730 switch (fv.get_fType())
731 {
732 case ft_String:
733 {
734 set_asString(fv.get_asString());
735 return *this;
736 break;
737 }
738 case ft_Boolean:
739 {
740 set_asBool(fv.get_asBool());
741 return *this;
742 break;
743 }
744 case ft_Char:
745 {
746 set_asChar(fv.get_asChar());
747 return *this;
748 break;
749 }
750 case ft_Short:
751 {
752 set_asShort(fv.get_asShort());
753 return *this;
754 break;
755 }
756 case ft_UShort:
757 {
758 set_asUShort(fv.get_asUShort());
759 return *this;
760 break;
761 }
762 case ft_Int:
763 {
764 set_asInt(fv.get_asInt());
765 return *this;
766 break;
767 }
768 case ft_UInt:
769 {
770 set_asUInt(fv.get_asUInt());
771 return *this;
772 break;
773 }
774 case ft_Float:
775 {
776 set_asFloat(fv.get_asFloat());
777 return *this;
778 break;
779 }
780 case ft_Double:
781 {
782 set_asDouble(fv.get_asDouble());
783 return *this;
784 break;
785 }
786 case ft_Int64:
787 {
788 set_asInt64(fv.get_asInt64());
789 return *this;
790 break;
791 }
792 default:
793 return *this;
794 }
795 }
796
797 field_value& field_value::operator=(field_value&& fv) noexcept
798 {
799 if (this == &fv)
800 return *this;
801
802 is_null = fv.get_isNull();
803
804 switch (fv.get_fType())
805 {
806 case ft_String:
807 set_asString(std::move(fv.str_value));
808 return *this;
809 default:
810 return *this = fv;
811 }
812 }
813
814 //Set functions
815 void field_value::set_asString(const char* s)
816 {
817 str_value = s;
818 field_type = ft_String;
819 }
820
821 void field_value::set_asString(const char* s, std::size_t len)
822 {
823 str_value = std::string_view(s, len);
824 field_type = ft_String;
825 }
826
827 void field_value::set_asString(const std::string& s)
828 {
829 str_value = s;
830 field_type = ft_String;
831 }
832
833 void field_value::set_asString(std::string&& s)
834 {
835 str_value = std::move(s);
836 field_type = ft_String;
837 }
838
839 void field_value::set_asBool(const bool b)
840 {
841 bool_value = b;
842 field_type = ft_Boolean;
843 }
844
845 void field_value::set_asChar(const char c)
846 {
847 char_value = c;
848 field_type = ft_Char;
849 }
850
851 void field_value::set_asShort(const short s)
852 {
853 short_value = s;
854 field_type = ft_Short;
855 }
856
857 void field_value::set_asUShort(const unsigned short us)
858 {
859 ushort_value = us;
860 field_type = ft_UShort;
861 }
862
863 void field_value::set_asInt(const int i)
864 {
865 int_value = i;
866 field_type = ft_Int;
867 }
868
869 void field_value::set_asUInt(const unsigned int ui)
870 {
871 int_value = ui;
872 field_type = ft_UInt;
873 }
874
875 void field_value::set_asFloat(const float f)
876 {
877 float_value = f;
878 field_type = ft_Float;
879 }
880
881 void field_value::set_asDouble(const double d)
882 {
883 double_value = d;
884 field_type = ft_Double;
885 }
886
887 void field_value::set_asInt64(const int64_t i)
888 {
889 int64_value = i;
890 field_type = ft_Int64;
891 }
892
893 fType field_value::get_field_type()
894 {
895 return field_type;
896 }
897
898 std::string field_value::gft()
899 {
900 std::string tmp;
901 switch (field_type)
902 {
903 case ft_String:
904 {
905 tmp.assign("string");
906 return tmp;
907 }
908 case ft_Boolean:
909 {
910 tmp.assign("bool");
911 return tmp;
912 }
913 case ft_Char:
914 {
915 tmp.assign("char");
916 return tmp;
917 }
918 case ft_Short:
919 {
920 tmp.assign("short");
921 return tmp;
922 }
923 case ft_Int:
924 {
925 tmp.assign("int");
926 return tmp;
927 }
928 case ft_Float:
929 {
930 tmp.assign("float");
931 return tmp;
932 }
933 case ft_Double:
934 {
935 tmp.assign("double");
936 return tmp;
937 }
938 case ft_Int64:
939 {
940 tmp.assign("int64");
941 return tmp;
942 }
943 default:
944 break;
945 }
946
947 return tmp;
948 }
949
950 } // namespace dbiplus