diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 130 |
1 files changed, 98 insertions, 32 deletions
@@ -44,13 +44,8 @@ static char *read_stdin(void) return string; } -static char *read_file(const char *path) +static char *read_file(FILE *f) { - debug("reading from %s\n", path); - FILE *f = fopen(path, "rb"); - if (!f) - fatal("can't open file %s: %s\n", path, strerror(errno)); - fseek(f, 0, SEEK_END); long fsize = ftell(f); fseek(f, 0, SEEK_SET); @@ -59,17 +54,104 @@ static char *read_file(const char *path) if (!string) fatal("out of memory!\n"); int ret = fread(string, fsize, 1, f); - fclose(f); if (ret != 1) { free(string); - fatal("can't read file %s: %s\n", path, strerror(errno)); + fatal("can't read file: %s\n", strerror(errno)); } string[fsize] = 0; return string; } +static char *read_path(const char *path) +{ + debug("reading from %s\n", path); + FILE *f = fopen(path, "rb"); + if (!f) + fatal("can't open file %s: %s\n", path, strerror(errno)); + char *string = read_file(f); + fclose(f); + return string; +} + +static void test(char *input) +{ + debug("parsing as blc\n"); + + struct term *parsed_1 = parse_blc(input); + free(input); + debug("parsed original blc\n"); + + debug("merging duplicates\n"); + struct list *table = tree_merge_duplicates(parsed_1); + + FILE *temp_bloc = tmpfile(); + write_bloc(table, temp_bloc); + tree_destroy(table); + + debug("parsing as bloc\n"); + char *temp = read_file(temp_bloc); + struct bloc_parsed *bloc = parse_bloc(temp); + fseek(temp_bloc, 0, SEEK_END); + fprintf(stderr, "size bloc: %lu\n", ftell(temp_bloc)); + fclose(temp_bloc); + + FILE *temp_blc = tmpfile(); + write_blc(bloc, temp_blc); + char *input_2 = read_file(temp_blc); + struct term *parsed_2 = parse_blc(input_2); + fseek(temp_blc, 0, SEEK_END); + fprintf(stderr, "size blc: ~%lu\n", ftell(temp_blc) / 8); + fclose(temp_blc); + free(input_2); + debug("parsed reconstructed blc\n"); + + diff_term(parsed_1, parsed_2); + debug("diffed two terms\n"); + + free_term(parsed_1); + free_term(parsed_2); + debug("done!\n"); +} + +static void from_blc(char *input, char *output_path) +{ + debug("parsing as blc\n"); + + struct term *parsed = parse_blc(input); + debug("parsed blc\n"); + + debug("merging duplicates\n"); + struct list *table = tree_merge_duplicates(parsed); + + FILE *file = fopen(output_path, "wb"); + write_bloc(table, file); + fclose(file); + + tree_destroy(table); + free_term(parsed); + free(input); + + debug("done!\n"); +} + +static void from_bloc(char *input, char *output_path, int dump) +{ + debug("parsing as bloc\n"); + + struct bloc_parsed *bloc = parse_bloc(input); + if (dump) + print_bloc(bloc); + + FILE *file = fopen(output_path, "wb"); + write_blc(bloc, file); + fclose(file); + + free(input); + free_bloc(bloc); +} + int main(int argc, char **argv) { struct gengetopt_args_info args; @@ -82,40 +164,24 @@ int main(int argc, char **argv) if (args.input_arg[0] == '-') { input = read_stdin(); } else { - input = read_file(args.input_arg); + input = read_path(args.input_arg); } if (!input) return 1; - if (args.from_blc_flag && !args.from_bloc_flag) { - debug("parsing as blc\n"); - - struct term *parsed = parse_blc(input); - debug("parsed blc\n"); - - debug("merging duplicates\n"); - struct list *table = tree_merge_duplicates(parsed); - - write_bloc(table, args.output_arg); - - tree_destroy(table); - free_term(parsed); - free(input); + if (args.test_flag && args.from_blc_flag && !args.from_bloc_flag) { + test(input); + return 0; + } - debug("done!\n"); + if (args.from_blc_flag && !args.from_bloc_flag) { + from_blc(input, args.output_arg); return 0; } if (args.from_bloc_flag && !args.from_blc_flag) { - debug("parsing as bloc\n"); - - struct bloc_parsed *bloc = parse_bloc(input); - if (args.dump_flag) - print_bloc(bloc); - write_blc(bloc, args.output_arg); - free(input); - free_bloc(bloc); + from_bloc(input, args.output_arg, args.dump_flag); return 0; } |