feat: preprocess input file

This commit is contained in:
2026-01-25 14:47:39 +01:00
parent 2c4098fb60
commit 58467cde4e

View File

@@ -223,6 +223,47 @@ fail:
return result; return result;
} }
bool preprocess_file(const char* file_path, char** result) {
char command[256] = {0};
sprintf(command, "gcc -E %s", file_path);
FILE* fp = popen(command, "r");
if (fp == NULL) {
fprintf(stderr, "ERROR: Failed to run command: %s\n", strerror(errno));
return false;
}
char line[512];
size_t line_num = 0;
char file_name[512];
// TODO: use dynamic array instead
size_t cursor = 0;
*result = (char*) malloc(2048 * sizeof(char));
bool collecting_content = false;
while (fgets(line, sizeof(line), fp) != NULL) {
if (sscanf(line, "# %zu \"%s\"", &line_num, file_name) == 2) {
// remove trailing "
file_name[strlen(file_name) - 1] = '\0';
if (strcmp(file_name, file_path) == 0) {
collecting_content = true;
continue;;
}
} else if(collecting_content) {
size_t line_len = strlen(line);
memcpy(*result + cursor, line, line_len);
cursor += line_len;
}
}
pclose(fp);
return true;
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
const char* program_name = argv[0]; const char* program_name = argv[0];
@@ -234,7 +275,7 @@ int main(int argc, char** argv)
// read input file // read input file
char* input_content; char* input_content;
if(!read_entire_file(argv[1], &input_content)) return 1; if (!preprocess_file(argv[1], &input_content)) return 1;
// init lexer // init lexer
char string_store[1024]; char string_store[1024];