lib: added back m_seen_usb_error check in DefaultRead()
[barry.git] / src / r_memo.cc
blob82c65915965dfb5d4b26c67930958e6f1d8195b7
1 ///
2 /// \file r_memo.cc
3 /// Record parsing class for the memo database.
4 ///
6 /*
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.
23 #include "r_memo.h"
24 #include "record-internal.h"
25 #include "protostructs.h"
26 #include "data.h"
27 #include "time.h"
28 #include "iconv.h"
29 #include <ostream>
30 #include <iomanip>
32 using namespace std;
33 using namespace Barry::Protocol;
35 namespace Barry {
37 ///////////////////////////////////////////////////////////////////////////////
38 // Memo Class
40 // Memo Field Codes
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 }
53 Memo::Memo()
55 Clear();
58 Memo::~Memo()
62 const unsigned char* Memo::ParseField(const unsigned char *begin,
63 const unsigned char *end,
64 const IConverter *ic)
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
71 return begin;
73 if( !btohs(field->size) ) // if field has no size, something's up
74 return begin;
76 if( field->type == MEMFC_MEMO_TYPE ) {
77 if( field->u.raw[0] != 'm' ) {
78 throw Error( "Memo::ParseField: MemoType is not 'm'" );
80 return begin;
84 // cycle through the type table
85 for( FieldLink<Memo> *b = MemoFieldLinks;
86 b->type != MEMFC_END;
87 b++ )
89 if( b->type == field->type ) {
90 if( b->strMember ) {
91 std::string &s = this->*(b->strMember);
92 s = ParseFieldString(field);
93 if( b->iconvNeeded && ic )
94 s = ic->FromBB(s);
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);
100 return begin;
104 // handle special cases
105 switch( field->type )
107 case MEMFC_CATEGORY:
109 std::string catstring = ParseFieldString(field);
110 if( ic )
111 catstring = ic->FromBB(catstring);
112 Categories.CategoryStr2List(catstring);
114 return begin;
117 // if still not handled, add to the Unknowns list
118 UnknownField uf;
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
124 return begin;
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
147 // Build
149 /// Build fields part of record.
151 void Memo::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
153 data.Zap();
155 // tack on the 'm' memo type field first
156 BuildField(data, offset, MEMFC_MEMO_TYPE, 'm');
158 // Categories
159 if( Categories.size() ) {
160 string store;
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;
168 b++ )
170 // print only fields with data
171 if( b->strMember ) {
172 const std::string &field = this->*(b->strMember);
173 if( field.size() ) {
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);
180 if( field.size() ) {
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";
204 os << " Body: ";
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 ) {
209 if( *i == '\r' )
210 os << "\n ";
211 else
212 os << *i;
214 os << "\n";
216 if( Categories.size() ) {
217 string display;
218 Categories.CategoryList2Str(display);
219 os << " Categories: " << display << "\n";
222 os << Unknowns;
223 os << "\n\n";
226 bool Memo::operator<(const Memo &other) const
228 int cmp = Title.compare(other.Title);
229 if( cmp == 0 )
230 cmp = Body.compare(other.Body);
231 return cmp < 0;
234 void Memo::Clear()
236 Title.clear();
237 Body.clear();
238 Categories.clear();
240 Unknowns.clear();
244 } // namespace Barry