3 /// Internal implementation of CommandTable parser class
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.
24 #include "record-internal.h"
30 using namespace Barry::Protocol
;
34 ///////////////////////////////////////////////////////////////////////////////
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
);
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
60 if( !field
->size
) // if field has no size, something's up
64 command
.Code
= field
->code
;
65 command
.Name
.assign((const char *)field
->name
, field
->size
);
66 Commands
.push_back(command
);
70 void CommandTable::Parse(const Data
&data
, size_t offset
)
72 if( offset
>= data
.GetSize() )
75 const unsigned char *begin
= data
.GetData() + offset
;
76 const unsigned char *end
= data
.GetData() + data
.GetSize();
79 begin
= ParseField(begin
, end
);
82 void CommandTable::Clear()
87 unsigned int CommandTable::GetCommand(const std::string
&name
) const
89 CommandArrayType::const_iterator b
= Commands
.begin();
90 for( ; b
!= Commands
.end(); b
++ )
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";