1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
19 #define SmallestValJulDate (1721426)
21 Date::Date(int year
, int month
, int day
)
22 { YMDToJulian(year
, month
, day
, julianDate
); }
23 int Date::set(int year
, int month
, int day
)
24 { return YMDToJulian(year
,month
,day
,julianDate
); }
25 int Date::get(int &year
, int &month
, int &day
) const
26 { return julianToYMD(julianDate
,year
,month
,day
); }
28 int Date::parseFrom(const char *s
) {
31 count
= sscanf(s
,"%d/%d/%d",&month
,&day
,&year
);
32 if (count
< 3) return -1;
34 if (year
< 100) year
+= 1900;
36 if (!isValidDate(year
, month
, day
))
39 return set(year
,month
,day
);
42 int Date::dayOfMonth() const {
47 int Date::month() const {
52 int Date::year() const {
58 int Date::dayOfWeek() const { return dayOfWeek(julianDate
); }
60 const char *Date::dayOfWeekName() const
61 { return dayOfWeekName(dayOfWeek(julianDate
)); }
63 const char *Date::dayOfWeekAbbr() const
64 { return dayOfWeekAbbr(dayOfWeek(julianDate
)); }
66 static const char *day_names
[] = {
67 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
69 static const char *day_abbrs
[] = {
70 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
73 static const char *month_names
[] = {
74 "January", "February", "March", "April", "May", "June",
75 "July", "August", "September", "October", "November", "December"
77 static const char *month_abbrs
[] = {
78 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
79 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
83 static int daysBeforeMonth
[] = {
84 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
86 static int days_per_month
[] = {
87 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
90 const char * Date::dayOfWeekName(int day
)
91 { return (day
< 1 || day
> 7) ? 0 : day_names
[day
-1]; }
93 const char * Date::dayOfWeekAbbr(int day
)
94 { return (day
< 1 || day
> 7) ? 0 : day_abbrs
[day
-1]; }
96 const char * Date::monthName() const { return monthName(month()); }
97 const char * Date::monthAbbr() const { return monthAbbr(month()); }
98 const char * Date::monthName(int month
)
99 { return (month
< 1 || month
> 12) ? 0 : month_names
[month
-1]; }
100 const char * Date::monthAbbr(int month
)
101 { return (month
< 1 || month
> 12) ? 0 : month_abbrs
[month
-1]; }
102 Date
operator+(const Date
&d1
, int days
)
103 { return Date(d1
.julianDate
+ days
); }
104 Date
operator+(int days
, const Date
&d1
)
105 { return Date(d1
.julianDate
+ days
); }
106 Date
operator-(const Date
&d1
, int days
)
107 { return Date(d1
.julianDate
- days
); }
109 int operator-(const Date
&d1
, const Date
& d2
)
110 { return d1
.julianDate
- d2
.julianDate
; }
112 int operator<(const Date
&d1
,const Date
&d2
)
113 { return d1
.julianDate
< d2
.julianDate
; }
114 int operator>(const Date
&d1
,const Date
&d2
)
115 { return d1
.julianDate
> d2
.julianDate
; }
116 int operator<=(const Date
&d1
,const Date
&d2
)
117 { return d1
.julianDate
<= d2
.julianDate
; }
118 int operator>=(const Date
&d1
,const Date
&d2
)
119 { return d1
.julianDate
>= d2
.julianDate
; }
120 int operator==(const Date
&d1
,const Date
&d2
)
121 { return d1
.julianDate
== d2
.julianDate
; }
122 int operator!=(const Date
&d1
,const Date
&d2
)
123 { return d1
.julianDate
!= d2
.julianDate
; }
125 bool Date::isValid() const
126 { return julianDate
>= SmallestValJulDate
; }
128 bool Date::isLeapYear(int year
)
130 return (year
% 400 == 0) || ((year
% 4 == 0) && !(year
% 100 == 0));
133 int Date::dayOfYear() const {
136 int tmp
= daysBeforeMonth
[month
-1];
137 if (month
>= 3 && isLeapYear(year
))
142 int Date::daysInMonth(int month
, int year
) {
144 int tmp
= days_per_month
[month
];
145 if (month
== 1 && isLeapYear(year
)) tmp
++;
149 bool Date::isValidDate(int year
, int month
, int day
) {
150 if (year
< 1 || year
> 10000) return false;
151 if (month
< 1 || month
> 12) return false;
152 return (day
>= 1) && (day
<= daysInMonth(month
,year
));
155 // Algorithm Author: Robert G. Tantzen
156 int Date::YMDToJulian(int year
, int mon
, int day
, JulianRep
&jul
) {
157 if (!isValidDate(year
, mon
, day
))
158 { jul
= (JulianRep
)0; return -1; }
160 // year, month, day are assumed to be valid
161 int m
= mon
, d
= day
, y
= year
;
165 else { m
+= 9; --y
; }
168 j
= (146097*c
)/4 + (1461*ya
)/4 + (153*m
+2)/5 + d
+ 1721119;
173 // Julian date converter. Takes a julian date (the number of days since some
174 // distant epoch or other), and returns month, day of month, and year in
175 // integer references.
176 // Algorithm Author: Robert G. Tantzen
177 int Date::dayOfWeek(JulianRep jul
) {
178 return (int)((jul
+1)%7+1);
181 int Date::julianToYMD(JulianRep jul
, int &year
, int &month
, int &day
) {
185 if (j
< SmallestValJulDate
)
186 { year
= month
= day
= 0; return -1; }
189 y
= (4 * j
- 1)/146097;
190 j
= 4 * j
- 1 - 146097 * y
;
192 j
= (4 * d
+ 3)/1461;
193 d
= 4 * d
+ 3 - 1461 * j
;
196 d
= 5 * d
- 3 - 153 * m
;
200 else { m
-= 9; ++y
; }
207 #define MAX_VALID_SECONDS (60 * 60 * 24 -1)
208 Time::Time(int hours
, int mins
, int secs
, int usec
)
209 { set(hours
,mins
,secs
, usec
); }
211 int Time::set(int hours
, int mins
, int secs
, int usec
) {
212 if((hours
| mins
| secs
| usec
) < 0) { timeVal
= -1; return -1; }
213 if(hours
>= 24 | mins
>= 60 | secs
>= 62)
214 { timeVal
= -1; return -1; }
215 timeVal
= secs
+ mins
* 60 + hours
* 3600;
217 if(usec
) timeVal
+= usec
/100;
221 int Time::get(int &hours
, int &mins
, int &secs
) const {
222 if (timeVal
< 0) return -1;
223 int s
= timeVal
/10000;
232 int Time::seconds() const { return (timeVal
/10000) % 60; }
233 int Time::minutes() const { return (timeVal
/(60*10000)) % 60; }
234 int Time::hours() const { return timeVal
/ (3600*10000); }
235 int Time::msec() const { return (timeVal
% 10000) / 10; }
236 int Time::usec() const { return (timeVal
% 10000) * 100; }
238 int Time::setMsec(int ms
) {
239 if(ms
< 0 || ms
>= 1000) return -1;
240 timeVal
= timeVal
+(10*ms
);
243 int Time::setUsec(int us
) {
244 if(us
< 0 || us
>= 1000000) return -1;
245 timeVal
= timeVal
+us
/100;
249 bool Time::isValid() const
250 { return timeVal
>= 0 && timeVal
<= (10000*(MAX_VALID_SECONDS
+1)-1); }
252 bool Time::isValidTime(int hours
, int mins
, int secs
) {
253 return (hours
>= 0 && hours
< 24) &&
254 (mins
>= 0 && mins
< 60) &&
255 (secs
>= 0 && secs
< 60);
258 Time
operator+(const Time
&t1
, int seconds
)
259 { return Time(t1
.timeVal
+ seconds
*10000); }
260 Time
operator+(int seconds
, const Time
&t1
)
261 { return Time(t1
.timeVal
+ seconds
*10000); }
262 Time
operator-(const Time
&t1
, int seconds
)
263 { return Time(t1
.timeVal
- seconds
*10000); }
265 int operator-(const Time
&t1
, const Time
& t2
)
266 { return (t1
.timeVal
- t2
.timeVal
)/10000; }
268 int operator<(const Time
&t1
,const Time
&t2
)
269 { return t1
.timeVal
< t2
.timeVal
; }
270 int operator>(const Time
&t1
,const Time
&t2
)
271 { return t1
.timeVal
> t2
.timeVal
; }
272 int operator<=(const Time
&t1
,const Time
&t2
)
273 { return t1
.timeVal
<= t2
.timeVal
; }
274 int operator>=(const Time
&t1
,const Time
&t2
)
275 { return t1
.timeVal
>= t2
.timeVal
; }
276 int operator==(const Time
&t1
,const Time
&t2
)
277 { return t1
.timeVal
== t2
.timeVal
; }
278 int operator!=(const Time
&t1
,const Time
&t2
)
279 { return t1
.timeVal
!= t2
.timeVal
; }
281 int Time::parseFrom(const char *s
) {
284 count
= sscanf(s
,"%d:%d:%d",&hours
,&mins
,&secs
);
285 if (count
< 2) return -1;
286 if (count
== 2) secs
= 0;
288 if (!isValidTime(hours
,mins
,secs
))
290 return set(hours
,mins
,secs
);
293 int operator< (const TimeStamp
&d1
, const TimeStamp
&d2
)
294 { return (d1
.date
!= d2
.date
) ? d1
.date
< d2
.date
: d1
.time
< d2
.time
; }
295 int operator> (const TimeStamp
&d1
, const TimeStamp
&d2
)
296 { return (d1
.date
!= d2
.date
) ? d1
.date
> d2
.date
: d1
.time
> d2
.time
; }
297 int operator<=(const TimeStamp
&d1
, const TimeStamp
&d2
)
298 { return (d1
.date
!= d2
.date
) ? d1
.date
< d2
.date
: d1
.time
<= d2
.time
; }
299 int operator>=(const TimeStamp
&d1
, const TimeStamp
&d2
)
300 { return (d1
.date
!= d2
.date
) ? d1
.date
> d2
.date
: d1
.time
>= d2
.time
; }
301 int operator==(const TimeStamp
&d1
, const TimeStamp
&d2
)
302 { return d1
.date
== d2
.date
&& d1
.time
== d2
.time
; }
303 int operator!=(const TimeStamp
&d1
, const TimeStamp
&d2
)
304 { return d1
.date
!= d2
.date
&& d1
.time
!= d2
.time
; }
307 long AllDataType::size(DataType type
, int length
)
319 size
= sizeof(long long);
322 size
= sizeof(short);
328 size
= sizeof(double);
331 size
= sizeof(float);
335 //fldDef.length_ = sizeof(long double);
344 size
= sizeof(TimeStamp
);
356 SQLSMALLINT
AllDataType::convertToSQLType(DataType type
)
358 //printf("CSQL TYPE is %d\n", type);
382 return SQL_TYPE_DATE
;
384 return SQL_TYPE_TIME
;
386 return SQL_TYPE_TIMESTAMP
;
392 SQLSMALLINT
AllDataType::convertToSQL_C_Type(DataType type
)
394 //printf("CSQL TYPE is %d\n", type);
403 return SQL_C_SBIGINT
;
407 return SQL_C_STINYINT
;
416 return SQL_C_TYPE_DATE
;
418 return SQL_C_TYPE_TIME
;
420 return SQL_C_TYPE_TIMESTAMP
;
427 DataType
AllDataType::convertFromSQLType(SQLSMALLINT type
)
429 //printf("SQL TYPE is %d\n", type);
445 case SQL_TYPE_TIMESTAMP
:
446 return typeTimeStamp
;
454 void AllDataType::copyVal(void* dest
, void *src
, DataType type
, int length
)
459 *(int*)dest
= *(int*)src
;
462 *(long*)dest
= *(long*)src
;
465 *(long long*)dest
= *(long long*)src
;
468 *(short*)dest
= *(short*)src
;
471 *(char*)dest
= *(char*)src
;
474 *(double*)dest
= *(double*)src
;
477 *(float*)dest
= *(float*)src
;
482 *(Date
*)dest
= *(Date
*)src
;
485 *(Time
*)dest
= *(Time
*)src
;
488 *(TimeStamp
*)dest
= *(TimeStamp
*)src
;
492 strncpy((char*)dest
, (char*)src
, length
);
493 char *d
=(char*)dest
;
498 os::memcpy((char*)dest
, (char*)src
, length
);
505 bool AllDataType::compareVal(void *val1
, void *val2
, ComparisionOp op
,
506 DataType type
, long length
)
512 result
= AllDataType::compareIntVal(val1
, val2
, op
);
515 result
= AllDataType::compareLongVal(val1
, val2
, op
);
518 result
= AllDataType::compareLongLongVal(val1
, val2
, op
);
521 result
= AllDataType::compareShortVal(val1
, val2
, op
);
524 result
= AllDataType::compareByteIntVal(val1
, val2
, op
);
527 result
= AllDataType::compareDoubleVal(val1
, val2
, op
);
530 result
= AllDataType::compareFloatVal(val1
, val2
, op
);
536 result
= AllDataType::compareDateVal(val1
, val2
, op
);
539 result
= AllDataType::compareTimeVal(val1
, val2
, op
);
542 result
= AllDataType::compareTimeStampVal(val1
, val2
, op
);
545 result
= AllDataType::compareStringVal(val1
, val2
, op
);
548 result
= AllDataType::compareBinaryVal(val1
, val2
, op
, length
);
554 bool AllDataType::compareIntVal(void* src1
, void *src2
, ComparisionOp op
)
560 if (*(int*)src1
== *(int*)src2
) result
= true;
564 if (*(int*)src1
!= *(int*)src2
) result
= true;
568 if (*(int*)src1
< *(int*)src2
) result
= true;
571 case OpLessThanEquals
:
572 if (*(int*)src1
<= *(int*)src2
) result
= true;
576 if (*(int*)src1
> *(int*)src2
) result
= true;
579 case OpGreaterThanEquals
:
580 if (*(int*)src1
>= *(int*)src2
) result
= true;
587 bool AllDataType::compareLongVal(void* src1
, void *src2
, ComparisionOp op
)
593 if (*(long*)src1
== *(long*)src2
) result
= true;
597 if (*(long*)src1
!= *(long*)src2
) result
= true;
601 if (*(long*)src1
< *(long*)src2
) result
= true;
604 case OpLessThanEquals
:
605 if (*(long*)src1
<= *(long*)src2
) result
= true;
609 if (*(long*)src1
> *(long*)src2
) result
= true;
612 case OpGreaterThanEquals
:
613 if (*(long*)src1
>= *(long*)src2
) result
= true;
620 bool AllDataType::compareLongLongVal(void* src1
, void *src2
,
627 if (*(long long*)src1
== *(long long*)src2
) result
= true;
631 if (*(long long*)src1
!= *(long long*)src2
) result
= true;
635 if (*(long long*)src1
< *(long long*)src2
) result
= true;
638 case OpLessThanEquals
:
639 if (*(long long*)src1
<= *(long long*)src2
) result
= true;
643 if (*(long long*)src1
> *(long long*)src2
) result
= true;
646 case OpGreaterThanEquals
:
647 if (*(long long*)src1
>= *(long long*)src2
) result
= true;
654 bool AllDataType::compareShortVal(void* src1
, void *src2
, ComparisionOp op
)
660 if (*(short*)src1
== *(short*)src2
) result
= true;
664 if (*(short*)src1
!= *(short*)src2
) result
= true;
668 if (*(short*)src1
< *(short*)src2
) result
= true;
671 case OpLessThanEquals
:
672 if (*(short*)src1
<= *(short*)src2
) result
= true;
676 if (*(short*)src1
> *(short*)src2
) result
= true;
679 case OpGreaterThanEquals
:
680 if (*(short*)src1
>= *(short*)src2
) result
= true;
687 bool AllDataType::compareByteIntVal(void* src1
, void *src2
, ComparisionOp op
)
693 if (*(ByteInt
*)src1
== *(ByteInt
*)src2
) result
= true;
697 if (*(ByteInt
*)src1
!= *(ByteInt
*)src2
) result
= true;
701 if (*(ByteInt
*)src1
< *(ByteInt
*)src2
) result
= true;
704 case OpLessThanEquals
:
705 if (*(ByteInt
*)src1
<= *(ByteInt
*)src2
) result
= true;
709 if (*(ByteInt
*)src1
> *(ByteInt
*)src2
) result
= true;
712 case OpGreaterThanEquals
:
713 if (*(ByteInt
*)src1
>= *(ByteInt
*)src2
) result
= true;
720 bool AllDataType::compareDoubleVal(void* src1
, void *src2
, ComparisionOp op
)
726 if (*(double*)src1
== *(double*)src2
) result
= true;
730 if (*(double*)src1
!= *(double*)src2
) result
= true;
734 if (*(double*)src1
< *(double*)src2
) result
= true;
737 case OpLessThanEquals
:
738 if (*(double*)src1
<= *(double*)src2
) result
= true;
742 if (*(double*)src1
> *(double*)src2
) result
= true;
745 case OpGreaterThanEquals
:
746 if (*(double*)src1
>= *(double*)src2
) result
= true;
753 bool AllDataType::compareFloatVal(void* src1
, void *src2
, ComparisionOp op
)
759 if (*(float*)src1
== *(float*)src2
) result
= true;
763 if (*(float*)src1
!= *(float*)src2
) result
= true;
767 if (*(float*)src1
< *(float*)src2
) result
= true;
770 case OpLessThanEquals
:
771 if (*(float*)src1
<= *(float*)src2
) result
= true;
775 if (*(float*)src1
> *(float*)src2
) result
= true;
778 case OpGreaterThanEquals
:
779 if (*(float*)src1
>= *(float*)src2
) result
= true;
786 bool AllDataType::compareDateVal(void* src1
, void *src2
, ComparisionOp op
)
792 if (*(Date
*)src1
== *(Date
*)src2
) result
= true;
796 if (*(Date
*)src1
!= *(Date
*)src2
) result
= true;
800 if (*(Date
*)src1
< *(Date
*)src2
) result
= true;
803 case OpLessThanEquals
:
804 if (*(Date
*)src1
<= *(Date
*)src2
) result
= true;
808 if (*(Date
*)src1
> *(Date
*)src2
) result
= true;
811 case OpGreaterThanEquals
:
812 if (*(Date
*)src1
>= *(Date
*)src2
) result
= true;
819 bool AllDataType::compareTimeVal(void* src1
, void *src2
, ComparisionOp op
)
825 if (*(Time
*)src1
== *(Time
*)src2
) result
= true;
829 if (*(Time
*)src1
!= *(Time
*)src2
) result
= true;
833 if (*(Time
*)src1
< *(Time
*)src2
) result
= true;
836 case OpLessThanEquals
:
837 if (*(Time
*)src1
<= *(Time
*)src2
) result
= true;
841 if (*(Time
*)src1
> *(Time
*)src2
) result
= true;
844 case OpGreaterThanEquals
:
845 if (*(Time
*)src1
>= *(Time
*)src2
) result
= true;
852 bool AllDataType::compareTimeStampVal(void* src1
, void *src2
, ComparisionOp op
)
858 if (*(TimeStamp
*)src1
== *(TimeStamp
*)src2
) result
= true;
862 if (*(TimeStamp
*)src1
!= *(TimeStamp
*)src2
) result
= true;
866 if (*(TimeStamp
*)src1
< *(TimeStamp
*)src2
) result
= true;
869 case OpLessThanEquals
:
870 if (*(TimeStamp
*)src1
<= *(TimeStamp
*)src2
) result
= true;
874 if (*(TimeStamp
*)src1
> *(TimeStamp
*)src2
) result
= true;
877 case OpGreaterThanEquals
:
878 if (*(TimeStamp
*)src1
>= *(TimeStamp
*)src2
) result
= true;
885 bool AllDataType::compareStringVal(void* src1
, void *src2
, ComparisionOp op
)
888 int ret
= strcmp((char*)src1
, (char*)src2
);
892 if (ret
== 0 ) result
= true; else result
= false;
895 if (ret
!= 0 ) result
= true; else result
= false;
898 if (ret
< 0 ) result
= true; else result
= false;
900 case OpLessThanEquals
:
901 printf("Illegal Operator:Not Supported for String\n");
904 if (ret
> 0 ) result
= true; else result
= false;
906 case OpGreaterThanEquals
:
907 printf("Illegal Operator:Not Supported for String\n");
913 bool AllDataType::compareBinaryVal(void* src1
, void *src2
,
914 ComparisionOp op
, int length
)
917 int ret
= os::memcmp(src1
, src2
, length
);
921 if (ret
== 0 ) result
= true; else result
= false;
924 if (ret
!= 0 ) result
= true; else result
= false;
927 if (ret
< 0 ) result
= true; else result
= false;
929 case OpLessThanEquals
:
930 printf("Illegal Operator:Not Supported for Binary\n");
933 if (ret
> 0 ) result
= true; else result
= false;
935 case OpGreaterThanEquals
:
936 printf("Illegal Operator:Not Supported for Binary\n");
944 ComparisionOp
AllDataType::getComparisionOperator(char *str
)
947 if (strcmp(str
, "<=") == 0)
948 op
= OpLessThanEquals
;
949 else if (strcmp(str
, ">=") == 0)
950 op
= OpGreaterThanEquals
;
951 else if (strcmp(str
, "<") == 0)
953 else if (strcmp(str
, ">") == 0)
955 else if (strcmp(str
, "=") == 0)
957 else if (strcmp(str
, "!=") == 0 || strcmp(str
, "<>") == 0 )
960 op
= OpInvalidComparisionOp
;
964 void* AllDataType::alloc(DataType type
, int length
)
970 dest
= malloc(sizeof(int));
973 dest
= malloc(sizeof(long));
976 dest
= malloc(sizeof(long long));
979 dest
= malloc(sizeof(short));
982 dest
= malloc(sizeof(char));
985 dest
= malloc(sizeof(double));
988 dest
= malloc(sizeof(float));
992 //fldDef.length_ = sizeof(long double);
995 if (length
== 0 ) return NULL
;
996 dest
= malloc(length
);
999 dest
= malloc(sizeof(Date
));
1002 dest
= malloc(sizeof(Time
));
1005 dest
= malloc(sizeof(TimeStamp
));
1010 void AllDataType::strToValue(void* dest
, char *src
, DataType type
, int length
)
1016 sscanf( src
, "%d", &val
);
1021 sscanf( src
, "%ld", &val
);
1024 case typeLongLong
: {
1026 sscanf( src
, "%lld", &val
);
1027 *(long long*)dest
= val
;
1031 sscanf( src
, "%hd", &val
);
1032 *(short*)dest
= val
;
1036 sscanf( src
, "%c", &val
);
1041 sscanf( src
, "%lg", &val
);
1042 *(double*)dest
= val
;
1046 sscanf( src
, "%f", &val
);
1047 *(float*)dest
= val
;
1052 strncpy((char*)dest
, (char*)src
, length
);
1053 char *d
=(char*)dest
;
1058 res
= sscanf( src
, "%d-%d-%d", &y
, &m
, &d
);
1060 res
= sscanf( src
, "%d/%d/%d", &y
, &m
, &d
);
1063 fprintf(stderr
,"Error reading date. yyyy{-/}mm{-/}dd is the valid format.");
1066 Date
dateObj(y
,m
,d
);
1067 *(Date
*)dest
= dateObj
;
1071 res
= sscanf( src
, "%d:%d:%d", &h
, &m
, &s
);
1074 fprintf(stderr
, "Error reading time, hh:mm:ss is the valid format.");
1077 Time
timeObj(h
,m
,s
);
1078 *(Time
*)dest
= timeObj
;
1080 case typeTimeStamp
: {
1081 int d
,m
,y
, h
,mn
,s
, res
=0;
1082 res
= sscanf( src
, "%d-%d-%d %d:%d:%d", &y
, &m
, &d
, &h
, &mn
, &s
);
1084 res
= sscanf( src
, "%d-%d-%d, %d:%d:%d", &y
, &m
, &d
, &h
, &mn
, &s
);
1086 res
= sscanf( src
, "%d/%d/%d %d:%d:%d", &y
, &m
, &d
, &h
, &mn
, &s
);
1088 res
= sscanf( src
, "%d/%d/%d, %d:%d:%d", &y
, &m
, &d
, &h
, &mn
, &s
);
1091 fprintf(stderr
, "Error reading timestamp, yyyy{-/}mm{-/}dd[,] hh:mm:ss is the valid format.");
1094 TimeStamp
timeStampObj(y
,m
,d
,h
,mn
,s
);
1095 *(TimeStamp
*)dest
= timeStampObj
;
1103 void AllDataType::convert(DataType srcType
, void *src
,
1104 DataType destType
, void *dest
)
1106 switch ((DataType
) destType
)
1108 case typeInt
: convertToInt(dest
, src
, srcType
); break;
1109 case typeLong
: convertToLong(dest
, src
, srcType
); break;
1110 case typeLongLong
: convertToLongLong(dest
, src
, srcType
); break;
1111 case typeShort
: convertToShort(dest
, src
, srcType
); break;
1112 case typeByteInt
: convertToByteInt(dest
, src
, srcType
); break;
1114 case typeFloat
: convertToFloat(dest
, src
, srcType
); break;
1115 case typeDouble
: convertToDouble(dest
, src
, srcType
); break;
1118 case typeDecimal
: convertToDouble(dest
, src
, srcType
); break;
1120 case typeString
: convertToString(dest
, src
, srcType
); break;
1130 void AllDataType::convertToInt( void* dest
, void* src
, DataType srcType
)
1134 case typeInt
: *(int *)dest
= *(int *)src
; break;
1135 case typeLong
: *(int *)dest
=(int) *(long *)src
; break;
1136 case typeLongLong
: *(int *)dest
=(int) *(long long *)src
; break;
1137 case typeShort
: *(int *)dest
=(int) *(short *)src
; break;
1138 case typeByteInt
: *(int *)dest
=(int) *(char *)src
; break;
1140 case typeFloat
: *(int *)dest
= (int) *(float *)src
; break;
1141 case typeDouble
: *(int *)dest
=(int) *(double *)src
; break;
1143 case typeString
: sscanf((const char*)src
, "%d", (int*) dest
); break;
1149 default: *(int *)dest
= (int) 0;
1153 void AllDataType::convertToLong( void* dest
, void* src
, DataType srcType
)
1157 case typeInt
: *(long *)dest
=(long) *(int *)src
; break;
1158 case typeLong
: *(long *)dest
= *(long *)src
; break;
1159 case typeLongLong
: *(long *)dest
=(long) *(long long *)src
; break;
1160 case typeShort
: *(long *)dest
=(long) *(short *)src
; break;
1161 case typeByteInt
: *(long *)dest
=(long) *(char *)src
; break;
1163 case typeFloat
: *(long *)dest
= (long) *(float *)src
; break;
1164 case typeDouble
: *(long *)dest
=(long) *(double *)src
; break;
1166 case typeString
: sscanf((const char*)src
, "%ld", (long*) dest
); break;
1172 default: *(long *)dest
= (long) 0;
1177 void AllDataType::convertToLongLong( void* dest
, void* src
, DataType srcType
)
1181 case typeInt
: *(long long *)dest
=(long long) *(int *)src
; break;
1182 case typeLong
: *(long long *)dest
= (long long) *(long *)src
; break;
1183 case typeLongLong
: *(long long *)dest
= *(long long *)src
; break;
1184 case typeShort
: *(long long *)dest
=(long long) *(short *)src
; break;
1185 case typeByteInt
: *(long long *)dest
=(long long) *(char *)src
; break;
1187 case typeFloat
: *(long long *)dest
= (long long) *(float *)src
; break;
1188 case typeDouble
: *(long long *)dest
=(long long) *(double *)src
; break;
1190 case typeString
: sscanf((const char*)src
, "%lld", (long long*) dest
); break;
1196 default: *(long long *)dest
= (long long) 0;
1200 void AllDataType::convertToShort( void* dest
, void* src
, DataType srcType
)
1204 case typeInt
: *(short*)dest
=(short) *(int*)src
; break;
1205 case typeLong
: *(short*)dest
= (short) *(long*)src
; break;
1206 case typeLongLong
: *(short*)dest
= (short) *(long long*)src
; break;
1207 case typeShort
: *(short*)dest
= *(short*)src
; break;
1208 case typeByteInt
: *(short*)dest
=(short) *(char *)src
; break;
1210 case typeFloat
: *(short*)dest
= (short) *(float *)src
; break;
1211 case typeDouble
: *(short*)dest
=(short) *(double *)src
; break;
1213 case typeString
: sscanf((const char*)src
, "%hd", (short*) dest
); break;
1219 default: *(short*)dest
= (short) 0;
1223 void AllDataType::convertToByteInt( void* dest
, void* src
, DataType srcType
)
1227 case typeInt
: *(char*)dest
= (char) *(int*)src
; break;
1228 case typeLong
: *(char*)dest
= (char) *(long*)src
; break;
1229 case typeLongLong
: *(char*)dest
= (char) *(long long*)src
; break;
1230 case typeShort
: *(char*)dest
= (char) *(short*)src
; break;
1231 case typeByteInt
: *(char*)dest
= *(char *)src
; break;
1233 case typeFloat
: *(char*)dest
= (char) *(float *)src
; break;
1234 case typeDouble
: *(char*)dest
=(char) *(double *)src
; break;
1236 case typeString
: sscanf((const char*)src
, "%c", (char*) dest
); break;
1242 default: *(char*)dest
= (char) 0;
1246 void AllDataType::convertToFloat( void* dest
, void* src
, DataType srcType
)
1250 case typeInt
: *(float *)dest
=(float) *(int *)src
; break;
1251 case typeLong
: *(float *)dest
=(float) *(long *)src
; break;
1252 case typeLongLong
: *(float *)dest
=(float) *(long long *)src
; break;
1253 case typeShort
: *(float *)dest
=(float) *(short *)src
; break;
1254 case typeByteInt
: *(float *)dest
=(float) *(char *)src
; break;
1256 case typeFloat
: *(float *)dest
= *(float *)src
; break;
1257 case typeDouble
: *(float *)dest
=(float) *(double *)src
; break;
1259 case typeString
: sscanf((const char*)src
, "%f", (float*) dest
); break;
1265 default: *(float *)dest
= (float) 0;
1269 void AllDataType::convertToDouble( void* dest
, void* src
, DataType srcType
)
1273 case typeInt
: *(double *)dest
=(double) *(int *)src
; break;
1274 case typeLong
: *(double *)dest
=(double) *(long *)src
; break;
1275 case typeLongLong
: *(double *)dest
=(double) *(long long *)src
; break;
1276 case typeShort
: *(double *)dest
=(double) *(short *)src
; break;
1277 case typeByteInt
: *(double *)dest
=(double) *(char *)src
; break;
1279 case typeFloat
: *(double *)dest
=(double) *(float *)src
; break;
1280 case typeDouble
: *(double *)dest
= *(double *)src
; break;
1282 case typeString
: sscanf((const char*)src
, "%lf", (double*) dest
); break;
1288 default: *(double *)dest
= (double) 0;
1292 void AllDataType::convertToString( void* dest
, void* src
, DataType srcType
)
1298 sprintf ((char *)dest
, "%d", *(int *)src
);
1303 sprintf ((char *)dest
, "%ld", *(long *)src
);
1308 sprintf ((char *)dest
, "%lld", *(long long *)src
);
1313 sprintf ((char *)dest
, "%hd", *(short *)src
);
1318 sprintf ((char *)dest
, "%hd", *(char *)src
);
1324 sprintf ((char *)dest
, "%f", *(float *)src
);
1329 sprintf ((char *) dest
, "%lf", *(double *)src
);
1335 strcpy((char*)dest
, (char*)src
);
1340 Date
* dt
= (Date
*)src
;
1341 sprintf((char*) dest
, "%d/%d/%d", dt
->year(),
1342 dt
->month(), dt
->dayOfMonth());
1347 Time
* tm
= (Time
*)src
;
1348 sprintf((char*)dest
,"%d:%d:%d.%d", tm
->hours(), tm
->minutes(), tm
->seconds(), 0);
1353 TimeStamp
* tm
= (TimeStamp
*)src
;
1354 sprintf((char*)dest
, "%d/%d/%d %d:%d:%d.%d", tm
->year(),
1355 tm
->month(), tm
->dayOfMonth(), tm
->hours(),
1356 tm
->minutes(), tm
->seconds(), 0 );
1359 default: ((char*)dest
)[0] = '\0';
1364 void AllDataType::printVal(void* src
, DataType srcType
, int length
)
1370 printf ("%d\t", *(int *)src
);
1375 printf ("%ld\t", *(long *)src
);
1380 printf ("%lld\t", *(long long *)src
);
1385 printf("%hd\t", *(short *)src
);
1390 printf("%hd\t", *(char *)src
);
1396 printf("%f\t", *(float *)src
);
1401 printf("%lf\t", *(double *)src
);
1407 printf("%s\t", (char*)src
);
1412 Date
* dt
= (Date
*)src
;
1413 printf("%d/%d/%d\t", dt
->year(),
1414 dt
->month(), dt
->dayOfMonth());
1419 Time
* tm
= (Time
*)src
;
1420 printf("%d:%d:%d.%d\t", tm
->hours(), tm
->minutes(), tm
->seconds(), 0);
1425 TimeStamp
* tm
= (TimeStamp
*)src
;
1426 printf("%d/%d/%d %d:%d:%d.%d\t", tm
->year(),
1427 tm
->month(), tm
->dayOfMonth(), tm
->hours(),
1428 tm
->minutes(), tm
->seconds(), 0 );
1431 default: { printf("DataType not supported\n"); break; }