-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdparser.cpp
63 lines (56 loc) · 1.25 KB
/
cmdparser.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
#include <Arduino.h>
#include <string.h>
#include "cmdparser.h"
namespace Cmd
{
//
//
Parser::Parser()
{
// Set pointers of several streams...
uint8_t i = 0;
#if defined( SERIAL_COMMANDS )
m_streams[ i++ ] = &m_serialStream;
#endif
for( ; i < CMDPARSER_MAX_STREAMS; i++ )
{
m_streams[ i ] = nullptr;
}
m_lastStream = nullptr;
}
//
//
bool Parser::process()
{
// Try each stream until one gets a command line
const char *cmdLine = nullptr;
for( int i = 0; ( nullptr == cmdLine ) && ( i < CMDPARSER_MAX_STREAMS ); i++ )
{
if( m_streams[ i ] && m_streams[ i ]->read() )
{
cmdLine = m_streams[ i ]->lastCmd();
m_streams[ i ]->reset();
m_lastStream = m_streams[ i ];
}
}
if( nullptr == cmdLine )
return false;
// First token is the main command
findNextToken( m_cmd, cmdLine );
resetArg();
return true;
}
//
//
void Parser::findNextToken( Token &token, const char *start )
{
token.setLength( 0 );
// find start of token
const char *letter = start;
for( ; ( ( ' ' == *letter ) || ( '\t' == *letter ) ) && ( 0 != *letter ); letter ++ );
token.setStart( letter );
// find end of token
for( ; ( ' ' != *letter ) && ( '\t' != *letter ) && ( 0 != *letter ); letter ++ );
token.setLength( static_cast< uint8_t >( letter - token.start() ) );
}
}