Updated copyright dates for 2009
[barry/progweb.git] / src / r_memo.cc
blob5a0e33ed078809f566178ea2c363d8e0ab2cc5eb
1 ///
2 /// \file r_memo.cc
3 /// Record parsing class for the memo database.
4 ///
6 /*
7 Copyright (C) 2005-2009, 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_CATEGORY, "Category", 0, 0, &Memo::Category, 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( ( MemoType = 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;
106 // if still not handled, add to the Unknowns list
107 UnknownField uf;
108 uf.type = field->type;
109 uf.data.assign((const char*)field->u.raw, btohs(field->size));
110 Unknowns.push_back(uf);
112 // return new pointer for next field
113 return begin;
116 void Memo::ParseHeader(const Data &data, size_t &offset)
118 // no header in Memo records
121 void Memo::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
123 const unsigned char *finish = ParseCommonFields(*this,
124 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
125 offset += finish - (data.GetData() + offset);
129 void Memo::Dump(std::ostream &os) const
131 os << "Memo entry: 0x" << setbase(16) << RecordId
132 << " (" << (unsigned int)RecType << ")\n";
133 os << " Title: " << Title << "\n";
134 os << " Body: " << Body << "\n";
135 os << " Category: " << Category << "\n";
137 os << Unknowns;
138 os << "\n\n";
141 void Memo::Clear()
143 Title.clear();
144 Body.clear();
145 Category.clear();
147 MemoType = 0;
149 Unknowns.clear();
152 } // namespace Barry