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