1 #include <msp/core/getopt.h>
2 #include <msp/io/buffered.h>
3 #include <msp/io/console.h>
4 #include <msp/io/file.h>
5 #include <msp/datafile/parser.h>
6 #include <msp/datafile/statement.h>
14 DataTool::DataTool(int argc, char **argv):
24 getopt.add_option('b', "binary", binary, GetOpt::NO_ARG);
25 getopt.add_option('c', "compile", compile, GetOpt::NO_ARG);
26 getopt.add_option('f', "float-size", float_size, GetOpt::REQUIRED_ARG);
27 getopt.add_option('g', "debug", debug, GetOpt::NO_ARG);
28 getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
29 getopt.add_option('p', "pack", pack, GetOpt::NO_ARG);
30 getopt.add_option('z', "compress", compress, GetOpt::NO_ARG);
33 in_fns = getopt.get_args();
35 in_fns.push_back("-");
37 if(pack && out_fn=="-")
38 throw usage_error("Can't write pack to stdout");
53 void DataTool::do_transfer()
55 IO::Base *out = open_output(out_fn);
56 DataFile::Writer *writer = create_writer(*out);
58 for(vector<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
60 IO::Base *in = open_input(*i);
61 DataFile::Parser parser(*in, *i);
65 DataFile::Statement st = parser.parse(true);
66 if(st.valid && (st.keyword.compare(0, 2, "__") || st.keyword=="__src" || debug))
77 void DataTool::do_compile()
79 IO::Base *out = open_output(out_fn);
80 DataFile::Writer *writer = create_writer(*out);
82 Compiler compiler(*writer);
83 for(vector<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
85 IO::Base *in = open_input(*i);
86 DataFile::Parser parser(*in, *i);
87 compiler.load(parser);
95 void DataTool::do_pack()
98 for(vector<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
100 packer.create_pack(out_fn);
103 IO::Base *DataTool::open_output(const string &fn)
106 return new IO::Buffered(IO::cout);
108 return new IO::BufferedFile(fn, IO::M_WRITE);
111 IO::Base *DataTool::open_input(const string &fn)
114 return new IO::Buffered(IO::cin);
116 return new IO::BufferedFile(fn, IO::M_READ);
119 DataFile::Writer *DataTool::create_writer(IO::Base &out)
121 DataFile::Writer *writer = new DataFile::Writer(out);
123 writer->set_compressed();
125 writer->set_binary(true);
127 writer->set_float_precision(float_size);