lib: fixed parsing of recurring VEVENTS: DAILY and interval support
[barry/progweb.git] / src / r_recordstate.cc
blobe807e631150792c6bf2a0f2dece372f442af6a98
1 ///
2 /// \file r_recordstate.cc
3 /// RecordStateTable database record parser class
4 ///
6 /*
7 Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "record.h"
23 #include "record-internal.h"
24 #include "data.h"
25 #include <sstream>
26 #include <iomanip>
28 using namespace std;
29 using namespace Barry::Protocol;
31 namespace Barry {
33 ///////////////////////////////////////////////////////////////////////////////
34 // RecordStateTable class
36 RecordStateTable::RecordStateTable()
37 : m_LastNewRecordId(1)
41 RecordStateTable::~RecordStateTable()
45 const unsigned char* RecordStateTable::ParseField(const unsigned char *begin,
46 const unsigned char *end)
48 const RecordStateTableField *field = (const RecordStateTableField *) begin;
50 // advance and check size
51 begin += sizeof(RecordStateTableField);
52 if( begin > end ) // if begin==end, we are ok
53 return begin;
55 State state;
56 state.Index = btohs(field->index);
57 state.RecordId = btohl(field->uniqueId);
58 state.Dirty = (field->flags & BARRY_RSTF_DIRTY) != 0;
59 state.RecType = field->rectype;
60 state.Unknown2.assign((const char*)field->unknown2, sizeof(field->unknown2));
61 StateMap[state.Index] = state;
63 return begin;
66 void RecordStateTable::Parse(const Data &data)
68 size_t offset = 12; // skipping the unknown 2 bytes at start
70 if( offset >= data.GetSize() )
71 return;
73 const unsigned char *begin = data.GetData() + offset;
74 const unsigned char *end = data.GetData() + data.GetSize();
76 while( begin < end )
77 begin = ParseField(begin, end);
80 void RecordStateTable::Clear()
82 StateMap.clear();
83 m_LastNewRecordId = 1;
86 // Searches the StateMap table for RecordId, and returns the "index"
87 // in the map if found. Returns true if found, false if not.
88 // pFoundIndex can be null if only the existence of the index is desired
89 bool RecordStateTable::GetIndex(uint32_t RecordId, IndexType *pFoundIndex) const
91 StateMapType::const_iterator i = StateMap.begin();
92 for( ; i != StateMap.end(); ++i ) {
93 if( i->second.RecordId == RecordId ) {
94 if( pFoundIndex )
95 *pFoundIndex = i->first;
96 return true;
99 return false;
102 // Generate a new RecordId that is not in the state table.
103 // Starts at 1 and keeps incrementing until a free one is found.
104 uint32_t RecordStateTable::MakeNewRecordId() const
106 // start with next Id
107 m_LastNewRecordId++;
109 // make sure it doesn't already exist
110 StateMapType::const_iterator i = StateMap.begin();
111 while( i != StateMap.end() ) {
112 if( m_LastNewRecordId == i->second.RecordId ) {
113 m_LastNewRecordId++; // try again
114 i = StateMap.begin(); // start over
116 else {
117 ++i; // next State
120 return m_LastNewRecordId;
123 void RecordStateTable::Dump(std::ostream &os) const
125 ios::fmtflags oldflags = os.setf(ios::right);
126 char fill = os.fill(' ');
127 bool bPrintAscii = Data::PrintAscii();
128 Data::PrintAscii(false);
130 os << " Index RecordId Dirty RecType" << endl;
131 os << "------- ---------- ----- -------" << endl;
133 StateMapType::const_iterator b, e = StateMap.end();
134 for( b = StateMap.begin(); b != e ; ++b ) {
135 const State &state = b->second;
137 os.fill(' ');
138 os << setbase(10) << setw(7) << state.Index;
139 os << " 0x" << setbase(16) << setfill('0') << setw(8) << state.RecordId;
140 os << " " << setfill(' ') << setw(5) << (state.Dirty ? "yes" : "no");
141 os << " 0x" << setbase(16) << setfill('0') << setw(2) << state.RecType;
142 os << " " << Data(state.Unknown2.data(), state.Unknown2.size());
145 // cleanup the stream
146 os.flags(oldflags);
147 os.fill(fill);
148 Data::PrintAscii(bPrintAscii);
152 } // namespace Barry