using namespace Msp;
DataTool::DataTool(int argc, char **argv):
- in_fn("-"),
out_fn("-"),
binary(false),
compile(false),
getopt.add_option('z', "compress", compress, GetOpt::NO_ARG);
getopt(argc, argv);
- const vector<string> &args = getopt.get_args();
- if(!args.empty())
- in_fn = args[0];
+ in_fns = getopt.get_args();
+ if(in_fns.empty())
+ in_fns.push_back("-");
}
int DataTool::main()
{
- IO::Base *in;
- if(in_fn=="-")
- in = &IO::cin;
+ if(compile)
+ do_compile();
else
- in = new IO::File(in_fn);
+ do_transfer();
- IO::Base *out;
- if(out_fn=="-")
- out = &IO::cout;
- else
- out = new IO::File(out_fn, IO::M_WRITE);
+ return 0;
+}
+void DataTool::do_transfer()
+{
+ IO::Base *out = open_output(out_fn);
+ DataFile::Writer writer(*out);
+ set_writer_parameters(writer);
+
+ for(vector<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
{
- IO::Buffered in_buf(*in);
- DataFile::Parser parser(in_buf, in_fn);
- IO::Buffered out_buf(*out);
- DataFile::Writer writer(out_buf);
- if(compress)
- writer.set_compressed();
- if(binary)
- writer.set_binary(true);
- if(float_size)
- writer.set_float_precision(float_size);
-
- if(compile)
- {
- Compiler compiler(writer);
- compiler.load(parser);
- }
- else
+ IO::Base *in = open_input(*i);
+ DataFile::Parser parser(*in, *i);
+
+ while(parser)
{
- while(parser)
- {
- DataFile::Statement st = parser.parse();
- if(st.valid)
- {
- writer.write(st);
- out_buf.flush();
- }
- }
+ DataFile::Statement st = parser.parse();
+ if(st.valid)
+ writer.write(st);
}
+
+ delete in;
}
- if(in!=&IO::cin)
+ delete out;
+}
+
+void DataTool::do_compile()
+{
+ IO::Base *out = open_output(out_fn);
+ DataFile::Writer writer(*out);
+ set_writer_parameters(writer);
+
+ Compiler compiler(writer);
+ for(vector<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
+ {
+ IO::Base *in = open_input(*i);
+ DataFile::Parser parser(*in, *i);
+ compiler.load(parser);
delete in;
- if(out!=&IO::cout)
- delete out;
+ }
- return 0;
+ delete out;
+}
+
+IO::Base *DataTool::open_output(const string &fn)
+{
+ if(fn=="-")
+ return new IO::Buffered(IO::cout);
+ else
+ return new IO::BufferedFile(fn, IO::M_WRITE);
+}
+
+IO::Base *DataTool::open_input(const string &fn)
+{
+ if(fn=="-")
+ return new IO::Buffered(IO::cin);
+ else
+ return new IO::BufferedFile(fn, IO::M_READ);
+}
+
+void DataTool::set_writer_parameters(DataFile::Writer &writer)
+{
+ if(compress)
+ writer.set_compressed();
+ if(binary)
+ writer.set_binary(true);
+ if(float_size)
+ writer.set_float_precision(float_size);
}