lib: created consistent method of printing strings with CR characters
[barry.git] / src / record.h
blob78c1e5f220c1bc0a280a144e926a3edbefda1c5f
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-2010, 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 "dll.h"
31 #include <iosfwd>
32 #include <string>
33 #include <vector>
34 #include <map>
35 #include <stdint.h>
37 // forward declarations
38 namespace Barry { class Data; }
40 namespace Barry {
43 // NOTE: All classes here must be container-safe! Perhaps add sorting
44 // operators in the future.
48 // stream-based wrapper to avoid printing strings that contain
49 // the \r carriage return characters
50 class BXEXPORT Cr2LfWrapper
52 friend std::ostream& operator<< (std::ostream &os, const Cr2LfWrapper &str);
53 const std::string &m_str;
54 public:
55 explicit Cr2LfWrapper(const std::string &str)
56 : m_str(str)
60 BXEXPORT std::ostream& operator<< (std::ostream &os, const Cr2LfWrapper &str);
62 struct BXEXPORT CommandTableCommand
64 unsigned int Code;
65 std::string Name;
68 class BXEXPORT CommandTable
70 public:
71 typedef CommandTableCommand Command;
72 typedef std::vector<Command> CommandArrayType;
74 CommandArrayType Commands;
76 private:
77 BXLOCAL const unsigned char* ParseField(const unsigned char *begin,
78 const unsigned char *end);
79 public:
80 CommandTable();
81 ~CommandTable();
83 void Parse(const Data &data, size_t offset);
84 void Clear();
86 // returns 0 if unable to find command name, which is safe, since
87 // 0 is a special command that shouldn't be in the table anyway
88 unsigned int GetCommand(const std::string &name) const;
90 void Dump(std::ostream &os) const;
93 BXEXPORT inline std::ostream& operator<< (std::ostream &os, const CommandTable &command) {
94 command.Dump(os);
95 return os;
100 struct BXEXPORT RecordStateTableState
102 unsigned int Index;
103 uint32_t RecordId;
104 bool Dirty;
105 unsigned int RecType;
106 std::string Unknown2;
109 class BXEXPORT RecordStateTable
111 public:
112 typedef RecordStateTableState State;
113 typedef unsigned int IndexType;
114 typedef std::map<IndexType, State> StateMapType;
116 StateMapType StateMap;
118 private:
119 mutable IndexType m_LastNewRecordId;
121 private:
122 BXLOCAL const unsigned char* ParseField(const unsigned char *begin,
123 const unsigned char *end);
125 public:
126 RecordStateTable();
127 ~RecordStateTable();
129 void Parse(const Data &data);
130 void Clear();
132 bool GetIndex(uint32_t RecordId, IndexType *pFoundIndex = 0) const;
133 uint32_t MakeNewRecordId() const;
135 void Dump(std::ostream &os) const;
138 BXEXPORT inline std::ostream& operator<< (std::ostream &os, const RecordStateTable &rst) {
139 rst.Dump(os);
140 return os;
145 struct BXEXPORT DatabaseItem
147 unsigned int Number;
148 unsigned int RecordCount;
149 std::string Name;
152 class BXEXPORT DatabaseDatabase
154 public:
155 typedef DatabaseItem Database;
156 typedef std::vector<Database> DatabaseArrayType;
158 DatabaseArrayType Databases;
160 private:
161 template <class RecordType, class FieldType>
162 void ParseRec(const RecordType &rec, const unsigned char *end);
164 template <class FieldType>
165 const unsigned char* ParseField(const unsigned char *begin,
166 const unsigned char *end);
168 public:
169 DatabaseDatabase();
170 ~DatabaseDatabase();
172 void Parse(const Data &data);
173 void Clear();
175 // returns true on success, and fills target
176 bool GetDBNumber(const std::string &name, unsigned int &number) const;
177 bool GetDBName(unsigned int number, std::string &name) const;
179 void Dump(std::ostream &os) const;
182 BXEXPORT inline std::ostream& operator<<(std::ostream &os, const DatabaseDatabase &dbdb) {
183 dbdb.Dump(os);
184 return os;
187 struct UnknownData
189 std::string raw_data;
191 const std::string::value_type* data() const { return raw_data.data(); }
192 std::string::size_type size() const { return raw_data.size(); }
193 void assign(const std::string::value_type *s, std::string::size_type n)
194 { raw_data.assign(s, n); }
197 struct BXEXPORT UnknownField
199 uint8_t type;
200 UnknownData data;
202 typedef std::vector<UnknownField> UnknownsType;
203 BXEXPORT std::ostream& operator<< (std::ostream &os, const UnknownsType &unknowns);
205 struct BXEXPORT EmailAddress
207 std::string Name;
208 std::string Email;
210 void clear()
212 Name.clear();
213 Email.clear();
216 size_t size() const
218 return Name.size() + Email.size();
221 BXEXPORT std::ostream& operator<<(std::ostream &os, const EmailAddress &msga);
223 typedef std::vector<EmailAddress> EmailAddressList;
224 BXEXPORT std::ostream& operator<<(std::ostream &os, const EmailAddressList &elist);
226 struct BXEXPORT PostalAddress
228 std::string
229 Address1,
230 Address2,
231 Address3,
232 City,
233 Province,
234 PostalCode,
235 Country;
237 std::string GetLabel() const;
238 void Clear();
240 bool HasData() const { return Address1.size() || Address2.size() ||
241 Address3.size() || City.size() || Province.size() ||
242 PostalCode.size() || Country.size(); }
244 BXEXPORT std::ostream& operator<<(std::ostream &os, const PostalAddress &msga);
246 struct BXEXPORT Date
248 int Month; // 0 to 11
249 int Day; // 1 to 31
250 int Year; // exact number, eg. 2008
252 Date() : Month(0), Day(0), Year(0) {}
253 explicit Date(const struct tm *timep);
255 bool HasData() const { return Month || Day || Year; }
256 void Clear();
258 void ToTm(struct tm *timep) const;
259 std::string ToYYYYMMDD() const;
260 std::string ToBBString() const; // converts to Blackberry string
261 // format of DD/MM/YYYY
263 bool FromTm(const struct tm *timep);
264 bool FromBBString(const std::string &str);
265 bool FromYYYYMMDD(const std::string &str);
267 BXEXPORT std::ostream& operator<<(std::ostream &os, const Date &date);
269 class BXEXPORT CategoryList : public std::vector<std::string>
271 public:
272 /// Parses the given comma delimited category string into
273 /// this CategoryList object, appending each token to the vector.
274 /// Will clear vector beforehand.
275 void CategoryStr2List(const std::string &str);
277 /// Turns the current vectory into a comma delimited category
278 /// string suitable for use in Calendar, Task, and Memo
279 /// protocol values.
280 void CategoryList2Str(std::string &str) const;
282 using std::vector<std::string>::operator=;
286 /// \addtogroup RecordParserClasses
287 /// Parser and data storage classes. These classes take a
288 /// Database Database record and convert them into C++ objects.
289 /// Each of these classes are safe to be used in standard
290 /// containers, and are meant to be used in conjunction with the
291 /// RecordParser<> template when calling Controller::LoadDatabase().
292 /// @{
293 /// @}
295 } // namespace Barry
297 #ifndef __BARRY_LIBRARY_BUILD__
298 // Include all parser classes, to make it easy for the application to use.
299 #include "r_calendar.h"
300 #include "r_calllog.h"
301 #include "r_bookmark.h"
302 #include "r_contact.h"
303 #include "r_cstore.h"
304 #include "r_memo.h"
305 #include "r_message.h"
306 #include "r_servicebook.h"
307 #include "r_task.h"
308 #include "r_pin_message.h"
309 #include "r_saved_message.h"
310 #include "r_sms.h"
311 #include "r_folder.h"
312 #include "r_timezone.h"
313 #endif
315 #endif