]> git.tdb.fi Git - libs/datafile.git/commitdiff
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 ffdc040f115565bbb18239ee9f2c14a137507fde..d1433304c251744a7a51c702a2b80b73b3d24139 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 2f67bbd24e43bad9403865a8488098ed82d703a9..81b4a46dfe70d0c9851498afbd794ce13e1c9944 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 65f4870ea6e466816ee25d9dd40dbc550d00faa8..5212c9760488330de78807ab7e4eb97bc3727827 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 a0ed1b79888e882a86b083d0a037c6126887ba78..e265d2fbb50d80c71352183928f8ae136fe2824a 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 c8d75e32dcd643bb09e3ec281331800691c99e02..df308266d976810217be152efec93d8f2a75b77a 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 21df83fd428db75f360dde92728c15896f8aebbe..05058abe94138dad630dd46281fc721618ac2196 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() { }