FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
rawdata.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_VFS_RAW_RAWDATA_H
23 #define FIFE_VFS_RAW_RAWDATA_H
24 
25 // Standard C++ library includes
26 #include <vector>
27 
28 // Platform specific includes
29 #include "util/base/fife_stdint.h"
30 
31 // 3rd party library includes
32 #include <boost/shared_ptr.hpp>
33 
34 // FIFE includes
35 // These includes are split up in two parts, separated by one empty line
36 // First block: files included from the FIFE root src directory
37 // Second block: files included from the same folder
38 
39 #include "rawdatasource.h"
40 
41 namespace FIFE {
42 
48  class RawData {
49  public:
50  RawData(RawDataSource* datasource);
51  virtual ~RawData();
52 
56  std::vector<uint8_t> getDataInBytes();
57 
60  std::vector<std::string> getDataInLines();
61 
62 
67  uint32_t getDataLength() const;
68 
73  uint32_t getCurrentIndex() const;
74 
80  void setIndex(uint32_t index);
81 
87  void moveIndex(int32_t offset);
88 
94  template <typename T> T readSingle() {
95  T val;
96  readInto(reinterpret_cast<uint8_t*>(&val), sizeof(T));
97  return val;
98  }
99 
106  void readInto(uint8_t* buffer, size_t len);
107 
109  uint8_t read8();
110 
115 
121 
127 
133 
141  std::string readString(size_t len);
142 
147  void read(std::string& outbuffer, int32_t size=-1);
148 
154  bool getLine(std::string& buffer);
155 
156  private:
159 
160  template <typename T> T littleToHost(T value) const {
161  if (littleEndian())
162  return value;
163  else
164  return revert(value);
165  }
166 
167  template <typename T> T bigToHost(T value) const {
168  if (!littleEndian())
169  return value;
170  else
171  return revert(value);
172  }
173 
174  template <typename T> T revert(T value) const {
175  T retval;
176  for (uint32_t i = 0; i < sizeof(T); ++i)
177  reinterpret_cast<uint8_t*>(&retval)[i] = reinterpret_cast<uint8_t*>(&value)[sizeof(T)-1-i];
178 
179  return retval;
180  }
181 
182  RawData(const RawData&);
183  RawData& operator=(const RawData&) { return *this; };
184 
185  static bool littleEndian();
186  };
187  typedef boost::shared_ptr<RawData> RawDataPtr;
188 
189  class IndexSaver {
190  public:
191  IndexSaver(RawData* d) : m_rd(d), m_index(m_rd->getCurrentIndex()) {}
192 
194  m_rd->setIndex(m_index);
195  }
196 
197  private:
200 
201  IndexSaver(const IndexSaver&);
202  IndexSaver& operator=(const IndexSaver&) { return *this; }
203  };
204 
205 }//FIFE
206 
207 #endif
uint16_t read16Big()
reads a uint16_t bigEndian and converts them to the host-byteorder
Definition: rawdata.cpp:118
uint32_t getDataLength() const
get the complete datalength
Definition: rawdata.cpp:75
bool getLine(std::string &buffer)
reads until a \n is encountered or no more data is available
Definition: rawdata.cpp:154
void moveIndex(int32_t offset)
move the current index
Definition: rawdata.cpp:90
std::vector< std::string > getDataInLines()
get the data in distinct lines
Definition: rawdata.cpp:65
Abstract baseclass - provides data for RawData.
Definition: rawdatasource.h:44
size_t m_index_current
Definition: rawdata.h:158
std::vector< uint8_t > getDataInBytes()
get the data as a vector of bytes This does not append a null terminator to the end ...
Definition: rawdata.cpp:49
IndexSaver & operator=(const IndexSaver &)
Definition: rawdata.h:202
RawDataSource * m_datasource
Definition: rawdata.h:157
virtual ~RawData()
Definition: rawdata.cpp:45
T bigToHost(T value) const
Definition: rawdata.h:167
T littleToHost(T value) const
Definition: rawdata.h:160
RawData * m_rd
Definition: rawdata.h:198
uint32_t getCurrentIndex() const
get the current index
Definition: rawdata.cpp:79
uint8_t read8()
reads 1 byte
Definition: rawdata.cpp:104
T readSingle()
helper-function
Definition: rawdata.h:94
IndexSaver(RawData *d)
Definition: rawdata.h:191
unsigned char uint8_t
Definition: core.h:38
uint32_t read32Big()
reads a uint16_t bigEndian and converts them to the host-byteorder
Definition: rawdata.cpp:123
RawData & operator=(const RawData &)
Definition: rawdata.h:183
unsigned short uint16_t
Definition: core.h:39
T revert(T value) const
Definition: rawdata.h:174
std::string readString(size_t len)
read a string with len bytes, not assuming a terminating 0 Appends a null terminator character to the...
Definition: rawdata.cpp:128
uint32_t read32Little()
reads a uint16_t littleEndian and converts them to the host-byteorder
Definition: rawdata.cpp:113
uint32_t m_index
Definition: rawdata.h:199
void readInto(uint8_t *buffer, size_t len)
read len bytes into buffer
Definition: rawdata.cpp:94
RawData(RawDataSource *datasource)
Definition: rawdata.cpp:41
uint16_t read16Little()
reads a uint16_t littleEndian and converts them to the host-byteorder
Definition: rawdata.cpp:108
void setIndex(uint32_t index)
set the current index
Definition: rawdata.cpp:83
unsigned int uint32_t
Definition: core.h:40
static bool littleEndian()
Definition: rawdata.cpp:166
void read(std::string &outbuffer, int32_t size=-1)
Reads all data into the buffer This does not append a null terminator to the end Created to especiall...
Definition: rawdata.cpp:138
Used to access diffrent kinds of data.
Definition: rawdata.h:48
boost::shared_ptr< RawData > RawDataPtr
Definition: rawdata.h:187