FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
rawdata.h
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
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 
114  uint16_t read16Little();
115 
120  uint32_t read32Little();
121 
126  uint16_t read16Big();
127 
132  uint32_t read32Big();
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:
157  RawDataSource* m_datasource;
158  size_t m_index_current;
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 
193  ~IndexSaver() {
194  m_rd->setIndex(m_index);
195  }
196 
197  private:
198  RawData* m_rd;
199  uint32_t m_index;
200 
201  IndexSaver(const IndexSaver&);
202  IndexSaver& operator=(const IndexSaver&) { return *this; }
203  };
204 
205 }//FIFE
206 
207 #endif