]> git.tdb.fi Git - libs/datafile.git/blob - source/textwriter.cpp
Fix binary format
[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/strings/utils.h>
9 #include "statement.h"
10 #include "textwriter.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace DataFile {
16
17 TextWriter::TextWriter(ostream &o):
18         WriterMode(o)
19 { }
20
21 void TextWriter::write(const Statement &st)
22 {
23         write_(st, 0);
24 }
25
26 void TextWriter::write_(const Statement &st, unsigned level)
27 {
28         string indent(level, '\t');
29
30         out<<indent<<st.keyword;
31         for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
32         {
33                 out<<' ';
34                 if(i->get_type()==STRING)
35                         out<<'\"'<<c_escape(i->get_raw(), false)<<'\"';
36                 else if(i->get_type()==BOOLEAN)
37                         out<<(i->get<bool>() ? "true" : "false");
38                 else
39                         out<<i->get_raw();
40         }
41         if(!st.sub.empty())
42         {
43                 out<<'\n'<<indent<<"{\n";
44                 for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
45                         write_(*i, level+1);
46                 out<<indent<<'}';
47         }
48         out<<";\n";
49 }
50
51 } // namespace DataFile
52 } // namespace Msp