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/buffered_ofile_stream.hpp"
20 #include "util/log.hpp"
24 Writer::Writer(const std::string& filename) :
30 BufferedOFileStream* filestream = new BufferedOFileStream(filename);
31 out = filestream->get_stream();
37 Writer::Writer(std::ostream* newout) :
51 if(lists.size() > 0) {
52 log_warning << "Not all sections closed in lispwriter" << std::endl;
59 Writer::write_comment(const std::string& comment)
61 *out << "; " << comment << "\n";
65 Writer::start_list(const std::string& listname, bool string)
70 write_escaped_string(listname);
76 lists.push_back(listname);
80 Writer::end_list(const std::string& listname)
82 if(lists.size() == 0) {
83 log_warning << "Trying to close list '" << listname << "', which is not open" << std::endl;
86 if(lists.back() != listname) {
87 log_warning << "trying to close list '" << listname << "' while list '" << lists.back() << "' is open" << std::endl;
98 Writer::write(const std::string& name, int value)
101 *out << '(' << name << ' ' << value << ")\n";
105 Writer::write(const std::string& name, float value)
108 *out << '(' << name << ' ' << value << ")\n";
112 Writer::write(const std::string& name, const std::string& value,
119 write_escaped_string(value);
123 write_escaped_string(value);
129 Writer::write(const std::string& name, bool value)
132 *out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
136 Writer::write(const std::string& name,
137 const std::vector<int>& value)
141 for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
147 Writer::write(const std::string& name,
148 const std::vector<unsigned int>& value)
152 for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
158 Writer::write(const std::string& name,
159 const std::vector<float>& value)
163 for(std::vector<float>::const_iterator i = value.begin(); i != value.end(); ++i)
169 Writer::write(const std::string& name,
170 const std::vector<std::string>& value)
174 for(std::vector<std::string>::const_iterator i = value.begin(); i != value.end(); ++i) {
176 write_escaped_string(*i);
182 Writer::write_escaped_string(const std::string& str)
185 for(const char* c = str.c_str(); *c != 0; ++c) {
199 for(int i = 0; i<indent_depth; ++i)
203 } // end of namespace lisp