-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.c
More file actions
119 lines (97 loc) · 2.99 KB
/
Copy pathtoken.c
File metadata and controls
119 lines (97 loc) · 2.99 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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "vect.h"
// Tokenizes the inputs
vect_t *parseInput(char *input);
// Handles cases that are in quotes
int handle_string(int i, char *input, vect_t *output);
// Checks if the given character is a special character
int isSpecialChar(char c);
// Checks if the given character is a special character
int isSpecialChar(char c) {
if (c == '(' || c == ')'
|| c == '<' || c == '>'
|| c == ';' || c == '|'
|| c == ' ') {
return 1;
}
return 0;
}
vect_t *parseInput(char *input) {
// Keeps track of the string not seperated by space
char *buffer = (char *) malloc(255);
// Keeps track of the last character within the string
int bufferIdx = 0;
vect_t *output = vect_new();
for (int i = 0; i < strlen(input); i++) {
char curChar = input[i];
// Checks if a special case was encountered to add all the temporarily stored string into the vector
if (bufferIdx > 0
&& (isSpecialChar(curChar)
|| (curChar == '\\' && (input[i + 1] == 'n' || input[i + 1] == 't'))
|| (curChar == '\"' || (curChar == '\\' && input[i + 1] == '\"'))
|| curChar == '\n' || curChar == '\t')) {
buffer[bufferIdx] = '\0';
vect_add(output, buffer);
bufferIdx = 0;
} else if (i == strlen(input) - 1
&& !(isSpecialChar(curChar)
|| (curChar == '\\' && (input[i + 1] == 'n' || input[i + 1] == 't'))
|| (curChar == '\"' || (curChar == '\\' && input[i + 1] == '\"'))
|| curChar == '\n' || curChar == '\t')) {
buffer[bufferIdx] = curChar;
buffer[bufferIdx + 1] = '\0';
bufferIdx = 0;
vect_add(output, buffer);
}
// Checks if the character on its own is a token
if (curChar == '(' || curChar == ')'
|| curChar == '<' || curChar == '>'
|| curChar == ';' || curChar == '|') {
vect_add(output, &curChar);
continue;
}
// Checks if it is an enter or a tab
if ((curChar == '\\' && (input[i + 1] == 'n' || input[i + 1] == 't')) || curChar == '\n' || curChar == '\t') {
i++;
continue;
}
// Checks if it is a quote
if ((curChar == '\\' && input[i + 1] == '\"') || curChar == '\"') {
i = handle_string(i + 1, input, output);
continue;
}
// Checks if it is a space
if (curChar == ' ') {
continue;
}
buffer[bufferIdx] = curChar;
bufferIdx++;
}
free(buffer);
return output;
}
// Handles cases that are in quotes
int handle_string(int i, char *input, vect_t *output) {
if (input[i] == '\"') {
i++;
}
char *buffer = (char *) malloc(255 * sizeof(char *));
int bufferIdx = 0;
while (input[i] != '\"') {
char curChar = input[i];
buffer[bufferIdx] = curChar;
bufferIdx++;
i++;
}
if (buffer[bufferIdx - 1] == '\\') {
buffer[bufferIdx - 1] = '\0';
} else {
buffer[bufferIdx] = '\0';
}
vect_add(output, buffer);
free(buffer);
return i;
}