]> git.tdb.fi Git - libs/datafile.git/commitdiff
Add back support for stdin/out to datatool
authorMikko Rasa <tdb@tdb.fi>
Thu, 26 Jun 2008 10:51:27 +0000 (10:51 +0000)
committerMikko Rasa <tdb@tdb.fi>
Thu, 26 Jun 2008 10:51:27 +0000 (10:51 +0000)
Make sure the buffers won't outlive their underlying objects

tool.cpp

index 68c0fdccff40fada878072df672d55a212db0238..1e4da9a67e2bc897e12e2e778f7fa19662f89455 100644 (file)
--- a/tool.cpp
+++ b/tool.cpp
@@ -3,6 +3,7 @@
 #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"
@@ -43,31 +44,38 @@ int DataTool::main()
 {
        IO::Base *in;
        if(in_fn=="-")
-               throw Exception("stdin/out not supported at the moment");
+               in=&IO::cin;
        else
                in=new IO::File(in_fn);
 
        IO::Base *out;
        if(out_fn=="-")
-               throw Exception("stdin/out not supported at the moment");
+               out=&IO::cout;
        else
                out=new IO::File(out_fn, IO::M_WRITE);
 
-       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);
+               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;