X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Ftextwriter.cpp;h=5212c9760488330de78807ab7e4eb97bc3727827;hp=def744c33b06ff945c4cfccbe28025bb43402872;hb=8e3fad222e174b7c659fd3d994d54314657ed989;hpb=2f79370bffe0bac865dc97c5114dc87c1936fbb4 diff --git a/source/textwriter.cpp b/source/textwriter.cpp index def744c..5212c97 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,10 +9,16 @@ using namespace std; namespace Msp { namespace DataFile { -TextWriter::TextWriter(ostream &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); @@ -27,25 +28,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(format(float_format, i->get())); + 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<