2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "lisp/writer.hpp"
19 #include "physfs/ofile_stream.hpp"
20 #include "util/log.hpp"
24 Writer::Writer(const std::string& filename) :
30 out = new OFileStream(filename);
36 Writer::Writer(std::ostream* newout) :
50 if(lists.size() > 0) {
51 log_warning << "Not all sections closed in lispwriter" << std::endl;
58 Writer::write_comment(const std::string& comment)
60 *out << "; " << comment << "\n";
64 Writer::start_list(const std::string& listname, bool string)
69 write_escaped_string(listname);
75 lists.push_back(listname);
79 Writer::end_list(const std::string& listname)
81 if(lists.size() == 0) {
82 log_warning << "Trying to close list '" << listname << "', which is not open" << std::endl;
85 if(lists.back() != listname) {
86 log_warning << "trying to close list '" << listname << "' while list '" << lists.back() << "' is open" << std::endl;
97 Writer::write(const std::string& name, int value)
100 *out << '(' << name << ' ' << value << ")\n";
104 Writer::write(const std::string& name, float value)
107 *out << '(' << name << ' ' << value << ")\n";
111 Writer::write(const std::string& name, const std::string& value,
118 write_escaped_string(value);
122 write_escaped_string(value);
128 Writer::write(const std::string& name, bool value)
131 *out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
135 Writer::write(const std::string& name,
136 const std::vector<int>& value)
140 for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
146 Writer::write(const std::string& name,
147 const std::vector<unsigned int>& value)
151 for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
157 Writer::write(const std::string& name,
158 const std::vector<float>& value)
162 for(std::vector<float>::const_iterator i = value.begin(); i != value.end(); ++i)
168 Writer::write(const std::string& name,
169 const std::vector<std::string>& value)
173 for(std::vector<std::string>::const_iterator i = value.begin(); i != value.end(); ++i) {
175 write_escaped_string(*i);
181 Writer::write_escaped_string(const std::string& str)
184 for(const char* c = str.c_str(); *c != 0; ++c) {
198 for(int i = 0; i<indent_depth; ++i)
202 } // end of namespace lisp