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/packsource.h>
6 #include <msp/datafile/parser.h>
7 #include <msp/datafile/statement.h>
15 DataTool::DataTool(int argc, char **argv):
26 getopt.add_option('b', "binary", binary, GetOpt::NO_ARG).set_help("Produce a binary datafile");
27 getopt.add_option('c', "compile", compile, GetOpt::NO_ARG).set_help("Create a collection based on a template file");
28 getopt.add_option('f', "float-size", float_size, GetOpt::REQUIRED_ARG).set_help("Floating-point precision", "BITS");
29 getopt.add_option('g', "debug", debug, GetOpt::NO_ARG).set_help("Display control statements");
30 getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG).set_help("Output to a file instead of stdout", "FILE");
31 getopt.add_option('p', "pack", pack, GetOpt::NO_ARG).set_help("Create a pack from multiple files");
32 getopt.add_option('u', "unpack", unpack, GetOpt::NO_ARG).set_help("Unpacks files from packs into the current directory");
33 getopt.add_option('z', "compress", compress, GetOpt::NO_ARG).set_help("Produce a compressed datafile");
34 getopt.add_argument("infile", in_fns, GetOpt::OPTIONAL_ARG).set_help("Files to process");
37 if(compile+pack+unpack>1)
38 throw usage_error("Only one of -c, -p and -u may be specified");
40 if(pack && out_fn=="-")
41 throw usage_error("Can't write pack to stdout");
44 in_fns.push_back("-");
48 for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
50 throw usage_error("Can't unpack from stdout");
68 void DataTool::do_transfer()
70 IO::Base *out = open_output(out_fn);
71 DataFile::Writer *writer = create_writer(*out);
73 for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
75 IO::Base *in = open_input(*i);
76 DataFile::Parser parser(*in, *i);
80 DataFile::Statement st = parser.parse(true);
81 if(st.valid && (!st.control || st.keyword=="__src" || debug))
92 void DataTool::do_compile()
94 IO::Base *out = open_output(out_fn);
95 DataFile::Writer *writer = create_writer(*out);
97 Compiler compiler(*writer);
98 for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
100 IO::Base *in = open_input(*i);
101 DataFile::Parser parser(*in, *i);
102 compiler.load(parser);
110 void DataTool::do_pack()
112 Packer packer(*this);
113 for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
114 packer.pack_file(*i);
115 packer.create_pack(out_fn);
118 void DataTool::do_unpack()
120 DataFile::PackSource source;
121 for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
122 source.add_pack_file(*i);
124 list<DataFile::PackSource::FileInfo> files = source.list_files();
125 for(list<DataFile::PackSource::FileInfo>::const_iterator i=files.begin(); i!=files.end(); ++i)
127 IO::Seekable *in = source.open(i->name);
128 IO::Base *out = open_output(i->name);
132 unsigned len = in->read(buf, sizeof(buf));
133 out->write(buf, len);
142 IO::Base *DataTool::open_output(const string &fn)
145 return new IO::Buffered(IO::cout);
147 return new IO::BufferedFile(fn, IO::M_WRITE);
150 IO::Base *DataTool::open_input(const string &fn)
153 return new IO::Buffered(IO::cin);
155 return new IO::BufferedFile(fn, IO::M_READ);
158 DataFile::Writer *DataTool::create_writer(IO::Base &out)
160 DataFile::Writer *writer = new DataFile::Writer(out);
162 writer->set_compressed();
164 writer->set_binary(true);
166 writer->set_float_precision(float_size);