menu: added new Keywords tag to .desktop files
[barry.git] / src / parser.cc
blob4c8d9505ecdac12303b43c27ecd14a323a8acee3
1 ///
2 /// \file parser.cc
3 /// Virtual parser wrapper
4 ///
6 /*
7 Copyright (C) 2005-2013, 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.
22 #include "i18n.h"
23 #include "parser.h"
24 #include "r_calendar.h"
25 #include "r_calllog.h"
26 #include "r_bookmark.h"
27 #include "r_contact.h"
28 #include "r_memo.h"
29 #include "r_message.h"
30 #include "r_servicebook.h"
31 #include "r_task.h"
32 #include "r_pin_message.h"
33 #include "r_saved_message.h"
34 #include "r_sms.h"
35 #include "r_folder.h"
36 #include "r_timezone.h"
37 #include "r_cstore.h"
38 #include "r_hhagent.h"
39 #include "ios_state.h"
41 #include <iostream>
42 #include <memory>
44 using namespace std;
46 namespace Barry {
48 //////////////////////////////////////////////////////////////////////////////
49 // HexDumpParser class
51 HexDumpParser::HexDumpParser(std::ostream &os)
52 : m_os(os)
56 void HexDumpParser::ParseRecord(const Barry::DBData &data,
57 const IConverter *ic)
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()
72 << endl;
73 m_os << data.GetData() << endl;
77 //////////////////////////////////////////////////////////////////////////////
78 // DBNamesOnlyParser class
80 DBNamesOnlyParser::DBNamesOnlyParser(std::ostream &os)
81 : m_os(os)
85 void DBNamesOnlyParser::ParseRecord(const Barry::DBData &data,
86 const IConverter *ic)
88 ios_format_state state(m_os);
90 if( m_last_dbname != data.GetDBName() ) {
91 m_os << data.GetDBName() << endl;
92 m_last_dbname = data.GetDBName();
97 //////////////////////////////////////////////////////////////////////////////
98 // MultiRecordParser class
100 // takes ownership of default_parser!
101 MultiRecordParser::MultiRecordParser(Parser *default_parser)
102 : m_delete_default(default_parser) // takes ownership
103 , m_default(default_parser)
107 // does not take ownership of the default_parser
108 MultiRecordParser::MultiRecordParser(Parser &default_parser)
109 : m_delete_default(0) // no ownership of reference
110 , m_default(&default_parser)
114 MultiRecordParser::~MultiRecordParser()
116 map_type::iterator i = m_parsers.begin();
117 for( ; i != m_parsers.end(); ++i ) {
118 delete i->second;
121 // and the default parser
122 delete m_delete_default;
125 void MultiRecordParser::Add(const std::string &dbname, Parser *parser)
127 std::auto_ptr<Parser> p(parser);
129 map_type::iterator i = m_parsers.find(dbname);
130 if( i != m_parsers.end() ) {
131 // found existing parser, so delete it first
132 delete i->second;
134 // assign it
135 i->second = p.release();
137 else {
138 m_parsers[dbname] = p.get();
139 p.release();
143 // takes ownership of parser!
144 void MultiRecordParser::Add(RecordParserBase *parser)
146 std::auto_ptr<Parser> p(parser);
147 std::string name = parser->GetDBName();
148 Add(name, p.release());
151 bool MultiRecordParser::Add(const std::string &dbname,
152 std::ostream &os)
154 std::auto_ptr<Parser> p;
156 #undef HANDLE_PARSER
157 #define HANDLE_PARSER(tname) if( dbname == tname::GetDBName() ) { p.reset( new RecordParser<tname, DumpStore<tname> > (new DumpStore<tname>(os)) ); }
159 // check for recognized database names
160 ALL_KNOWN_PARSER_TYPES
162 if( !p.get() ) {
163 // name not known
164 return false;
167 Add(dbname, p.release());
168 return true;
171 bool MultiRecordParser::Add(const std::string &dbname, AllRecordStore &store)
173 #undef HANDLE_PARSER
174 #define HANDLE_PARSER(tname) \
175 if( dbname == tname::GetDBName() ) { \
176 Add(dbname, new RecordParser<tname, AllRecordStore>(store)); \
177 return true; \
180 // check for recognized database names
181 ALL_KNOWN_PARSER_TYPES
183 // if we get here, record was not found
184 return false;
187 // Parser overrides
188 void MultiRecordParser::ParseRecord(const DBData &data, const IConverter *ic)
190 // search for a named parser
191 map_type::iterator i = m_parsers.find(data.GetDBName());
192 if( i != m_parsers.end() ) {
193 // found one, use it
194 i->second->ParseRecord(data, ic);
196 else if( m_default ) {
197 // use default parser
198 m_default->ParseRecord(data, ic);
203 //////////////////////////////////////////////////////////////////////////////
204 // AllRecordDumpStore class
206 // Use the macro here to implement the overrides, so that
207 // the compiler will catch if we are missing any.
208 #undef HANDLE_PARSER
209 #define HANDLE_PARSER(tname) \
210 void AllRecordDumpStore::operator() (const Barry::tname &r) \
212 m_os << r << std::endl; \
215 ALL_KNOWN_PARSER_TYPES
218 //////////////////////////////////////////////////////////////////////////////
219 // AllRecordDumpParser class
221 AllRecordParser::AllRecordParser(std::ostream &os,
222 Parser *default_parser,
223 AllRecordStore *store)
224 : MultiRecordParser(default_parser)
225 , m_store(store) // takes ownership here
227 AddRecords(&os, store);
230 // does not take ownership of default_parser or store
231 AllRecordParser::AllRecordParser(Parser &default_parser, AllRecordStore &store)
232 : MultiRecordParser(default_parser)
233 , m_store(0)
235 AddRecords(0, &store);
238 AllRecordParser::~AllRecordParser()
240 delete m_store;
243 void AllRecordParser::AddRecords(std::ostream *os, AllRecordStore *store)
245 // Does not allow RecordParser<> to own store, since we're using
246 // it multiple times as the same store for each record type.
247 #undef HANDLE_PARSER
248 #define HANDLE_PARSER(tname) \
249 if( store ) { \
250 Add( new RecordParser<tname, AllRecordStore>(*store)); \
251 } else if( os ) { \
252 Add(tname::GetDBName(), *os); \
255 ALL_KNOWN_PARSER_TYPES;
259 //////////////////////////////////////////////////////////////////////////////
260 // TeeParser class
262 TeeParser::TeeParser()
266 TeeParser::~TeeParser()
268 // free all the owned parser pointers
269 for( parser_list_type::iterator i = m_owned_parsers.begin();
270 i != m_owned_parsers.end();
271 ++i )
273 delete *i;
277 // takes ownership of the pointer!
278 void TeeParser::Add(Parser *p)
280 std::auto_ptr<Parser> ap(p);
281 m_owned_parsers.push_back(ap.get());
282 ap.release();
285 // does NOT take ownership
286 void TeeParser::Add(Parser &p)
288 m_external_parsers.push_back(&p);
291 void TeeParser::ParseRecord(const DBData &data, const IConverter *ic)
293 // call all owned parsers
294 for( parser_list_type::iterator i = m_owned_parsers.begin();
295 i != m_owned_parsers.end();
296 ++i )
298 (*i)->ParseRecord(data, ic);
301 // call all external parsers
302 for( parser_list_type::iterator i = m_external_parsers.begin();
303 i != m_external_parsers.end();
304 ++i )
306 (*i)->ParseRecord(data, ic);
310 } // namespace Barry