forgot to add some files
authorMatthias Braun <matze@braunis.de>
Thu, 20 May 2004 19:57:20 +0000 (19:57 +0000)
committerMatthias Braun <matze@braunis.de>
Thu, 20 May 2004 19:57:20 +0000 (19:57 +0000)
SVN-Revision: 1283

src/lispwriter.cpp [new file with mode: 0644]
src/lispwriter.h [new file with mode: 0644]
src/serializable.h [new file with mode: 0644]

diff --git a/src/lispwriter.cpp b/src/lispwriter.cpp
new file mode 100644 (file)
index 0000000..4221f81
--- /dev/null
@@ -0,0 +1,108 @@
+#include "lispwriter.h"
+#include <iostream>
+
+LispWriter::LispWriter(std::ostream& newout)
+  : out(newout), indent_depth(0)
+{
+}
+
+LispWriter::~LispWriter()
+{
+  if(lists.size() > 0) {
+    std::cerr << "Warning: Not all sections closed in lispwriter!\n";
+  }
+}
+
+void
+LispWriter::writeComment(const std::string& comment)
+{
+  out << "; " << comment << "\n";
+}
+
+void
+LispWriter::startList(const std::string& listname)
+{
+  indent();
+  out << '(' << listname << '\n';
+  indent_depth += 2;
+
+  lists.push_back(listname);
+}
+
+void
+LispWriter::endList(const std::string& listname)
+{
+  if(lists.size() == 0) {
+    std::cerr << "Trying to close list '" << listname 
+              << "', which is not open.\n";
+    return;
+  }
+  if(lists.back() != listname) {
+    std::cerr << "Warning: trying to close list '" << listname 
+              << "' while list '" << lists.back() << "' is open.\n";
+    return;
+  }
+  lists.pop_back();
+  
+  indent_depth -= 2;
+  indent();
+  out << ")\n";
+}
+
+void
+LispWriter::writeInt(const std::string& name, int value)
+{
+  indent();
+  out << '(' << name << ' ' << value << ")\n";
+}
+
+void
+LispWriter::writeFloat(const std::string& name, float value)
+{
+  indent();
+  out << '(' << name << ' ' << value << ")\n";
+}
+
+void
+LispWriter::writeString(const std::string& name, const std::string& value)
+{
+  indent();
+  out << '(' << name << " \"" << value << "\")\n";
+}
+
+void
+LispWriter::writeBool(const std::string& name, bool value)
+{
+  indent();
+  out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
+}
+
+void
+LispWriter::writeIntVector(const std::string& name,
+    const std::vector<int>& value)
+{
+  indent();
+  out << '(' << name;
+  for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
+    out << " " << *i;
+  out << ")\n";
+}
+
+void
+LispWriter::writeIntVector(const std::string& name,
+    const std::vector<unsigned int>& value)
+{
+  indent();
+  out << '(' << name;
+  for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
+    out << " " << *i;
+  out << ")\n";
+}
+
+void
+LispWriter::indent()
+{
+  for(int i = 0; i<indent_depth; ++i)
+    out << ' ';
+}
+
diff --git a/src/lispwriter.h b/src/lispwriter.h
new file mode 100644 (file)
index 0000000..9443b38
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef __LISPWRITER_H__
+#define __LISPWRITER_H__
+
+#include <iostream>
+#include <vector>
+
+class LispWriter
+{
+public:
+  LispWriter(std::ostream& out);
+  ~LispWriter();
+
+  void writeComment(const std::string& comment);
+  
+  void startList(const std::string& listname);
+
+  void writeInt(const std::string& name, int value);
+  void writeFloat(const std::string& name, float value);
+  void writeString(const std::string& name, const std::string& value);
+  void writeBool(const std::string& name, bool value);
+  void writeIntVector(const std::string& name, const std::vector<int>& value);
+  void writeIntVector(const std::string& name, const std::vector<unsigned int>& value);
+  // add more write-functions when needed...
+  
+  void endList(const std::string& listname);
+
+private:
+  void indent();
+    
+  std::ostream& out;
+  int indent_depth;
+  std::vector<std::string> lists;
+};
+
+#endif
+
diff --git a/src/serializable.h b/src/serializable.h
new file mode 100644 (file)
index 0000000..ab6bebc
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef __SERIALIZABLE_H__
+#define __SERIALIZABLE_H__
+
+class LispWriter;
+
+class Serializable
+{
+public:
+  virtual void write(LispWriter& writer) = 0;
+};
+
+#endif
+