lib: Fixing checking for incorrect return value in while loop.
[barry.git] / src / r_memo.cc
blobedfe021839d73d9fd19184f613523e32412d3879
1 ///
2 /// \file r_memo.cc
3 /// Record parsing class for the memo database.
4 ///
6 /*
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.
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>
31 #include "ios_state.h"
33 using namespace std;
34 using namespace Barry::Protocol;
36 namespace Barry {
38 ///////////////////////////////////////////////////////////////////////////////
39 // Memo Class
41 // Memo Field Codes
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 }
54 Memo::Memo()
56 Clear();
59 Memo::~Memo()
63 const unsigned char* Memo::ParseField(const unsigned char *begin,
64 const unsigned char *end,
65 const IConverter *ic)
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
72 return begin;
74 if( !btohs(field->size) ) // if field has no size, something's up
75 return begin;
77 if( field->type == MEMFC_MEMO_TYPE ) {
78 if( field->u.raw[0] != 'm' ) {
79 throw Error( "Memo::ParseField: MemoType is not 'm'" );
81 return begin;
85 // cycle through the type table
86 for( FieldLink<Memo> *b = MemoFieldLinks;
87 b->type != MEMFC_END;
88 b++ )
90 if( b->type == field->type ) {
91 if( b->strMember ) {
92 std::string &s = this->*(b->strMember);
93 s = ParseFieldString(field);
94 if( b->iconvNeeded && ic )
95 s = ic->FromBB(s);
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);
101 return begin;
105 // handle special cases
106 switch( field->type )
108 case MEMFC_CATEGORY:
110 std::string catstring = ParseFieldString(field);
111 if( ic )
112 catstring = ic->FromBB(catstring);
113 Categories.CategoryStr2List(catstring);
115 return begin;
118 // if still not handled, add to the Unknowns list
119 UnknownField uf;
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
125 return begin;
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
152 // Build
154 /// Build fields part of record.
156 void Memo::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
158 data.Zap();
160 // tack on the 'm' memo type field first
161 BuildField(data, offset, MEMFC_MEMO_TYPE, 'm');
163 // Categories
164 if( Categories.size() ) {
165 string store;
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;
173 b++ )
175 // print only fields with data
176 if( b->strMember ) {
177 const std::string &field = this->*(b->strMember);
178 if( field.size() ) {
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);
185 if( field.size() ) {
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";
211 os << " Body: ";
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 ) {
216 if( *i == '\r' )
217 os << "\n ";
218 else
219 os << *i;
221 os << "\n";
223 if( Categories.size() ) {
224 string display;
225 Categories.CategoryList2Str(display);
226 os << " Categories: " << display << "\n";
229 os << Unknowns;
230 os << "\n\n";
233 bool Memo::operator<(const Memo &other) const
235 int cmp = Title.compare(other.Title);
236 if( cmp == 0 )
237 cmp = Body.compare(other.Body);
238 return cmp < 0;
241 void Memo::Clear()
243 RecType = GetDefaultRecType();
244 RecordId = 0;
246 Title.clear();
247 Body.clear();
248 Categories.clear();
250 Unknowns.clear();
253 const FieldHandle<Memo>::ListT& Memo::GetFieldHandles()
255 static FieldHandle<Memo>::ListT fhv;
257 if( fhv.size() )
258 return 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");
275 return fhv;
278 std::string Memo::GetDescription() const
280 return Title;
283 } // namespace Barry