89 lines
2.0 KiB
C

#include <SterlingCompiler.h>
char *LoadFile(const char *filename) {
char *data = NULL;
FILE *file = NULL;
const int buff_size = 2048;//may change for max size of file in character
char buffer[buff_size];
int count = 0;
file = fopen(filename, "r");
count = fread(buffer, 1, buff_size, file);
data = calloc(count + 1, sizeof(char));
memcpy(data, buffer, count);
fclose(file);
return (data);
}
TokenList SeparateString(char *data) {
char *span = NULL;
char *tok = NULL;
char *tmp = NULL;
bool toggle = false;
Token_t buffer[256];
tok = strtok_r(data, "\"\'", &span);
int i = 0;
buffer[i].ctx = TOK_NONE;
while (tok && i < (sizeof(buffer) / sizeof(Token_t))) {
size_t size = (span - tok);
tmp = calloc(size + 1, sizeof(char));
memcpy_s(tmp, size, tok, size);
tmp[size] = 0x00;
buffer[i].data = tmp;
buffer[i].size = size;
if (toggle) {
buffer[i].ctx = TOK_STRING;
} else {
buffer[i].ctx = TOK_NONE;
}
tok = strtok_r(NULL, "\"\'", &span);
i++;
toggle ^= toggle;
}
TokenList token_list;
token_list.token = calloc(i, sizeof(Token_t));
token_list.size = i;
memcpy(token_list.token, buffer, i);
for (int k = 0; k < i; k++) {
token_list.token[k] = buffer[i];
}
return (token_list);
}
void tmpFunction() {
char* data = NULL;
char* span = NULL;
char* tmp = NULL;
char* delim = " \t\n";
char* special = ";#";
size_t size = 0;
char* tok = strtok_r(data, delim, &span);
while (tok) {
size = (span - tok);
tmp = malloc(size + 1);
memcpy_s(tmp, size, tok, size);
tmp[size] = 0x00;
printf("|%s|\n", tmp);
free(tmp);
tok = strtok_r(NULL, " \t\n", &span);
}
}
int main(int ac, char **av) {
if (ac <= 1) {
printf("no file specified");
return (-1);
}
char* data = LoadFile(av[1]);
TokenList tkn_lst;
//first pass on string
tkn_lst = SeparateString(data);
//second pass on ; and \n
//third pass on \t and space
for (int i = 0; i < tkn_lst.size; i++) {
free(tkn_lst.token[i].data);
}
free(data);
return(0);
}