forked from jpralves/TokenProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenProcessor.cpp
108 lines (94 loc) · 2.9 KB
/
TokenProcessor.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
98
99
100
101
102
103
104
105
106
107
108
/*
* Library Name: TokenProcessor
*
* Filename: TokenProcessor.cpp
* Description: library TokenProcessor implementation
*
* Version: 1.0.0
* Author: Joao Alves <[email protected]>
* Required files: TokenProcessor.cpp, TokenProcessor.h
*
* History:
* 1.0.0 - 2017-03-14 - Initial Version
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TokenProcessor.h"
TokenProcessor::TokenProcessor(Stream& stream, int buffer_Size, char delim, int command_List_Size,
const char *command_List, void(**callback_Functions)(TokenProcessor *), void(*defaultHandler)(TokenProcessor *)) :
stream(&stream), bufferSize(buffer_Size), commandListSize(command_List_Size), commandList(command_List), callbackFunctions(callback_Functions), defaultHandler(defaultHandler)
{
_delim[0] = delim;
_delim[1] = '\0';
buffer = (char *)malloc(bufferSize * sizeof(char));
clearBuffer();
}
TokenProcessor::~TokenProcessor() {
free(buffer);
}
void TokenProcessor::clearBuffer() {
memset(buffer, '\0', bufferSize);
// for (byte i = 0; i < bufferSize; i++) {
// buffer[i] = '\0';
// }
bufferPos = 0;
}
#define BACKSPACE 8
void TokenProcessor::process() {
char c;
while (stream->available() > 0) {
c = stream->read();
stream->print(c);
if (c == '\n' || c == '\r') {
processCommand(buffer);
clearBuffer();
break;
}
if (c == BACKSPACE && bufferPos) {
bufferPos--;
buffer[bufferPos] = '\0';
stream->print(F(" \b"));
}
if (isprint(c)) {
buffer[bufferPos++] = c;
buffer[bufferPos] = '\0';
if (bufferPos > bufferSize - 1)
bufferPos = 0;
}
}
}
char *TokenProcessor::nextToken() {
char *nextToken;
nextToken = strtok_r(NULL, _delim, &save_ptr);
return nextToken;
}
void TokenProcessor::processCommand(char *buffer) {
boolean found = false;
token = strtok_r(buffer, _delim, &save_ptr);
if (token == NULL)
return;
lastCommand = token[0];
for (byte i = 0; i < commandListSize; i++) {
found = (token[0] == commandList[i]);
if (found && callbackFunctions[i] != NULL) {
callbackFunctions[i](this);
break;
}
}
if (!found && defaultHandler != NULL) {
defaultHandler(this);
}
}
void TokenProcessor::getCommand(int p, char &cmd) {
if (p<commandListSize)
cmd = commandList[p];
}