From: Mikko Rasa Date: Thu, 27 Sep 2012 16:23:58 +0000 (+0300) Subject: Restructure the tool and make it able to handle multiple input files X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=8955db30f9cd1c1566b349da29e669f065f84e36;p=libs%2Fdatafile.git Restructure the tool and make it able to handle multiple input files --- diff --git a/tool/tool.cpp b/tool/tool.cpp index b816ee3..d0499cb 100644 --- a/tool/tool.cpp +++ b/tool/tool.cpp @@ -12,7 +12,6 @@ using namespace std; using namespace Msp; DataTool::DataTool(int argc, char **argv): - in_fn("-"), out_fn("-"), binary(false), compile(false), @@ -27,60 +26,85 @@ DataTool::DataTool(int argc, char **argv): getopt.add_option('z', "compress", compress, GetOpt::NO_ARG); getopt(argc, argv); - const vector &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::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::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); } diff --git a/tool/tool.h b/tool/tool.h index e649909..2cfcf15 100644 --- a/tool/tool.h +++ b/tool/tool.h @@ -7,7 +7,7 @@ class DataTool: public Msp::RegisteredApplication { private: - std::string in_fn; + std::vector in_fns; std::string out_fn; bool binary; bool compile; @@ -16,7 +16,14 @@ private: public: DataTool(int argc, char **argv); - int main(); + + virtual int main(); +private: + void do_transfer(); + void do_compile(); + Msp::IO::Base *open_output(const std::string &); + Msp::IO::Base *open_input(const std::string &); + void set_writer_parameters(Msp::DataFile::Writer &); }; #endif