-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.c
81 lines (73 loc) · 2.43 KB
/
tokens.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
#include "tokens.h"
/*
Function to duplicate a string and return a pointer to it.
*/
char* my_strdup(const char* s) {
size_t size = strlen(s) + 1;
char* p = malloc(size);
if (p != NULL) {
memcpy(p, s, size);
}
return p;
}
/*
Function to tokenize a string and store the tokens in an array
*/
void tokenize(char* input, char* tokens[], int* token_count) {
char* token;
int in_quote = 0;
// Initialize an array to store the content within double quotes
char quote_content[MAX_INPUT_LENGTH];
int quote_index = 0;
// Iterate through the input character by character
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] == '"') {
if (in_quote) {
// If we were inside a quote, we have reached the end
in_quote = 0;
quote_content[quote_index] = '\0'; // Null-terminate the content
tokens[(*token_count)++] =
my_strdup(quote_content); // Store the content within quotes as a
// single token
quote_index = 0; // Reset the index
} else {
// If we were not inside a quote, we are entering one
in_quote = 1;
}
} else if (in_quote) {
// If inside a quote, add the character to the quote content
quote_content[quote_index] = input[i];
quote_index++;
} else if (strchr(";<>()|", input[i]) != NULL) {
// If a special char, treat it as a separate token
char char_token[] = {input[i], '\0'};
tokens[(*token_count)++] = my_strdup(char_token);
} else if (input[i] != ' ' && input[i] != '\n' && input[i] != '\t') {
// If not inside a quote, not whitespace, and not a semicolon, store the
// character
char* word = (char*)malloc(MAX_INPUT_LENGTH);
int word_index = 0;
while (input[i] != '\0' && input[i] != ' ' && input[i] != '\n' &&
input[i] != '\t' && (strchr(";<>()|", input[i]) == NULL)) {
word[word_index++] = input[i];
i++;
}
word[word_index] = '\0';
tokens[(*token_count)++] = word;
while (input[i] != '\0' && input[i] != ' ' && input[i] != '\n' &&
input[i] != '\t') {
if (strchr(";<>()|", input[i]) != NULL) {
char char_token[] = {input[i], '\0'};
tokens[(*token_count)++] = my_strdup(char_token);
i++;
} else {
i--;
break;
}
}
if (input[i] == '\0') {
break;
}
}
}
}