]> git.tdb.fi Git - libs/datafile.git/blob - source/textwriter.cpp
Rewrite the type system
[libs/datafile.git] / source / textwriter.cpp
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2007-2008, 2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/io/print.h>
9 #include <msp/strings/utils.h>
10 #include "statement.h"
11 #include "textwriter.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace DataFile {
17
18 TextWriter::TextWriter(IO::Base &o):
19         WriterMode(o)
20 { }
21
22 void TextWriter::write(const Statement &st)
23 {
24         write_(st, 0);
25 }
26
27 void TextWriter::write_(const Statement &st, unsigned level)
28 {
29         string indent(level, '\t');
30
31         IO::print(out, "%s%s", indent, st.keyword);
32         for(ValueArray::const_iterator i = st.args.begin(); i!=st.args.end(); ++i)
33         {
34                 out.put(' ');
35                 if(i->get_signature()==StringType::signature)
36                         IO::print(out, "\"%s\"", c_escape(i->get<StringType::Store>(), false));
37                 else if(i->get_signature()==BoolType::signature)
38                         out.write(i->get<BoolType::Store>() ? "true" : "false");
39                 else if(i->get_signature()==IntType::signature)
40                         out.write(lexical_cast(i->get<IntType::Store>()));
41                 else if(i->get_signature()==FloatType::signature)
42                         out.write(format("%15g", (i->get<FloatType::Store>())));
43                 else if(i->get_signature()==SymbolType::signature)
44                         out.write(i->get<SymbolType::Store>().name);
45         }
46         if(!st.sub.empty())
47         {
48                 IO::print(out, "\n%s{\n", indent);
49                 for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
50                         write_(*i, level+1);
51                 IO::print(out, "%s}", indent);
52         }
53         out.write(";\n", 2);
54 }
55
56 } // namespace DataFile
57 } // namespace Msp