From: Mikko Rasa Date: Fri, 3 Aug 2012 09:55:03 +0000 (+0300) Subject: Add an abstraction layer for output X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=commitdiff_plain;h=8e3fad222e174b7c659fd3d994d54314657ed989 Add an abstraction layer for output --- diff --git a/source/binarywriter.cpp b/source/binarywriter.cpp index ffdc040..d143330 100644 --- a/source/binarywriter.cpp +++ b/source/binarywriter.cpp @@ -2,6 +2,7 @@ #include #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); } } diff --git a/source/binarywriter.h b/source/binarywriter.h index 2f67bbd..81b4a46 100644 --- a/source/binarywriter.h +++ b/source/binarywriter.h @@ -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 index 0000000..62886d3 --- /dev/null +++ b/source/output.cpp @@ -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 index 0000000..453260e --- /dev/null +++ b/source/output.h @@ -0,0 +1,24 @@ +#ifndef MSP_DATAFILE_OUTPUT_H_ +#define MSP_DATAFILE_OUTPUT_H_ + +#include + +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 diff --git a/source/textwriter.cpp b/source/textwriter.cpp index 65f4870..5212c97 100644 --- a/source/textwriter.cpp +++ b/source/textwriter.cpp @@ -1,5 +1,6 @@ #include #include +#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(), false)); + out.write(format("\"%s\"", c_escape(i->get(), false))); else if(i->get_signature()==BoolType::signature) out.write(i->get() ? "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::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 diff --git a/source/textwriter.h b/source/textwriter.h index a0ed1b7..e265d2f 100644 --- a/source/textwriter.h +++ b/source/textwriter.h @@ -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); diff --git a/source/writer.h b/source/writer.h index c8d75e3..df30826 100644 --- a/source/writer.h +++ b/source/writer.h @@ -3,7 +3,7 @@ #include #include -#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; diff --git a/source/writermode.h b/source/writermode.h index 21df83f..05058ab 100644 --- a/source/writermode.h +++ b/source/writermode.h @@ -1,19 +1,18 @@ #ifndef MSP_DATAFILE_WRITERMODE_H_ #define MSP_DATAFILE_WRITERMODE_H_ -#include - 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() { }