-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnano_parser.c
executable file
·185 lines (144 loc) · 5.5 KB
/
nano_parser.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "nano_parser.h"
/* Number of allowed tokens to be registered in the system*/
#define MAX_NUM_TOKENS 10
/* Number of characters each token can store */
#define MAX_TOKEN_SIZE 10
/* Number of digits the input number can receive */
#define MAX_NUMBER_SIZE 4
/* ASCII decimal of the delimiter token */
#define DELIMITER_TOKEN 13
/* Store the tokens and callback functions to process them.
When these tokens are found, the callback functions are called */
typedef struct commands{
//array of void function pointers
void (*function[MAX_NUM_TOKENS]) (int argv);
//array of tokens to compare
char tokens[MAX_NUM_TOKENS][MAX_TOKEN_SIZE];
} Commands;
static Commands commands;
/* Index where to add new tokens and callbacks on the Commands structs */
int empty_index = 0;
/*
* Receives a string, extracts command token and number parameter and
* tries to match with already registered commands.
*
*/
void process_input(const char* buffer, int buffer_length)
{
//array of temp_token to compare to stored tokens
char temp_token[MAX_TOKEN_SIZE];
//array of digits to calc the actual number parameter
char number_array[MAX_NUMBER_SIZE];
//index to store the next digit found in the buffer
int number_index = 0;
//actual number to be calculed, and passed to the command callback
int number = -1;
/*Stops if the buffer to be parsed is bigger than allowed
* MAX_TOKEN_SIZE + MAX_NUMBER_SIZE + DELIMITER_TOKEN(CHAR)
*/
if(buffer_length > (MAX_TOKEN_SIZE + MAX_NUMBER_SIZE + 1)){
return;
}
//Clear temporary tokens
int i,j = 0;
for(i=0;i<MAX_TOKEN_SIZE;i++){
temp_token[i] = 0;
}
for(j=0;j<MAX_NUMBER_SIZE;j++){
number_array[j] = 0;
}
//Start parsing buffer
for(j=0;j<buffer_length;j++){
//Enqueue letter until MAX_TOKEN_SIZE characters processed
if(j<MAX_TOKEN_SIZE){
//65 - 90 ascii (A-Z)
if( (buffer[j] >= 65) && (buffer[j] <= 90) ){
temp_token[j] = buffer[j];
}
//97 - 122 ascii (a-z)
if( (buffer[j] >= 97) && (buffer[j] <= 122) ){
temp_token[j] = buffer[j];
}
}
//Enqueueing numbers
//48 - 57 ascii (0-9)
if( (buffer[j] >= 48) && (buffer[j] <= 57) ){
if(number_index < MAX_NUMBER_SIZE){
number_array[number_index]=buffer[j];
number_index++;
}
}
//Delimiter_found
if(buffer[j] == DELIMITER_TOKEN){
int k,l = 0;
//If it has digits, we need to represent as
//an integer to pass to the callback function
if(number_index>0){
number = 0;
//Transform enqueued digits list in an integer
for(k=0;k<number_index;k++){
int multiplier = 1;
for(l=number_index;l>(k+1);l--){
multiplier *= 10;
}
number += multiplier* (number_array[k]-48);
}
}
/*Try to match the registered tokens with the input*/
for(k=0;k<empty_index;k++){
int matched = 1;
int end_index = -1;
for(l=0;l<MAX_TOKEN_SIZE;l++){
if(commands.tokens[k][l]!= 0){
if(commands.tokens[k][l]!=temp_token[l]){
matched = 0;
}
}else{
/* Store the end of the matched stored token
*/
if(end_index==-1){
end_index = l;
}
}
}
/* Discards if registered token is only a subset of the input
*/
if(end_index<MAX_TOKEN_SIZE){
if(temp_token[end_index] != 0){
matched = 0;
}
}
if(matched){
(*commands.function[k])(number);
return;
}
}
}
}
};
/*
* Register commands is a function that stores tokens and function callbacks in
* a command list to parse and execute commands received in strings,
* usually from a serial terminal console.
*
*/
int register_command(const char* token, int token_length, void (*function_address)(int))
{
/* If the list has space*/
if(empty_index < MAX_NUM_TOKENS){
commands.function[empty_index] = function_address;
//clear token
int i,j = 0;
for(i=0;i<MAX_TOKEN_SIZE;i++){
commands.tokens[empty_index][i] = 0;
}
for(j=0;j<token_length;j++){
if(j<MAX_TOKEN_SIZE)
commands.tokens[empty_index][j] = token[j];
}
empty_index++;
return 0;
}else{
return -1;
}
};