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             
00056             std::vector<uint8_t> getDataInBytes();
00057 
00060             std::vector<std::string> getDataInLines();
00061 
00062 
00067             uint32_t getDataLength() const;
00068 
00073             uint32_t getCurrentIndex() const;
00074 
00080             void setIndex(uint32_t index);
00081 
00087             void moveIndex(int32_t offset);
00088 
00094             template <typename T> T readSingle() {
00095                 T val;
00096                 readInto(reinterpret_cast<uint8_t*>(&val), sizeof(T));
00097                 return val;
00098             }
00099 
00106             void readInto(uint8_t* buffer, size_t len);
00107 
00109             uint8_t read8();
00110 
00114             uint16_t read16Little();
00115 
00120             uint32_t read32Little();
00121 
00126             uint16_t read16Big();
00127 
00132             uint32_t read32Big();
00133 
00141             std::string readString(size_t len);
00142 
00147             void read(std::string& outbuffer, int32_t size=-1);
00148             
00154             bool getLine(std::string& buffer);
00155 
00156         private:
00157             RawDataSource* m_datasource;
00158             size_t m_index_current;
00159 
00160             template <typename T> T littleToHost(T value) const {
00161                 if (littleEndian())
00162                     return value;
00163                 else
00164                     return revert(value);
00165             }
00166 
00167             template <typename T> T bigToHost(T value) const {
00168                 if (!littleEndian())
00169                     return value;
00170                 else
00171                     return revert(value);
00172             }
00173 
00174             template <typename T> T revert(T value) const {
00175                 T retval;
00176                 for (uint32_t i = 0; i < sizeof(T); ++i)
00177                     reinterpret_cast<uint8_t*>(&retval)[i] = reinterpret_cast<uint8_t*>(&value)[sizeof(T)-1-i];
00178 
00179                 return retval;
00180             }
00181 
00182             RawData(const RawData&);
00183             RawData& operator=(const RawData&) { return *this; };
00184 
00185             static bool littleEndian();
00186     };
00187     typedef boost::shared_ptr<RawData> RawDataPtr;
00188 
00189     class IndexSaver {
00190         public:
00191             IndexSaver(RawData* d) : m_rd(d), m_index(m_rd->getCurrentIndex()) {}
00192 
00193             ~IndexSaver() {
00194                 m_rd->setIndex(m_index);
00195             }
00196 
00197         private:
00198             RawData* m_rd;
00199             uint32_t m_index;
00200 
00201             IndexSaver(const IndexSaver&);
00202             IndexSaver& operator=(const IndexSaver&) { return *this; }
00203     };
00204 
00205 }//FIFE
00206 
00207 #endif