feat: preprocess input file
This commit is contained in:
45
src/main.c
45
src/main.c
@@ -223,10 +223,51 @@ 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];
|
||||||
if(argc == 1) {
|
if (argc == 1) {
|
||||||
fprintf(stderr, "ERROR: missing required argument <input_file>\n");
|
fprintf(stderr, "ERROR: missing required argument <input_file>\n");
|
||||||
fprintf(stderr, "Usage: %s <input_file>\n", program_name);
|
fprintf(stderr, "Usage: %s <input_file>\n", program_name);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -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];
|
||||||
|
|||||||
Reference in New Issue
Block a user