3 /// Record parsing class for the memo database.
7 Copyright (C) 2005-2013, 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.
25 #include "record-internal.h"
26 #include "protostructs.h"
32 #include "ios_state.h"
35 using namespace Barry::Protocol
;
39 ///////////////////////////////////////////////////////////////////////////////
43 #define MEMFC_TITLE 0x01
44 #define MEMFC_BODY 0x02
45 #define MEMFC_MEMO_TYPE 0x03
46 #define MEMFC_CATEGORY 0x04
47 #define MEMFC_END 0xffff
49 static FieldLink
<Memo
> MemoFieldLinks
[] = {
50 { MEMFC_TITLE
, N_("Title"), 0, 0, &Memo::Title
, 0, 0, 0, 0, true },
51 { MEMFC_BODY
, N_("Body"), 0, 0, &Memo::Body
, 0, 0, 0, 0, true },
52 { MEMFC_END
, N_("End of List"), 0, 0, 0, 0, 0, 0, 0, false }
64 const unsigned char* Memo::ParseField(const unsigned char *begin
,
65 const unsigned char *end
,
68 const CommonField
*field
= (const CommonField
*) begin
;
70 // advance and check size
71 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
72 if( begin
> end
) // if begin==end, we are ok
75 if( !btohs(field
->size
) ) // if field has no size, something's up
78 if( field
->type
== MEMFC_MEMO_TYPE
) {
79 if( field
->u
.raw
[0] != 'm' ) {
80 throw Error(_("Memo::ParseField: MemoType is not 'm'"));
86 // cycle through the type table
87 for( FieldLink
<Memo
> *b
= MemoFieldLinks
;
91 if( b
->type
== field
->type
) {
93 std::string
&s
= this->*(b
->strMember
);
94 s
= ParseFieldString(field
);
95 if( b
->iconvNeeded
&& ic
)
97 return begin
; // done!
99 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
100 TimeT
&t
= this->*(b
->timeMember
);
101 t
.Time
= min2time(field
->u
.min1900
);
106 // handle special cases
107 switch( field
->type
)
111 std::string catstring
= ParseFieldString(field
);
113 catstring
= ic
->FromBB(catstring
);
114 Categories
.CategoryStr2List(catstring
);
119 // if still not handled, add to the Unknowns list
121 uf
.type
= field
->type
;
122 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
123 Unknowns
.push_back(uf
);
125 // return new pointer for next field
129 void Memo::ParseHeader(const Data
&data
, size_t &offset
)
131 // no header in Memo records
134 void Memo::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
136 const unsigned char *finish
= ParseCommonFields(*this,
137 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
138 offset
+= finish
- (data
.GetData() + offset
);
142 void Memo::Validate() const
146 void Memo::BuildHeader(Data
&data
, size_t &offset
) const
148 // no header in Memo records
155 /// Build fields part of record.
157 void Memo::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
161 // tack on the 'm' memo type field first
162 BuildField(data
, offset
, MEMFC_MEMO_TYPE
, 'm');
165 if( Categories
.size() ) {
167 Categories
.CategoryList2Str(store
);
168 BuildField(data
, offset
, MEMFC_CATEGORY
, ic
? ic
->ToBB(store
) : store
);
171 // cycle through the type table
172 for( FieldLink
<Memo
> *b
= MemoFieldLinks
;
173 b
->type
!= MEMFC_END
;
176 // print only fields with data
178 const std::string
&field
= this->*(b
->strMember
);
180 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
181 BuildField(data
, offset
, b
->type
, s
);
184 else if( b
->postMember
&& b
->postField
) {
185 const std::string
&field
= (this->*(b
->postMember
)).*(b
->postField
);
187 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
188 BuildField(data
, offset
, b
->type
, s
);
193 // and finally save unknowns
194 UnknownsType::const_iterator
195 ub
= Unknowns
.begin(), ue
= Unknowns
.end();
196 for( ; ub
!= ue
; ub
++ ) {
197 BuildField(data
, offset
, *ub
);
200 data
.ReleaseBuffer(offset
);
205 void Memo::Dump(std::ostream
&os
) const
207 ios_format_state
state(os
);
209 os
<< _("Memo entry: ") << "0x" << setbase(16) << RecordId
210 << " (" << (unsigned int)RecType
<< ")\n";
211 os
<< _(" Title: ") << Title
<< "\n";
214 // The Body may have '\r' characters... translate them
215 // in the output to make it look more pretty
216 for( string::const_iterator i
= Body
.begin(); i
!= Body
.end(); ++i
) {
224 if( Categories
.size() ) {
226 Categories
.CategoryList2Str(display
);
227 os
<< _(" Categories: ") << display
<< "\n";
234 bool Memo::operator<(const Memo
&other
) const
236 int cmp
= Title
.compare(other
.Title
);
238 cmp
= Body
.compare(other
.Body
);
244 RecType
= GetDefaultRecType();
254 const FieldHandle
<Memo
>::ListT
& Memo::GetFieldHandles()
256 static FieldHandle
<Memo
>::ListT fhv
;
261 #undef CONTAINER_OBJECT_NAME
262 #define CONTAINER_OBJECT_NAME fhv
264 #undef RECORD_CLASS_NAME
265 #define RECORD_CLASS_NAME Memo
267 FHP(RecType
, _("Record Type Code"));
268 FHP(RecordId
, _("Unique Record ID"));
270 FHD(Title
, _("Title"), MEMFC_TITLE
, true);
271 FHD(Body
, _("Body"), MEMFC_BODY
, true);
272 FHD(Categories
, _("Categories"), MEMFC_CATEGORY
, true);
274 FHP(Unknowns
, _("Unknown Fields"));
279 std::string
Memo::GetDescription() const