3 /// Record parsing class for the memo database.
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2007, Brian Edginton
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
24 #include "record-internal.h"
25 #include "protostructs.h"
33 using namespace Barry::Protocol
;
37 ///////////////////////////////////////////////////////////////////////////////
41 #define MEMFC_TITLE 0x01
42 #define MEMFC_BODY 0x02
43 #define MEMFC_MEMO_TYPE 0x03
44 #define MEMFC_CATEGORY 0x04
45 #define MEMFC_END 0xffff
47 static FieldLink
<Memo
> MemoFieldLinks
[] = {
48 { MEMFC_TITLE
, "Title", 0, 0, &Memo::Title
, 0, 0, 0, 0, true },
49 { MEMFC_BODY
, "Body", 0, 0, &Memo::Body
, 0, 0, 0, 0, true },
50 { MEMFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
62 const unsigned char* Memo::ParseField(const unsigned char *begin
,
63 const unsigned char *end
,
66 const CommonField
*field
= (const CommonField
*) begin
;
68 // advance and check size
69 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
70 if( begin
> end
) // if begin==end, we are ok
73 if( !btohs(field
->size
) ) // if field has no size, something's up
76 if( field
->type
== MEMFC_MEMO_TYPE
) {
77 if( field
->u
.raw
[0] != 'm' ) {
78 throw Error( "Memo::ParseField: MemoType is not 'm'" );
84 // cycle through the type table
85 for( FieldLink
<Memo
> *b
= MemoFieldLinks
;
89 if( b
->type
== field
->type
) {
91 std::string
&s
= this->*(b
->strMember
);
92 s
= ParseFieldString(field
);
93 if( b
->iconvNeeded
&& ic
)
95 return begin
; // done!
97 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
98 time_t &t
= this->*(b
->timeMember
);
99 t
= min2time(field
->u
.min1900
);
104 // handle special cases
105 switch( field
->type
)
109 std::string catstring
= ParseFieldString(field
);
111 catstring
= ic
->FromBB(catstring
);
112 Categories
.CategoryStr2List(catstring
);
117 // if still not handled, add to the Unknowns list
119 uf
.type
= field
->type
;
120 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
121 Unknowns
.push_back(uf
);
123 // return new pointer for next field
127 void Memo::ParseHeader(const Data
&data
, size_t &offset
)
129 // no header in Memo records
132 void Memo::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
134 const unsigned char *finish
= ParseCommonFields(*this,
135 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
136 offset
+= finish
- (data
.GetData() + offset
);
140 void Memo::BuildHeader(Data
&data
, size_t &offset
) const
142 // no header in Memo records
149 /// Build fields part of record.
151 void Memo::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
155 // tack on the 'm' memo type field first
156 BuildField(data
, offset
, MEMFC_MEMO_TYPE
, 'm');
159 if( Categories
.size() ) {
161 Categories
.CategoryList2Str(store
);
162 BuildField(data
, offset
, MEMFC_CATEGORY
, ic
? ic
->ToBB(store
) : store
);
165 // cycle through the type table
166 for( FieldLink
<Memo
> *b
= MemoFieldLinks
;
167 b
->type
!= MEMFC_END
;
170 // print only fields with data
172 const std::string
&field
= this->*(b
->strMember
);
174 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
175 BuildField(data
, offset
, b
->type
, s
);
178 else if( b
->postMember
&& b
->postField
) {
179 const std::string
&field
= (this->*(b
->postMember
)).*(b
->postField
);
181 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
182 BuildField(data
, offset
, b
->type
, s
);
187 // and finally save unknowns
188 UnknownsType::const_iterator
189 ub
= Unknowns
.begin(), ue
= Unknowns
.end();
190 for( ; ub
!= ue
; ub
++ ) {
191 BuildField(data
, offset
, *ub
);
194 data
.ReleaseBuffer(offset
);
199 void Memo::Dump(std::ostream
&os
) const
201 os
<< "Memo entry: 0x" << setbase(16) << RecordId
202 << " (" << (unsigned int)RecType
<< ")\n";
203 os
<< " Title: " << Title
<< "\n";
206 // The Body may have '\r' characters... translate them
207 // in the output to make it look more pretty
208 for( string::const_iterator i
= Body
.begin(); i
!= Body
.end(); ++i
) {
216 if( Categories
.size() ) {
218 Categories
.CategoryList2Str(display
);
219 os
<< " Categories: " << display
<< "\n";
226 bool Memo::operator<(const Memo
&other
) const
228 int cmp
= Title
.compare(other
.Title
);
230 cmp
= Body
.compare(other
.Body
);