3 /// Record parsing class for the memo database.
7 Copyright (C) 2005-2011, 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"
31 #include "ios_state.h"
34 using namespace Barry::Protocol
;
38 ///////////////////////////////////////////////////////////////////////////////
42 #define MEMFC_TITLE 0x01
43 #define MEMFC_BODY 0x02
44 #define MEMFC_MEMO_TYPE 0x03
45 #define MEMFC_CATEGORY 0x04
46 #define MEMFC_END 0xffff
48 static FieldLink
<Memo
> MemoFieldLinks
[] = {
49 { MEMFC_TITLE
, "Title", 0, 0, &Memo::Title
, 0, 0, 0, 0, true },
50 { MEMFC_BODY
, "Body", 0, 0, &Memo::Body
, 0, 0, 0, 0, true },
51 { MEMFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
63 const unsigned char* Memo::ParseField(const unsigned char *begin
,
64 const unsigned char *end
,
67 const CommonField
*field
= (const CommonField
*) begin
;
69 // advance and check size
70 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
71 if( begin
> end
) // if begin==end, we are ok
74 if( !btohs(field
->size
) ) // if field has no size, something's up
77 if( field
->type
== MEMFC_MEMO_TYPE
) {
78 if( field
->u
.raw
[0] != 'm' ) {
79 throw Error( "Memo::ParseField: MemoType is not 'm'" );
85 // cycle through the type table
86 for( FieldLink
<Memo
> *b
= MemoFieldLinks
;
90 if( b
->type
== field
->type
) {
92 std::string
&s
= this->*(b
->strMember
);
93 s
= ParseFieldString(field
);
94 if( b
->iconvNeeded
&& ic
)
96 return begin
; // done!
98 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
99 time_t &t
= this->*(b
->timeMember
);
100 t
= min2time(field
->u
.min1900
);
105 // handle special cases
106 switch( field
->type
)
110 std::string catstring
= ParseFieldString(field
);
112 catstring
= ic
->FromBB(catstring
);
113 Categories
.CategoryStr2List(catstring
);
118 // if still not handled, add to the Unknowns list
120 uf
.type
= field
->type
;
121 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
122 Unknowns
.push_back(uf
);
124 // return new pointer for next field
128 void Memo::ParseHeader(const Data
&data
, size_t &offset
)
130 // no header in Memo records
133 void Memo::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
135 const unsigned char *finish
= ParseCommonFields(*this,
136 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
137 offset
+= finish
- (data
.GetData() + offset
);
141 void Memo::BuildHeader(Data
&data
, size_t &offset
) const
143 // no header in Memo records
150 /// Build fields part of record.
152 void Memo::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
156 // tack on the 'm' memo type field first
157 BuildField(data
, offset
, MEMFC_MEMO_TYPE
, 'm');
160 if( Categories
.size() ) {
162 Categories
.CategoryList2Str(store
);
163 BuildField(data
, offset
, MEMFC_CATEGORY
, ic
? ic
->ToBB(store
) : store
);
166 // cycle through the type table
167 for( FieldLink
<Memo
> *b
= MemoFieldLinks
;
168 b
->type
!= MEMFC_END
;
171 // print only fields with data
173 const std::string
&field
= this->*(b
->strMember
);
175 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
176 BuildField(data
, offset
, b
->type
, s
);
179 else if( b
->postMember
&& b
->postField
) {
180 const std::string
&field
= (this->*(b
->postMember
)).*(b
->postField
);
182 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
183 BuildField(data
, offset
, b
->type
, s
);
188 // and finally save unknowns
189 UnknownsType::const_iterator
190 ub
= Unknowns
.begin(), ue
= Unknowns
.end();
191 for( ; ub
!= ue
; ub
++ ) {
192 BuildField(data
, offset
, *ub
);
195 data
.ReleaseBuffer(offset
);
200 void Memo::Dump(std::ostream
&os
) const
202 ios_format_state
state(os
);
204 os
<< "Memo entry: 0x" << setbase(16) << RecordId
205 << " (" << (unsigned int)RecType
<< ")\n";
206 os
<< " Title: " << Title
<< "\n";
209 // The Body may have '\r' characters... translate them
210 // in the output to make it look more pretty
211 for( string::const_iterator i
= Body
.begin(); i
!= Body
.end(); ++i
) {
219 if( Categories
.size() ) {
221 Categories
.CategoryList2Str(display
);
222 os
<< " Categories: " << display
<< "\n";
229 bool Memo::operator<(const Memo
&other
) const
231 int cmp
= Title
.compare(other
.Title
);
233 cmp
= Body
.compare(other
.Body
);
239 RecType
= GetDefaultRecType();
249 std::string
Memo::GetDescription() const