]> git.tdb.fi Git - libs/datafile.git/blob - source/textwriter.cpp
Style update: add spaces around assignments
[libs/datafile.git] / source / textwriter.cpp
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006  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_type()==STRING)
36                         IO::print(out, "\"%s\"", c_escape(i->get_raw(), false));
37                 else if(i->get_type()==BOOLEAN)
38                         out.write(i->get<bool>() ? "true" : "false");
39                 else
40                         out.write(i->get_raw());
41         }
42         if(!st.sub.empty())
43         {
44                 IO::print(out, "\n%s{\n", indent);
45                 for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
46                         write_(*i, level+1);
47                 IO::print(out, "%s}", indent);
48         }
49         out.write(";\n", 2);
50 }
51
52 } // namespace DataFile
53 } // namespace Msp