~ubuntu-branches/ubuntu/precise/supertuxkart/precise

« back to all changes in this revision

Viewing changes to src/lisp/writer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2011-02-24 22:36:25 UTC
  • mfrom: (1.1.9 upstream) (6.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110224223625-ygrjfpg92obovuch
Tags: 0.7+dfsg1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//  $Id: writer.cpp 3034 2009-01-23 05:23:22Z hikerstk $
2
 
//
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
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 3
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 "lisp/writer.hpp"
26
 
#include "utils/translation.hpp"
27
 
 
28
 
namespace lisp
29
 
{
30
 
 
31
 
    Writer::Writer(const std::string& filename)
32
 
            : m_indent_depth(0)
33
 
    {
34
 
        m_owner = true;
35
 
#ifdef WIN32
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);
40
 
#else
41
 
        m_out = new std::ofstream(filename.c_str());
42
 
#endif
43
 
        if(!m_out->good())
44
 
        {
45
 
            std::ostringstream msg;
46
 
            msg << "LispWriter Error: Couldn't open file '" << filename
47
 
                << "' for writing.";
48
 
            throw std::runtime_error(msg.str());
49
 
        }
50
 
    }
51
 
 
52
 
//-----------------------------------------------------------------------------
53
 
 
54
 
    Writer::Writer(std::ostream& newout)
55
 
            : m_indent_depth(0)
56
 
    {
57
 
        m_owner = false;
58
 
        m_out = &newout;
59
 
    }
60
 
 
61
 
//-----------------------------------------------------------------------------
62
 
 
63
 
    Writer::~Writer()
64
 
    {
65
 
        if(m_lists.size() > 0)
66
 
        {
67
 
            std::cerr << "Warning: Not all sections closed in lispwriter!\n";
68
 
        }
69
 
 
70
 
        if(m_owner)
71
 
            delete m_out;
72
 
    }
73
 
 
74
 
//-----------------------------------------------------------------------------
75
 
 
76
 
    void
77
 
    Writer::writeComment(const std::string& comment)
78
 
    {
79
 
        indent();
80
 
        *m_out << "; " << comment << "\n";
81
 
    }
82
 
 
83
 
//-----------------------------------------------------------------------------
84
 
 
85
 
    void
86
 
    Writer::beginList(const std::string& listname)
87
 
    {
88
 
        indent();
89
 
        *m_out << '(' << listname << '\n';
90
 
        m_indent_depth += 2;
91
 
 
92
 
        m_lists.push_back(listname);
93
 
    }
94
 
 
95
 
//-----------------------------------------------------------------------------
96
 
 
97
 
    void
98
 
    Writer::endList(const std::string& listname)
99
 
    {
100
 
        if(m_lists.size() == 0)
101
 
        {
102
 
            fprintf(stderr, "Trying to close list '%s, which is not open.\n",
103
 
                    listname.c_str());
104
 
            return;
105
 
        }
106
 
        if(m_lists.back() != listname)
107
 
        {
108
 
            fprintf(stderr, 
109
 
                    "Warning: trying to close list '%s' while list '%s' is open.\n",
110
 
                    listname.c_str(),  m_lists.back().c_str());
111
 
            return;
112
 
        }
113
 
        m_lists.pop_back();
114
 
 
115
 
        m_indent_depth -= 2;
116
 
        indent();
117
 
        *m_out << ")\n";
118
 
    }
119
 
 
120
 
//-----------------------------------------------------------------------------
121
 
 
122
 
    void
123
 
    Writer::write(const std::string& name, int value)
124
 
    {
125
 
        indent();
126
 
        *m_out << '(' << name << ' ' << value << ")\n";
127
 
    }
128
 
 
129
 
//-----------------------------------------------------------------------------
130
 
 
131
 
    void
132
 
    Writer::write(const std::string& name, float value)
133
 
    {
134
 
        indent();
135
 
        *m_out << '(' << name << ' ' << value << ")\n";
136
 
    }
137
 
 
138
 
//-----------------------------------------------------------------------------
139
 
 
140
 
    void
141
 
    Writer::write(const std::string& name, const std::string& value)
142
 
    {
143
 
        indent();
144
 
        *m_out << '(' << name << " \"" << value << "\")\n";
145
 
    }
146
 
 
147
 
//-----------------------------------------------------------------------------
148
 
 
149
 
    void
150
 
    Writer::write(const std::string& name, const char* value)
151
 
    {
152
 
        indent();
153
 
        *m_out << '(' << name << " \"" << value << "\")\n";
154
 
    }
155
 
 
156
 
//-----------------------------------------------------------------------------
157
 
 
158
 
    void
159
 
    Writer::write(const std::string& name, bool value)
160
 
    {
161
 
        indent();
162
 
        *m_out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
163
 
    }
164
 
 
165
 
//-----------------------------------------------------------------------------
166
 
 
167
 
    void
168
 
    Writer::write(const std::string& name, const std::vector<int>& value)
169
 
    {
170
 
        indent();
171
 
        *m_out << '(' << name;
172
 
        for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
173
 
            *m_out << " " << *i;
174
 
        *m_out << ")\n";
175
 
    }
176
 
 
177
 
//-----------------------------------------------------------------------------
178
 
 
179
 
    void
180
 
    Writer::write(const std::string& name, const std::vector<unsigned int>& value)
181
 
    {
182
 
        indent();
183
 
        *m_out << '(' << name;
184
 
        for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
185
 
            *m_out << " " << *i;
186
 
        *m_out << ")\n";
187
 
    }
188
 
 
189
 
//-----------------------------------------------------------------------------
190
 
        
191
 
        void
192
 
    Writer::write(const std::string& name, const std::vector<std::string>& value)
193
 
    {
194
 
        indent();
195
 
        *m_out << '(' << name;
196
 
        for(std::vector<std::string>::const_iterator i = value.begin(); i != value.end(); ++i)
197
 
            *m_out << " " << "\"" << *i << "\"";
198
 
        *m_out << ")\n";
199
 
    }
200
 
 
201
 
//-----------------------------------------------------------------------------
202
 
    
203
 
    void
204
 
    Writer::indent()
205
 
    {
206
 
        for(int i = 0; i<m_indent_depth; ++i)
207
 
            *m_out << ' ';
208
 
    }
209
 
 
210
 
} // end of namespace lisp