-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNOT_USED_hackerman.c
151 lines (131 loc) · 5.35 KB
/
NOT_USED_hackerman.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
// gcc -shared -o libhackerman.dylib -fPIC hackerman.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#define MAX_NAME_COUNT 100
const char *NAME[] = {
"font", "font_weight", "font_size", "tab_width", "cursor_width",
"margin", "theme", "file_explorer_root", "model_to_use", "eol_mode",
"show_line_numbers", "transparent", "blockcursor", "wrap_word",
"blinking_cursor", "show_scrollbar", "show_minimap", "highlight_todos",
"whitespace_visible", "indent_guides", "highlight_current_line",
"highlight_current_line_on_jump", "show_eol"
};
size_t NAME_COUNT = sizeof(NAME) / sizeof(NAME[0]);
bool is_name(const char *value) {
for (size_t i = 0; i < NAME_COUNT; i++) {
if (strcmp(NAME[i], value) == 0) {
return true;
}
}
return false;
}
bool is_conditional(const char *value) {
return strcmp(value, "true") == 0 || strcmp(value, "false") == 0;
}
char *tokenize(const char *text) {
// size_t buffer_size = 4096;
size_t buffer_size = strlen(text) * 4;
size_t index = 0;
size_t result_index = 0;
char *result = malloc(buffer_size);
if (!result) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
while (text[index] != '\0') {
// skip whitespace
if (isspace(text[index])) {
index++;
continue;
}
// comment
if (text[index] == '-') {
if (text[index + 1] == '-') {
result_index += snprintf(result + result_index, buffer_size - result_index, "\nTYPE=COMMENT START=%zu VALUE=--", index);
index += 2;
while (text[index] != '\0' && text[index] != '\n') {
result_index += snprintf(result + result_index, buffer_size - result_index, "%c", text[index]);
index++;
}
} else {
result_index += snprintf(result + result_index, buffer_size - result_index, "\nTYPE=ERROR START=%zu VALUE=-", index);
index++;
}
continue;
}
// header
if (text[index] == '[') {
size_t start_pos = index;
result_index += snprintf(result + result_index, buffer_size - result_index, "\nTYPE=KEYWORD START=%zu VALUE=[", start_pos);
index++;
while (text[index] != '\0' && text[index] != ']') {
result_index += snprintf(result + result_index, buffer_size - result_index, "%c", text[index]);
index++;
}
if (text[index] == ']') {
result_index += snprintf(result + result_index, buffer_size - result_index, "] ");
index++;
}
continue;
}
// string
if (text[index] == '"' || text[index] == '\'') {
size_t start_pos = index;
char quote = text[index];
result_index += snprintf(result + result_index, buffer_size - result_index, "\nTYPE=STRING START=%zu VALUE=", start_pos);
result_index += snprintf(result + result_index, buffer_size - result_index, "%c", quote);
index++;
while (text[index] != '\0' && text[index] != quote) {
result_index += snprintf(result + result_index, buffer_size - result_index, "%c", text[index]);
index++;
}
if (text[index] == quote) {
result_index += snprintf(result + result_index, buffer_size - result_index, "%c ", quote);
index++;
}
continue;
}
// number
if (isdigit(text[index])) {
size_t start_pos = index;
result_index += snprintf(result + result_index, buffer_size - result_index, "\nTYPE=NUMBER START=%zu VALUE=", start_pos);
while (isdigit(text[index])) {
result_index += snprintf(result + result_index, buffer_size - result_index, "%c", text[index]);
index++;
}
result_index += snprintf(result + result_index, buffer_size - result_index, " ");
continue;
}
if (isalpha(text[index]) || text[index] == '_') {
size_t start_pos = index;
char buffer[128];
size_t length = 0;
while (isalnum(text[index]) || text[index] == '_') {
buffer[length++] = text[index];
index++;
}
buffer[length] = '\0';
if (is_conditional(buffer)) {
result_index += snprintf(result + result_index, buffer_size - result_index, "\nTYPE=CONDITIONAL START=%zu VALUE=%s ", start_pos, buffer);
} else if (is_name(buffer)) {
result_index += snprintf(result + result_index, buffer_size - result_index, "\nTYPE=NAME START=%zu VALUE=%s ", start_pos, buffer);
} else {
result_index += snprintf(result + result_index, buffer_size - result_index, "\nTYPE=IDENTIFIER START=%zu VALUE=%s ", start_pos, buffer);
}
continue;
}
result_index += snprintf(result + result_index, buffer_size - result_index, "\nTYPE=DEFAULT START=%zu VALUE=%c ", index, text[index]);
index++;
}
result[result_index] = '\0';
return result;
}
void free_memory(char *ptr) {
if (ptr) {
free(ptr);
ptr = NULL;
}
}