X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Ftextwriter.cpp;h=c870ba0c20d7888f19725761924171c6b01662d9;hb=cac0716e8c12088ba833cb28809f5a7b8430a68d;hp=0e5b6742653d80599809d645b77e1f91b9f60675;hpb=7df5e45c7f414f6a07681dc4ec2abb63b091a309;p=libs%2Fdatafile.git diff --git a/source/textwriter.cpp b/source/textwriter.cpp index 0e5b674..c870ba0 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,10 +9,16 @@ using namespace std; namespace Msp { namespace DataFile { -TextWriter::TextWriter(IO::Base &o): - WriterMode(o) +TextWriter::TextWriter(Output &o): + WriterMode(o), + float_format("%#.7g") { } +void TextWriter::set_float_precision(unsigned fp) +{ + float_format = format("%%#.%dg", fp/4-1); +} + void TextWriter::write(const Statement &st) { write_(st, 0); @@ -21,18 +28,18 @@ 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) - out.write(lexical_cast(i->get())); + out.write(lexical_cast(i->get())); else if(i->get_signature()==FloatType::signature) - out.write(format("%15g", (i->get()))); + out.write(format(float_format, i->get())); else if(i->get_signature()==SymbolType::signature) { string name = i->get().name; @@ -44,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