ppp: minor cleanups to match other chatscripts order and documentation
[barry.git] / src / r_timezone.cc
blob57500d18b652d07bc9fa945de2da828a4a2a4489
1 ///
2 /// \file r_timezone.cc
3 /// Record parsing class for the timezone database.
4 ///
6 /*
7 Copyright (C) 2005-2010, 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"
26 #include "data.h"
27 #include "time.h"
28 #include "iconv.h"
29 #include "debug.h"
30 #include <ostream>
31 #include <iomanip>
33 using namespace std;
34 using namespace Barry::Protocol;
36 namespace Barry
39 ///////////////////////////////////////////////////////////////////////////////
40 // Timezone Class
42 // Timezone Field Codes
43 #define TZFC_INDEX 0x01
44 #define TZFC_NAME 0x02
45 #define TZFC_OFFSET 0x03
46 #define TZFC_DST 0x04
47 #define TZFC_STARTMONTH 0x06
48 #define TZFC_ENDMONTH 0x0B
49 #define TZFC_TZTYPE 0x64
51 #define TZFC_END 0xffff
53 static FieldLink<Timezone> TimezoneFieldLinks[] = {
54 { TZFC_NAME, "Name", 0, 0, &Timezone::TimeZoneName, 0, 0, 0, 0, true },
55 { TZFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
58 Timezone::Timezone()
60 Clear();
63 Timezone::~Timezone()
67 const unsigned char* Timezone::ParseField(const unsigned char *begin,
68 const unsigned char *end,
69 const IConverter *ic)
71 const CommonField *field = (const CommonField *) begin;
73 // advance and check size
74 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
75 if( begin > end ) // if begin==end, we are ok
76 return begin;
78 if( !btohs(field->size) ) // if field has no size, something's up
79 return begin;
81 if( field->type == TZFC_TZTYPE ) {
82 if( ( TZType = field->u.uint32 ) != 1 ) {
83 throw Error("Timezone::ParseField: Timezone Type is not valid");
85 return begin;
88 // cycle through the type table
89 for( FieldLink<Timezone> *b = TimezoneFieldLinks;
90 b->type != TZFC_END;
91 b++ )
93 if( b->type == field->type ) {
94 if( b->strMember ) {
95 std::string &s = this->*(b->strMember);
96 s = ParseFieldString(field);
97 if( b->iconvNeeded && ic )
98 s = ic->FromBB(s);
99 return begin; // done!
104 switch( field->type )
106 case TZFC_INDEX:
107 Index = btohl(field->u.uint32);
108 return begin;
110 case TZFC_OFFSET:
111 Offset = btohs(field->u.int16);
112 if (Offset < 0) {
113 Offset =~ Offset;
114 Offset++;
115 OffsetFraction = Offset % 60;
116 Offset = Offset / 60;
117 Left = true;
118 } else {
119 OffsetFraction = Offset % 60;
120 Offset = Offset / 60;
121 Left = false;
123 return begin;
125 case TZFC_DST:
126 DSTOffset = btohl(field->u.uint32);
127 if (DSTOffset) {
128 UseDST = true;
130 return begin;
132 case TZFC_STARTMONTH:
133 StartMonth = btohl(field->u.uint32);
134 return begin;
136 case TZFC_ENDMONTH:
137 EndMonth = btohl(field->u.uint32);
138 return begin;
141 // if still not handled, add to the Unknowns list
142 UnknownField uf;
143 uf.type = field->type;
144 uf.data.assign((const char*)field->u.raw, btohs(field->size));
145 Unknowns.push_back(uf);
147 // return new pointer for next field
148 return begin;
151 void Timezone::ParseHeader(const Data &data, size_t &offset)
153 // no header in Task records
156 void Timezone::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
158 const unsigned char *finish = ParseCommonFields(*this,
159 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
160 offset += finish - (data.GetData() + offset);
163 void Timezone::Clear()
165 RecType = GetDefaultRecType();
166 RecordId = 0;
168 TZType = 0;
169 DSTOffset = 0;
170 Index = 0;
171 Offset = 0;
172 OffsetFraction = 0;
173 StartMonth = -1;
174 EndMonth = -1;
175 Left = false;
176 UseDST = false;
178 TimeZoneName.clear();
180 Unknowns.clear();
183 void Timezone::Dump(std::ostream &os) const
185 static const char *month[] = {
186 "Jan", "Feb", "Mar", "Apr", "May",
187 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
190 os << "Task entry: 0x" << setbase(16) << RecordId
191 << " (" << (unsigned int)RecType << ")\n";
193 // cycle through the type table
194 for( const FieldLink<Timezone> *b = TimezoneFieldLinks;
195 b->type != TZFC_END;
196 b++ )
198 if( b->strMember ) {
199 const std::string &s = this->*(b->strMember);
200 if( s.size() )
201 os << " " << b->name << ": " << s << "\n";
205 os << " Index: 0x" <<setw(2) << Index << "\n";
206 os << " Offset: " << (Left ? "-" : "+") << setbase(10) << Offset << "." << OffsetFraction << "\n";
207 os << " Use DST: " << (UseDST ? "true" : "false") << "\n";
208 if (UseDST) {
209 if ((StartMonth > 0) && (StartMonth < 11))
210 os << "Start Month: " << month[StartMonth] << "\n";
211 else
212 os << "Start Month: unknown (" << setbase(10) << StartMonth << ")\n";
213 if ((EndMonth > 0) && (EndMonth < 11))
214 os << " End Month: " << month[EndMonth] << "\n";
215 else
216 os << " End Month: unknown (" << setbase(10) << EndMonth << ")\n";
219 os << Unknowns;
220 os << "\n\n";