[Test] Added tests for CUtil::SplitParams
[xbmc.git] / xbmc / dbwrappers / qry_dat.cpp
blob093db8feaf38f72c1665295d9e1f8a9fd19bbd9f
1 /*
2 * Copyright (C) 2004, Leo Seib, Hannover
4 * Project: C++ Dynamic Library
5 * Module: FieldValue class realisation file
6 * Author: Leo Seib E-Mail: leoseib@web.de
7 * Begin: 5/04/2002
9 * SPDX-License-Identifier: MIT
10 * See LICENSES/README.md for more information.
13 /**********************************************************************
14 * 2005-03-29 - Minor modifications to allow get_asBool to function on
15 * on string values that are 1 or 0
16 **********************************************************************/
18 #include "qry_dat.h"
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
24 #ifndef __GNUC__
25 #pragma warning(disable : 4800)
26 #pragma warning(disable : 4715)
27 #endif
29 namespace dbiplus
32 //Constructors
33 field_value::field_value()
35 field_type = ft_String;
36 is_null = false;
39 field_value::field_value(const char* s) : str_value(s)
41 field_type = ft_String;
42 is_null = false;
45 field_value::field_value(const bool b)
47 bool_value = b;
48 field_type = ft_Boolean;
49 is_null = false;
52 field_value::field_value(const char c)
54 char_value = c;
55 field_type = ft_Char;
56 is_null = false;
59 field_value::field_value(const short s)
61 short_value = s;
62 field_type = ft_Short;
63 is_null = false;
66 field_value::field_value(const unsigned short us)
68 ushort_value = us;
69 field_type = ft_UShort;
70 is_null = false;
73 field_value::field_value(const int i)
75 int_value = i;
76 field_type = ft_Int;
77 is_null = false;
80 field_value::field_value(const unsigned int ui)
82 uint_value = ui;
83 field_type = ft_UInt;
84 is_null = false;
87 field_value::field_value(const float f)
89 float_value = f;
90 field_type = ft_Float;
91 is_null = false;
94 field_value::field_value(const double d)
96 double_value = d;
97 field_type = ft_Double;
98 is_null = false;
101 field_value::field_value(const int64_t i)
103 int64_value = i;
104 field_type = ft_Int64;
105 is_null = false;
108 field_value::field_value(const char* s, std::size_t len)
109 : field_type(ft_String), str_value(s, len), is_null(false)
113 field_value::field_value(const field_value& fv)
115 switch (fv.get_fType())
117 case ft_String:
119 set_asString(fv.get_asString());
120 break;
122 case ft_Boolean:
124 set_asBool(fv.get_asBool());
125 break;
127 case ft_Char:
129 set_asChar(fv.get_asChar());
130 break;
132 case ft_Short:
134 set_asShort(fv.get_asShort());
135 break;
137 case ft_UShort:
139 set_asUShort(fv.get_asUShort());
140 break;
142 case ft_Int:
144 set_asInt(fv.get_asInt());
145 break;
147 case ft_UInt:
149 set_asUInt(fv.get_asUInt());
150 break;
152 case ft_Float:
154 set_asFloat(fv.get_asFloat());
155 break;
157 case ft_Double:
159 set_asDouble(fv.get_asDouble());
160 break;
162 case ft_Int64:
164 set_asInt64(fv.get_asInt64());
165 break;
167 default:
168 break;
170 is_null = fv.get_isNull();
173 field_value::field_value(field_value&& fv) noexcept
175 *this = std::move(fv);
178 //empty destructor
179 field_value::~field_value() = default;
181 //Conversations functions
182 std::string field_value::get_asString() const&
184 switch (field_type)
186 case ft_String:
188 return str_value;
190 case ft_Boolean:
192 if (bool_value)
193 return "True";
194 else
195 return "False";
197 case ft_Char:
199 return {char_value};
201 case ft_Short:
203 char t[10];
204 snprintf(t, sizeof(t), "%i", short_value);
205 return t;
207 case ft_UShort:
209 char t[10];
210 snprintf(t, sizeof(t), "%i", ushort_value);
211 return t;
213 case ft_Int:
215 char t[12];
216 snprintf(t, sizeof(t), "%d", int_value);
217 return t;
219 case ft_UInt:
221 char t[12];
222 snprintf(t, sizeof(t), "%u", uint_value);
223 return t;
225 case ft_Float:
227 char t[16];
228 snprintf(t, sizeof(t), "%f", static_cast<double>(float_value));
229 return t;
231 case ft_Double:
233 char t[32];
234 snprintf(t, sizeof(t), "%f", double_value);
235 return t;
237 case ft_Int64:
239 char t[23];
240 snprintf(t, sizeof(t), "%" PRId64, int64_value);
241 return t;
243 default:
244 return "";
248 std::string field_value::get_asString() &&
250 switch (field_type)
252 case ft_String:
253 return std::move(str_value);
254 default:
255 return get_asString();
259 bool field_value::get_asBool() const
261 switch (field_type)
263 case ft_String:
265 if (str_value == "True" || str_value == "true" || str_value == "1")
266 return true;
267 else
268 return false;
270 case ft_Boolean:
272 return bool_value;
274 case ft_Char:
276 if (char_value == 'T' || char_value == 't')
277 return true;
278 else
279 return false;
281 case ft_Short:
283 return (bool)short_value;
285 case ft_UShort:
287 return (bool)ushort_value;
289 case ft_Int:
291 return (bool)int_value;
293 case ft_UInt:
295 return (bool)uint_value;
297 case ft_Float:
299 return (bool)float_value;
301 case ft_Double:
303 return (bool)double_value;
305 case ft_Int64:
307 return (bool)int64_value;
309 default:
310 return false;
314 char field_value::get_asChar() const
316 switch (field_type)
318 case ft_String:
320 return str_value[0];
322 case ft_Boolean:
324 if (bool_value)
325 return 'T';
326 else
327 return 'F';
329 case ft_Char:
331 return char_value;
333 case ft_Short:
335 char t[10];
336 snprintf(t, sizeof(t), "%i", short_value);
337 return t[0];
339 case ft_UShort:
341 char t[10];
342 snprintf(t, sizeof(t), "%i", ushort_value);
343 return t[0];
345 case ft_Int:
347 char t[12];
348 snprintf(t, sizeof(t), "%d", int_value);
349 return t[0];
351 case ft_UInt:
353 char t[12];
354 snprintf(t, sizeof(t), "%u", uint_value);
355 return t[0];
357 case ft_Float:
359 char t[16];
360 snprintf(t, sizeof(t), "%f", static_cast<double>(float_value));
361 return t[0];
363 case ft_Double:
365 char t[32];
366 snprintf(t, sizeof(t), "%f", double_value);
367 return t[0];
369 case ft_Int64:
371 char t[24];
372 snprintf(t, sizeof(t), "%" PRId64, int64_value);
373 return t[0];
375 default:
376 return '\0';
380 short field_value::get_asShort() const
382 switch (field_type)
384 case ft_String:
386 return (short)atoi(str_value.c_str());
388 case ft_Boolean:
390 return (short)bool_value;
392 case ft_Char:
394 return (short)char_value;
396 case ft_Short:
398 return short_value;
400 case ft_UShort:
402 return (short)ushort_value;
404 case ft_Int:
406 return (short)int_value;
408 case ft_UInt:
410 return (short)uint_value;
412 case ft_Float:
414 return (short)float_value;
416 case ft_Double:
418 return (short)double_value;
420 case ft_Int64:
422 return (short)int64_value;
424 default:
425 return 0;
429 unsigned short field_value::get_asUShort() const
431 switch (field_type)
433 case ft_String:
435 return (unsigned short)atoi(str_value.c_str());
437 case ft_Boolean:
439 return (unsigned short)bool_value;
441 case ft_Char:
443 return (unsigned short)char_value;
445 case ft_Short:
447 return (unsigned short)short_value;
449 case ft_UShort:
451 return ushort_value;
453 case ft_Int:
455 return (unsigned short)int_value;
457 case ft_UInt:
459 return (unsigned short)uint_value;
461 case ft_Float:
463 return (unsigned short)float_value;
465 case ft_Double:
467 return (unsigned short)double_value;
469 case ft_Int64:
471 return (unsigned short)int64_value;
473 default:
474 return 0;
478 int field_value::get_asInt() const
480 switch (field_type)
482 case ft_String:
484 return atoi(str_value.c_str());
486 case ft_Boolean:
488 return (int)bool_value;
490 case ft_Char:
492 return (int)char_value;
494 case ft_Short:
496 return (int)short_value;
498 case ft_UShort:
500 return (int)ushort_value;
502 case ft_Int:
504 return int_value;
506 case ft_UInt:
508 return (int)uint_value;
510 case ft_Float:
512 return (int)float_value;
514 case ft_Double:
516 return (int)double_value;
518 case ft_Int64:
520 return (int)int64_value;
522 default:
523 return 0;
527 unsigned int field_value::get_asUInt() const
529 switch (field_type)
531 case ft_String:
533 return (unsigned int)atoi(str_value.c_str());
535 case ft_Boolean:
537 return (unsigned int)bool_value;
539 case ft_Char:
541 return (unsigned int)char_value;
543 case ft_Short:
545 return (unsigned int)short_value;
547 case ft_UShort:
549 return (unsigned int)ushort_value;
551 case ft_Int:
553 return (unsigned int)int_value;
555 case ft_UInt:
557 return uint_value;
559 case ft_Float:
561 return (unsigned int)float_value;
563 case ft_Double:
565 return (unsigned int)double_value;
567 case ft_Int64:
569 return (unsigned int)int64_value;
571 default:
572 return 0;
576 float field_value::get_asFloat() const
578 switch (field_type)
580 case ft_String:
582 return (float)atof(str_value.c_str());
584 case ft_Boolean:
586 return (float)bool_value;
588 case ft_Char:
590 return (float)char_value;
592 case ft_Short:
594 return (float)short_value;
596 case ft_UShort:
598 return (float)ushort_value;
600 case ft_Int:
602 return (float)int_value;
604 case ft_UInt:
606 return (float)uint_value;
608 case ft_Float:
610 return float_value;
612 case ft_Double:
614 return (float)double_value;
616 case ft_Int64:
618 return (float)int64_value;
620 default:
621 return 0.0;
625 double field_value::get_asDouble() const
627 switch (field_type)
629 case ft_String:
631 return atof(str_value.c_str());
633 case ft_Boolean:
635 return (double)bool_value;
637 case ft_Char:
639 return (double)char_value;
641 case ft_Short:
643 return (double)short_value;
645 case ft_UShort:
647 return (double)ushort_value;
649 case ft_Int:
651 return (double)int_value;
653 case ft_UInt:
655 return (double)uint_value;
657 case ft_Float:
659 return (double)float_value;
661 case ft_Double:
663 return (double)double_value;
665 case ft_Int64:
667 return (double)int64_value;
669 default:
670 return 0.0;
674 int64_t field_value::get_asInt64() const
676 switch (field_type)
678 case ft_String:
680 return std::atoll(str_value.c_str());
682 case ft_Boolean:
684 return (int64_t)bool_value;
686 case ft_Char:
688 return (int64_t)char_value;
690 case ft_Short:
692 return (int64_t)short_value;
694 case ft_UShort:
696 return (int64_t)ushort_value;
698 case ft_Int:
700 return (int64_t)int_value;
702 case ft_UInt:
704 return (int64_t)uint_value;
706 case ft_Float:
708 return (int64_t)float_value;
710 case ft_Double:
712 return (int64_t)double_value;
714 case ft_Int64:
716 return int64_value;
718 default:
719 return 0;
723 field_value& field_value::operator=(const field_value& fv)
725 if (this == &fv)
726 return *this;
728 is_null = fv.get_isNull();
730 switch (fv.get_fType())
732 case ft_String:
734 set_asString(fv.get_asString());
735 return *this;
736 break;
738 case ft_Boolean:
740 set_asBool(fv.get_asBool());
741 return *this;
742 break;
744 case ft_Char:
746 set_asChar(fv.get_asChar());
747 return *this;
748 break;
750 case ft_Short:
752 set_asShort(fv.get_asShort());
753 return *this;
754 break;
756 case ft_UShort:
758 set_asUShort(fv.get_asUShort());
759 return *this;
760 break;
762 case ft_Int:
764 set_asInt(fv.get_asInt());
765 return *this;
766 break;
768 case ft_UInt:
770 set_asUInt(fv.get_asUInt());
771 return *this;
772 break;
774 case ft_Float:
776 set_asFloat(fv.get_asFloat());
777 return *this;
778 break;
780 case ft_Double:
782 set_asDouble(fv.get_asDouble());
783 return *this;
784 break;
786 case ft_Int64:
788 set_asInt64(fv.get_asInt64());
789 return *this;
790 break;
792 default:
793 return *this;
797 field_value& field_value::operator=(field_value&& fv) noexcept
799 if (this == &fv)
800 return *this;
802 is_null = fv.get_isNull();
804 switch (fv.get_fType())
806 case ft_String:
807 set_asString(std::move(fv.str_value));
808 return *this;
809 default:
810 return *this = fv;
814 //Set functions
815 void field_value::set_asString(const char* s)
817 str_value = s;
818 field_type = ft_String;
821 void field_value::set_asString(const char* s, std::size_t len)
823 str_value = std::string_view(s, len);
824 field_type = ft_String;
827 void field_value::set_asString(const std::string& s)
829 str_value = s;
830 field_type = ft_String;
833 void field_value::set_asString(std::string&& s)
835 str_value = std::move(s);
836 field_type = ft_String;
839 void field_value::set_asBool(const bool b)
841 bool_value = b;
842 field_type = ft_Boolean;
845 void field_value::set_asChar(const char c)
847 char_value = c;
848 field_type = ft_Char;
851 void field_value::set_asShort(const short s)
853 short_value = s;
854 field_type = ft_Short;
857 void field_value::set_asUShort(const unsigned short us)
859 ushort_value = us;
860 field_type = ft_UShort;
863 void field_value::set_asInt(const int i)
865 int_value = i;
866 field_type = ft_Int;
869 void field_value::set_asUInt(const unsigned int ui)
871 int_value = ui;
872 field_type = ft_UInt;
875 void field_value::set_asFloat(const float f)
877 float_value = f;
878 field_type = ft_Float;
881 void field_value::set_asDouble(const double d)
883 double_value = d;
884 field_type = ft_Double;
887 void field_value::set_asInt64(const int64_t i)
889 int64_value = i;
890 field_type = ft_Int64;
893 fType field_value::get_field_type()
895 return field_type;
898 std::string field_value::gft()
900 std::string tmp;
901 switch (field_type)
903 case ft_String:
905 tmp.assign("string");
906 return tmp;
908 case ft_Boolean:
910 tmp.assign("bool");
911 return tmp;
913 case ft_Char:
915 tmp.assign("char");
916 return tmp;
918 case ft_Short:
920 tmp.assign("short");
921 return tmp;
923 case ft_Int:
925 tmp.assign("int");
926 return tmp;
928 case ft_Float:
930 tmp.assign("float");
931 return tmp;
933 case ft_Double:
935 tmp.assign("double");
936 return tmp;
938 case ft_Int64:
940 tmp.assign("int64");
941 return tmp;
943 default:
944 break;
947 return tmp;
950 } // namespace dbiplus