feat: preprocess input file
This commit is contained in:
43
src/main.c
43
src/main.c
@@ -223,6 +223,47 @@ fail:
|
||||
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)
|
||||
{
|
||||
const char* program_name = argv[0];
|
||||
@@ -234,7 +275,7 @@ int main(int argc, char** argv)
|
||||
|
||||
// read input file
|
||||
char* input_content;
|
||||
if(!read_entire_file(argv[1], &input_content)) return 1;
|
||||
if (!preprocess_file(argv[1], &input_content)) return 1;
|
||||
|
||||
// init lexer
|
||||
char string_store[1024];
|
||||
|
||||
Reference in New Issue
Block a user