rpm: removed opensuse special cases
[barry.git] / src / r_memo.cc
blobebe8896bad8a10e8195f8207bccd0c747726656e
1 ///
2 /// \file r_memo.cc
3 /// Record parsing class for the memo database.
4 ///
6 /*
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.
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 time_t &t = this->*(b->timeMember);
100 t = 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::BuildHeader(Data &data, size_t &offset) const
143 // no header in Memo records
148 // Build
150 /// Build fields part of record.
152 void Memo::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
154 data.Zap();
156 // tack on the 'm' memo type field first
157 BuildField(data, offset, MEMFC_MEMO_TYPE, 'm');
159 // Categories
160 if( Categories.size() ) {
161 string store;
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;
169 b++ )
171 // print only fields with data
172 if( b->strMember ) {
173 const std::string &field = this->*(b->strMember);
174 if( field.size() ) {
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);
181 if( field.size() ) {
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";
207 os << " Body: ";
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 ) {
212 if( *i == '\r' )
213 os << "\n ";
214 else
215 os << *i;
217 os << "\n";
219 if( Categories.size() ) {
220 string display;
221 Categories.CategoryList2Str(display);
222 os << " Categories: " << display << "\n";
225 os << Unknowns;
226 os << "\n\n";
229 bool Memo::operator<(const Memo &other) const
231 int cmp = Title.compare(other.Title);
232 if( cmp == 0 )
233 cmp = Body.compare(other.Body);
234 return cmp < 0;
237 void Memo::Clear()
239 RecType = GetDefaultRecType();
240 RecordId = 0;
242 Title.clear();
243 Body.clear();
244 Categories.clear();
246 Unknowns.clear();
249 std::string Memo::GetDescription() const
251 return Title;
254 } // namespace Barry