Messaging subsystem rewrite, step I
[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 "msg.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     msg_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)
62 {
63   indent();
64   *out << '(' << listname << '\n';
65   indent_depth += 2;
66
67   lists.push_back(listname);
68 }
69
70 void
71 Writer::end_list(const std::string& listname)
72 {
73   if(lists.size() == 0) {
74     msg_warning << "Trying to close list '" << listname << "', which is not open" << std::endl;
75     return;
76   }
77   if(lists.back() != listname) {
78     msg_warning << "trying to close list '" << listname << "' while list '" << lists.back() << "' is open" << std::endl;
79     return;
80   }
81   lists.pop_back();
82   
83   indent_depth -= 2;
84   indent();
85   *out << ")\n";
86 }
87
88 void
89 Writer::write_int(const std::string& name, int value)
90 {
91   indent();
92   *out << '(' << name << ' ' << value << ")\n";
93 }
94
95 void
96 Writer::write_float(const std::string& name, float value)
97 {
98   indent();
99   *out << '(' << name << ' ' << value << ")\n";
100 }
101
102 void
103 Writer::write_string(const std::string& name, const std::string& value,
104     bool translatable)
105 {
106   indent();
107   *out << '(' << name;
108   if(translatable) {
109     *out << " (_ \"" << value << "\"))\n";
110   } else {
111     *out << " \"" << value << "\")\n";
112   }
113 }
114
115 void
116 Writer::write_bool(const std::string& name, bool value)
117 {
118   indent();
119   *out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
120 }
121
122 void
123 Writer::write_int_vector(const std::string& name,
124     const std::vector<int>& value)
125 {
126   indent();
127   *out << '(' << name;
128   for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
129     *out << " " << *i;
130   *out << ")\n";
131 }
132
133 void
134 Writer::write_int_vector(const std::string& name,
135     const std::vector<unsigned int>& value)
136 {
137   indent();
138   *out << '(' << name;
139   for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
140     *out << " " << *i;
141   *out << ")\n";
142 }
143
144 void
145 Writer::write_float_vector(const std::string& name,
146                            const std::vector<float>& value)
147 {
148   indent();
149   *out << '(' << name;
150   for(std::vector<float>::const_iterator i = value.begin(); i != value.end(); ++i)
151     *out << " " << *i;
152   *out << ")\n";
153 }
154
155 void
156 Writer::indent()
157 {
158   for(int i = 0; i<indent_depth; ++i)
159     *out << ' ';
160 }
161
162 } // end of namespace lisp