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
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 **********************************************************************/
28 /**********************************************************************
29 * 2005-03-29 - Minor modifications to allow get_asBool to function on
30 * on string values that are 1 or 0
31 **********************************************************************/
34 #include "system.h" // for PRId64
40 #pragma warning (disable:4800)
41 #pragma warning (disable:4715)
49 field_value::field_value(){
51 field_type = ft_String;
55 field_value::field_value(const char *s) {
57 field_type = ft_String;
61 field_value::field_value(const bool b) {
63 field_type = ft_Boolean;
67 field_value::field_value(const char c) {
73 field_value::field_value(const short s) {
75 field_type = ft_Short;
79 field_value::field_value(const unsigned short us) {
81 field_type = ft_UShort;
85 field_value::field_value(const int i) {
91 field_value::field_value(const unsigned int ui) {
97 field_value::field_value(const float f) {
99 field_type = ft_Float;
103 field_value::field_value(const double d) {
105 field_type = ft_Double;
109 field_value::field_value(const int64_t i) {
111 field_type = ft_Int64;
115 field_value::field_value (const field_value & fv) {
116 switch (fv.get_fType()) {
118 set_asString(fv.get_asString());
122 set_asBool(fv.get_asBool());
126 set_asChar(fv.get_asChar());
130 set_asShort(fv.get_asShort());
134 set_asUShort(fv.get_asUShort());
138 set_asInt(fv.get_asInt());
142 set_asUInt(fv.get_asUInt());
146 set_asFloat(fv.get_asFloat());
150 set_asDouble(fv.get_asDouble());
154 set_asInt64(fv.get_asInt64());
160 is_null = fv.get_isNull();
165 field_value::~field_value(){
170 //Conversations functions
171 string field_value::get_asString() const {
173 switch (field_type) {
182 return tmp = "False";
185 return tmp = char_value;
189 sprintf(t,"%i",short_value);
194 sprintf(t,"%i",ushort_value);
199 sprintf(t,"%d",int_value);
204 sprintf(t,"%u",uint_value);
209 sprintf(t,"%f",float_value);
214 sprintf(t,"%f",double_value);
219 sprintf(t,"%"PRId64,int64_value);
229 bool field_value::get_asBool() const {
230 switch (field_type) {
232 if (str_value == "True" || str_value == "true" || str_value == "1")
241 if (char_value == 'T' || char_value == 't')
247 return (bool)short_value;
250 return (bool)ushort_value;
253 return (bool)int_value;
256 return (bool)uint_value;
259 return (bool)float_value;
262 return (bool)double_value;
265 return (bool)int64_value;
273 char field_value::get_asChar() const {
274 switch (field_type) {
290 sprintf(t,"%i",short_value);
295 sprintf(t,"%i",ushort_value);
300 sprintf(t,"%d",int_value);
305 sprintf(t,"%u",uint_value);
310 sprintf(t,"%f",float_value);
315 sprintf(t,"%f",double_value);
320 sprintf(t,"%"PRId64,int64_value);
329 short field_value::get_asShort() const {
330 switch (field_type) {
332 return (short)atoi(str_value.c_str());
335 return (short)bool_value;
338 return (short)char_value;
344 return (short)ushort_value;
347 return (short)int_value;
350 return (short)uint_value;
353 return (short)float_value;
356 return (short)double_value;
359 return (short)int64_value;
367 unsigned short field_value::get_asUShort() const {
368 switch (field_type) {
370 return (unsigned short)atoi(str_value.c_str());
373 return (unsigned short)bool_value;
376 return (unsigned short)char_value;
379 return (unsigned short)short_value;
385 return (unsigned short)int_value;
388 return (unsigned short)uint_value;
391 return (unsigned short)float_value;
394 return (unsigned short)double_value;
397 return (unsigned short)int64_value;
404 int field_value::get_asInt() const {
405 switch (field_type) {
407 return (int)atoi(str_value.c_str());
410 return (int)bool_value;
413 return (int)char_value;
416 return (int)short_value;
419 return (int)ushort_value;
425 return (int)uint_value;
428 return (int)float_value;
431 return (int)double_value;
434 return (int)int64_value;
441 unsigned int field_value::get_asUInt() const {
442 switch (field_type) {
444 return (unsigned int)atoi(str_value.c_str());
447 return (unsigned int)bool_value;
450 return (unsigned int)char_value;
453 return (unsigned int)short_value;
456 return (unsigned int)ushort_value;
459 return (unsigned int)int_value;
465 return (unsigned int)float_value;
468 return (unsigned int)double_value;
471 return (unsigned int)int64_value;
478 float field_value::get_asFloat() const {
479 switch (field_type) {
481 return (float)atof(str_value.c_str());
484 return (float)bool_value;
487 return (float)char_value;
490 return (float)short_value;
493 return (float)ushort_value;
496 return (float)int_value;
499 return (float)uint_value;
505 return (float)double_value;
508 return (float)int64_value;
515 double field_value::get_asDouble() const {
516 switch (field_type) {
518 return atof(str_value.c_str());
521 return (double)bool_value;
524 return (double)char_value;
527 return (double)short_value;
530 return (double)ushort_value;
533 return (double)int_value;
536 return (double)uint_value;
539 return (double)float_value;
542 return (double)double_value;
545 return (double)int64_value;
552 int64_t field_value::get_asInt64() const {
553 switch (field_type) {
555 return _atoi64(str_value.c_str());
558 return (int64_t)bool_value;
561 return (int64_t)char_value;
564 return (int64_t)short_value;
567 return (int64_t)ushort_value;
570 return (int64_t)int_value;
573 return (int64_t)uint_value;
576 return (int64_t)float_value;
579 return (int64_t)double_value;
590 field_value& field_value::operator= (const field_value & fv) {
591 if ( this == &fv ) return *this;
593 is_null = fv.get_isNull();
595 switch (fv.get_fType()) {
597 set_asString(fv.get_asString());
602 set_asBool(fv.get_asBool());
607 set_asChar(fv.get_asChar());
612 set_asShort(fv.get_asShort());
617 set_asUShort(fv.get_asUShort());
622 set_asInt(fv.get_asInt());
627 set_asUInt(fv.get_asUInt());
632 set_asFloat(fv.get_asFloat());
637 set_asDouble(fv.get_asDouble());
642 set_asInt64(fv.get_asInt64());
654 void field_value::set_asString(const char *s) {
656 field_type = ft_String;}
658 void field_value::set_asString(const string & s) {
660 field_type = ft_String;}
662 void field_value::set_asBool(const bool b) {
664 field_type = ft_Boolean;}
666 void field_value::set_asChar(const char c) {
668 field_type = ft_Char;}
670 void field_value::set_asShort(const short s) {
672 field_type = ft_Short;}
674 void field_value::set_asUShort(const unsigned short us) {
676 field_type = ft_UShort;
679 void field_value::set_asInt(const int i) {
684 void field_value::set_asUInt(const unsigned int ui) {
686 field_type = ft_UInt;
689 void field_value::set_asFloat(const float f) {
691 field_type = ft_Float;}
693 void field_value::set_asDouble(const double d) {
695 field_type = ft_Double;}
697 void field_value::set_asInt64(const int64_t i) {
699 field_type = ft_Int64;}
701 fType field_value::get_field_type() {
705 string field_value::gft() {
707 switch (field_type) {
709 tmp.assign("string");
733 tmp.assign("double");