- Reworked Surface class and drawing stuff:
[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
27 namespace lisp
28 {
29
30 Writer::Writer(const std::string& filename)
31 {
32   out = new OFileStream(filename);
33   out_owned = true;
34   indent_depth = 0;
35 }
36   
37 Writer::Writer(std::ostream* newout)
38 {
39   out = newout;
40   out_owned = false;
41   indent_depth = 0;
42 }
43
44 Writer::~Writer()
45 {
46   if(lists.size() > 0) {
47     std::cerr << "Warning: Not all sections closed in lispwriter!\n";
48   }
49   if(out_owned)
50     delete out;
51 }
52
53 void
54 Writer::write_comment(const std::string& comment)
55 {
56   *out << "; " << comment << "\n";
57 }
58
59 void
60 Writer::start_list(const std::string& listname)
61 {
62   indent();
63   *out << '(' << listname << '\n';
64   indent_depth += 2;
65
66   lists.push_back(listname);
67 }
68
69 void
70 Writer::end_list(const std::string& listname)
71 {
72   if(lists.size() == 0) {
73     std::cerr << "Trying to close list '" << listname 
74               << "', which is not open.\n";
75     return;
76   }
77   if(lists.back() != listname) {
78     std::cerr << "Warning: trying to close list '" << listname 
79               << "' while list '" << lists.back() << "' is open.\n";
80     return;
81   }
82   lists.pop_back();
83   
84   indent_depth -= 2;
85   indent();
86   *out << ")\n";
87 }
88
89 void
90 Writer::write_int(const std::string& name, int value)
91 {
92   indent();
93   *out << '(' << name << ' ' << value << ")\n";
94 }
95
96 void
97 Writer::write_float(const std::string& name, float value)
98 {
99   indent();
100   *out << '(' << name << ' ' << value << ")\n";
101 }
102
103 void
104 Writer::write_string(const std::string& name, const std::string& value,
105     bool translatable)
106 {
107   indent();
108   *out << '(' << name;
109   if(translatable) {
110     *out << " (_ \"" << value << "\"))\n";
111   } else {
112     *out << " \"" << value << "\")\n";
113   }
114 }
115
116 void
117 Writer::write_bool(const std::string& name, bool value)
118 {
119   indent();
120   *out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
121 }
122
123 void
124 Writer::write_int_vector(const std::string& name,
125     const std::vector<int>& value)
126 {
127   indent();
128   *out << '(' << name;
129   for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
130     *out << " " << *i;
131   *out << ")\n";
132 }
133
134 void
135 Writer::write_int_vector(const std::string& name,
136     const std::vector<unsigned int>& value)
137 {
138   indent();
139   *out << '(' << name;
140   for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
141     *out << " " << *i;
142   *out << ")\n";
143 }
144
145 void
146 Writer::write_float_vector(const std::string& name,
147                            const std::vector<float>& value)
148 {
149   indent();
150   *out << '(' << name;
151   for(std::vector<float>::const_iterator i = value.begin(); i != value.end(); ++i)
152     *out << " " << *i;
153   *out << ")\n";
154 }
155
156 void
157 Writer::indent()
158 {
159   for(int i = 0; i<indent_depth; ++i)
160     *out << ' ';
161 }
162
163 } // end of namespace lisp