Mercurial > fife-parpg
comparison engine/core/vfs/raw/rawdata.h @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 90005975cdbb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
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 modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program 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 * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, 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 | |
43 /** Used to access diffrent kinds of data. | |
44 * | |
45 * RawData uses RawDataSource to get the real data - that way the user doesn't have to know where the data comes | |
46 * from (real files, files inside archives etc.) | |
47 */ | |
48 class RawData { | |
49 public: | |
50 RawData(RawDataSource* datasource); | |
51 virtual ~RawData(); | |
52 | |
53 /** get the data as a vector of bytes | |
54 */ | |
55 std::vector<uint8_t> getDataInBytes(); | |
56 | |
57 /** get the data in distinct lines | |
58 */ | |
59 std::vector<std::string> getDataInLines(); | |
60 | |
61 | |
62 /** get the complete datalength | |
63 * | |
64 * @return the complete datalength | |
65 */ | |
66 unsigned int getDataLength() const; | |
67 | |
68 /** get the current index | |
69 * | |
70 * @return the current index | |
71 */ | |
72 unsigned int getCurrentIndex() const; | |
73 | |
74 /** set the current index | |
75 * | |
76 * @param index the new index | |
77 * @throws IndexOverflow if index is >= getDataLength() | |
78 */ | |
79 void setIndex(unsigned int index); | |
80 | |
81 /** move the current index | |
82 * | |
83 * @param offset the offset | |
84 * @throws IndexOverflow if we move outside the datalength | |
85 */ | |
86 void moveIndex(int offset); | |
87 | |
88 /** helper-function | |
89 * | |
90 * reads sizeof(T) bytes - should be used with fixed-size datatypes like uint32_t, uint16_t, uint8_t etc. | |
91 * @return the data | |
92 */ | |
93 template <typename T> T readSingle() { | |
94 T val; | |
95 readInto(reinterpret_cast<uint8_t*>(&val), sizeof(T)); | |
96 return val; | |
97 } | |
98 | |
99 /** read len bytes into buffer | |
100 * | |
101 * @param buffer the data will be written into it | |
102 * @param len len bytes will be written | |
103 * @throws IndexOverflow if currentindex + len > getCurrentIndex() | |
104 */ | |
105 void readInto(uint8_t* buffer, size_t len); | |
106 | |
107 /** reads 1 byte */ | |
108 uint8_t read8(); | |
109 | |
110 /** reads a uint16_t littleEndian and converts them to the host-byteorder | |
111 * @throws IndexOverflow | |
112 */ | |
113 uint16_t read16Little(); | |
114 | |
115 /** reads a uint16_t littleEndian and converts them to the host-byteorder | |
116 * | |
117 * @throws IndexOverflow | |
118 */ | |
119 uint32_t read32Little(); | |
120 | |
121 /** reads a uint16_t bigEndian and converts them to the host-byteorder | |
122 * | |
123 * @throws IndexOverflow | |
124 */ | |
125 uint16_t read16Big(); | |
126 | |
127 /** reads a uint16_t bigEndian and converts them to the host-byteorder | |
128 * | |
129 * @throws IndexOverflow | |
130 */ | |
131 uint32_t read32Big(); | |
132 | |
133 /** read a string with len bytes, not assuming a terminating 0 | |
134 * | |
135 * @param len the stringlen | |
136 * @return the string | |
137 * @throws IndexOverflow | |
138 */ | |
139 std::string readString(size_t len); | |
140 | |
141 /** Reads all data into the buffer | |
142 * Created to especially fulfill python file interface requirements | |
143 */ | |
144 void read(std::string& outbuffer, int size=-1); | |
145 | |
146 /** reads until a \\n is encountered or no more data is available | |
147 * | |
148 * @param buffer if successfull the new string will be assigned to buffer | |
149 * @return true if data was available, false otherwise (in that case buffer won't be touched) | |
150 */ | |
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 |