]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/textwriter.cpp
Add binary data format
[libs/datafile.git] / source / textwriter.cpp
diff --git a/source/textwriter.cpp b/source/textwriter.cpp
new file mode 100644 (file)
index 0000000..68111c0
--- /dev/null
@@ -0,0 +1,51 @@
+/* $Id$
+
+This file is part of libmspdatafile
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "statement.h"
+#include "textwriter.h"
+
+using namespace std;
+
+namespace Msp {
+namespace DataFile {
+
+TextWriter::TextWriter(ostream &o):
+       WriterMode(o)
+{ }
+
+void TextWriter::write(const Statement &st)
+{
+       write_(st, 0);
+}
+
+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<<' ';
+               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();
+       }
+       if(!st.sub.empty())
+       {
+               out<<'\n'<<indent<<"{\n";
+               for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
+                       write_(*i, level+1);
+               out<<indent<<'}';
+       }
+       out<<";\n";
+}
+
+} // namespace DataFile
+} // namespace Msp