From 2917cd4e29ba3fbad09980f20b820beb585402d5 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Wed, 25 Nov 2009 11:13:33 +0000 Subject: [PATCH] Add support for output files Input and output files are mapped as follows: (* represents the `else' condition, usually - and/or NULL) Provided | Used Input : Output | Input : Output ------+--------+--------+-------- in.ct : out.c | in.ct : out.c in.ct : - | in.ct : stdout in.ct : NULL | in.ct : in.c * : out.c | stdin : out.c * : * | stdin : stdout --- ct.c | 48 +++++++++++++++++++++++++++++++++--------------- mkfile | 5 +++-- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/ct.c b/ct.c index 7eb1a18..93eac2e 100644 --- a/ct.c +++ b/ct.c @@ -1,4 +1,5 @@ #include +#include #include #include "parse.h" @@ -8,33 +9,50 @@ gpointer parse(FILE *input, GList **data, GList **code); int main(int argc, char **argv) { /* Parse arguments */ - int option_log_level = 7; + char *option_output = NULL; GOptionEntry entries[] = { - {"debug", 'd', 0, G_OPTION_ARG_INT, &option_log_level, - "Change default log level", "[1-7]"}, + {"output", 'o', 0, G_OPTION_ARG_STRING, &option_output, + "Output file", ""}, {NULL} }; - GOptionContext *context = g_option_context_new("infile"); + GOptionContext *context = g_option_context_new("[template]"); g_option_context_add_main_entries(context, entries, NULL); g_option_context_parse(context, &argc, &argv, NULL); - FILE *input = stdin; - for (int i = 1; i < argc; i++) - if (g_str_has_suffix(argv[i], ".ct")) - input = fopen(argv[i], "r"); + /* Handle input and output */ + FILE *input = stdin; + if (argv[1] && !g_str_equal(input, "-")) + input = fopen(argv[1], "r"); + if (!input) + g_error("invalid input file"); - /* Start compiling */ + FILE *output = stdout; + if (option_output && !g_str_equal(option_output, "-")) + output = fopen(option_output, "w"); + else if (input != stdin && option_output == NULL) { + char *outf = g_strdup(argv[1]); + outf[strlen(outf)-1] = '\0'; + output = fopen(outf, "w"); + g_free(outf); + } + if (!output) + g_error("invalid output file"); + + /* Start compiling */ GList *data = NULL; GList *code = NULL; - parse(input, &data, &code); + parse(input, &data, &code); data = g_list_reverse(data); code = g_list_reverse(code); - g_print("#include \n"); - g_print("\n"); + fprintf(output, "#include \n"); + fprintf(output, "\n"); for (GList *cur = data; cur; cur = cur->next) - g_print("%s", (gchar *)cur->data); - g_print("\n"); + fprintf(output, "%s", (gchar *)cur->data); + fprintf(output, "\n"); for (GList *cur = code; cur; cur = cur->next) - g_print("%s", (gchar *)cur->data); + fprintf(output, "%s", (gchar *)cur->data); + + fclose(input); + fclose(output); } diff --git a/mkfile b/mkfile index db6213b..cbbaef1 100644 --- a/mkfile +++ b/mkfile @@ -1,8 +1,9 @@ PROGS=ct lib PKGS=glib-2.0 -lib-test:V: lib +default:V: run +lib-run:V: lib ./lib -ct-test:V: ct +ct-run:V: ct ./ct < example/html.ct ct: ct.o parse.o scan.o ct.o: parse.h -- 2.43.2