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
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 }
00206
00207 #endif