- applied Brian Edginton's Saved Email Messages patch
[barry.git] / src / record.h
blobc4e34d4baeb29390c8e4186e2d903b7d67cf9872
1 ///
2 /// \file record.h
3 /// Blackberry database record classes. Help translate data
4 /// from data packets to useful structurs, and back.
5 /// This header provides the common types and classes
6 /// used by the general record parser classes in the
7 /// r_*.h files. Only application-safe API stuff goes in
8 /// here. Internal library types go in record-internal.h
9 ///
12 Copyright (C) 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 See the GNU General Public License in the COPYING file at the
24 root directory of this project for more details.
27 #ifndef __BARRY_RECORD_H__
28 #define __BARRY_RECORD_H__
30 #include <iosfwd>
31 #include <string>
32 #include <vector>
33 #include <map>
34 #include <stdint.h>
36 // forward declarations
37 namespace Barry { class Data; }
39 namespace Barry {
42 // NOTE: All classes here must be container-safe! Perhaps add sorting
43 // operators in the future.
48 class CommandTable
50 public:
51 struct Command
53 unsigned int Code;
54 std::string Name;
57 typedef std::vector<Command> CommandArrayType;
59 CommandArrayType Commands;
61 private:
62 const unsigned char* ParseField(const unsigned char *begin,
63 const unsigned char *end);
64 public:
65 CommandTable();
66 ~CommandTable();
68 void Parse(const Data &data, size_t offset);
69 void Clear();
71 // returns 0 if unable to find command name, which is safe, since
72 // 0 is a special command that shouldn't be in the table anyway
73 unsigned int GetCommand(const std::string &name) const;
75 void Dump(std::ostream &os) const;
78 inline std::ostream& operator<< (std::ostream &os, const CommandTable &command) {
79 command.Dump(os);
80 return os;
85 class RecordStateTable
87 public:
88 struct State
90 unsigned int Index;
91 uint32_t RecordId;
92 bool Dirty;
93 unsigned int RecType;
94 std::string Unknown2;
97 typedef unsigned int IndexType;
98 typedef std::map<IndexType, State> StateMapType;
100 StateMapType StateMap;
102 private:
103 mutable IndexType m_LastNewRecordId;
105 private:
106 const unsigned char* ParseField(const unsigned char *begin,
107 const unsigned char *end);
109 public:
110 RecordStateTable();
111 ~RecordStateTable();
113 void Parse(const Data &data);
114 void Clear();
116 bool GetIndex(uint32_t RecordId, IndexType *pFoundIndex = 0) const;
117 uint32_t MakeNewRecordId() const;
119 void Dump(std::ostream &os) const;
122 inline std::ostream& operator<< (std::ostream &os, const RecordStateTable &rst) {
123 rst.Dump(os);
124 return os;
129 class DatabaseDatabase
131 public:
132 struct Database
134 unsigned int Number;
135 unsigned int RecordCount;
136 std::string Name;
139 typedef std::vector<Database> DatabaseArrayType;
141 DatabaseArrayType Databases;
143 private:
144 template <class RecordType, class FieldType>
145 void ParseRec(const RecordType &rec, const unsigned char *end);
147 template <class FieldType>
148 const unsigned char* ParseField(const unsigned char *begin,
149 const unsigned char *end);
151 public:
152 DatabaseDatabase();
153 ~DatabaseDatabase();
155 void Parse(const Data &data);
156 void Clear();
158 // returns true on success, and fills target
159 bool GetDBNumber(const std::string &name, unsigned int &number) const;
160 bool GetDBName(unsigned int number, std::string &name) const;
162 void Dump(std::ostream &os) const;
165 inline std::ostream& operator<<(std::ostream &os, const DatabaseDatabase &dbdb) {
166 dbdb.Dump(os);
167 return os;
170 struct UnknownField
172 uint8_t type;
173 std::string data;
175 std::ostream& operator<< (std::ostream &os, const std::vector<UnknownField> &unknowns);
177 struct Address
179 std::string Name;
180 std::string Email;
182 void clear()
184 Name.clear();
185 Email.clear();
189 /// \addtogroup RecordParserClasses
190 /// Parser and data storage classes. These classes take a
191 /// Database Database record and convert them into C++ objects.
192 /// Each of these classes are safe to be used in standard
193 /// containers, and are meant to be used in conjunction with the
194 /// RecordParser<> template when calling Controller::LoadDatabase().
195 /// @{
196 /// @}
198 } // namespace Barry
200 // Include all parser classes, to make it easy for the application to use.
201 #include "r_calendar.h"
202 #include "r_contact.h"
203 #include "r_memo.h"
204 #include "r_message.h"
205 #include "r_servicebook.h"
206 #include "r_task.h"
207 #include "r_pin_message.h"
208 #include "r_saved_message.h"
210 #endif