-converted remaining classes to GameObject
[supertux.git] / src / lispwriter.cpp
1 #include "lispwriter.h"
2 #include <iostream>
3
4 LispWriter::LispWriter(std::ostream& newout)
5   : out(newout), indent_depth(0)
6 {
7 }
8
9 LispWriter::~LispWriter()
10 {
11   if(lists.size() > 0) {
12     std::cerr << "Warning: Not all sections closed in lispwriter!\n";
13   }
14 }
15
16 void
17 LispWriter::write_comment(const std::string& comment)
18 {
19   out << "; " << comment << "\n";
20 }
21
22 void
23 LispWriter::start_list(const std::string& listname)
24 {
25   indent();
26   out << '(' << listname << '\n';
27   indent_depth += 2;
28
29   lists.push_back(listname);
30 }
31
32 void
33 LispWriter::end_list(const std::string& listname)
34 {
35   if(lists.size() == 0) {
36     std::cerr << "Trying to close list '" << listname 
37               << "', which is not open.\n";
38     return;
39   }
40   if(lists.back() != listname) {
41     std::cerr << "Warning: trying to close list '" << listname 
42               << "' while list '" << lists.back() << "' is open.\n";
43     return;
44   }
45   lists.pop_back();
46   
47   indent_depth -= 2;
48   indent();
49   out << ")\n";
50 }
51
52 void
53 LispWriter::write_int(const std::string& name, int value)
54 {
55   indent();
56   out << '(' << name << ' ' << value << ")\n";
57 }
58
59 void
60 LispWriter::write_float(const std::string& name, float value)
61 {
62   indent();
63   out << '(' << name << ' ' << value << ")\n";
64 }
65
66 void
67 LispWriter::write_string(const std::string& name, const std::string& value)
68 {
69   indent();
70   out << '(' << name << " \"" << value << "\")\n";
71 }
72
73 void
74 LispWriter::write_bool(const std::string& name, bool value)
75 {
76   indent();
77   out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
78 }
79
80 void
81 LispWriter::write_int_vector(const std::string& name,
82     const std::vector<int>& value)
83 {
84   indent();
85   out << '(' << name;
86   for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
87     out << " " << *i;
88   out << ")\n";
89 }
90
91 void
92 LispWriter::write_int_vector(const std::string& name,
93     const std::vector<unsigned int>& value)
94 {
95   indent();
96   out << '(' << name;
97   for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
98     out << " " << *i;
99   out << ")\n";
100 }
101
102 void
103 LispWriter::indent()
104 {
105   for(int i = 0; i<indent_depth; ++i)
106     out << ' ';
107 }
108