Debian Codegraph @

src/lisp/writer.cpp

raw symbols SCIP
1 // $Id: writer.cpp 1138 2007-05-30 07:26:50Z hiker $
2 //
3 // TuxKart - a fun racing game with go-kart
4 // Copyright (C) 2004 Matthias Braun <[email protected]>
5 // code in this file based on lispreader from Mark Probst
6 //
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 2
10 // of the License, or (at your option) any later version.
11 //
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.
16 //
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.
20 #include <iostream>
21 #include <stdexcept>
22 #include <fstream>
23 #include <sstream>
24
25 #include "writer.hpp"
26 #include "translation.hpp"
27 #if defined(WIN32) && !defined(__CYGWIN__)
28 # define snprintf _snprintf
29 #endif
30
31 namespace lisp
32 {
33
34 Writer::Writer(const std::string& filename)
35 : m_indent_depth(0)
36 {
37 m_owner = true;
38 #ifdef WIN32
39 // With mingw, the files are written dos style (crlf), but
40 // these files can't be read with the lexer here. So we have
41 // to force the file to be written as binary for windows.
42 printf("Setting binary");
43 m_out = new std::ofstream(filename.c_str(),::std::ios_base::binary);
44 #else
45 m_out = new std::ofstream(filename.c_str());
46 #endif
47 if(!m_out->good())
48 {
49 char msg[MAX_ERROR_MESSAGE_LENGTH];
50 snprintf(msg, sizeof(msg),
51 _("LispWriter Error: Couldn't open file '%s' for writing."),
52 filename.c_str());
53 throw std::runtime_error(msg);
54 }
55 }
56
57 //-----------------------------------------------------------------------------
58
59 Writer::Writer(std::ostream& newout)
60 : m_indent_depth(0)
61 {
62 m_owner = false;
63 m_out = &newout;
64 }
65
66 //-----------------------------------------------------------------------------
67
68 Writer::~Writer()
69 {
70 if(m_lists.size() > 0)
71 {
72 std::cerr << "Warning: Not all sections closed in lispwriter!\n";
73 }
74
75 if(m_owner)
76 delete m_out;
77 }
78
79 //-----------------------------------------------------------------------------
80
81 void
82 Writer::writeComment(const std::string& comment)
83 {
84 indent();
85 *m_out << "; " << comment << "\n";
86 }
87
88 //-----------------------------------------------------------------------------
89
90 void
91 Writer::beginList(const std::string& listname)
92 {
93 indent();
94 *m_out << '(' << listname << '\n';
95 m_indent_depth += 2;
96
97 m_lists.push_back(listname);
98 }
99
100 //-----------------------------------------------------------------------------
101
102 void
103 Writer::endList(const std::string& listname)
104 {
105 if(m_lists.size() == 0)
106 {
107 fprintf(stderr, "Trying to close list '%s, which is not open.\n",
108 listname.c_str());
109 return;
110 }
111 if(m_lists.back() != listname)
112 {
113 fprintf(stderr,
114 "Warning: trying to close list '%s' while list '%s' is open.\n",
115 listname.c_str(), m_lists.back().c_str());
116 return;
117 }
118 m_lists.pop_back();
119
120 m_indent_depth -= 2;
121 indent();
122 *m_out << ")\n";
123 }
124
125 //-----------------------------------------------------------------------------
126
127 void
128 Writer::write(const std::string& name, int value)
129 {
130 indent();
131 *m_out << '(' << name << ' ' << value << ")\n";
132 }
133
134 //-----------------------------------------------------------------------------
135
136 void
137 Writer::write(const std::string& name, float value)
138 {
139 indent();
140 *m_out << '(' << name << ' ' << value << ")\n";
141 }
142
143 //-----------------------------------------------------------------------------
144
145 void
146 Writer::write(const std::string& name, const std::string& value)
147 {
148 indent();
149 *m_out << '(' << name << " \"" << value << "\")\n";
150 }
151
152 //-----------------------------------------------------------------------------
153
154 void
155 Writer::write(const std::string& name, const char* value)
156 {
157 indent();
158 *m_out << '(' << name << " \"" << value << "\")\n";
159 }
160
161 //-----------------------------------------------------------------------------
162
163 void
164 Writer::write(const std::string& name, bool value)
165 {
166 indent();
167 *m_out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
168 }
169
170 //-----------------------------------------------------------------------------
171
172 void
173 Writer::write(const std::string& name, const std::vector<int>& value)
174 {
175 indent();
176 *m_out << '(' << name;
177 for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
178 *m_out << " " << *i;
179 *m_out << ")\n";
180 }
181
182 //-----------------------------------------------------------------------------
183
184 void
185 Writer::write(const std::string& name, const std::vector<unsigned int>& value)
186 {
187 indent();
188 *m_out << '(' << name;
189 for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
190 *m_out << " " << *i;
191 *m_out << ")\n";
192 }
193
194 //-----------------------------------------------------------------------------
195
196 void
197 Writer::indent()
198 {
199 for(int i = 0; i<m_indent_depth; ++i)
200 *m_out << ' ';
201 }
202
203 } // end of namespace lisp