rawdata.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef FIFE_VFS_RAW_RAWDATA_H
00023 #define FIFE_VFS_RAW_RAWDATA_H
00024
00025
00026 #include <vector>
00027
00028
00029 #include "util/base/fife_stdint.h"
00030
00031
00032 #include <boost/shared_ptr.hpp>
00033
00034
00035
00036
00037
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 }
00203
00204 #endif