3 /// Virtual parser wrapper
7 Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
24 #include "r_calendar.h"
25 #include "r_calllog.h"
26 #include "r_bookmark.h"
27 #include "r_contact.h"
29 #include "r_message.h"
30 #include "r_servicebook.h"
32 #include "r_pin_message.h"
33 #include "r_saved_message.h"
36 #include "r_timezone.h"
38 #include "r_hhagent.h"
39 #include "ios_state.h"
48 //////////////////////////////////////////////////////////////////////////////
49 // HexDumpParser class
51 HexDumpParser::HexDumpParser(std::ostream
&os
)
56 void HexDumpParser::ParseRecord(const Barry::DBData
&data
,
59 ios_format_state
state(m_os
);
61 if( m_last_dbname
!= data
.GetDBName() ) {
62 m_os
<< _("Records for database: ") << data
.GetDBName() << endl
;
63 m_last_dbname
= data
.GetDBName();
66 m_os
<< _("Raw record dump for record: ") << "0x"
67 << hex
<< data
.GetUniqueId()
68 << ", " << _("type: ")
69 << "0x" << hex
<< (unsigned int) data
.GetRecType()
70 << ", " << _("offset: ")
71 << "0x" << hex
<< data
.GetOffset()
73 m_os
<< data
.GetData() << endl
;
77 //////////////////////////////////////////////////////////////////////////////
78 // MultiRecordParser class
80 // takes ownership of default_parser!
81 MultiRecordParser::MultiRecordParser(Parser
*default_parser
)
82 : m_delete_default(default_parser
) // takes ownership
83 , m_default(default_parser
)
87 // does not take ownership of the default_parser
88 MultiRecordParser::MultiRecordParser(Parser
&default_parser
)
89 : m_delete_default(0) // no ownership of reference
90 , m_default(&default_parser
)
94 MultiRecordParser::~MultiRecordParser()
96 map_type::iterator i
= m_parsers
.begin();
97 for( ; i
!= m_parsers
.end(); ++i
) {
101 // and the default parser
102 delete m_delete_default
;
105 void MultiRecordParser::Add(const std::string
&dbname
, Parser
*parser
)
107 std::auto_ptr
<Parser
> p(parser
);
109 map_type::iterator i
= m_parsers
.find(dbname
);
110 if( i
!= m_parsers
.end() ) {
111 // found existing parser, so delete it first
115 i
->second
= p
.release();
118 m_parsers
[dbname
] = p
.get();
123 // takes ownership of parser!
124 void MultiRecordParser::Add(RecordParserBase
*parser
)
126 std::auto_ptr
<Parser
> p(parser
);
127 std::string name
= parser
->GetDBName();
128 Add(name
, p
.release());
131 bool MultiRecordParser::Add(const std::string
&dbname
,
134 std::auto_ptr
<Parser
> p
;
137 #define HANDLE_PARSER(tname) if( dbname == tname::GetDBName() ) { p.reset( new RecordParser<tname, DumpStore<tname> > (new DumpStore<tname>(os)) ); }
139 // check for recognized database names
140 ALL_KNOWN_PARSER_TYPES
147 Add(dbname
, p
.release());
151 bool MultiRecordParser::Add(const std::string
&dbname
, AllRecordStore
&store
)
154 #define HANDLE_PARSER(tname) \
155 if( dbname == tname::GetDBName() ) { \
156 Add(dbname, new RecordParser<tname, AllRecordStore>(store)); \
160 // check for recognized database names
161 ALL_KNOWN_PARSER_TYPES
163 // if we get here, record was not found
168 void MultiRecordParser::ParseRecord(const DBData
&data
, const IConverter
*ic
)
170 // search for a named parser
171 map_type::iterator i
= m_parsers
.find(data
.GetDBName());
172 if( i
!= m_parsers
.end() ) {
174 i
->second
->ParseRecord(data
, ic
);
176 else if( m_default
) {
177 // use default parser
178 m_default
->ParseRecord(data
, ic
);
183 //////////////////////////////////////////////////////////////////////////////
184 // AllRecordDumpStore class
186 // Use the macro here to implement the overrides, so that
187 // the compiler will catch if we are missing any.
189 #define HANDLE_PARSER(tname) \
190 void AllRecordDumpStore::operator() (const Barry::tname &r) \
192 m_os << r << std::endl; \
195 ALL_KNOWN_PARSER_TYPES
198 //////////////////////////////////////////////////////////////////////////////
199 // AllRecordDumpParser class
201 AllRecordParser::AllRecordParser(std::ostream
&os
,
202 Parser
*default_parser
,
203 AllRecordStore
*store
)
204 : MultiRecordParser(default_parser
)
205 , m_store(store
) // takes ownership here
207 AddRecords(&os
, store
);
210 // does not take ownership of default_parser or store
211 AllRecordParser::AllRecordParser(Parser
&default_parser
, AllRecordStore
&store
)
212 : MultiRecordParser(default_parser
)
215 AddRecords(0, &store
);
218 AllRecordParser::~AllRecordParser()
223 void AllRecordParser::AddRecords(std::ostream
*os
, AllRecordStore
*store
)
225 // Does not allow RecordParser<> to own store, since we're using
226 // it multiple times as the same store for each record type.
228 #define HANDLE_PARSER(tname) \
230 Add( new RecordParser<tname, AllRecordStore>(*store)); \
232 Add(tname::GetDBName(), *os); \
235 ALL_KNOWN_PARSER_TYPES
;
239 //////////////////////////////////////////////////////////////////////////////
242 TeeParser::TeeParser()
246 TeeParser::~TeeParser()
248 // free all the owned parser pointers
249 for( parser_list_type::iterator i
= m_owned_parsers
.begin();
250 i
!= m_owned_parsers
.end();
257 // takes ownership of the pointer!
258 void TeeParser::Add(Parser
*p
)
260 std::auto_ptr
<Parser
> ap(p
);
261 m_owned_parsers
.push_back(ap
.get());
265 // does NOT take ownership
266 void TeeParser::Add(Parser
&p
)
268 m_external_parsers
.push_back(&p
);
271 void TeeParser::ParseRecord(const DBData
&data
, const IConverter
*ic
)
273 // call all owned parsers
274 for( parser_list_type::iterator i
= m_owned_parsers
.begin();
275 i
!= m_owned_parsers
.end();
278 (*i
)->ParseRecord(data
, ic
);
281 // call all external parsers
282 for( parser_list_type::iterator i
= m_external_parsers
.begin();
283 i
!= m_external_parsers
.end();
286 (*i
)->ParseRecord(data
, ic
);