Add an abstraction layer for output
authorMikko Rasa <tdb@tdb.fi>
Fri, 3 Aug 2012 09:55:03 +0000 (12:55 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 3 Aug 2012 09:55:03 +0000 (12:55 +0300)
source/binarywriter.cpp
source/binarywriter.h
source/output.cpp [new file with mode: 0644]
source/output.h [new file with mode: 0644]
source/textwriter.cpp
source/textwriter.h
source/writer.h
source/writermode.h

index ffdc040..d143330 100644 (file)
@@ -2,6 +2,7 @@
 #include <msp/core/maputils.h>
 #include "binarywriter.h"
 #include "binfloat.h"
+#include "output.h"
 #include "statement.h"
 
 using namespace std;
@@ -9,7 +10,7 @@ using namespace std;
 namespace Msp {
 namespace DataFile {
 
-BinaryWriter::BinaryWriter(IO::Base &o):
+BinaryWriter::BinaryWriter(Output &o):
        WriterMode(o),
        next_kwd_id(1),
        next_str_id(1),
@@ -125,7 +126,7 @@ void BinaryWriter::write_string(const StringType::Store &s)
        else
        {
                write_int(s.size());
-               out.write(s.data(), s.size());
+               out.write(s);
        }
 }
 
index 2f67bbd..81b4a46 100644 (file)
@@ -25,7 +25,7 @@ private:
        unsigned float_precision;
 
 public:
-       BinaryWriter(IO::Base &o);
+       BinaryWriter(Output &o);
 
        virtual void set_float_precision(unsigned);
        virtual void write(const Statement &st);
diff --git a/source/output.cpp b/source/output.cpp
new file mode 100644 (file)
index 0000000..62886d3
--- /dev/null
@@ -0,0 +1,23 @@
+#include "output.h"
+
+using namespace std;
+
+namespace Msp {
+namespace DataFile {
+
+Output::Output(IO::Base &o):
+       out(&o)
+{ }
+
+unsigned Output::put(char c)
+{
+       return out->put(c);
+}
+
+unsigned Output::write(const string &s)
+{
+       return out->write(s);
+}
+
+} // namespace DataFile
+} // namespace Msp
diff --git a/source/output.h b/source/output.h
new file mode 100644 (file)
index 0000000..453260e
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef MSP_DATAFILE_OUTPUT_H_
+#define MSP_DATAFILE_OUTPUT_H_
+
+#include <msp/io/base.h>
+
+namespace Msp {
+namespace DataFile {
+
+class Output
+{
+private:
+       IO::Base *out;
+
+public:
+       Output(IO::Base &);
+
+       unsigned put(char);
+       unsigned write(const std::string &);
+};
+
+} // namespace DataFile
+} // namespace Msp
+
+#endif
index 65f4870..5212c97 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/io/print.h>
 #include <msp/strings/utils.h>
+#include "output.h"
 #include "statement.h"
 #include "textwriter.h"
 
@@ -8,7 +9,7 @@ using namespace std;
 namespace Msp {
 namespace DataFile {
 
-TextWriter::TextWriter(IO::Base &o):
+TextWriter::TextWriter(Output &o):
        WriterMode(o),
        float_format("%#.7g")
 { }
@@ -27,12 +28,12 @@ void TextWriter::write_(const Statement &st, unsigned level)
 {
        string indent(level, '\t');
 
-       IO::print(out, "%s%s", indent, st.keyword);
+       out.write(format("%s%s", indent, st.keyword));
        for(ValueArray::const_iterator i = st.args.begin(); i!=st.args.end(); ++i)
        {
                out.put(' ');
                if(i->get_signature()==StringType::signature)
-                       IO::print(out, "\"%s\"", c_escape(i->get<StringType::Store>(), false));
+                       out.write(format("\"%s\"", c_escape(i->get<StringType::Store>(), false)));
                else if(i->get_signature()==BoolType::signature)
                        out.write(i->get<BoolType::Store>() ? "true" : "false");
                else if(i->get_signature()==IntType::signature)
@@ -50,12 +51,12 @@ void TextWriter::write_(const Statement &st, unsigned level)
        }
        if(!st.sub.empty())
        {
-               IO::print(out, "\n%s{\n", indent);
+               out.write(format("\n%s{\n", indent));
                for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
                        write_(*i, level+1);
-               IO::print(out, "%s}", indent);
+               out.write(format("%s}", indent));
        }
-       out.write(";\n", 2);
+       out.write(";\n");
 }
 
 } // namespace DataFile
index a0ed1b7..e265d2f 100644 (file)
@@ -12,7 +12,7 @@ private:
        std::string float_format;
 
 public:
-       TextWriter(IO::Base &o);
+       TextWriter(Output &o);
 
        virtual void set_float_precision(unsigned);
        virtual void write(const Statement &st);
index c8d75e3..df30826 100644 (file)
@@ -3,7 +3,7 @@
 
 #include <map>
 #include <msp/io/base.h>
-#include "binarydict.h"
+#include "output.h"
 
 namespace Msp {
 namespace DataFile {
@@ -17,7 +17,7 @@ Frontend for writing data.
 class Writer
 {
 private:
-       IO::Base &out;
+       Output out;
        WriterMode *mode;
        bool binary;
 
index 21df83f..05058ab 100644 (file)
@@ -1,19 +1,18 @@
 #ifndef MSP_DATAFILE_WRITERMODE_H_
 #define MSP_DATAFILE_WRITERMODE_H_
 
-#include <msp/io/base.h>
-
 namespace Msp {
 namespace DataFile {
 
+class Output;
 class Statement;
 
 class WriterMode
 {
 protected:
-       IO::Base &out;
+       Output &out;
 
-       WriterMode(IO::Base &o): out(o) { }
+       WriterMode(Output &o): out(o) { }
 public:
        virtual ~WriterMode() { }