]> git.tdb.fi Git - libs/datafile.git/commitdiff
Create writers dynamically and delete them before deleting output
authorMikko Rasa <tdb@tdb.fi>
Thu, 27 Sep 2012 22:24:13 +0000 (01:24 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 27 Sep 2012 22:24:13 +0000 (01:24 +0300)
This ensures that the IO objects are destroyed in the correct order, and
filters won't try to access deleted objects.

tool/tool.cpp
tool/tool.h

index d0499cb663970c818f3f2d8dfeced6bdf31d48e0..59c5cbcb1df30f33a6263697e216a2fb26719e38 100644 (file)
@@ -4,7 +4,6 @@
 #include <msp/io/file.h>
 #include <msp/datafile/parser.h>
 #include <msp/datafile/statement.h>
-#include <msp/datafile/writer.h>
 #include "compiler.h"
 #include "tool.h"
 
@@ -44,8 +43,7 @@ int DataTool::main()
 void DataTool::do_transfer()
 {
        IO::Base *out = open_output(out_fn);
-       DataFile::Writer writer(*out);
-       set_writer_parameters(writer);
+       DataFile::Writer *writer = create_writer(*out);
 
        for(vector<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
        {
@@ -56,22 +54,22 @@ void DataTool::do_transfer()
                {
                        DataFile::Statement st = parser.parse();
                        if(st.valid)
-                               writer.write(st);
+                               writer->write(st);
                }
 
                delete in;
        }
 
+       delete writer;
        delete out;
 }
 
 void DataTool::do_compile()
 {
        IO::Base *out = open_output(out_fn);
-       DataFile::Writer writer(*out);
-       set_writer_parameters(writer);
+       DataFile::Writer *writer = create_writer(*out);
 
-       Compiler compiler(writer);
+       Compiler compiler(*writer);
        for(vector<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
        {
                IO::Base *in = open_input(*i);
@@ -80,6 +78,7 @@ void DataTool::do_compile()
                delete in;
        }
 
+       delete writer;
        delete out;
 }
 
@@ -99,12 +98,14 @@ IO::Base *DataTool::open_input(const string &fn)
                return new IO::BufferedFile(fn, IO::M_READ);
 }
 
-void DataTool::set_writer_parameters(DataFile::Writer &writer)
+DataFile::Writer *DataTool::create_writer(IO::Base &out)
 {
+       DataFile::Writer *writer = new DataFile::Writer(out);
        if(compress)
-               writer.set_compressed();
+               writer->set_compressed();
        if(binary)
-               writer.set_binary(true);
+               writer->set_binary(true);
        if(float_size)
-               writer.set_float_precision(float_size);
+               writer->set_float_precision(float_size);
+       return writer;
 }
index 2cfcf1573514343da799ad9c1407237c2cf9b30e..0c5a77a85bed010c51807b5862509795e3ae4bca 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <string>
 #include <msp/core/application.h>
+#include <msp/datafile/writer.h>
 
 class DataTool: public Msp::RegisteredApplication<DataTool>
 {
@@ -23,7 +24,8 @@ private:
        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 &);
+public:
+       Msp::DataFile::Writer *create_writer(Msp::IO::Base &);
 };
 
 #endif