doc: updated bio man page with new cstore output mode
[barry.git] / src / r_command.cc
blobf40e21ae1a348afd826b8df0635c0135017be710
1 ///
2 /// \file r_command.cc
3 /// Internal implementation of CommandTable parser class
4 ///
6 /*
7 Copyright (C) 2005-2010, 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 // CommandTable class
36 CommandTable::CommandTable()
40 CommandTable::~CommandTable()
44 const unsigned char* CommandTable::ParseField(const unsigned char *begin,
45 const unsigned char *end)
47 // check if there is enough data for a header
48 const unsigned char *headend = begin + sizeof(CommandTableField);
49 if( headend > end )
50 return headend;
52 const CommandTableField *field = (const CommandTableField *) begin;
54 // advance and check size
55 begin += COMMAND_FIELD_HEADER_SIZE + field->size; // size is byte
56 if( begin > end ) // if begin==end, we are ok
57 return begin;
59 if( !field->size ) // if field has no size, something's up
60 return begin;
62 Command command;
63 command.Code = field->code;
64 command.Name.assign((const char *)field->name, field->size);
65 Commands.push_back(command);
66 return begin;
69 void CommandTable::Parse(const Data &data, size_t offset)
71 if( offset >= data.GetSize() )
72 return;
74 const unsigned char *begin = data.GetData() + offset;
75 const unsigned char *end = data.GetData() + data.GetSize();
77 while( begin < end )
78 begin = ParseField(begin, end);
81 void CommandTable::Clear()
83 Commands.clear();
86 unsigned int CommandTable::GetCommand(const std::string &name) const
88 CommandArrayType::const_iterator b = Commands.begin();
89 for( ; b != Commands.end(); b++ )
90 if( b->Name == name )
91 return b->Code;
92 return 0;
95 void CommandTable::Dump(std::ostream &os) const
97 CommandArrayType::const_iterator b = Commands.begin();
98 os << "Command table:\n";
99 for( ; b != Commands.end(); b++ ) {
100 os << " Command: 0x" << setbase(16) << b->Code
101 << " '" << b->Name << "'\n";
106 } // namespace Barry