Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

switches.cpp

Go to the documentation of this file.
00001 // libdoomwad: manipulates Doom wad files.
00002 // Copyright (C) 2005  John Gaughan
00003 //
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 //
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 //
00018 // This library is distributed with the full text of the LGPL. Please see
00019 // the accompanying file named COPYING.
00020 // 
00021 // You may contact the author at john@johngaughan.net
00022 
00028 // C++ required.
00029 #if !defined __cplusplus
00030 #error C++ compiler required
00031 #endif
00032 
00033 // C++ headers
00034 #include <exception>
00035 #include <sstream>
00036 #include <stdexcept>
00037 #include <string>
00038 #include <vector>
00039 
00040 #include <cstring>
00041 
00042 // Doom headers
00043 #include "global.hpp"
00044 #include "lump.hpp"
00045 #include "switches.hpp"
00046 #include "wadentry.hpp"
00047 
00048 using namespace Doomwad;
00049 
00050 const std::string Switches::NAME = "SWITCHES";
00051 
00052 const int16 Switch::SHAREWARE = 0x0001;
00053 const int16 Switch::DOOM      = 0x0002;
00054 const int16 Switch::DOOM2     = 0x0003;
00055 const int16 Switch::TERMINATE = 0x0000;
00056 const int16 Switch::DEFAULT   = Switch::DOOM2;
00057 const size_t Switch::LENGTH   = 0x00000014;
00058 
00062 Switch::Switch (void) throw () : type (DEFAULT)
00063 {
00064   return;
00065 }
00066 
00077 Switch::Switch (const std::string &_off, const std::string &_on, int16 _type) : on (_on), off (_off), type (_type)
00078 {
00079   return;
00080 }
00081 
00082 size_t Switch::getLength (void) const throw ()
00083 {
00084   return LENGTH;
00085 }
00086 
00087 bool Switch::write (Lump &lump, Lump::size_type i) const throw ()
00088 {
00089   try
00090   {
00091     lump.setString (this->off, i);
00092     lump.setString (this->on, i + 9);
00093     lump.setInt16 (this->type, i + 18);
00094   }
00095   catch (std::range_error &e)
00096   {
00097     return false;
00098   }
00099   return true;
00100 }
00101 
00102 bool Switch::read (const Lump &lump, Lump::size_type i) throw ()
00103 {
00104   try
00105   {
00106     this->off  = lump.getString (i);
00107     this->on   = lump.getString (i + 9);
00108     this->type = lump.getInt16 (i + 18);
00109   }
00110   catch (std::range_error &e)
00111   {
00112     return false;
00113   }
00114   return true;
00115 }
00116 
00117 std::string Switch::toString (void) const throw ()
00118 {
00119   std::ostringstream str;
00120   str << "On=" << this->on << ":Off=" << this->off << ":Type=" << this->type << '\n';
00121   return str.str ();
00122 }
00123 
00127 Switches::Switches (void) throw ()
00128 {
00129   return;
00130 }
00131 
00139 Switches::Switches (const Lump &lump) throw ()
00140 {
00141   this->setFromLump (lump);
00142   return;
00143 }
00144 
00150 Switches::~Switches (void) throw ()
00151 {
00152   return;
00153 }
00154 
00155 bool Switches::setFromLump (const Lump &lump) throw ()
00156 {
00157   size_t i = 0;
00158   Switch temp;
00159   if (lump.size () % Switch::LENGTH != 0) return false;
00160 
00161   this->clear ();
00162   for (i = 0; i < lump.size () && lump[i] != Switch::TERMINATE; i += Switch::LENGTH)
00163   {
00164     temp.read (lump, i);
00165     this->insert (this->end (), temp);
00166   }
00167 
00168   return true;
00169 }
00170 
00171 Lump Switches::toLump (void) const throw ()
00172 {
00173   Lump lump (NAME, (this->size () + 1) * Switch::LENGTH);
00174   Switch final ("", "", Switch::TERMINATE);
00175 
00176   size_t i = 0;
00177   for (const_iterator iter = this->begin (); iter != this->end (); ++iter, i += Switch::LENGTH)
00178     iter->write (lump, i);
00179 
00180   final.write (lump, i);
00181 
00182   return lump;
00183 }
00184 
00185 std::string Switches::toString (void) const throw ()
00186 {
00187   std::ostringstream str;
00188   bool first = true;
00189   for (const_iterator i = this->begin (); i != this->end (); ++i, first = false)
00190   {
00191     if (!first) str << '\n';
00192     str << i->toString ();
00193   }
00194   return str.str ();
00195 }

Generated on Fri Jun 10 19:38:51 2005 for libdoomwad by  doxygen 1.4.0