]> git.tdb.fi Git - libs/datafile.git/blobdiff - tool.cpp
More flexible framework for loading substatements with custom loaders
[libs/datafile.git] / tool.cpp
index 533abb0f3c1592ac7c73f4defae8adb36af8185c..1e4da9a67e2bc897e12e2e778f7fa19662f89455 100644 (file)
--- a/tool.cpp
+++ b/tool.cpp
@@ -1,8 +1,10 @@
 /* $Id$ */
-#include <fstream>
 #include <iostream>
 #include <msp/core/application.h>
 #include <msp/core/getopt.h>
+#include <msp/io/buffered.h>
+#include <msp/io/console.h>
+#include <msp/io/file.h>
 #include "source/parser.h"
 #include "source/statement.h"
 #include "source/writer.h"
@@ -25,6 +27,7 @@ public:
 
 
 DataTool::DataTool(int argc, char **argv):
+       in_fn("-"),
        out_fn("-")
 {
        GetOpt getopt;
@@ -33,40 +36,46 @@ DataTool::DataTool(int argc, char **argv):
        getopt(argc, argv);
 
        const vector<string> &args=getopt.get_args();
-       if(args.empty())
-               throw UsageError("Must give input filename");
-
-       in_fn=args[0];
+       if(!args.empty())
+               in_fn=args[0];
 }
 
 int DataTool::main()
 {
-       ifstream in(in_fn.c_str());
-       if(!in)
-       {
-               cerr<<"Couldn't open input file\n";
-               return 1;
-       }
+       IO::Base *in;
+       if(in_fn=="-")
+               in=&IO::cin;
+       else
+               in=new IO::File(in_fn);
 
-       ostream *out;
+       IO::Base *out;
        if(out_fn=="-")
-               out=&cout;
+               out=&IO::cout;
        else
-               out=new ofstream(out_fn.c_str());
-
-       DataFile::Parser parser(in, in_fn);
-       DataFile::Writer writer(*out);
-       if(binary)
-               writer.set_binary(true);
+               out=new IO::File(out_fn, IO::M_WRITE);
 
-       while(parser)
        {
-               DataFile::Statement st=parser.parse();
-               if(st.valid)
-                       writer.write(st);
+               IO::Buffered in_buf(*in);
+               DataFile::Parser parser(in_buf, in_fn);
+               IO::Buffered out_buf(*out);
+               DataFile::Writer writer(out_buf);
+               if(binary)
+                       writer.set_binary(true);
+
+               while(parser)
+               {
+                       DataFile::Statement st=parser.parse();
+                       if(st.valid)
+                       {
+                               writer.write(st);
+                               out_buf.flush();
+                       }
+               }
        }
 
-       if(out!=&cout)
+       if(in!=&IO::cin)
+               delete in;
+       if(out!=&IO::cout)
                delete out;
 
        return 0;