]> git.tdb.fi Git - libs/datafile.git/blobdiff - tool/tool.cpp
Add support for generating .cpp files for BuiltinSource
[libs/datafile.git] / tool / tool.cpp
index 73c49f7433ad6631a2f32a42607f79736fde68b2..2f52724f75543546ebd4b494448550696ad69bdc 100644 (file)
@@ -1,10 +1,12 @@
 #include <msp/core/getopt.h>
+#include <msp/fs/utils.h>
 #include <msp/io/buffered.h>
 #include <msp/io/console.h>
 #include <msp/io/file.h>
 #include <msp/datafile/packsource.h>
 #include <msp/datafile/parser.h>
 #include <msp/datafile/statement.h>
+#include "builtingenerator.h"
 #include "compiler.h"
 #include "packer.h"
 #include "tool.h"
@@ -27,6 +29,9 @@ DataTool::DataTool(int argc, char **argv):
        getopt.add_option('c', "compile", compile, GetOpt::NO_ARG).set_help("Create a collection based on a template file");
        getopt.add_option('f', "float-size", float_size, GetOpt::REQUIRED_ARG).set_help("Floating-point precision", "BITS");
        getopt.add_option('g', "debug", debug, GetOpt::NO_ARG).set_help("Display control statements");
+       getopt.add_option('i', "builtin", builtin, GetOpt::NO_ARG).set_help("Generate a source file for a BuiltinSource");
+       getopt.add_option('m', "module", builtin_module, GetOpt::REQUIRED_ARG).set_help("Name of the builtin source module");
+       getopt.add_option('n', "namespace", builtin_ns, GetOpt::REQUIRED_ARG).set_help("Namespace for the builtin source");
        getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG).set_help("Output to a file instead of stdout", "FILE");
        getopt.add_option('p', "pack", pack, GetOpt::NO_ARG).set_help("Create a pack from multiple files");
        getopt.add_option('u', "unpack", unpack, GetOpt::NO_ARG).set_help("Unpacks files from packs into the current directory");
@@ -34,8 +39,8 @@ DataTool::DataTool(int argc, char **argv):
        getopt.add_argument("infile", in_fns, GetOpt::OPTIONAL_ARG).set_help("Files to process");
        getopt(argc, argv);
 
-       if(compile+pack+unpack>1)
-               throw usage_error("Only one of -c, -p and -u may be specified");
+       if(compile+pack+unpack+builtin>1)
+               throw usage_error("Only one of -c, -p, -u and -i may be specified");
 
        if(pack && out_fn=="-")
                throw usage_error("Can't write pack to stdout");
@@ -49,6 +54,14 @@ DataTool::DataTool(int argc, char **argv):
                        if(*i=="-")
                                throw usage_error("Can't unpack from stdout");
        }
+
+       if(builtin && builtin_module.empty())
+       {
+               if(out_fn=="-")
+                       throw usage_error("Can't determine builtin module name");
+               else
+                       builtin_module = FS::basepart(FS::basename(out_fn));
+       }
 }
 
 int DataTool::main()
@@ -59,6 +72,8 @@ int DataTool::main()
                do_unpack();
        else if(compile)
                do_compile();
+       else if(builtin)
+               do_generate_builtin();
        else
                do_transfer();
 
@@ -139,6 +154,17 @@ void DataTool::do_unpack()
        }
 }
 
+void DataTool::do_generate_builtin()
+{
+       IO::Base *out = open_output(out_fn);
+       BuiltinGenerator generator(*out);
+       generator.begin(builtin_ns);
+       for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
+               generator.add_file(*i);
+       generator.end(builtin_module);
+       delete out;
+}
+
 IO::Base *DataTool::open_output(const string &fn)
 {
        if(fn=="-")