menu: added new Keywords tag to .desktop files
[barry.git] / src / r_command.cc
blob40d0188214f28a82b18ba0bcadf71977f9ae6b75
1 ///
2 /// \file r_command.cc
3 /// Internal implementation of CommandTable parser class
4 ///
6 /*
7 Copyright (C) 2005-2013, 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 "i18n.h"
23 #include "record.h"
24 #include "record-internal.h"
25 #include "data.h"
26 #include <sstream>
27 #include <iomanip>
29 using namespace std;
30 using namespace Barry::Protocol;
32 namespace Barry {
34 ///////////////////////////////////////////////////////////////////////////////
35 // CommandTable class
37 CommandTable::CommandTable()
41 CommandTable::~CommandTable()
45 const unsigned char* CommandTable::ParseField(const unsigned char *begin,
46 const unsigned char *end)
48 // check if there is enough data for a header
49 const unsigned char *headend = begin + sizeof(CommandTableField);
50 if( headend > end )
51 return headend;
53 const CommandTableField *field = (const CommandTableField *) begin;
55 // advance and check size
56 begin += COMMAND_FIELD_HEADER_SIZE + field->size; // size is byte
57 if( begin > end ) // if begin==end, we are ok
58 return begin;
60 if( !field->size ) // if field has no size, something's up
61 return begin;
63 Command command;
64 command.Code = field->code;
65 command.Name.assign((const char *)field->name, field->size);
66 Commands.push_back(command);
67 return begin;
70 void CommandTable::Parse(const Data &data, size_t offset)
72 if( offset >= data.GetSize() )
73 return;
75 const unsigned char *begin = data.GetData() + offset;
76 const unsigned char *end = data.GetData() + data.GetSize();
78 while( begin < end )
79 begin = ParseField(begin, end);
82 void CommandTable::Clear()
84 Commands.clear();
87 unsigned int CommandTable::GetCommand(const std::string &name) const
89 CommandArrayType::const_iterator b = Commands.begin();
90 for( ; b != Commands.end(); b++ )
91 if( b->Name == name )
92 return b->Code;
93 return 0;
96 void CommandTable::Dump(std::ostream &os) const
98 CommandArrayType::const_iterator b = Commands.begin();
99 os << _("Command table:\n");
100 for( ; b != Commands.end(); b++ ) {
101 os << _(" Command: ") << "0x" << setbase(16) << b->Code
102 << " '" << b->Name << "'\n";
107 } // namespace Barry