rawdata.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_VFS_RAW_RAWDATA_H
00023 #define FIFE_VFS_RAW_RAWDATA_H
00024 
00025 // Standard C++ library includes
00026 #include <vector>
00027 
00028 // Platform specific includes
00029 #include "util/base/fife_stdint.h"
00030 
00031 // 3rd party library includes
00032 #include <boost/shared_ptr.hpp>
00033 
00034 // FIFE includes
00035 // These includes are split up in two parts, separated by one empty line
00036 // First block: files included from the FIFE root src directory
00037 // Second block: files included from the same folder
00038 
00039 #include "rawdatasource.h"
00040 
00041 namespace FIFE {
00042 
00048     class RawData {
00049         public:
00050             RawData(RawDataSource* datasource);
00051             virtual ~RawData();
00052             
00055             std::vector<uint8_t> getDataInBytes();
00056 
00059             std::vector<std::string> getDataInLines();
00060 
00061 
00066             unsigned int getDataLength() const;
00067 
00072             unsigned int getCurrentIndex() const;
00073 
00079             void setIndex(unsigned int index);
00080 
00086             void moveIndex(int offset);
00087 
00093             template <typename T> T readSingle() {
00094                 T val;
00095                 readInto(reinterpret_cast<uint8_t*>(&val), sizeof(T));
00096                 return val;
00097             }
00098 
00105             void readInto(uint8_t* buffer, size_t len);
00106 
00108             uint8_t read8();
00109 
00113             uint16_t read16Little();
00114 
00119             uint32_t read32Little();
00120 
00125             uint16_t read16Big();
00126 
00131             uint32_t read32Big();
00132 
00139             std::string readString(size_t len);
00140 
00144             void read(std::string& outbuffer, int size=-1);
00145             
00151             bool getLine(std::string& buffer);
00152 
00153         private:
00154             RawDataSource* m_datasource;
00155             size_t m_index_current;
00156 
00157             template <typename T> T littleToHost(T value) const {
00158                 if (littleEndian())
00159                     return value;
00160                 else
00161                     return revert(value);
00162             }
00163 
00164             template <typename T> T bigToHost(T value) const {
00165                 if (!littleEndian())
00166                     return value;
00167                 else
00168                     return revert(value);
00169             }
00170 
00171             template <typename T> T revert(T value) const {
00172                 T retval;
00173                 for (unsigned int i = 0; i < sizeof(T); ++i)
00174                     reinterpret_cast<uint8_t*>(&retval)[i] = reinterpret_cast<uint8_t*>(&value)[sizeof(T)-1-i];
00175 
00176                 return retval;
00177             }
00178 
00179             RawData(const RawData&);
00180             RawData& operator=(const RawData&) { return *this; };
00181 
00182             static bool littleEndian();
00183     };
00184     typedef boost::shared_ptr<RawData> RawDataPtr;
00185 
00186     class IndexSaver {
00187         public:
00188             IndexSaver(RawData* d) : m_rd(d), m_index(m_rd->getCurrentIndex()) {}
00189 
00190             ~IndexSaver() {
00191                 m_rd->setIndex(m_index);
00192             }
00193 
00194         private:
00195             RawData* m_rd;
00196             unsigned int m_index;
00197 
00198             IndexSaver(const IndexSaver&);
00199             IndexSaver& operator=(const IndexSaver&) { return *this; }
00200     };
00201 
00202 }//FIFE
00203 
00204 #endif