From b8dc50fb1c784f909b116915f4f86013fa8a84ec Mon Sep 17 00:00:00 2001 From: dx Date: Fri, 22 May 2020 20:24:53 +0100 Subject: ~!refactor main Using new file algorithm to generate html files from markdown file, supports repl and testing as usual. --- Converter/src/main.c | 96 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 24 deletions(-) (limited to 'Converter') diff --git a/Converter/src/main.c b/Converter/src/main.c index 8d102f2..87708f6 100644 --- a/Converter/src/main.c +++ b/Converter/src/main.c @@ -4,42 +4,90 @@ #include #include +string convert_filename(string filename) +{ + size_t sz_filename = strnlen(filename, 1024); + int cur = sz_filename - 3; + string new_filename = malloc(sizeof(*new_filename) * (sz_filename + 2)); + + strncpy(new_filename, filename, sz_filename - 3); + strncpy(new_filename + cur, ".html", 5); + + return new_filename; +} + int main(int argc, char *argv[]) { if (argc > 1) - { - if (strncmp(argv[1], "--test", 7) == 0) + { // Test or file + if (strcmp(argv[1], "--test") == 0) + { + fprintf(stderr, "Running tests\n"); + exit(0); + } + + bool free_output = false; + string filename; + string output; + + if (argc >= 3) + { + filename = argv[1]; + output = argv[2]; + } + else if (argc == 2) { - // run tests - fputs("Running header test (how does compile_string handle # headers?)\n", - stderr); - compile_line_header_depth(); + filename = argv[1]; + output = convert_filename(filename); + free_output = true; } else { - string output = - (compile_line(argv[1], strnlen(argv[1], 1024), "")); - puts(output); - free(output); + fputs("No inputs given for files\n", stderr); + exit(1); } - } - else - { - int i; - char *buf; - while (1) + + FILE *fp = fopen(filename, "r"); + if (fp != NULL) { + int sz_lines = nlines(fp); + printf("Lines: %d\n", sz_lines); + string *compiled_lines = compile_file(fp, filename, sz_lines); + puts("Compiled document"); + fclose(fp); - printf("> "); - buf = malloc(sizeof(*buf) * 1024); - fgets(buf, 1024, stdin); + fp = fopen(output, "w"); + for (int line = 0; line < sz_lines; ++line) + { + fprintf(fp, "%s\n", compiled_lines[line]); + free(compiled_lines[line]); + } + fclose(fp); - for (i = 0; buf[i] != '\n'; ++i) continue; - buf[i] = '\0'; // terminate - string output = (compile_line(buf, strlen(buf), "")); - puts(output); - free(output); + printf("Written to file (%s)\n", output); + if (free_output) + free(output); + free(compiled_lines); + } + else + { + fprintf(stderr, "%s is not a file\n", filename); + exit(1); + } + } + else + { // Repl + while (true) + { + string input, compiled_input; + input = malloc(sizeof(*input) * 1024); + fputs("> ", stdout); + fgets(input, 1024, stdin); + compiled_input = compile_line(input, strlen(input), ""); + fputs(compiled_input, stdout); + free(input); + free(compiled_input); } } } -- cgit v1.2.3-13-gbd6f