]> Pileus Git - ~andy/ct/commitdiff
Add support for output files
authorAndy Spencer <andy753421@gmail.com>
Wed, 25 Nov 2009 11:13:33 +0000 (11:13 +0000)
committerAndy Spencer <andy753421@gmail.com>
Wed, 25 Nov 2009 11:13:33 +0000 (11:13 +0000)
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
mkfile

diff --git a/ct.c b/ct.c
index 7eb1a18f32960d8ef1fd397da2848f3619bb6695..93eac2e425d3073dca322f99ebbf5faa9c229fa0 100644 (file)
--- a/ct.c
+++ b/ct.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <string.h>
 #include <glib.h>
 
 #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 <stdio.h>\n");
-       g_print("\n");
+       fprintf(output, "#include <stdio.h>\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 db6213b4853d78b61377a3827a96ecd35eb37fa8..cbbaef13007119ffa230cba11d0135fa96599e9f 100644 (file)
--- 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