-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcanparser.c
More file actions
224 lines (188 loc) · 8.65 KB
/
Copy pathcanparser.c
File metadata and controls
224 lines (188 loc) · 8.65 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <errno.h>
#define MAX_LINE_LENGTH 1024
// Function prototype
char *process_line(char *line);
char *trim_whitespace(char *str);
char *convert_to_lowercase(char *line);
struct timeval add_milliseconds(struct timeval tv, long milliseconds) ;
struct timeval tv;
int main() {
FILE *input_file, *output_file;
char line[MAX_LINE_LENGTH];
// Open input file
input_file = fopen("scripts/input.txt", "r");
if (input_file == NULL) {
printf("Error opening input file.\n");
return 1;
}
// Open output file
output_file = fopen("scripts/output.txt", "w");
if (output_file == NULL) {
printf("Error opening output file.\n");
fclose(input_file);
return 1;
}
gettimeofday(&tv, NULL);
// Read input file line by line
while (fgets(line, MAX_LINE_LENGTH, input_file)) {
// Process the line
char *processed_line = process_line(line);
if(processed_line != NULL && *processed_line != '\0')
{
// Write processed line to output file
fputs(processed_line, output_file);
// Clean up
free(processed_line);
}
else
{
printf("empty");
}
}
printf("out");
// Close files
fclose(input_file);
fclose(output_file);
printf("parsing completed");
int return_value = system("canplayer -I scripts/output.txt");
return 0;
}
// Function to process a line of text
char *process_line(char *line) {
if(line == NULL || *line == '\0')
{
return NULL;
}
char *result = malloc(MAX_LINE_LENGTH); // Allocate memory for processed line
char *str_frame = malloc(MAX_LINE_LENGTH); // Allocate memory for processed line
line = convert_to_lowercase(line);
// Split the line into tokens based on whitespace
char *first_token = strtok(line, " "); // Split on space
char *second_token = strtok(NULL, " "); // Split on space
first_token=trim_whitespace(first_token);//trim spces
second_token=trim_whitespace(second_token);//trim spces
if (first_token != NULL && strcmp(first_token, "delay") == 0 && second_token != NULL) {
// Convert the string to a long integer
char *endptr; // Pointer to track where the conversion stopped
errno = 0; // Reset errno before conversion
long value = strtol(second_token, &endptr, 10); // Base 10 conversion
// Check for conversion errors
if (errno == ERANGE) {
printf("Error: The number is out of range for long.\n");
} else if (endptr == second_token) {
printf("Error: No digits were found in the input string.\n");
}
tv = add_milliseconds(tv, value);
return NULL;
}
else if (first_token != NULL && strcmp(first_token, "all_door") == 0 && second_token != NULL && (strcmp(second_token, "unlocked") == 0 || strcmp(second_token, "unlock") == 0)) {
sprintf(str_frame, "%s", "19B#000000000000");
}
else if (first_token != NULL && strcmp(first_token, "all_door") == 0 && second_token != NULL && (strcmp(second_token, "locked") == 0 || strcmp(second_token, "lock") == 0)) {
sprintf(str_frame, "%s", "19B#00000F000000");
}
else if (first_token != NULL && strcmp(first_token, "front_left_door") == 0 && second_token != NULL && (strcmp(second_token, "unlocked") == 0 || strcmp(second_token, "unlock") == 0)) {
sprintf(str_frame, "%s", "19B#00000E000000");
}
else if (first_token != NULL && strcmp(first_token, "front_left_door") == 0 && second_token != NULL && (strcmp(second_token, "locked") == 0 || strcmp(second_token, "lock") == 0)) {
sprintf(str_frame, "%s", "19B#00000F000000");
}
else if (first_token != NULL && strcmp(first_token, "front_right_door") == 0 && second_token != NULL && (strcmp(second_token, "unlocked") == 0 || strcmp(second_token, "unlock") == 0)) {
sprintf(str_frame, "%s", "19B#00000D000000");
}
else if (first_token != NULL && strcmp(first_token, "front_right_door") == 0 && second_token != NULL && (strcmp(second_token, "locked") == 0 || strcmp(second_token, "lock") == 0)) {
sprintf(str_frame, "%s", "19B#00000F000000");
}
else if (first_token != NULL && strcmp(first_token, "back_left_door") == 0 && second_token != NULL && (strcmp(second_token, "unlocked") == 0 || strcmp(second_token, "unlock") == 0)) {
sprintf(str_frame, "%s", "19B#000007000000");
}
else if (first_token != NULL && strcmp(first_token, "back_left_door") == 0 && second_token != NULL && (strcmp(second_token, "locked") == 0 || strcmp(second_token, "lock") == 0)) {
sprintf(str_frame, "%s", "19B#00000F000000");
}
else if (first_token != NULL && strcmp(first_token, "back_right_door") == 0 && second_token != NULL && (strcmp(second_token, "unlocked") == 0 || strcmp(second_token, "unlock") == 0)) {
sprintf(str_frame, "%s", "19B#00000B000000");
}
else if (first_token != NULL && strcmp(first_token, "back_right_door") == 0 && second_token != NULL && (strcmp(second_token, "locked") == 0 || strcmp(second_token, "lock") == 0)) {
sprintf(str_frame, "%s", "19B#00000F000000");
}
else if (first_token != NULL && strcmp(first_token, "head_light") == 0 && second_token != NULL && strcmp(second_token, "on") == 0) {
sprintf(str_frame, "%s", "249#0000000B");
}
else if (first_token != NULL && strcmp(first_token, "head_light") == 0 && second_token != NULL && strcmp(second_token, "off") == 0) {
sprintf(str_frame, "%s", "249#0000000A");
}
else if (first_token != NULL && strcmp(first_token, "wiper") == 0 && second_token != NULL && strcmp(second_token, "on") == 0) {
sprintf(str_frame, "%s", "240#000000000009");
}
else if (first_token != NULL && strcmp(first_token, "wiper") == 0 && second_token != NULL && strcmp(second_token, "off") == 0 ) {
sprintf(str_frame, "%s", "240#000000000008");
}
else if (first_token != NULL && strcmp(first_token, "throttle") == 0 && second_token != NULL && strcmp(second_token, "on") == 0) {
sprintf(str_frame, "%s", "238#00000000010000");
}
else if (first_token != NULL && strcmp(first_token, "throttle") == 0 && second_token != NULL && strcmp(second_token, "off") == 0 ) {
sprintf(str_frame, "%s", "238#00000000000000");
}
else if (first_token != NULL && strcmp(first_token, "handbrake") == 0 && second_token != NULL && strcmp(second_token, "on") == 0) {
sprintf(str_frame, "%s", "241#00000F");
}
else if (first_token != NULL && strcmp(first_token, "handbrake") == 0 && second_token != NULL && strcmp(second_token, "off") == 0 ) {
sprintf(str_frame, "%s", "241#00000E");
}
else {
return NULL;
}
sprintf(result, "(%010llu.%06llu) vcan0 %s\n", (unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec, str_frame);
return result; // Return the processed line
}
// Function to trim whitespace from the beginning and end of a string
char *trim_whitespace(char *str) {
char *end;
// Trim leading space
while (isspace((unsigned char)*str)) str++;
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;
// Null terminate the trimmed string
end[1] = '\0';
// Allocate memory for the trimmed string
char *trimmed = malloc(strlen(str) + 1);
if (trimmed == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
strcpy(trimmed, str); // Copy the trimmed string
return trimmed; // Return the trimmed string
}
struct timeval add_milliseconds(struct timeval tv, long milliseconds) {
// Convert milliseconds to seconds and microseconds
long seconds = milliseconds / 1000;
long useconds = (milliseconds % 1000) * 1000;
// Add the seconds and microseconds to the timeval structure
tv.tv_sec += seconds;
tv.tv_usec += useconds;
// Handle overflow in microseconds
if (tv.tv_usec >= 1000000) {
tv.tv_sec += tv.tv_usec / 1000000; // Add the overflow to seconds
tv.tv_usec %= 1000000; // Keep only the remainder
}
return tv; // Return the updated timeval structure
}
char *convert_to_lowercase(char *line) {
char *result = malloc(strlen(line) + 1); // Allocate memory for the result
if (result == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
// Copy the input line to the result
strcpy(result, line);
// Convert each character to lowercase
for (int i = 0; result[i]; i++) {
result[i] = tolower(result[i]);
}
return result; // Return the lowercase line
}