2026-03-03 00:36:28 +01:00

95 lines
1.9 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *LoadFile(const char *filename) {
char *data = NULL;
FILE *file = NULL;
const int buff_size = 2048;
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);
}
typedef enum {
TOK_NONE,
TOK_STRING,
TOK_TOKEN,
TOK_PREPROCESSOR,
} TKN_CTX;
typedef struct Token_s {
int size;
char *data;
TKN_CTX ctx;
} Token_t;
typedef struct {
int size;
Token_t *data;
} TokenList;
TokenList SeparateString(char *data) {
char* span = NULL;
char* tok = NULL;
char* tmp = NULL;
Token_t buffer[256];
tok = strtok_r(data, "\"\'", &span);
int i = 0;
buffer[i].ctx = TOK_NONE;
while (tok) {
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;
//printf("|%s|\n", tmp);
//free(tmp);
tok = strtok_r(NULL, "\"\'", &span);
i++;
buffer[i].ctx = TOK_STRING;
}
TokenList token_list;
token_list.data = calloc(i, sizeof(Token_t));
token_list.size = i;
return (token_list);
}
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
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);
}
free(data);
return(0);
}