3 /// Record parsing class for the memo database.
7 Copyright (C) 2005-2012, 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 TimeT
&t
= this->*(b
->timeMember
);
100 t
.Time
= 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::Validate() const
145 void Memo::BuildHeader(Data
&data
, size_t &offset
) const
147 // no header in Memo records
154 /// Build fields part of record.
156 void Memo::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
160 // tack on the 'm' memo type field first
161 BuildField(data
, offset
, MEMFC_MEMO_TYPE
, 'm');
164 if( Categories
.size() ) {
166 Categories
.CategoryList2Str(store
);
167 BuildField(data
, offset
, MEMFC_CATEGORY
, ic
? ic
->ToBB(store
) : store
);
170 // cycle through the type table
171 for( FieldLink
<Memo
> *b
= MemoFieldLinks
;
172 b
->type
!= MEMFC_END
;
175 // print only fields with data
177 const std::string
&field
= this->*(b
->strMember
);
179 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
180 BuildField(data
, offset
, b
->type
, s
);
183 else if( b
->postMember
&& b
->postField
) {
184 const std::string
&field
= (this->*(b
->postMember
)).*(b
->postField
);
186 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
187 BuildField(data
, offset
, b
->type
, s
);
192 // and finally save unknowns
193 UnknownsType::const_iterator
194 ub
= Unknowns
.begin(), ue
= Unknowns
.end();
195 for( ; ub
!= ue
; ub
++ ) {
196 BuildField(data
, offset
, *ub
);
199 data
.ReleaseBuffer(offset
);
204 void Memo::Dump(std::ostream
&os
) const
206 ios_format_state
state(os
);
208 os
<< "Memo entry: 0x" << setbase(16) << RecordId
209 << " (" << (unsigned int)RecType
<< ")\n";
210 os
<< " Title: " << Title
<< "\n";
213 // The Body may have '\r' characters... translate them
214 // in the output to make it look more pretty
215 for( string::const_iterator i
= Body
.begin(); i
!= Body
.end(); ++i
) {
223 if( Categories
.size() ) {
225 Categories
.CategoryList2Str(display
);
226 os
<< " Categories: " << display
<< "\n";
233 bool Memo::operator<(const Memo
&other
) const
235 int cmp
= Title
.compare(other
.Title
);
237 cmp
= Body
.compare(other
.Body
);
243 RecType
= GetDefaultRecType();
253 const FieldHandle
<Memo
>::ListT
& Memo::GetFieldHandles()
255 static FieldHandle
<Memo
>::ListT fhv
;
260 #undef CONTAINER_OBJECT_NAME
261 #define CONTAINER_OBJECT_NAME fhv
263 #undef RECORD_CLASS_NAME
264 #define RECORD_CLASS_NAME Memo
266 FHP(RecType
, "Record Type Code");
267 FHP(RecordId
, "Unique Record ID");
269 FHD(Title
, "Title", MEMFC_TITLE
, true);
270 FHD(Body
, "Body", MEMFC_BODY
, true);
271 FHD(Categories
, "Categories", MEMFC_CATEGORY
, true);
273 FHP(Unknowns
, "Unknown Fields");
278 std::string
Memo::GetDescription() const