fade out console
[supertux.git] / src / lisp / writer.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <iostream>
23
24 #include "writer.hpp"
25 #include "physfs/physfs_stream.hpp"
26 #include "log.hpp"
27
28 namespace lisp
29 {
30
31 Writer::Writer(const std::string& filename)
32 {
33   out = new OFileStream(filename);
34   out_owned = true;
35   indent_depth = 0;
36 }
37   
38 Writer::Writer(std::ostream* newout)
39 {
40   out = newout;
41   out_owned = false;
42   indent_depth = 0;
43 }
44
45 Writer::~Writer()
46 {
47   if(lists.size() > 0) {
48     log_warning << "Not all sections closed in lispwriter" << std::endl;
49   }
50   if(out_owned)
51     delete out;
52 }
53
54 void
55 Writer::write_comment(const std::string& comment)
56 {
57   *out << "; " << comment << "\n";
58 }
59
60 void
61 Writer::start_list(const std::string& listname, bool string)
62 {
63   indent();
64   *out << '(';
65   if(string)
66     write_escaped_string(listname);
67   else
68     *out << listname;
69   *out << '\n';
70   indent_depth += 2;
71
72   lists.push_back(listname);
73 }
74
75 void
76 Writer::end_list(const std::string& listname)
77 {
78   if(lists.size() == 0) {
79     log_warning << "Trying to close list '" << listname << "', which is not open" << std::endl;
80     return;
81   }
82   if(lists.back() != listname) {
83     log_warning << "trying to close list '" << listname << "' while list '" << lists.back() << "' is open" << std::endl;
84     return;
85   }
86   lists.pop_back();
87   
88   indent_depth -= 2;
89   indent();
90   *out << ")\n";
91 }
92
93 void
94 Writer::write_int(const std::string& name, int value)
95 {
96   indent();
97   *out << '(' << name << ' ' << value << ")\n";
98 }
99
100 void
101 Writer::write_float(const std::string& name, float value)
102 {
103   indent();
104   *out << '(' << name << ' ' << value << ")\n";
105 }
106
107 void
108 Writer::write_string(const std::string& name, const std::string& value,
109     bool translatable)
110 {
111   indent();
112   *out << '(' << name;
113   if(translatable) {
114     *out << " (_ ";
115     write_escaped_string(value);
116     *out << "))\n";
117   } else {
118     *out << " ";
119     write_escaped_string(value);
120     *out << ")\n";
121   }
122 }
123
124 void
125 Writer::write_bool(const std::string& name, bool value)
126 {
127   indent();
128   *out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
129 }
130
131 void
132 Writer::write_int_vector(const std::string& name,
133     const std::vector<int>& value)
134 {
135   indent();
136   *out << '(' << name;
137   for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
138     *out << " " << *i;
139   *out << ")\n";
140 }
141
142 void
143 Writer::write_int_vector(const std::string& name,
144     const std::vector<unsigned int>& value)
145 {
146   indent();
147   *out << '(' << name;
148   for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
149     *out << " " << *i;
150   *out << ")\n";
151 }
152
153 void
154 Writer::write_float_vector(const std::string& name,
155                            const std::vector<float>& value)
156 {
157   indent();
158   *out << '(' << name;
159   for(std::vector<float>::const_iterator i = value.begin(); i != value.end(); ++i)
160     *out << " " << *i;
161   *out << ")\n";
162 }
163
164 void
165 Writer::write_escaped_string(const std::string& str)
166 {
167   *out << '"';
168   for(const char* c = str.c_str(); *c != 0; ++c) {
169     if(*c == '\"')
170       *out << "\\\"";
171     else if(*c == '\\')
172       *out << "\\\\";
173     else
174       *out << *c;
175   }
176   *out << '"';
177 }
178
179 void
180 Writer::indent()
181 {
182   for(int i = 0; i<indent_depth; ++i)
183     *out << ' ';
184 }
185
186 } // end of namespace lisp