-
Notifications
You must be signed in to change notification settings - Fork 0
/
statemachine.cpp
97 lines (85 loc) · 2.49 KB
/
statemachine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include <vector>
#include <algorithm>
#include "statemachine.h"
StateMachine::StateMachine() {
}
void StateMachine::Interpret( const char* buffer, StateMachine* tracker = nullptr ) {
while ( *buffer != '\0' ) {
//std::cout << "Getting state name" << std::endl;
std::string name( buffer );
std::cout << "Name: " << name << std::endl << std::endl;
//bool readonly = false;
buffer += name.size() + 1;
//if ( name[ 0 ] == 0 ) {
// readonly = true;
// name.erase( name.begin() );
//}
char size = *buffer++;
std::cout << "Size: " << size << std::endl;
//std::cout << "Interpreting value" << std::endl;
std::string value( buffer, size );
std::cout << "Value: " << value << std::endl;
buffer += value.size();
//, readonly );
if ( !SetState( name, value ) && tracker ){
tracker->SetState( name, value );
}
}
}
std::string StateMachine::GetMessage() {
std::string out;
for ( int i = 0; i < StateNames.size(); i++ ) {
out += StateNames[ i ];
out += '\0';
out += StateValues[ i ].size();
out += StateValues[ i ];
//std::cout << StateNames[ i ] << ", " << 0 << ", " << StateValues[ i ].size() << ", " << ( int ) StateValues[ i ][ 0 ] << std::endl;
}
return out;
}
bool StateMachine::SetState( std::string name, std::string value ) {
std::string& state = LocateState( name );
std::string previous = state;
state.assign( value );
return previous == state;
}
//void StateMachine::SetState( std::string name, std::string value, bool readonly ) {
// auto state = LocateState( name );
// if ( !readonly ) {
// if ( value != state ) {
// message += name;
// message.push_back( '\0' );
// message.push_back( value.size() );
// message += value;
// }
// }
//
// state.assign( value );
//}
//
//void StateMachine::SetState( std::string name, char* value, char size ) {
// auto state = LocateState( name );
// if ( value != state ) {
// message += name;
// message.push_back( '\0' );
// message.push_back( size );
// message += std::string( value, size );
// }
//
// state.assign( value );
//}
std::string& StateMachine::LocateState( std::string name ) {
auto it = std::find( StateNames.begin(), StateNames.end(), name );
if ( it != StateNames.end() ) {
return StateValues[ it - StateNames.begin() ];
}
StateNames.push_back( name );
StateValues.push_back( std::string( 1, '\0' ) );
return StateValues[ StateNames.size() - 1 ];
}
const char* StateMachine::GetState( std::string name ) {
return LocateState( name ).c_str();
}