]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/textwriter.cpp
Add the target type to lexical_cast invocations
[libs/datafile.git] / source / textwriter.cpp
index 0e5b6742653d80599809d645b77e1f91b9f60675..c870ba0c20d7888f19725761924171c6b01662d9 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/io/print.h>
 #include <msp/strings/utils.h>
+#include "output.h"
 #include "statement.h"
 #include "textwriter.h"
 
@@ -8,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);
@@ -21,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<StringType::Store>(), false));
+                       out.write(format("\"%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>()));
+                       out.write(lexical_cast<string>(i->get<IntType::Store>()));
                else if(i->get_signature()==FloatType::signature)
-                       out.write(format("%15g", (i->get<FloatType::Store>())));
+                       out.write(format(float_format, i->get<FloatType::Store>()));
                else if(i->get_signature()==SymbolType::signature)
                {
                        string name = i->get<SymbolType::Store>().name;
@@ -44,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<Statement>::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