lib: cleaned up Clear() functions in all record classes
[barry.git] / src / r_servicebook.cc
blob34bd72e6f22fcb38416fd36e13cea2ad3c68539e
1 ///
2 /// \file r_servicebook.cc
3 /// Blackberry database record parser class for
4 /// Service Book records.
5 ///
7 /*
8 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
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_servicebook.h"
24 #include "record-internal.h"
25 #include "protocol.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "time.h"
29 #include "error.h"
30 #include "endian.h"
31 #include "iconv.h"
32 #include <ostream>
33 #include <iomanip>
34 #include <time.h>
35 #include <stdexcept>
37 #define __DEBUG_MODE__
38 #include "debug.h"
40 using namespace std;
41 using namespace Barry::Protocol;
43 namespace Barry {
45 ///////////////////////////////////////////////////////////////////////////////
46 // ServiceBookConfig class
48 // service book packed field codes
49 #define SBFCC_END 0xffff
51 static FieldLink<ServiceBookConfig> ServiceBookConfigFieldLinks[] = {
52 // { SBFC_DSID, "DSID", 0, 0, &ServiceBook::DSID, 0, 0 },
53 { SBFCC_END, "End of List",0, 0, 0, 0, 0 }
56 ServiceBookConfig::ServiceBookConfig()
57 : Format(0)
59 Clear();
62 ServiceBookConfig::~ServiceBookConfig()
66 const unsigned char* ServiceBookConfig::ParseField(const unsigned char *begin,
67 const unsigned char *end,
68 const IConverter *ic)
70 const void *raw;
71 uint16_t size, type;
73 switch( Format )
75 case 0x01:
76 case 0x02:
78 const PackedField_02 *field = (const PackedField_02 *) begin;
79 raw = field->raw;
80 size = field->size;
81 type = field->type;
82 begin += PACKED_FIELD_02_HEADER_SIZE + size;
84 break;
86 case 0x10:
88 const PackedField_10 *field = (const PackedField_10 *) begin;
89 raw = field->raw;
90 size = field->size;
91 type = field->type;
92 begin += PACKED_FIELD_10_HEADER_SIZE + size;
94 break;
96 default:
97 eout("------> Unknown packed field format: 0x" << std::hex <<
98 (unsigned int) Format);
99 throw BadPackedFormat(Format);
100 return begin + 1;
104 // check size
105 if( begin > end ) // if begin==end, we are ok
106 return begin;
108 if( !size ) // if field has no size, something's up
109 return begin;
111 // cycle through the type table
112 for( FieldLink<ServiceBookConfig> *b = ServiceBookConfigFieldLinks;
113 b->type != SBFCC_END;
114 b++ )
116 if( b->type == type ) {
117 if( b->strMember ) {
118 std::string &s = this->*(b->strMember);
119 s = ParseFieldString(raw, size-1);
120 return begin; // done!
126 // handle special cases
127 switch( type )
132 // if still not handled, add to the Unknowns list
133 UnknownField uf;
134 uf.type = type;
135 uf.data.assign((const char*)raw, size);
136 Unknowns.push_back(uf);
138 // return new pointer for next field
139 return begin;
142 void ServiceBookConfig::ParseHeader(const Data &data, size_t &offset)
144 MAKE_RECORD(const Barry::Protocol::ServiceBookConfigField, sbc, data, offset);
145 offset += SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE;
146 if( data.GetSize() >= offset ) { // size check!
147 Format = sbc->format;
151 void ServiceBookConfig::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
153 const unsigned char *finish = ParseCommonFields(*this,
154 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
155 offset += finish - (data.GetData() + offset);
158 void ServiceBookConfig::BuildHeader(Data &data, size_t &offset) const
160 // make sure there is enough space
161 data.GetBuffer(offset + SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE);
163 MAKE_RECORD(Barry::Protocol::ServiceBookConfigField, sbc, data, offset);
164 sbc->format = Format;
166 offset += SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE;
170 // BuildFields
172 /// Build fields part of record
174 void ServiceBookConfig::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
176 throw std::logic_error("ServiceBookConfig::Build not yet implemented");
179 void ServiceBookConfig::Clear()
181 Format = 0;
182 Unknowns.clear();
185 void ServiceBookConfig::Dump(std::ostream &os) const
187 os << " ServiceBookConfig Format: " << setbase(16) << (uint16_t)Format << "\n";
189 // cycle through the type table
190 for( const FieldLink<ServiceBookConfig> *b = ServiceBookConfigFieldLinks;
191 b->type != SBFCC_END;
192 b++ )
194 if( b->strMember ) {
195 const std::string &s = this->*(b->strMember);
196 if( s.size() )
197 os << " " << b->name << ": " << s << "\n";
199 else if( b->timeMember ) {
200 time_t t = this->*(b->timeMember);
201 if( t > 0 )
202 os << " " << b->name << ": " << ctime(&t);
206 // print any unknowns
207 os << Unknowns;
208 os << " ------------------- End of Config Field\n";
212 ///////////////////////////////////////////////////////////////////////////////
213 // ServiceBook class
215 // service book field codes
216 #define SBFC_OLD_NAME 0x01
217 #define SBFC_HIDDEN_NAME 0x02
218 #define SBFC_NAME 0x03
219 #define SBFC_OLD_UNIQUE_ID 0x06
220 #define SBFC_UNIQUE_ID 0x07
221 #define SBFC_CONTENT_ID 0x08
222 #define SBFC_CONFIG 0x09
223 #define SBFC_OLD_DESC 0x32
224 #define SBFC_DESCRIPTION 0x0f
225 #define SBFC_DSID 0xa1
226 #define SBFC_BES_DOMAIN 0xa2
227 #define SBFC_USER_ID 0xa3
228 #define SBFC_END 0xffff
230 // private data class, containing internal structures
231 class ServiceBookData
233 public:
234 FieldLink<ServiceBook> *m_typeSet;
235 ServiceBookData(FieldLink<ServiceBook> *typeSet) : m_typeSet(typeSet) {}
238 // The Old/New tables contain the same fields, but use different
239 // type codes. Keeping them separate yet linked makes it possible
240 // to convert between old and new type codes, while hopefully keeping
241 // things generic.
242 static FieldLink<ServiceBook> ServiceBookOldFieldLinks[] = {
243 { SBFC_OLD_NAME, "Old Name", 0, 0, &ServiceBook::Name, 0, 0, 0, 0, true },
244 { SBFC_OLD_DESC, "Old Desc", 0, 0, &ServiceBook::Description, 0, 0, 0, 0, true },
245 { SBFC_OLD_UNIQUE_ID, "Old UniqueId", 0, 0, &ServiceBook::UniqueId, 0, 0, 0, 0, false },
246 { SBFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
249 static FieldLink<ServiceBook> ServiceBookNewFieldLinks[] = {
250 { SBFC_NAME, "Name", 0, 0, &ServiceBook::Name, 0, 0, 0, 0, true },
251 { SBFC_DESCRIPTION, "Description", 0, 0, &ServiceBook::Description, 0, 0, 0, 0, true },
252 { SBFC_UNIQUE_ID, "UniqueId", 0, 0, &ServiceBook::UniqueId, 0, 0, 0, 0, false },
253 { SBFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
256 // This table holds all
257 static FieldLink<ServiceBook> ServiceBookFieldLinks[] = {
258 { SBFC_HIDDEN_NAME, "Hidden Name",0, 0, &ServiceBook::HiddenName, 0, 0, 0, 0, true },
259 { SBFC_DSID, "DSID", 0, 0, &ServiceBook::DSID, 0, 0, 0, 0, false },
260 { SBFC_CONTENT_ID, "ContentId", 0, 0, &ServiceBook::ContentId, 0, 0, 0, 0, false },
261 { SBFC_BES_DOMAIN, "BES Domain", 0, 0, &ServiceBook::BesDomain, 0, 0, 0, 0, false },
262 { SBFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
265 // Array of conflicting tables only
266 static FieldLink<ServiceBook> *ServiceBookLinkTable[] = {
267 ServiceBookOldFieldLinks,
268 ServiceBookNewFieldLinks,
272 #define FIELDLINK_END 0xffff
274 template <class RecordT>
275 FieldLink<RecordT>* ParseFieldByTable(RecordT *rec,
276 const CommonField *field,
277 const IConverter *ic,
278 FieldLink<RecordT> *links)
280 // cycle through the type table
281 for( FieldLink<RecordT> *b = links; b->type != FIELDLINK_END; b++ ) {
282 if( b->type == field->type ) {
283 if( b->strMember ) {
284 std::string &s = rec->*(b->strMember);
285 if( s.size() ) {
286 dout(RecordT::GetDBName() << ": field '" << b->name << "' already has data (" << s << "). Overwriting.");
288 s = ParseFieldString(field);
289 if( b->iconvNeeded && ic )
290 s = ic->FromBB(s);
291 return links;
293 else if( b->timeMember && btohs(field->size) == 4 ) {
294 time_t &t = rec->*(b->timeMember);
295 t = min2time(field->u.min1900);
296 return links;
300 return 0;
303 template <class RecordT>
304 FieldLink<RecordT>* ParseFieldByTable(RecordT *rec,
305 const CommonField *field,
306 const IConverter *ic,
307 FieldLink<RecordT> **b)
309 for( ; *b; b++ ) {
310 FieldLink<RecordT> *link =
311 ParseFieldByTable<RecordT>(rec, field, ic, *b);
312 if( link )
313 return link;
315 return 0;
318 ServiceBook::ServiceBook()
319 : m_data( new ServiceBookData(ServiceBookOldFieldLinks) )
320 , RecordId(0)
322 Clear();
325 ServiceBook::~ServiceBook()
329 const unsigned char* ServiceBook::ParseField(const unsigned char *begin,
330 const unsigned char *end,
331 const IConverter *ic)
333 const CommonField *field = (const CommonField *) begin;
335 // advance and check size
336 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
337 if( begin > end ) // if begin==end, we are ok
338 return begin;
340 if( !btohs(field->size) ) // if field has no size, something's up
341 return begin;
343 // cycle through the type tables
344 FieldLink<ServiceBook> *typeSet =
345 ParseFieldByTable(this, field, ic, ServiceBookLinkTable);
346 if( typeSet ) {
347 if( m_data->m_typeSet && m_data->m_typeSet != typeSet ) {
348 dout("ServiceBook record has a mix of old and new field types.");
350 m_data->m_typeSet = typeSet;
351 return begin;
353 else {
354 if( ParseFieldByTable(this, field, ic, ServiceBookFieldLinks) )
355 return begin; // done!
358 // handle special cases
359 switch( field->type )
361 case SBFC_CONFIG:
362 try {
363 Data config((const void *)field->u.raw, btohs(field->size));
364 size_t offset = 0;
365 Config.ParseHeader(config, offset);
366 Config.ParseFields(config, offset);
367 return begin; // success
369 catch( BadPackedFormat & ) {
370 // break here so unprocessed raw packet is still
371 // visible in dump
372 break;
376 // if still not handled, add to the Unknowns list
377 UnknownField uf;
378 uf.type = field->type;
379 uf.data.assign((const char*)field->u.raw, btohs(field->size));
380 Unknowns.push_back(uf);
382 // return new pointer for next field
383 return begin;
386 void ServiceBook::ParseHeader(const Data &data, size_t &offset)
388 // no header in this record (?)
391 void ServiceBook::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
393 const unsigned char *finish = ParseCommonFields(*this,
394 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
395 offset += finish - (data.GetData() + offset);
398 void ServiceBook::BuildHeader(Data &data, size_t &offset) const
400 // no header in this record (?)
404 // BuildFields
406 /// Build fields part of record
408 void ServiceBook::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
410 throw std::logic_error("ServiceBook::BuildFields not yet implemented");
413 void ServiceBook::Clear()
415 m_data->m_typeSet = ServiceBookOldFieldLinks;
416 Unknowns.clear();
417 Config.Clear();
420 inline void FormatStr(std::ostream &os, const char *name, const std::string &str)
422 if( str.size() ) {
423 os << " " << setw(20) << name;
424 os << ": " << str << "\n";
428 void ServiceBook::Dump(std::ostream &os) const
430 ios::fmtflags oldflags = os.setf(ios::left);
431 char fill = os.fill(' ');
433 os << "ServiceBook entry: 0x" << setbase(16) << RecordId
434 << " (" << (unsigned int)RecType << ")\n";
436 FormatStr(os, "Name", Name);
437 FormatStr(os, "Hidden Name", HiddenName);
438 FormatStr(os, "Description", Description);
439 FormatStr(os, "DSID", DSID);
440 FormatStr(os, "Unique ID", UniqueId);
441 FormatStr(os, "Content ID", ContentId);
442 FormatStr(os, "(BES) Domain", BesDomain);
444 os << Config;
446 // print any unknowns
447 os << Unknowns;
449 // cleanup the stream
450 os.flags(oldflags);
451 os.fill(fill);
454 bool ServiceBook::operator<(const ServiceBook &other) const
456 int cmp = BesDomain.compare(other.BesDomain);
457 if( cmp == 0 )
458 cmp = DSID.compare(other.DSID);
459 if( cmp == 0 )
460 cmp = Name.compare(other.Name);
461 if( cmp == 0 )
462 cmp = UniqueId.compare(other.UniqueId);
463 return cmp < 0;
466 } // namespace Barry