3 /// Misc. Blackberry database record helper classes and functions.
4 /// Helps translate data from data packets to useful structures,
9 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License in the COPYING file at the
21 root directory of this project for more details.
25 #include "record-internal.h"
26 #include "protostructs.h"
32 #include "ios_state.h"
37 #include <stdio.h> // for sscanf()
40 #define __DEBUG_MODE__
44 using namespace Barry::Protocol
;
48 BXEXPORT
std::ostream
& operator<< (std::ostream
&os
, const Cr2LfWrapper
&str
)
50 for( std::string::const_iterator i
= str
.m_str
.begin();
51 i
!= str
.m_str
.end() && *i
;
62 //////////////////////////////////////////////////////////////////////////////
63 // Field builder helper functions
65 void BuildField1900(Data
&data
, size_t &size
, uint8_t type
, time_t t
)
67 size_t timesize
= COMMON_FIELD_MIN1900_SIZE
;
68 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ timesize
;
69 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
70 CommonField
*field
= (CommonField
*) pd
;
72 field
->size
= htobs(timesize
);
74 field
->u
.min1900
= time2min(t
);
79 void BuildField(Data
&data
, size_t &size
, uint8_t type
, char c
)
81 BuildField(data
, size
, type
, (uint8_t)c
);
84 void BuildField(Data
&data
, size_t &size
, uint8_t type
, uint8_t c
)
87 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ strsize
;
88 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
89 CommonField
*field
= (CommonField
*) pd
;
91 field
->size
= htobs(strsize
);
93 memcpy(field
->u
.raw
, &c
, strsize
);
98 void BuildField(Data
&data
, size_t &size
, uint8_t type
, uint16_t value
)
101 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ strsize
;
102 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
103 CommonField
*field
= (CommonField
*) pd
;
105 field
->size
= htobs(strsize
);
108 uint16_t store
= htobs(value
);
109 memcpy(field
->u
.raw
, &store
, strsize
);
114 void BuildField(Data
&data
, size_t &size
, uint8_t type
, uint32_t value
)
117 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ strsize
;
118 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
119 CommonField
*field
= (CommonField
*) pd
;
121 field
->size
= htobl(strsize
);
124 uint32_t store
= htobl(value
);
125 memcpy(field
->u
.raw
, &store
, strsize
);
130 void BuildField(Data
&data
, size_t &size
, uint8_t type
, uint64_t value
)
133 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ strsize
;
134 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
135 CommonField
*field
= (CommonField
*) pd
;
137 field
->size
= htobl(strsize
);
140 uint64_t store
= htobll(value
);
141 memcpy(field
->u
.raw
, &store
, strsize
);
146 void BuildField(Data
&data
, size_t &size
, uint8_t type
, const std::string
&str
)
148 // include null terminator
149 BuildField(data
, size
, type
, str
.c_str(), str
.size() + 1);
152 void BuildField(Data
&data
, size_t &size
, uint8_t type
,
153 const void *buf
, size_t bufsize
)
155 // include null terminator
156 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ bufsize
;
157 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
158 CommonField
*field
= (CommonField
*) pd
;
160 field
->size
= htobs(bufsize
);
162 memcpy(field
->u
.raw
, buf
, bufsize
);
167 void BuildField(Data
&data
, size_t &size
, const Barry::UnknownField
&field
)
169 BuildField(data
, size
, field
.type
,
170 field
.data
.raw_data
.data(), field
.data
.raw_data
.size());
173 void BuildField(Data
&data
, size_t &size
, uint8_t type
, const Barry::Protocol::GroupLink
&link
)
175 size_t linksize
= sizeof(Barry::Protocol::GroupLink
);
176 size_t fieldsize
= COMMON_FIELD_HEADER_SIZE
+ linksize
;
177 unsigned char *pd
= data
.GetBuffer(size
+ fieldsize
) + size
;
178 CommonField
*field
= (CommonField
*) pd
;
180 field
->size
= htobs(linksize
);
182 field
->u
.link
= link
;
187 std::string
ParseFieldString(const Barry::Protocol::CommonField
*field
)
189 // make no assumptions here, and pass the full size in as
190 // the maxlen, even though 99% of the time, it will be a null...
191 // this function can be used by non-null terminated strings as well
192 return ParseFieldString(field
->u
.raw
, btohs(field
->size
));
195 std::string
ParseFieldString(const void *data
, uint16_t maxlen
)
197 const char *str
= (const char *)data
;
199 // find last non-null character, since some fields
200 // can have multiple null terminators
201 while( maxlen
&& str
[maxlen
-1] == 0 )
204 return std::string(str
, maxlen
);
208 ///////////////////////////////////////////////////////////////////////////////
211 std::ostream
& operator<< (std::ostream
&os
, const std::vector
<UnknownField
> &unknowns
)
213 ios_format_state
state(os
);
215 std::vector
<UnknownField
>::const_iterator
216 ub
= unknowns
.begin(), ue
= unknowns
.end();
218 os
<< " Unknowns:\n";
219 for( ; ub
!= ue
; ub
++ ) {
220 os
<< " Type: 0x" << setbase(16)
221 << (unsigned int) ub
->type
222 << " Data:\n" << Data(ub
->data
.data(), ub
->data
.size());
228 ///////////////////////////////////////////////////////////////////////////////
229 // EmailAddress class
231 EmailAddress::EmailAddress(const std::string
&complex_address
)
233 size_t end
= complex_address
.rfind('>');
234 size_t start
= complex_address
.rfind('<');
235 if( start
== string::npos
|| end
== string::npos
|| start
> end
) {
236 // simple address, add it
237 Email
= complex_address
;
238 Inplace::trim(Email
);
241 Name
= complex_address
.substr(0, start
);
244 Email
= complex_address
.substr(start
+1, end
- start
- 1);
245 Inplace::trim(Email
);
249 std::ostream
& operator<<(std::ostream
&os
, const EmailAddress
&msga
)
251 if( msga
.Name
.size() )
252 os
<< msga
.Name
<< " <";
254 if( msga
.Name
.size() )
260 ///////////////////////////////////////////////////////////////////////////////
261 // EmailAddressList class
263 std::string
EmailAddressList::ToCommaSeparated() const
265 std::ostringstream oss
;
270 /// Adds every email address found in the comma separated list.
271 /// Does not clear() first.
272 void EmailAddressList::AddCommaSeparated(const std::string
&list
)
274 istringstream
iss(list
);
278 while( getline(iss
, address
, ',') ) {
279 // trim any trailing whitespace in the address
280 size_t len
= address
.size();
281 while( len
&& ::isspace(address
[len
-1]) )
282 address
.resize(len
-1);
284 // add to list if anything left
285 if( address
.size() ) {
286 EmailAddress
ea(address
);
292 std::ostream
& operator<<(std::ostream
&os
, const EmailAddressList
&elist
)
294 for( EmailAddressList::const_iterator i
= elist
.begin(); i
!= elist
.end(); ++i
) {
295 if( i
!= elist
.begin() )
303 ///////////////////////////////////////////////////////////////////////////////
304 // PostalAddress class
309 /// Format a mailing address into a single string, handling missing fields.
311 std::string
PostalAddress::GetLabel() const
313 std::string address
= Address1
;
314 if( Address2
.size() ) {
319 if( Address3
.size() ) {
327 address
+= City
+ " ";
328 if( Province
.size() )
329 address
+= Province
+ " ";
334 if( PostalCode
.size() )
335 address
+= PostalCode
;
340 void PostalAddress::Clear()
351 std::ostream
& operator<<(std::ostream
&os
, const PostalAddress
&post
) {
352 os
<< post
.GetLabel();
358 ///////////////////////////////////////////////////////////////////////////////
361 Date::Date(const struct tm
*timep
)
368 Month
= Day
= Year
= 0;
371 void Date::ToTm(struct tm
*timep
) const
373 memset(timep
, 0, sizeof(tm
));
374 timep
->tm_year
= Year
- 1900;
375 timep
->tm_mon
= Month
;
376 timep
->tm_mday
= Day
;
379 std::string
Date::ToYYYYMMDD() const
381 std::ostringstream oss
;
382 // setfill and setw not sticky.
383 oss
<< setw(4) << setfill('0') << dec
<< Year
384 << setw(2) << setfill('0') << dec
<< (Month
+ 1)
385 << setw(2) << setfill('0') << dec
<< Day
;
392 /// The Blackberry stores Birthday and Anniversary date fields
393 /// with the format: DD/MM/YYYY
395 std::string
Date::ToBBString() const
397 std::ostringstream oss
;
398 // setw() ain't 'sticky'!
399 oss
<< setw(2) << setfill('0') << dec
<< Day
<< '/'
400 << setw(2) << setfill('0') << dec
<< (Month
+ 1) << '/'
401 << setw(2) << setfill('0') << dec
<< Year
;
405 bool Date::FromTm(const struct tm
*timep
)
408 throw std::logic_error("NULL time pointer passed to Date::FromTm");
410 Year
= timep
->tm_year
+ 1900;
411 Month
= timep
->tm_mon
;
412 Day
= timep
->tm_mday
;
416 bool Date::FromBBString(const std::string
&str
)
419 if( 3 == sscanf(str
.c_str(), "%d/%d/%d", &d
, &m
, &y
) ) {
428 bool Date::FromYYYYMMDD(const std::string
&str
)
431 if( 3 == sscanf(str
.c_str(), "%4d%2d%2d", &y
, &m
, &d
) ) {
440 std::ostream
& operator<<(std::ostream
&os
, const Date
&date
)
442 ios_format_state
state(os
);
447 os
<< setw(4) << dec
<< date
.Year
<< '/'
448 << setw(2) << dec
<< (date
.Month
+ 1) << '/'
449 << setw(2) << dec
<< date
.Day
;
455 ///////////////////////////////////////////////////////////////////////////////
456 // CategoryList class
458 /// Parses the given comma delimited category string into
459 /// this CategoryList object, appending each token to the vector.
460 /// Will clear vector beforehand.
461 void CategoryList::CategoryStr2List(const std::string
&str
)
469 // parse the comma-delimited string to a list, stripping away
470 // any white space around each category name
471 string::size_type start
= 0, end
= 0, delim
= str
.find(',', start
);
472 while( start
!= string::npos
) {
473 if( delim
== string::npos
)
474 end
= str
.size() - 1;
478 // strip surrounding whitespace
479 while( str
[start
] == ' ' )
481 while( end
&& str
[end
] == ' ' )
485 string token
= str
.substr(start
, end
-start
+1);
491 if( start
!= string::npos
)
493 delim
= str
.find(',', start
);
497 /// Turns the current vectory into a comma delimited category
498 /// string suitable for use in Calendar, Task, and Memo protocol values.
499 void CategoryList::CategoryList2Str(std::string
&str
) const
503 Barry::CategoryList::const_iterator i
= begin();
504 for( ; i
!= end(); ++i
) {
519 int main(int argc
, char *argv
[])
522 cerr
<< "Usage: test <datafile>" << endl
;
526 std::vector
<Data
> array
;
527 if( !LoadDataArray(argv
[1], array
) ) {
528 cerr
<< "Unable to load file: " << argv
[1] << endl
;
532 cout
<< "Loaded " << array
.size() << " items" << endl
;
534 for( std::vector
<Data
>::iterator b
= array
.begin(), e
= array
.end();
538 // cout << d << endl;
539 if( d
.GetSize() > 13 && d
.GetData()[6] == 0x4f ) {
540 Barry::Contact contact
;
542 contact
.ParseFields(d
, size
);
543 cout
<< contact
<< endl
;
544 contact
.DumpLdif(cout
, "ou=People,dc=example,dc=com");
546 else if( d
.GetSize() > 13 && d
.GetData()[6] == 0x44 ) {
549 cal
.ParseFields(d
, size
);