X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Ftextwriter.cpp;h=921842279f133eabf555d57d97e609d563dacb72;hp=08909a1c6ed952b190754063d628f4e0e3dbe3dc;hb=9d97c6c5d3f125dd0ed23ccfa4bf0bb728a753fa;hpb=a582163d380833b1370ba067a1fd0ad5c2984723 diff --git a/source/textwriter.cpp b/source/textwriter.cpp index 08909a1..9218422 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,9 +9,16 @@ using namespace std; namespace Msp { namespace DataFile { -TextWriter::TextWriter(IO::Base &o): +TextWriter::TextWriter(Output &o): WriterMode(o) -{ } +{ + float_format.showpoint().precision(7); +} + +void TextWriter::set_float_precision(unsigned fp) +{ + float_format.precision(fp/4-1); +} void TextWriter::write(const Statement &st) { @@ -28,29 +29,35 @@ void TextWriter::write_(const Statement &st, unsigned level) { string indent(level, '\t'); - IO::print(out, "%s%s", indent, st.keyword); - for(ValueArray::const_iterator i = st.args.begin(); i!=st.args.end(); ++i) + out.write(format("%s%s", indent, st.keyword)); + for(vector::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(lexical_cast(i->get(), float_format)); else if(i->get_signature()==SymbolType::signature) - out.write(i->get().name); + { + string name = i->get().name; + if(isdigit(name[0])) + out.write("\\"+name); + else + out.write(name); + } } 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