Enhanced PPP filter class to support prepending
[barry/pauldeden.git] / src / r_memo.cc
blob6c2ad65fadc126c454da0c5c67bda047ad9af8c4
1 ///
2 /// \file r_memo.cc
3 /// Record parsing class for the memo database.
4 ///
6 /*
7 Copyright (C) 2005-2008, 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 <ostream>
29 #include <iomanip>
31 using namespace std;
32 using namespace Barry::Protocol;
34 namespace Barry {
36 ///////////////////////////////////////////////////////////////////////////////
37 // Memo Class
39 // Memo Field Codes
40 #define MEMFC_TITLE 0x01
41 #define MEMFC_BODY 0x02
42 #define MEMFC_MEMO_TYPE 0x03
43 #define MEMFC_CATEGORY 0x04
44 #define MEMFC_END 0xffff
46 FieldLink<Memo> MemoFieldLinks[] = {
47 { MEMFC_TITLE, "Title", 0, 0, &Memo::Title, 0, 0 },
48 { MEMFC_BODY, "Body", 0, 0, &Memo::Body, 0, 0 },
49 { MEMFC_CATEGORY, "Category", 0, 0, &Memo::Category, 0, 0 },
50 { MEMFC_END, "End of List", 0, 0, 0, 0, 0 }
53 Memo::Memo()
55 Clear();
58 Memo::~Memo()
62 const unsigned char* Memo::ParseField(const unsigned char *begin,
63 const unsigned char *end)
65 const CommonField *field = (const CommonField *) begin;
67 // advance and check size
68 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
69 if( begin > end ) // if begin==end, we are ok
70 return begin;
72 if( !btohs(field->size) ) // if field has no size, something's up
73 return begin;
75 if( field->type == MEMFC_MEMO_TYPE ) {
76 if( ( MemoType = field->u.raw[0] ) != 'm' ) {
77 throw Error( "Memo::ParseField: MemoType is not 'm'" );
79 return begin;
83 // cycle through the type table
84 for( FieldLink<Memo> *b = MemoFieldLinks;
85 b->type != MEMFC_END;
86 b++ )
88 if( b->type == field->type ) {
89 if( b->strMember ) {
90 std::string &s = this->*(b->strMember);
91 s = ParseFieldString(field);
92 return begin; // done!
94 else if( b->timeMember && btohs(field->size) == 4 ) {
95 time_t &t = this->*(b->timeMember);
96 t = min2time(field->u.min1900);
97 return begin;
102 // if still not handled, add to the Unknowns list
103 UnknownField uf;
104 uf.type = field->type;
105 uf.data.assign((const char*)field->u.raw, btohs(field->size));
106 Unknowns.push_back(uf);
108 // return new pointer for next field
109 return begin;
112 void Memo::ParseHeader(const Data &data, size_t &offset)
114 // no header in Memo records
117 void Memo::ParseFields(const Data &data, size_t &offset)
119 const unsigned char *finish = ParseCommonFields(*this,
120 data.GetData() + offset, data.GetData() + data.GetSize());
121 offset += finish - (data.GetData() + offset);
125 void Memo::Dump(std::ostream &os) const
127 os << "Memo entry: 0x" << setbase(16) << RecordId
128 << " (" << (unsigned int)RecType << ")\n";
129 os << " Title: " << Title << "\n";
130 os << " Body: " << Body << "\n";
131 os << " Category: " << Category << "\n";
133 os << Unknowns;
134 os << "\n\n";
137 void Memo::Clear()
139 Title.clear();
140 Body.clear();
141 Category.clear();
143 MemoType = 0;
145 Unknowns.clear();
148 } // namespace Barry