1
// $Id: writer.cpp 3034 2009-01-23 05:23:22Z hikerstk $
3
// TuxKart - a fun racing game with go-kart
4
// Copyright (C) 2004 Matthias Braun <matze@braunis.de>
5
// code in this file based on lispreader from Mark Probst
7
// This program is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU General Public License
9
// as published by the Free Software Foundation; either version 3
10
// of the License, or (at your option) any later version.
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
17
// You should have received a copy of the GNU General Public License
18
// along with this program; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
#include "lisp/writer.hpp"
26
#include "utils/translation.hpp"
31
Writer::Writer(const std::string& filename)
36
// With mingw, the files are written dos style (crlf), but
37
// these files can't be read with the lexer here. So we have
38
// to force the file to be written as binary for windows.
39
m_out = new std::ofstream(filename.c_str(),::std::ios_base::binary);
41
m_out = new std::ofstream(filename.c_str());
45
std::ostringstream msg;
46
msg << "LispWriter Error: Couldn't open file '" << filename
48
throw std::runtime_error(msg.str());
52
//-----------------------------------------------------------------------------
54
Writer::Writer(std::ostream& newout)
61
//-----------------------------------------------------------------------------
65
if(m_lists.size() > 0)
67
std::cerr << "Warning: Not all sections closed in lispwriter!\n";
74
//-----------------------------------------------------------------------------
77
Writer::writeComment(const std::string& comment)
80
*m_out << "; " << comment << "\n";
83
//-----------------------------------------------------------------------------
86
Writer::beginList(const std::string& listname)
89
*m_out << '(' << listname << '\n';
92
m_lists.push_back(listname);
95
//-----------------------------------------------------------------------------
98
Writer::endList(const std::string& listname)
100
if(m_lists.size() == 0)
102
fprintf(stderr, "Trying to close list '%s, which is not open.\n",
106
if(m_lists.back() != listname)
109
"Warning: trying to close list '%s' while list '%s' is open.\n",
110
listname.c_str(), m_lists.back().c_str());
120
//-----------------------------------------------------------------------------
123
Writer::write(const std::string& name, int value)
126
*m_out << '(' << name << ' ' << value << ")\n";
129
//-----------------------------------------------------------------------------
132
Writer::write(const std::string& name, float value)
135
*m_out << '(' << name << ' ' << value << ")\n";
138
//-----------------------------------------------------------------------------
141
Writer::write(const std::string& name, const std::string& value)
144
*m_out << '(' << name << " \"" << value << "\")\n";
147
//-----------------------------------------------------------------------------
150
Writer::write(const std::string& name, const char* value)
153
*m_out << '(' << name << " \"" << value << "\")\n";
156
//-----------------------------------------------------------------------------
159
Writer::write(const std::string& name, bool value)
162
*m_out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
165
//-----------------------------------------------------------------------------
168
Writer::write(const std::string& name, const std::vector<int>& value)
171
*m_out << '(' << name;
172
for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
177
//-----------------------------------------------------------------------------
180
Writer::write(const std::string& name, const std::vector<unsigned int>& value)
183
*m_out << '(' << name;
184
for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
189
//-----------------------------------------------------------------------------
192
Writer::write(const std::string& name, const std::vector<std::string>& value)
195
*m_out << '(' << name;
196
for(std::vector<std::string>::const_iterator i = value.begin(); i != value.end(); ++i)
197
*m_out << " " << "\"" << *i << "\"";
201
//-----------------------------------------------------------------------------
206
for(int i = 0; i<m_indent_depth; ++i)
210
} // end of namespace lisp