record.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       record.cc
00003 ///             Misc. Blackberry database record helper classes and functions.
00004 ///             Helps translate data from data packets to useful structures,
00005 ///             and back.
00006 ///
00007 
00008 /*
00009     Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
00010 
00011     This program is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     This program is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00019 
00020     See the GNU General Public License in the COPYING file at the
00021     root directory of this project for more details.
00022 */
00023 
00024 #include "record.h"
00025 #include "record-internal.h"
00026 #include "protocol.h"
00027 #include "protostructs.h"
00028 #include "data.h"
00029 #include "time.h"
00030 #include "error.h"
00031 #include "endian.h"
00032 #include <sstream>
00033 #include <iomanip>
00034 #include <time.h>
00035 #include <string.h>
00036 #include <stdio.h>                      // for sscanf()
00037 #include <stdexcept>
00038 
00039 #define __DEBUG_MODE__
00040 #include "debug.h"
00041 
00042 using namespace std;
00043 using namespace Barry::Protocol;
00044 
00045 namespace Barry {
00046 
00047 //////////////////////////////////////////////////////////////////////////////
00048 // Field builder helper functions
00049 
00050 void BuildField1900(Data &data, size_t &size, uint8_t type, time_t t)
00051 {
00052         size_t timesize = COMMON_FIELD_MIN1900_SIZE;
00053         size_t fieldsize = COMMON_FIELD_HEADER_SIZE + timesize;
00054         unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
00055         CommonField *field = (CommonField *) pd;
00056 
00057         field->size = htobs(timesize);
00058         field->type = type;
00059         field->u.min1900 = time2min(t);
00060 
00061         size += fieldsize;
00062 }
00063 
00064 void BuildField(Data &data, size_t &size, uint8_t type, char c)
00065 {
00066         BuildField(data, size, type, (uint8_t)c);
00067 }
00068 
00069 void BuildField(Data &data, size_t &size, uint8_t type, uint8_t c)
00070 {
00071         size_t strsize = 1;
00072         size_t fieldsize = COMMON_FIELD_HEADER_SIZE + strsize;
00073         unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
00074         CommonField *field = (CommonField *) pd;
00075 
00076         field->size = htobs(strsize);
00077         field->type = type;
00078         memcpy(field->u.raw, &c, strsize);
00079 
00080         size += fieldsize;
00081 }
00082 
00083 void BuildField(Data &data, size_t &size, uint8_t type, uint16_t value)
00084 {
00085         size_t strsize = 2;
00086         size_t fieldsize = COMMON_FIELD_HEADER_SIZE + strsize;
00087         unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
00088         CommonField *field = (CommonField *) pd;
00089 
00090         field->size = htobs(strsize);
00091         field->type = type;
00092 
00093         uint16_t store = htobs(value);
00094         memcpy(field->u.raw, &store, strsize);
00095 
00096         size += fieldsize;
00097 }
00098 
00099 void BuildField(Data &data, size_t &size, uint8_t type, uint32_t value)
00100 {
00101         size_t strsize = 4;
00102         size_t fieldsize = COMMON_FIELD_HEADER_SIZE + strsize;
00103         unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
00104         CommonField *field = (CommonField *) pd;
00105 
00106         field->size = htobl(strsize);
00107         field->type = type;
00108 
00109         uint32_t store = htobs(value);
00110         memcpy(field->u.raw, &store, strsize);
00111 
00112         size += fieldsize;
00113 }
00114 
00115 void BuildField(Data &data, size_t &size, uint8_t type, const std::string &str)
00116 {
00117         // include null terminator
00118         BuildField(data, size, type, str.c_str(), str.size() + 1);
00119 }
00120 
00121 void BuildField(Data &data, size_t &size, uint8_t type,
00122                 const void *buf, size_t bufsize)
00123 {
00124         // include null terminator
00125         size_t fieldsize = COMMON_FIELD_HEADER_SIZE + bufsize;
00126         unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
00127         CommonField *field = (CommonField *) pd;
00128 
00129         field->size = htobs(bufsize);
00130         field->type = type;
00131         memcpy(field->u.raw, buf, bufsize);
00132 
00133         size += fieldsize;
00134 }
00135 
00136 void BuildField(Data &data, size_t &size, const Barry::UnknownField &field)
00137 {
00138         BuildField(data, size, field.type,
00139                 field.data.raw_data.data(), field.data.raw_data.size());
00140 }
00141 
00142 void BuildField(Data &data, size_t &size, uint8_t type, const Barry::Protocol::GroupLink &link)
00143 {
00144         size_t linksize = sizeof(Barry::Protocol::GroupLink);
00145         size_t fieldsize = COMMON_FIELD_HEADER_SIZE + linksize;
00146         unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
00147         CommonField *field = (CommonField *) pd;
00148 
00149         field->size = htobs(linksize);
00150         field->type = type;
00151         field->u.link = link;
00152 
00153         size += fieldsize;
00154 }
00155 
00156 std::string ParseFieldString(const Barry::Protocol::CommonField *field)
00157 {
00158         // make no assumptions here, and pass the full size in as
00159         // the maxlen, even though 99% of the time, it will be a null...
00160         // this function can be used by non-null terminated strings as well
00161         return ParseFieldString(field->u.raw, btohs(field->size));
00162 }
00163 
00164 std::string ParseFieldString(const void *data, uint16_t maxlen)
00165 {
00166         const char *str = (const char *)data;
00167 
00168         // find last non-null character, since some fields
00169         // can have multiple null terminators
00170         while( maxlen && str[maxlen-1] == 0 )
00171                 maxlen--;
00172 
00173         return std::string(str, maxlen);
00174 }
00175 
00176 
00177 ///////////////////////////////////////////////////////////////////////////////
00178 // UnknownField
00179 
00180 std::ostream& operator<< (std::ostream &os, const std::vector<UnknownField> &unknowns)
00181 {
00182         std::vector<UnknownField>::const_iterator
00183                 ub = unknowns.begin(), ue = unknowns.end();
00184         if( ub != ue )
00185                 os << "    Unknowns:\n";
00186         for( ; ub != ue; ub++ ) {
00187                 os << "        Type: 0x" << setbase(16)
00188                    << (unsigned int) ub->type
00189                    << " Data:\n" << Data(ub->data.data(), ub->data.size());
00190         }
00191         return os;
00192 }
00193 
00194 
00195 ///////////////////////////////////////////////////////////////////////////////
00196 // EmailAddress class
00197 
00198 std::ostream& operator<<(std::ostream &os, const EmailAddress &msga) {
00199         os << msga.Name << " <" << msga.Email << ">";
00200         return os;
00201 }
00202 
00203 std::ostream& operator<<(std::ostream &os, const EmailAddressList &elist) {
00204         for( EmailAddressList::const_iterator i = elist.begin(); i != elist.end(); ++i ) {
00205                 if( i != elist.begin() )
00206                         os << ", ";
00207                 os << *i;
00208         }
00209         return os;
00210 }
00211 
00212 
00213 ///////////////////////////////////////////////////////////////////////////////
00214 // PostalAddress class
00215 
00216 //
00217 // GetLabel
00218 //
00219 /// Format a mailing address into a single string, handling missing fields.
00220 ///
00221 std::string PostalAddress::GetLabel() const
00222 {
00223         std::string address = Address1;
00224         if( Address2.size() ) {
00225                 if( address.size() )
00226                         address += "\n";
00227                 address += Address2;
00228         }
00229         if( Address3.size() ) {
00230                 if( address.size() )
00231                         address += "\n";
00232                 address += Address3;
00233         }
00234         if( address.size() )
00235                 address += "\n";
00236         if( City.size() )
00237                 address += City + " ";
00238         if( Province.size() )
00239                 address += Province + " ";
00240         if( Country.size() )
00241                 address += Country;
00242         if( address.size() )
00243                 address += "\n";
00244         if( PostalCode.size() )
00245                 address += PostalCode;
00246 
00247         return address;
00248 }
00249 
00250 void PostalAddress::Clear()
00251 {
00252         Address1.clear();
00253         Address2.clear();
00254         Address3.clear();
00255         City.clear();
00256         Province.clear();
00257         PostalCode.clear();
00258         Country.clear();
00259 }
00260 
00261 std::ostream& operator<<(std::ostream &os, const PostalAddress &post) {
00262         os << post.GetLabel();
00263         return os;
00264 }
00265 
00266 
00267 
00268 ///////////////////////////////////////////////////////////////////////////////
00269 // Date class
00270 
00271 Date::Date(const struct tm *timep)
00272 {
00273         FromTm(timep);
00274 }
00275 
00276 void Date::Clear()
00277 {
00278         Month = Day = Year = 0;
00279 }
00280 
00281 void Date::ToTm(struct tm *timep) const
00282 {
00283         memset(timep, 0, sizeof(tm));
00284         timep->tm_year = Year - 1900;
00285         timep->tm_mon = Month;
00286         timep->tm_mday = Day;
00287 }
00288 
00289 std::string Date::ToYYYYMMDD() const
00290 {
00291         std::ostringstream oss;
00292         // setfill and setw not sticky.
00293         oss     << setw(4) << setfill('0') << Year
00294                 << setw(2) << setfill('0') << Month + 1
00295                 << setw(2) << setfill('0') << Day;
00296         return oss.str();
00297 }
00298 
00299 //
00300 // ToBBString
00301 //
00302 /// The Blackberry stores Birthday and Anniversary date fields
00303 /// with the format: DD/MM/YYYY
00304 ///
00305 std::string Date::ToBBString() const
00306 {
00307         std::ostringstream oss;
00308         // setw() ain't 'sticky'!
00309         oss     << setw(2) << setfill('0') << Day << '/'
00310                 << setw(2) << setfill('0') << Month + 1 << '/'
00311                 << setw(2) << setfill('0') << Year;
00312         return oss.str();
00313 }
00314 
00315 bool Date::FromTm(const struct tm *timep)
00316 {
00317         Year = timep->tm_year + 1900;
00318         Month = timep->tm_mon;
00319         Day = timep->tm_mday;
00320         return true;
00321 }
00322 
00323 bool Date::FromBBString(const std::string &str)
00324 {
00325         int m, d, y;
00326         if( 3 == sscanf(str.c_str(), "%d/%d/%d", &d, &m, &y) ) {
00327                 Year = y;
00328                 Month = m - 1;
00329                 Day = d;
00330                 return true;
00331         }
00332         return false;
00333 }
00334 
00335 bool Date::FromYYYYMMDD(const std::string &str)
00336 {
00337         int m, d, y;
00338         if( 3 == sscanf(str.c_str(), "%4d%2d%2d", &y, &m, &d) ) {
00339                 Year = y;
00340                 Month = m - 1;
00341                 Day = d;
00342                 return true;
00343         }
00344         return false;
00345 }
00346 
00347 std::ostream& operator<<(std::ostream &os, const Date &date)
00348 {
00349         os      << setw(4) << date.Year << '/'
00350                 << setw(2) << date.Month << '/'
00351                 << setw(2) << date.Day;
00352         return os;
00353 }
00354 
00355 
00356 ///////////////////////////////////////////////////////////////////////////////
00357 // CategoryList class
00358 
00359 /// Parses the given comma delimited category string into
00360 /// this CategoryList object, appending each token to the vector.
00361 /// Will clear vector beforehand.
00362 void CategoryList::CategoryStr2List(const std::string &str)
00363 {
00364         // start fresh
00365         clear();
00366 
00367         if( !str.size() )
00368                 return;
00369 
00370         // parse the comma-delimited string to a list, stripping away
00371         // any white space around each category name
00372         string::size_type start = 0, end = 0, delim = str.find(',', start);
00373         while( start != string::npos ) {
00374                 if( delim == string::npos )
00375                         end = str.size() - 1;
00376                 else
00377                         end = delim - 1;
00378 
00379                 // strip surrounding whitespace
00380                 while( str[start] == ' ' )
00381                         start++;
00382                 while( end && str[end] == ' ' )
00383                         end--;
00384 
00385                 if( start <= end ) {
00386                         string token = str.substr(start, end-start+1);
00387                         push_back(token);
00388                 }
00389 
00390                 // next
00391                 start = delim;
00392                 if( start != string::npos )
00393                         start++;
00394                 delim = str.find(',', start);
00395         }
00396 }
00397 
00398 /// Turns the current vectory into a comma delimited category
00399 /// string suitable for use in Calendar, Task, and Memo protocol values.
00400 void CategoryList::CategoryList2Str(std::string &str) const
00401 {
00402         str.clear();
00403 
00404         Barry::CategoryList::const_iterator i = begin();
00405         for( ; i != end(); ++i ) {
00406                 if( str.size() )
00407                         str += ", ";
00408                 str += *i;
00409         }
00410 }
00411 
00412 
00413 } // namespace Barry
00414 
00415 
00416 #ifdef __TEST_MODE__
00417 
00418 #include <iostream>
00419 
00420 int main(int argc, char *argv[])
00421 {
00422         if( argc < 2 ) {
00423                 cerr << "Usage: test <datafile>" << endl;
00424                 return 1;
00425         }
00426 
00427         std::vector<Data> array;
00428         if( !LoadDataArray(argv[1], array) ) {
00429                 cerr << "Unable to load file: " << argv[1] << endl;
00430                 return 1;
00431         }
00432 
00433         cout << "Loaded " << array.size() << " items" << endl;
00434 
00435         for( std::vector<Data>::iterator b = array.begin(), e = array.end();
00436                 b != e; b++ )
00437         {
00438                 Data &d = *b;
00439 //              cout << d << endl;
00440                 if( d.GetSize() > 13 && d.GetData()[6] == 0x4f ) {
00441                         Barry::Contact contact;
00442                         size_t size = 13;
00443                         contact.ParseFields(d, size);
00444                         cout << contact << endl;
00445                         contact.DumpLdif(cout, "ou=People,dc=example,dc=com");
00446                 }
00447                 else if( d.GetSize() > 13 && d.GetData()[6] == 0x44 ) {
00448                         Barry::Calendar cal;
00449                         size_t size = 13;
00450                         cal.ParseFields(d, size);
00451                         cout << cal << endl;
00452                 }
00453         }
00454 }
00455 
00456 #endif
00457 

Generated on 29 Mar 2010 for Barry by  doxygen 1.6.1