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-2008, 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 
00049 struct BXEXPORT CommandTableCommand
00050 {
00051         unsigned int Code;
00052         std::string Name;
00053 };
00054 
00055 class BXEXPORT CommandTable
00056 {
00057 public:
00058         typedef CommandTableCommand Command;
00059         typedef std::vector<Command> CommandArrayType;
00060 
00061         CommandArrayType Commands;
00062 
00063 private:
00064         BXLOCAL const unsigned char* ParseField(const unsigned char *begin,
00065                 const unsigned char *end);
00066 public:
00067         CommandTable();
00068         ~CommandTable();
00069 
00070         void Parse(const Data &data, size_t offset);
00071         void Clear();
00072 
00073         // returns 0 if unable to find command name, which is safe, since
00074         // 0 is a special command that shouldn't be in the table anyway
00075         unsigned int GetCommand(const std::string &name) const;
00076 
00077         void Dump(std::ostream &os) const;
00078 };
00079 
00080 BXEXPORT inline std::ostream& operator<< (std::ostream &os, const CommandTable &command) {
00081         command.Dump(os);
00082         return os;
00083 }
00084 
00085 
00086 
00087 struct BXEXPORT RecordStateTableState
00088 {
00089         unsigned int Index;
00090         uint32_t RecordId;
00091         bool Dirty;
00092         unsigned int RecType;
00093         std::string Unknown2;
00094 };
00095 
00096 class BXEXPORT RecordStateTable
00097 {
00098 public:
00099         typedef RecordStateTableState State;
00100         typedef unsigned int IndexType;
00101         typedef std::map<IndexType, State> StateMapType;
00102 
00103         StateMapType StateMap;
00104 
00105 private:
00106         mutable IndexType m_LastNewRecordId;
00107 
00108 private:
00109         BXLOCAL const unsigned char* ParseField(const unsigned char *begin,
00110                 const unsigned char *end);
00111 
00112 public:
00113         RecordStateTable();
00114         ~RecordStateTable();
00115 
00116         void Parse(const Data &data);
00117         void Clear();
00118 
00119         bool GetIndex(uint32_t RecordId, IndexType *pFoundIndex = 0) const;
00120         uint32_t MakeNewRecordId() const;
00121 
00122         void Dump(std::ostream &os) const;
00123 };
00124 
00125 BXEXPORT inline std::ostream& operator<< (std::ostream &os, const RecordStateTable &rst) {
00126         rst.Dump(os);
00127         return os;
00128 }
00129 
00130 
00131 
00132 struct BXEXPORT DatabaseItem
00133 {
00134         unsigned int Number;
00135         unsigned int RecordCount;
00136         std::string Name;
00137 };
00138 
00139 class BXEXPORT DatabaseDatabase
00140 {
00141 public:
00142         typedef DatabaseItem Database;
00143         typedef std::vector<Database> DatabaseArrayType;
00144 
00145         DatabaseArrayType Databases;
00146 
00147 private:
00148         template <class RecordType, class FieldType>
00149         void ParseRec(const RecordType &rec, const unsigned char *end);
00150 
00151         template <class FieldType>
00152         const unsigned char* ParseField(const unsigned char *begin,
00153                 const unsigned char *end);
00154 
00155 public:
00156         DatabaseDatabase();
00157         ~DatabaseDatabase();
00158 
00159         void Parse(const Data &data);
00160         void Clear();
00161 
00162         // returns true on success, and fills target
00163         bool GetDBNumber(const std::string &name, unsigned int &number) const;
00164         bool GetDBName(unsigned int number, std::string &name) const;
00165 
00166         void Dump(std::ostream &os) const;
00167 };
00168 
00169 BXEXPORT inline std::ostream& operator<<(std::ostream &os, const DatabaseDatabase &dbdb) {
00170         dbdb.Dump(os);
00171         return os;
00172 }
00173 
00174 struct UnknownData
00175 {
00176         std::string raw_data;
00177 
00178         const std::string::value_type* data() const { return raw_data.data(); }
00179         std::string::size_type size() const { return raw_data.size(); }
00180         void assign(const std::string::value_type *s, std::string::size_type n)
00181                 { raw_data.assign(s, n); }
00182 };
00183 
00184 struct BXEXPORT UnknownField
00185 {
00186         uint8_t type;
00187         UnknownData data;
00188 };
00189 BXEXPORT std::ostream& operator<< (std::ostream &os, const std::vector<UnknownField> &unknowns);
00190 
00191 struct BXEXPORT EmailAddress
00192 {
00193         std::string Name;
00194         std::string Email;
00195 
00196         void clear()
00197         {
00198                 Name.clear();
00199                 Email.clear();
00200         }
00201 };
00202 BXEXPORT std::ostream& operator<<(std::ostream &os, const EmailAddress &msga);
00203 
00204 struct BXEXPORT PostalAddress
00205 {
00206         std::string
00207                 Address1,
00208                 Address2,
00209                 Address3,
00210                 City,
00211                 Province,
00212                 PostalCode,
00213                 Country;
00214 
00215         std::string GetLabel() const;
00216         void Clear();
00217 
00218         bool HasData() const { return Address1.size() || Address2.size() ||
00219                 Address3.size() || City.size() || Province.size() ||
00220                 PostalCode.size() || Country.size(); }
00221 };
00222 BXEXPORT std::ostream& operator<<(std::ostream &os, const PostalAddress &msga);
00223 
00224 struct BXEXPORT Date
00225 {
00226         int Month;                      // 0 to 11
00227         int Day;                        // 1 to 31
00228         int Year;                       // exact number, eg. 2008
00229 
00230         Date() : Month(0), Day(0), Year(0) {}
00231         explicit Date(const struct tm *timep);
00232 
00233         bool HasData() const { return Month || Day || Year; }
00234         void Clear();
00235 
00236         void ToTm(struct tm *timep) const;
00237         std::string ToYYYYMMDD() const;
00238         std::string ToBBString() const; // converts to Blackberry string
00239                                         // format of DD/MM/YYYY
00240 
00241         bool FromTm(const struct tm *timep);
00242         bool FromBBString(const std::string &str);
00243         bool FromYYYYMMDD(const std::string &str);
00244 };
00245 BXEXPORT std::ostream& operator<<(std::ostream &os, const Date &date);
00246 
00247 
00248 /// \addtogroup RecordParserClasses
00249 ///             Parser and data storage classes.  These classes take a
00250 ///             Database Database record and convert them into C++ objects.
00251 ///             Each of these classes are safe to be used in standard
00252 ///             containers, and are meant to be used in conjunction with the
00253 ///             RecordParser<> template when calling Controller::LoadDatabase().
00254 /// @{
00255 /// @}
00256 
00257 } // namespace Barry
00258 
00259 // Include all parser classes, to make it easy for the application to use.
00260 #include "r_calendar.h"
00261 #include "r_contact.h"
00262 #include "r_memo.h"
00263 #include "r_message.h"
00264 #include "r_servicebook.h"
00265 #include "r_task.h"
00266 #include "r_pin_message.h"
00267 #include "r_saved_message.h"
00268 #include "r_folder.h"
00269 #include "r_timezone.h"
00270 
00271 #endif
00272 

Generated on Wed Sep 24 21:27:32 2008 for Barry by  doxygen 1.5.1