]> git.tdb.fi Git - libs/datafile.git/commitdiff
Restructure the tool and make it able to handle multiple input files
authorMikko Rasa <tdb@tdb.fi>
Thu, 27 Sep 2012 16:23:58 +0000 (19:23 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 27 Sep 2012 16:23:58 +0000 (19:23 +0300)
tool/tool.cpp
tool/tool.h

index b816ee33cd1e8d916d65c2b7d9315a7d8773db70..d0499cb663970c818f3f2d8dfeced6bdf31d48e0 100644 (file)
@@ -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<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);
 }
index e649909c0f9be925ce599c6b20c46b79b62675e0..2cfcf1573514343da799ad9c1407237c2cf9b30e 100644 (file)
@@ -7,7 +7,7 @@
 class DataTool: public Msp::RegisteredApplication<DataTool>
 {
 private:
-       std::string in_fn;
+       std::vector<std::string> 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