debian: added giffgaff chatscripts
[barry.git] / src / r_memo.cc
bloba69169efe3120a37b4661cf04aa2aa91f50d1d7c
1 ///
2 /// \file r_memo.cc
3 /// Record parsing class for the memo database.
4 ///
6 /*
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.
23 #include "i18n.h"
24 #include "r_memo.h"
25 #include "record-internal.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "time.h"
29 #include "iconv.h"
30 #include <ostream>
31 #include <iomanip>
32 #include "ios_state.h"
34 using namespace std;
35 using namespace Barry::Protocol;
37 namespace Barry {
39 ///////////////////////////////////////////////////////////////////////////////
40 // Memo Class
42 // Memo Field Codes
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 }
55 Memo::Memo()
57 Clear();
60 Memo::~Memo()
64 const unsigned char* Memo::ParseField(const unsigned char *begin,
65 const unsigned char *end,
66 const IConverter *ic)
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
73 return begin;
75 if( !btohs(field->size) ) // if field has no size, something's up
76 return begin;
78 if( field->type == MEMFC_MEMO_TYPE ) {
79 if( field->u.raw[0] != 'm' ) {
80 throw Error(_("Memo::ParseField: MemoType is not 'm'"));
82 return begin;
86 // cycle through the type table
87 for( FieldLink<Memo> *b = MemoFieldLinks;
88 b->type != MEMFC_END;
89 b++ )
91 if( b->type == field->type ) {
92 if( b->strMember ) {
93 std::string &s = this->*(b->strMember);
94 s = ParseFieldString(field);
95 if( b->iconvNeeded && ic )
96 s = ic->FromBB(s);
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);
102 return begin;
106 // handle special cases
107 switch( field->type )
109 case MEMFC_CATEGORY:
111 std::string catstring = ParseFieldString(field);
112 if( ic )
113 catstring = ic->FromBB(catstring);
114 Categories.CategoryStr2List(catstring);
116 return begin;
119 // if still not handled, add to the Unknowns list
120 UnknownField uf;
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
126 return begin;
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
153 // Build
155 /// Build fields part of record.
157 void Memo::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
159 data.Zap();
161 // tack on the 'm' memo type field first
162 BuildField(data, offset, MEMFC_MEMO_TYPE, 'm');
164 // Categories
165 if( Categories.size() ) {
166 string store;
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;
174 b++ )
176 // print only fields with data
177 if( b->strMember ) {
178 const std::string &field = this->*(b->strMember);
179 if( field.size() ) {
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);
186 if( field.size() ) {
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";
212 os << _(" Body: ");
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 ) {
217 if( *i == '\r' )
218 os << "\n ";
219 else
220 os << *i;
222 os << "\n";
224 if( Categories.size() ) {
225 string display;
226 Categories.CategoryList2Str(display);
227 os << _(" Categories: ") << display << "\n";
230 os << Unknowns;
231 os << "\n\n";
234 bool Memo::operator<(const Memo &other) const
236 int cmp = Title.compare(other.Title);
237 if( cmp == 0 )
238 cmp = Body.compare(other.Body);
239 return cmp < 0;
242 void Memo::Clear()
244 RecType = GetDefaultRecType();
245 RecordId = 0;
247 Title.clear();
248 Body.clear();
249 Categories.clear();
251 Unknowns.clear();
254 const FieldHandle<Memo>::ListT& Memo::GetFieldHandles()
256 static FieldHandle<Memo>::ListT fhv;
258 if( fhv.size() )
259 return 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"));
276 return fhv;
279 std::string Memo::GetDescription() const
281 return Title;
284 } // namespace Barry