-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDynamicCommandParser.cpp
More file actions
82 lines (72 loc) · 1.58 KB
/
DynamicCommandParser.cpp
File metadata and controls
82 lines (72 loc) · 1.58 KB
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
#include "DynamicCommandParser.h"
void DynamicCommandParser::addParser(char *cmd, ParserFunction function)
{
mParserLookupSize++;
mParserLookup = (ParserFunctionLookup*)realloc(mParserLookup, (mParserLookupSize) * sizeof(ParserFunctionLookup));
mParserLookup[mParserLookupSize-1].command = cmd;
mParserLookup[mParserLookupSize-1].function = function;
}
void DynamicCommandParser::append(char *str)
{
for(size_t i = 0; i < strlen(str); i++)
{
appendChar(str[i]);
}
}
void DynamicCommandParser::appendChar(char c)
{
size_t bufferLength = strlen(buffer);
if(c == mStart)
{
mInCommand = true;
buffer[0] = 0;
return;
}
else if(c == mEnd)
{
parseBuffer();
buffer[0] = '\0';
mInCommand = false;
}
else if(mInCommand)
{
buffer[bufferLength] = c;
buffer[bufferLength+1] = '\0';
}
}
void DynamicCommandParser::parseBuffer()
{
// Split buffer
int partCount = getBufferPartCount();
char **parts = (char**)malloc(partCount * sizeof(char*));
parts[0] = buffer;
int currentPart = 0;
for(int i = 0; buffer[i] != 0; i++)
{
if(buffer[i] == mDelimiter)
{
buffer[i] = 0;
currentPart++;
parts[currentPart] = &buffer[i+1];
}
}
for(size_t i = 0; i < mParserLookupSize; i++)
{
if(strcmp(mParserLookup[i].command, parts[0]) == 0)
{
mParserLookup[i].function(parts, partCount);
break;
}
}
free(parts);
}
int DynamicCommandParser::getBufferPartCount()
{
int count = 1;
for(size_t i = 0; i < strlen(buffer); i++)
{
if(buffer[i] == mDelimiter)
count++;
}
return count;
}