Mercurial > fife-parpg
comparison engine/core/vfs/raw/rawdata.cpp @ 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 // Standard C++ library includes | |
23 #include <algorithm> | |
24 #include <vector> | |
25 #include <string> | |
26 | |
27 // 3rd party library includes | |
28 | |
29 // FIFE includes | |
30 // These includes are split up in two parts, separated by one empty line | |
31 // First block: files included from the FIFE root src directory | |
32 // Second block: files included from the same folder | |
33 #include "util/base/exception.h" | |
34 #include "util/log/logger.h" | |
35 | |
36 #include "rawdata.h" | |
37 | |
38 namespace FIFE { | |
39 static Logger _log(LM_VFS); | |
40 | |
41 RawData::RawData(RawDataSource* datasource) : m_datasource(datasource), m_index_current(0) { | |
42 | |
43 } | |
44 | |
45 RawData::~RawData() { | |
46 delete m_datasource; | |
47 } | |
48 | |
49 std::vector<uint8_t> RawData::getDataInBytes() { | |
50 std::vector<uint8_t> target; | |
51 uint32_t size = getDataLength(); | |
52 uint8_t* array = new uint8_t[size]; | |
53 readInto(array, size); | |
54 for (uint32_t i = 0; i < size; i++) { | |
55 target.push_back(array[i]); | |
56 } | |
57 delete array; | |
58 return target; | |
59 } | |
60 | |
61 std::vector<std::string> RawData::getDataInLines() { | |
62 std::vector<std::string> target; | |
63 | |
64 std::string line; | |
65 while (getLine(line)) { | |
66 target.push_back(line); | |
67 } | |
68 return target; | |
69 } | |
70 | |
71 unsigned int RawData::getDataLength() const { | |
72 return m_datasource->getSize(); | |
73 } | |
74 | |
75 unsigned int RawData::getCurrentIndex() const { | |
76 return m_index_current; | |
77 } | |
78 | |
79 void RawData::setIndex(unsigned int index) { | |
80 if (index > getDataLength()) | |
81 throw IndexOverflow(__FUNCTION__); | |
82 | |
83 m_index_current = index; | |
84 } | |
85 | |
86 void RawData::moveIndex(int offset) { | |
87 setIndex(getCurrentIndex() + offset); | |
88 } | |
89 | |
90 void RawData::readInto(uint8_t* buffer, size_t len) { | |
91 if (m_index_current + len > getDataLength()) { | |
92 FL_LOG(_log, LMsg("RawData") << m_index_current << " : " << len << " : " << getDataLength()); | |
93 throw IndexOverflow(__FUNCTION__); | |
94 } | |
95 | |
96 m_datasource->readInto(buffer, m_index_current, len); | |
97 m_index_current += len; | |
98 } | |
99 | |
100 uint8_t RawData::read8() { | |
101 return readSingle<uint8_t>(); | |
102 } | |
103 | |
104 uint16_t RawData::read16Little() { | |
105 uint16_t val = readSingle<uint16_t>(); | |
106 return littleToHost(val); | |
107 } | |
108 | |
109 uint32_t RawData::read32Little() { | |
110 uint32_t val = readSingle<uint32_t>(); | |
111 return littleToHost(val); | |
112 } | |
113 | |
114 uint16_t RawData::read16Big() { | |
115 uint16_t val = readSingle<uint16_t>(); | |
116 return bigToHost(val); | |
117 } | |
118 | |
119 uint32_t RawData::read32Big() { | |
120 uint32_t val = readSingle<uint32_t>(); | |
121 return bigToHost(val); | |
122 } | |
123 | |
124 std::string RawData::readString(size_t len) { | |
125 char* str = new char[len+1]; | |
126 readInto(reinterpret_cast<uint8_t*>(str), len); | |
127 str[len] = 0x00; | |
128 std::string ret = str; | |
129 delete [] str; | |
130 return ret; | |
131 } | |
132 | |
133 void RawData::read(std::string& outbuffer, int size) { | |
134 if ((size < 0) || ((size + m_index_current + 1) > getDataLength())) { | |
135 size = getDataLength() - m_index_current - 1; | |
136 } | |
137 if (size == 0) { | |
138 outbuffer = ""; | |
139 return; | |
140 } | |
141 uint8_t* array = new uint8_t[size + 1]; | |
142 m_datasource->readInto(array, m_index_current, size); | |
143 array[size] = 0x00; | |
144 outbuffer = reinterpret_cast<char*>(array); | |
145 delete[] array; | |
146 m_index_current += size; | |
147 } | |
148 | |
149 | |
150 bool RawData::getLine(std::string& buffer) { | |
151 if (getCurrentIndex() >= getDataLength()) | |
152 return false; | |
153 | |
154 buffer = ""; | |
155 char c; | |
156 while (getCurrentIndex() < getDataLength() && (c = read8()) != '\n') | |
157 buffer += c; | |
158 | |
159 return true; | |
160 } | |
161 | |
162 bool RawData::littleEndian() { | |
163 static int endian = 2; | |
164 if (endian == 2) { | |
165 uint32_t value = 0x01; | |
166 endian = reinterpret_cast<uint8_t*>(&value)[0]; | |
167 FL_LOG(_log, LMsg("RawData") << "we are on a " << (endian == 1 ? "little endian" : "big endian") << " machine"); | |
168 } | |
169 | |
170 return endian == 1; | |
171 } | |
172 | |
173 | |
174 | |
175 }//FIFE |