Back to index

supertuxkart  0.5+dfsg1
writer.cpp
Go to the documentation of this file.
00001 //  $Id: writer.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  TuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de>
00005 //  code in this file based on lispreader from Mark Probst
00006 //
00007 //  This program is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU General Public License
00009 //  as published by the Free Software Foundation; either version 2
00010 //  of the License, or (at your option) any later version.
00011 //
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 #include <iostream>
00021 #include <stdexcept>
00022 #include <fstream>
00023 #include <sstream>
00024 
00025 #include "writer.hpp"
00026 #include "translation.hpp"
00027 #if defined(WIN32) && !defined(__CYGWIN__)
00028 #  define snprintf _snprintf
00029 #endif
00030 
00031 namespace lisp
00032 {
00033 
00034     Writer::Writer(const std::string& filename)
00035             : m_indent_depth(0)
00036     {
00037         m_owner = true;
00038 #ifdef WIN32
00039        // With mingw, the files are written dos style (crlf), but
00040        // these files can't be read with the lexer here. So we have
00041        // to force the file to be written as binary for windows.
00042         m_out = new std::ofstream(filename.c_str(),::std::ios_base::binary);
00043 #else
00044         m_out = new std::ofstream(filename.c_str());
00045 #endif
00046         if(!m_out->good())
00047         {
00048             char msg[MAX_ERROR_MESSAGE_LENGTH];
00049             snprintf(msg, sizeof(msg), 
00050                      "LispWriter Error: Couldn't open file '%s' for writing.",
00051                      filename.c_str());
00052             throw std::runtime_error(msg);
00053         }
00054     }
00055 
00056 //-----------------------------------------------------------------------------
00057 
00058     Writer::Writer(std::ostream& newout)
00059             : m_indent_depth(0)
00060     {
00061         m_owner = false;
00062         m_out = &newout;
00063     }
00064 
00065 //-----------------------------------------------------------------------------
00066 
00067     Writer::~Writer()
00068     {
00069         if(m_lists.size() > 0)
00070         {
00071             std::cerr << "Warning: Not all sections closed in lispwriter!\n";
00072         }
00073 
00074         if(m_owner)
00075             delete m_out;
00076     }
00077 
00078 //-----------------------------------------------------------------------------
00079 
00080     void
00081     Writer::writeComment(const std::string& comment)
00082     {
00083         indent();
00084         *m_out << "; " << comment << "\n";
00085     }
00086 
00087 //-----------------------------------------------------------------------------
00088 
00089     void
00090     Writer::beginList(const std::string& listname)
00091     {
00092         indent();
00093         *m_out << '(' << listname << '\n';
00094         m_indent_depth += 2;
00095 
00096         m_lists.push_back(listname);
00097     }
00098 
00099 //-----------------------------------------------------------------------------
00100 
00101     void
00102     Writer::endList(const std::string& listname)
00103     {
00104         if(m_lists.size() == 0)
00105         {
00106             fprintf(stderr, "Trying to close list '%s, which is not open.\n",
00107                     listname.c_str());
00108             return;
00109         }
00110         if(m_lists.back() != listname)
00111         {
00112             fprintf(stderr, 
00113                     "Warning: trying to close list '%s' while list '%s' is open.\n",
00114                     listname.c_str(),  m_lists.back().c_str());
00115             return;
00116         }
00117         m_lists.pop_back();
00118 
00119         m_indent_depth -= 2;
00120         indent();
00121         *m_out << ")\n";
00122     }
00123 
00124 //-----------------------------------------------------------------------------
00125 
00126     void
00127     Writer::write(const std::string& name, int value)
00128     {
00129         indent();
00130         *m_out << '(' << name << ' ' << value << ")\n";
00131     }
00132 
00133 //-----------------------------------------------------------------------------
00134 
00135     void
00136     Writer::write(const std::string& name, float value)
00137     {
00138         indent();
00139         *m_out << '(' << name << ' ' << value << ")\n";
00140     }
00141 
00142 //-----------------------------------------------------------------------------
00143 
00144     void
00145     Writer::write(const std::string& name, const std::string& value)
00146     {
00147         indent();
00148         *m_out << '(' << name << " \"" << value << "\")\n";
00149     }
00150 
00151 //-----------------------------------------------------------------------------
00152 
00153     void
00154     Writer::write(const std::string& name, const char* value)
00155     {
00156         indent();
00157         *m_out << '(' << name << " \"" << value << "\")\n";
00158     }
00159 
00160 //-----------------------------------------------------------------------------
00161 
00162     void
00163     Writer::write(const std::string& name, bool value)
00164     {
00165         indent();
00166         *m_out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
00167     }
00168 
00169 //-----------------------------------------------------------------------------
00170 
00171     void
00172     Writer::write(const std::string& name, const std::vector<int>& value)
00173     {
00174         indent();
00175         *m_out << '(' << name;
00176         for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
00177             *m_out << " " << *i;
00178         *m_out << ")\n";
00179     }
00180 
00181 //-----------------------------------------------------------------------------
00182 
00183     void
00184     Writer::write(const std::string& name, const std::vector<unsigned int>& value)
00185     {
00186         indent();
00187         *m_out << '(' << name;
00188         for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
00189             *m_out << " " << *i;
00190         *m_out << ")\n";
00191     }
00192 
00193 //-----------------------------------------------------------------------------
00194        
00195        void
00196     Writer::write(const std::string& name, const std::vector<std::string>& value)
00197     {
00198         indent();
00199         *m_out << '(' << name;
00200         for(std::vector<std::string>::const_iterator i = value.begin(); i != value.end(); ++i)
00201             *m_out << " " << "\"" << *i << "\"";
00202         *m_out << ")\n";
00203     }
00204 
00205 //-----------------------------------------------------------------------------
00206     
00207     void
00208     Writer::indent()
00209     {
00210         for(int i = 0; i<m_indent_depth; ++i)
00211             *m_out << ' ';
00212     }
00213 
00214 } // end of namespace lisp