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 
55  std::vector<uint8_t> getDataInBytes();
56 
59  std::vector<std::string> getDataInLines();
60 
61 
66  unsigned int getDataLength() const;
67 
72  unsigned int getCurrentIndex() const;
73 
79  void setIndex(unsigned int index);
80 
86  void moveIndex(int offset);
87 
93  template <typename T> T readSingle() {
94  T val;
95  readInto(reinterpret_cast<uint8_t*>(&val), sizeof(T));
96  return val;
97  }
98 
105  void readInto(uint8_t* buffer, size_t len);
106 
108  uint8_t read8();
109 
113  uint16_t read16Little();
114 
119  uint32_t read32Little();
120 
125  uint16_t read16Big();
126 
131  uint32_t read32Big();
132 
139  std::string readString(size_t len);
140 
144  void read(std::string& outbuffer, int size=-1);
145 
151  bool getLine(std::string& buffer);
152 
153  private:
154  RawDataSource* m_datasource;
155  size_t m_index_current;
156 
157  template <typename T> T littleToHost(T value) const {
158  if (littleEndian())
159  return value;
160  else
161  return revert(value);
162  }
163 
164  template <typename T> T bigToHost(T value) const {
165  if (!littleEndian())
166  return value;
167  else
168  return revert(value);
169  }
170 
171  template <typename T> T revert(T value) const {
172  T retval;
173  for (unsigned int i = 0; i < sizeof(T); ++i)
174  reinterpret_cast<uint8_t*>(&retval)[i] = reinterpret_cast<uint8_t*>(&value)[sizeof(T)-1-i];
175 
176  return retval;
177  }
178 
179  RawData(const RawData&);
180  RawData& operator=(const RawData&) { return *this; };
181 
182  static bool littleEndian();
183  };
184  typedef boost::shared_ptr<RawData> RawDataPtr;
185 
186  class IndexSaver {
187  public:
188  IndexSaver(RawData* d) : m_rd(d), m_index(m_rd->getCurrentIndex()) {}
189 
190  ~IndexSaver() {
191  m_rd->setIndex(m_index);
192  }
193 
194  private:
195  RawData* m_rd;
196  unsigned int m_index;
197 
198  IndexSaver(const IndexSaver&);
199  IndexSaver& operator=(const IndexSaver&) { return *this; }
200  };
201 
202 }//FIFE
203 
204 #endif