X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Ftextwriter.cpp;h=5212c9760488330de78807ab7e4eb97bc3727827;hp=eb3aa2d2f6ec778b0411228e6ac6b2f21533e3ce;hb=8e3fad222e174b7c659fd3d994d54314657ed989;hpb=e5d760ccfaaa01884be2424b62e47a24466e0c4b diff --git a/source/textwriter.cpp b/source/textwriter.cpp index eb3aa2d..5212c97 100644 --- a/source/textwriter.cpp +++ b/source/textwriter.cpp @@ -1,12 +1,6 @@ -/* $Id$ - -This file is part of libmspdatafile -Copyright © 2007-2008, 2010 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include #include +#include "output.h" #include "statement.h" #include "textwriter.h" @@ -15,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); @@ -28,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())); 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; @@ -51,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