record.h

Go to the documentation of this file.
00001 ///
00002 /// \file       record.h
00003 ///             Blackberry database record classes.  Help translate data
00004 ///             from data packets to useful structurs, and back.
00005 ///             This header provides the common types and classes
00006 ///             used by the general record parser classes in the
00007 ///             r_*.h files.  Only application-safe API stuff goes in
00008 ///             here.  Internal library types go in record-internal.h
00009 ///
00010 
00011 /*
00012     Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
00013 
00014     This program is free software; you can redistribute it and/or modify
00015     it under the terms of the GNU General Public License as published by
00016     the Free Software Foundation; either version 2 of the License, or
00017     (at your option) any later version.
00018 
00019     This program is distributed in the hope that it will be useful,
00020     but WITHOUT ANY WARRANTY; without even the implied warranty of
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 
00023     See the GNU General Public License in the COPYING file at the
00024     root directory of this project for more details.
00025 */
00026 
00027 #ifndef __BARRY_RECORD_H__
00028 #define __BARRY_RECORD_H__
00029 
00030 #include "dll.h"
00031 #include <iosfwd>
00032 #include <string>
00033 #include <vector>
00034 #include <map>
00035 #include <stdint.h>
00036 
00037 // forward declarations
00038 namespace Barry { class Data; }
00039 
00040 namespace Barry {
00041 
00042 //
00043 // NOTE:  All classes here must be container-safe!  Perhaps add sorting
00044 //        operators in the future.
00045 //
00046 
00047 
00048 // stream-based wrapper to avoid printing strings that contain
00049 // the \r carriage return characters
00050 class BXEXPORT Cr2LfWrapper
00051 {
00052         friend std::ostream& operator<< (std::ostream &os, const Cr2LfWrapper &str);
00053         const std::string &m_str;
00054 public:
00055         explicit Cr2LfWrapper(const std::string &str)
00056                 : m_str(str)
00057         {
00058         }
00059 };
00060 BXEXPORT std::ostream& operator<< (std::ostream &os, const Cr2LfWrapper &str);
00061 
00062 struct BXEXPORT CommandTableCommand
00063 {
00064         unsigned int Code;
00065         std::string Name;
00066 };
00067 
00068 class BXEXPORT CommandTable
00069 {
00070 public:
00071         typedef CommandTableCommand Command;
00072         typedef std::vector<Command> CommandArrayType;
00073 
00074         CommandArrayType Commands;
00075 
00076 private:
00077         BXLOCAL const unsigned char* ParseField(const unsigned char *begin,
00078                 const unsigned char *end);
00079 public:
00080         CommandTable();
00081         ~CommandTable();
00082 
00083         void Parse(const Data &data, size_t offset);
00084         void Clear();
00085 
00086         // returns 0 if unable to find command name, which is safe, since
00087         // 0 is a special command that shouldn't be in the table anyway
00088         unsigned int GetCommand(const std::string &name) const;
00089 
00090         void Dump(std::ostream &os) const;
00091 };
00092 
00093 BXEXPORT inline std::ostream& operator<< (std::ostream &os, const CommandTable &command) {
00094         command.Dump(os);
00095         return os;
00096 }
00097 
00098 
00099 
00100 struct BXEXPORT RecordStateTableState
00101 {
00102         unsigned int Index;
00103         uint32_t RecordId;
00104         bool Dirty;
00105         unsigned int RecType;
00106         std::string Unknown2;
00107 };
00108 
00109 class BXEXPORT RecordStateTable
00110 {
00111 public:
00112         typedef RecordStateTableState State;
00113         typedef unsigned int IndexType;
00114         typedef std::map<IndexType, State> StateMapType;
00115 
00116         StateMapType StateMap;
00117 
00118 private:
00119         mutable IndexType m_LastNewRecordId;
00120 
00121 private:
00122         BXLOCAL const unsigned char* ParseField(const unsigned char *begin,
00123                 const unsigned char *end);
00124 
00125 public:
00126         RecordStateTable();
00127         ~RecordStateTable();
00128 
00129         void Parse(const Data &data);
00130         void Clear();
00131 
00132         bool GetIndex(uint32_t RecordId, IndexType *pFoundIndex = 0) const;
00133         uint32_t MakeNewRecordId() const;
00134 
00135         void Dump(std::ostream &os) const;
00136 };
00137 
00138 BXEXPORT inline std::ostream& operator<< (std::ostream &os, const RecordStateTable &rst) {
00139         rst.Dump(os);
00140         return os;
00141 }
00142 
00143 
00144 
00145 struct BXEXPORT DatabaseItem
00146 {
00147         unsigned int Number;
00148         unsigned int RecordCount;
00149         std::string Name;
00150 };
00151 
00152 class BXEXPORT DatabaseDatabase
00153 {
00154 public:
00155         typedef DatabaseItem Database;
00156         typedef std::vector<Database> DatabaseArrayType;
00157 
00158         DatabaseArrayType Databases;
00159 
00160 private:
00161         template <class RecordType, class FieldType>
00162         void ParseRec(const RecordType &rec, const unsigned char *end);
00163 
00164         template <class FieldType>
00165         const unsigned char* ParseField(const unsigned char *begin,
00166                 const unsigned char *end);
00167 
00168 public:
00169         DatabaseDatabase();
00170         ~DatabaseDatabase();
00171 
00172         void Parse(const Data &data);
00173         void Clear();
00174 
00175         // returns true on success, and fills target
00176         bool GetDBNumber(const std::string &name, unsigned int &number) const;
00177         bool GetDBName(unsigned int number, std::string &name) const;
00178 
00179         void Dump(std::ostream &os) const;
00180 };
00181 
00182 BXEXPORT inline std::ostream& operator<<(std::ostream &os, const DatabaseDatabase &dbdb) {
00183         dbdb.Dump(os);
00184         return os;
00185 }
00186 
00187 struct UnknownData
00188 {
00189         std::string raw_data;
00190 
00191         const std::string::value_type* data() const { return raw_data.data(); }
00192         std::string::size_type size() const { return raw_data.size(); }
00193         void assign(const std::string::value_type *s, std::string::size_type n)
00194                 { raw_data.assign(s, n); }
00195 };
00196 
00197 struct BXEXPORT UnknownField
00198 {
00199         uint8_t type;
00200         UnknownData data;
00201 };
00202 typedef std::vector<UnknownField> UnknownsType;
00203 BXEXPORT std::ostream& operator<< (std::ostream &os, const UnknownsType &unknowns);
00204 
00205 struct BXEXPORT EmailAddress
00206 {
00207         std::string Name;
00208         std::string Email;
00209 
00210         void clear()
00211         {
00212                 Name.clear();
00213                 Email.clear();
00214         }
00215 
00216         size_t size() const
00217         {
00218                 return Name.size() + Email.size();
00219         }
00220 };
00221 BXEXPORT std::ostream& operator<<(std::ostream &os, const EmailAddress &msga);
00222 
00223 typedef std::vector<EmailAddress>       EmailAddressList;
00224 BXEXPORT std::ostream& operator<<(std::ostream &os, const EmailAddressList &elist);
00225 
00226 struct BXEXPORT PostalAddress
00227 {
00228         std::string
00229                 Address1,
00230                 Address2,
00231                 Address3,
00232                 City,
00233                 Province,
00234                 PostalCode,
00235                 Country;
00236 
00237         std::string GetLabel() const;
00238         void Clear();
00239 
00240         bool HasData() const { return Address1.size() || Address2.size() ||
00241                 Address3.size() || City.size() || Province.size() ||
00242                 PostalCode.size() || Country.size(); }
00243 };
00244 BXEXPORT std::ostream& operator<<(std::ostream &os, const PostalAddress &msga);
00245 
00246 struct BXEXPORT Date
00247 {
00248         int Month;                      // 0 to 11
00249         int Day;                        // 1 to 31
00250         int Year;                       // exact number, eg. 2008
00251 
00252         Date() : Month(0), Day(0), Year(0) {}
00253         explicit Date(const struct tm *timep);
00254 
00255         bool HasData() const { return Month || Day || Year; }
00256         void Clear();
00257 
00258         void ToTm(struct tm *timep) const;
00259         std::string ToYYYYMMDD() const;
00260         std::string ToBBString() const; // converts to Blackberry string
00261                                         // format of DD/MM/YYYY
00262 
00263         bool FromTm(const struct tm *timep);
00264         bool FromBBString(const std::string &str);
00265         bool FromYYYYMMDD(const std::string &str);
00266 };
00267 BXEXPORT std::ostream& operator<<(std::ostream &os, const Date &date);
00268 
00269 class BXEXPORT CategoryList : public std::vector<std::string>
00270 {
00271 public:
00272         /// Parses the given comma delimited category string into
00273         /// this CategoryList object, appending each token to the vector.
00274         /// Will clear vector beforehand.
00275         void CategoryStr2List(const std::string &str);
00276 
00277         /// Turns the current vectory into a comma delimited category
00278         /// string suitable for use in Calendar, Task, and Memo
00279         /// protocol values.
00280         void CategoryList2Str(std::string &str) const;
00281 
00282         using std::vector<std::string>::operator=;
00283 };
00284 
00285 
00286 /// \addtogroup RecordParserClasses
00287 ///             Parser and data storage classes.  These classes take a
00288 ///             Database Database record and convert them into C++ objects.
00289 ///             Each of these classes are safe to be used in standard
00290 ///             containers, and are meant to be used in conjunction with the
00291 ///             RecordParser<> template when calling Controller::LoadDatabase().
00292 /// @{
00293 /// @}
00294 
00295 } // namespace Barry
00296 
00297 #ifndef __BARRY_LIBRARY_BUILD__
00298 // Include all parser classes, to make it easy for the application to use.
00299 #include "r_calendar.h"
00300 #include "r_calllog.h"
00301 #include "r_bookmark.h"
00302 #include "r_contact.h"
00303 #include "r_cstore.h"
00304 #include "r_memo.h"
00305 #include "r_message.h"
00306 #include "r_servicebook.h"
00307 #include "r_task.h"
00308 #include "r_pin_message.h"
00309 #include "r_saved_message.h"
00310 #include "r_sms.h"
00311 #include "r_folder.h"
00312 #include "r_timezone.h"
00313 #endif
00314 
00315 #endif
00316