2 /// \file r_timezone.cc
3 /// Record parsing class for the timezone database.
7 Copyright (C) 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2008, Brian Edginton
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include "r_timezone.h"
24 #include "record-internal.h"
25 #include "protostructs.h"
33 using namespace Barry::Protocol
;
38 ///////////////////////////////////////////////////////////////////////////////
41 // Timezone Field Codes
42 #define TZFC_INDEX 0x01
43 #define TZFC_NAME 0x02
44 #define TZFC_OFFSET 0x03
46 #define TZFC_STARTMONTH 0x06
47 #define TZFC_ENDMONTH 0x0B
48 #define TZFC_TZTYPE 0x64
50 #define TZFC_END 0xffff
52 FieldLink
<Timezone
> TimezoneFieldLinks
[] = {
53 { TZFC_NAME
, "Name", 0, 0, &Timezone::TimeZoneName
, 0, 0 },
54 { TZFC_END
, "End of List", 0, 0, 0, 0, 0 },
66 const unsigned char* Timezone::ParseField(const unsigned char *begin
,
67 const unsigned char *end
)
69 const CommonField
*field
= (const CommonField
*) begin
;
71 // advance and check size
72 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
73 if( begin
> end
) // if begin==end, we are ok
76 if( !btohs(field
->size
) ) // if field has no size, something's up
79 if( field
->type
== TZFC_TZTYPE
) {
80 if( ( TZType
= field
->u
.uint32
) != 1 ) {
81 throw Error("Timezone::ParseField: Timezone Type is not valid");
86 // cycle through the type table
87 for( FieldLink
<Timezone
> *b
= TimezoneFieldLinks
;
91 if( b
->type
== field
->type
) {
93 std::string
&s
= this->*(b
->strMember
);
94 s
= ParseFieldString(field
);
95 return begin
; // done!
100 switch( field
->type
)
103 Index
= btohl(field
->u
.uint32
);
107 Offset
= btohs(field
->u
.int16
);
111 OffsetFraction
= Offset
% 60;
112 Offset
= Offset
/ 60;
115 OffsetFraction
= Offset
% 60;
116 Offset
= Offset
/ 60;
122 DSTOffset
= btohl(field
->u
.uint32
);
128 case TZFC_STARTMONTH
:
129 StartMonth
= btohl(field
->u
.uint32
);
133 EndMonth
= btohl(field
->u
.uint32
);
137 // if still not handled, add to the Unknowns list
139 uf
.type
= field
->type
;
140 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
141 Unknowns
.push_back(uf
);
143 // return new pointer for next field
147 void Timezone::ParseHeader(const Data
&data
, size_t &offset
)
149 // no header in Task records
152 void Timezone::ParseFields(const Data
&data
, size_t &offset
)
154 const unsigned char *finish
= ParseCommonFields(*this,
155 data
.GetData() + offset
, data
.GetData() + data
.GetSize());
156 offset
+= finish
- (data
.GetData() + offset
);
159 void Timezone::Clear()
161 TimeZoneName
.clear();
175 void Timezone::Dump(std::ostream
&os
) const
177 static const char *month
[] = {
178 "Jan", "Feb", "Mar", "Apr", "May",
179 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
182 os
<< "Task entry: 0x" << setbase(16) << RecordId
183 << " (" << (unsigned int)RecType
<< ")\n";
185 // cycle through the type table
186 for( const FieldLink
<Timezone
> *b
= TimezoneFieldLinks
;
191 const std::string
&s
= this->*(b
->strMember
);
193 os
<< " " << b
->name
<< ": " << s
<< "\n";
197 os
<< " Index: 0x" <<setw(2) << Index
<< "\n";
198 os
<< " Offset: " << (Left
? "-" : "+") << setbase(10) << Offset
<< "." << OffsetFraction
<< "\n";
199 os
<< " Use DST: " << (UseDST
? "true" : "false") << "\n";
201 if ((StartMonth
> 0) && (StartMonth
< 11))
202 os
<< "Start Month: " << month
[StartMonth
] << "\n";
204 os
<< "Start Month: unknown (" << setbase(10) << StartMonth
<< ")\n";
205 if ((EndMonth
> 0) && (EndMonth
< 11))
206 os
<< " End Month: " << month
[EndMonth
] << "\n";
208 os
<< " End Month: unknown (" << setbase(10) << EndMonth
<< ")\n";