-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2007-2008, 2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
#include <msp/io/print.h>
#include <msp/strings/utils.h>
+#include "output.h"
#include "statement.h"
#include "textwriter.h"
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)
{
{
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(const Value &v: st.args)
{
out.put(' ');
- if(i->get_signature()==StringType::signature)
- IO::print(out, "\"%s\"", c_escape(i->get<StringType::Store>(), false));
- else if(i->get_signature()==BoolType::signature)
- out.write(i->get<BoolType::Store>() ? "true" : "false");
- else if(i->get_signature()==IntType::signature)
- out.write(lexical_cast(i->get<IntType::Store>()));
- else if(i->get_signature()==FloatType::signature)
- out.write(format("%15g", (i->get<FloatType::Store>())));
- else if(i->get_signature()==SymbolType::signature)
+ if(v.get_signature()==StringType::signature)
+ out.write(format("\"%s\"", c_escape(v.get<StringType::Store>(), false)));
+ else if(v.get_signature()==BoolType::signature)
+ out.write(v.get<BoolType::Store>() ? "true" : "false");
+ else if(v.get_signature()==IntType::signature)
+ out.write(lexical_cast<string>(v.get<IntType::Store>()));
+ else if(v.get_signature()==FloatType::signature)
+ out.write(lexical_cast<string>(v.get<FloatType::Store>(), float_format));
+ else if(v.get_signature()==SymbolType::signature)
{
- string name = i->get<SymbolType::Store>().name;
+ string name = v.get<SymbolType::Store>().name;
if(isdigit(name[0]))
out.write("\\"+name);
else
}
if(!st.sub.empty())
{
- IO::print(out, "\n%s{\n", indent);
- for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
- write_(*i, level+1);
- IO::print(out, "%s}", indent);
+ out.write(format("\n%s{\n", indent));
+ for(const Statement &s: st.sub)
+ write_(s, level+1);
+ out.write(format("%s}", indent));
}
- out.write(";\n", 2);
+ out.write(";\n");
}
} // namespace DataFile