]> 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 68111c0d3038c9b6b4b0f1f0a70df6b436d0c167..c870ba0c20d7888f19725761924171c6b01662d9 100644 (file)
@@ -1,10 +1,6 @@
-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2006  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"
 
@@ -13,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);
@@ -26,25 +28,35 @@ void TextWriter::write_(const Statement &st, unsigned level)
 {
        string indent(level, '\t');
 
-       out<<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(ValueArray::const_iterator i = st.args.begin(); i!=st.args.end(); ++i)
        {
-               out<<' ';
-               if(i->get_type()==STRING)
-                       out<<'\"'<<i->get_raw()<<'\"';
-               else if(i->get_type()==BOOLEAN)
-                       out<<(i->get<bool>() ? "true" : "false");
-               else
-                       out<<i->get_raw();
+               out.put(' ');
+               if(i->get_signature()==StringType::signature)
+                       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<string>(i->get<IntType::Store>()));
+               else if(i->get_signature()==FloatType::signature)
+                       out.write(format(float_format, i->get<FloatType::Store>()));
+               else if(i->get_signature()==SymbolType::signature)
+               {
+                       string name = i->get<SymbolType::Store>().name;
+                       if(isdigit(name[0]))
+                               out.write("\\"+name);
+                       else
+                               out.write(name);
+               }
        }
        if(!st.sub.empty())
        {
-               out<<'\n'<<indent<<"{\n";
-               for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
+               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);
-               out<<indent<<'}';
+               out.write(format("%s}", indent));
        }
-       out<<";\n";
+       out.write(";\n");
 }
 
 } // namespace DataFile