3 /// Internal implementation of CommandTable parser class
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.
23 #include "record-internal.h"
29 using namespace Barry::Protocol
;
33 ///////////////////////////////////////////////////////////////////////////////
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
);
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
59 if( !field
->size
) // if field has no size, something's up
63 command
.Code
= field
->code
;
64 command
.Name
.assign((const char *)field
->name
, field
->size
);
65 Commands
.push_back(command
);
69 void CommandTable::Parse(const Data
&data
, size_t offset
)
71 if( offset
>= data
.GetSize() )
74 const unsigned char *begin
= data
.GetData() + offset
;
75 const unsigned char *end
= data
.GetData() + data
.GetSize();
78 begin
= ParseField(begin
, end
);
81 void CommandTable::Clear()
86 unsigned int CommandTable::GetCommand(const std::string
&name
) const
88 CommandArrayType::const_iterator b
= Commands
.begin();
89 for( ; b
!= Commands
.end(); b
++ )
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";