X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftextwriter.cpp;h=f37b7339e462f35f1cc5461c60dbb60a8a8a3b4b;hb=b39ce68f12c30eedb272b65fe78baec5864d89ca;hp=def744c33b06ff945c4cfccbe28025bb43402872;hpb=2f79370bffe0bac865dc97c5114dc87c1936fbb4;p=libs%2Fdatafile.git diff --git a/source/textwriter.cpp b/source/textwriter.cpp index def744c..f37b733 100644 --- a/source/textwriter.cpp +++ b/source/textwriter.cpp @@ -1,11 +1,6 @@ -/* $Id$ - -This file is part of libmspdatafile -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - +#include #include +#include "output.h" #include "statement.h" #include "textwriter.h" @@ -14,9 +9,16 @@ using namespace std; namespace Msp { namespace DataFile { -TextWriter::TextWriter(ostream &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) { @@ -27,25 +29,35 @@ void TextWriter::write_(const Statement &st, unsigned level) { string indent(level, '\t'); - out<get_type()==STRING) - out<<'\"'<get_raw(), false)<<'\"'; - else if(i->get_type()==BOOLEAN) - out<<(i->get() ? "true" : "false"); - else - out<get_raw(); + out.put(' '); + if(i->get_signature()==StringType::signature) + 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(lexical_cast(i->get(), float_format)); + else if(i->get_signature()==SymbolType::signature) + { + string name = i->get().name; + if(isdigit(name[0])) + out.write("\\"+name); + else + out.write(name); + } } if(!st.sub.empty()) { - out<<'\n'<::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i) + out.write(format("\n%s{\n", indent)); + for(list::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i) write_(*i, level+1); - out<