| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 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 |
// 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 |
// get the total file size |
| 51 |
uint32_t size = getDataLength(); |
| 52 |
|
| 53 |
// create output vector |
| 54 |
std::vector<uint8_t> target; |
| 55 |
|
| 56 |
// resize vector to file size |
| 57 |
target.resize(size); |
| 58 |
|
| 59 |
// read bytes directly into vector |
| 60 |
readInto(&target[0], target.size()); |
| 61 |
|
| 62 |
return target; |
| 63 |
} |
| 64 |
|
| 65 |
std::vector<std::string> RawData::getDataInLines() { |
| 66 |
std::vector<std::string> target; |
| 67 |
|
| 68 |
std::string line; |
| 69 |
while (getLine(line)) { |
| 70 |
target.push_back(line); |
| 71 |
} |
| 72 |
return target; |
| 73 |
} |
| 74 |
|
| 75 |
uint32_t RawData::getDataLength() const { |
| 76 |
return m_datasource->getSize(); |
| 77 |
} |
| 78 |
|
| 79 |
uint32_t RawData::getCurrentIndex() const { |
| 80 |
return m_index_current; |
| 81 |
} |
| 82 |
|
| 83 |
void RawData::setIndex(uint32_t index) { |
| 84 |
if (index > getDataLength()) |
| 85 |
throw IndexOverflow(__FUNCTION__); |
| 86 |
|
| 87 |
m_index_current = index; |
| 88 |
} |
| 89 |
|
| 90 |
void RawData::moveIndex(int32_t offset) { |
| 91 |
setIndex(getCurrentIndex() + offset); |
| 92 |
} |
| 93 |
|
| 94 |
void RawData::readInto(uint8_t* buffer, size_t len) { |
| 95 |
if (m_index_current + len > getDataLength()) { |
| 96 |
FL_LOG(_log, LMsg("RawData") << m_index_current << " : " << len << " : " << getDataLength()); |
| 97 |
throw IndexOverflow(__FUNCTION__); |
| 98 |
} |
| 99 |
|
| 100 |
m_datasource->readInto(buffer, m_index_current, len); |
| 101 |
m_index_current += len; |
| 102 |
} |
| 103 |
|
| 104 |
uint8_t RawData::read8() { |
| 105 |
return readSingle<uint8_t>(); |
| 106 |
} |
| 107 |
|
| 108 |
uint16_t RawData::read16Little() { |
| 109 |
uint16_t val = readSingle<uint16_t>(); |
| 110 |
return littleToHost(val); |
| 111 |
} |
| 112 |
|
| 113 |
uint32_t RawData::read32Little() { |
| 114 |
uint32_t val = readSingle<uint32_t>(); |
| 115 |
return littleToHost(val); |
| 116 |
} |
| 117 |
|
| 118 |
uint16_t RawData::read16Big() { |
| 119 |
uint16_t val = readSingle<uint16_t>(); |
| 120 |
return bigToHost(val); |
| 121 |
} |
| 122 |
|
| 123 |
uint32_t RawData::read32Big() { |
| 124 |
uint32_t val = readSingle<uint32_t>(); |
| 125 |
return bigToHost(val); |
| 126 |
} |
| 127 |
|
| 128 |
std::string RawData::readString(size_t len) { |
| 129 |
std::vector<uint8_t> strVector; |
| 130 |
strVector.resize(len); |
| 131 |
readInto(&strVector[0], len); |
| 132 |
|
| 133 |
std::string ret(strVector.begin(), strVector.end()); |
| 134 |
|
| 135 |
return ret; |
| 136 |
} |
| 137 |
|
| 138 |
void RawData::read(std::string& outbuffer, int32_t size) { |
| 139 |
if ((size < 0) || ((size + m_index_current) > getDataLength())) { |
| 140 |
size = getDataLength() - m_index_current; |
| 141 |
} |
| 142 |
if (size == 0) { |
| 143 |
outbuffer = ""; |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
outbuffer.resize(size); |
| 148 |
|
| 149 |
// read directly into string |
| 150 |
readInto(reinterpret_cast<uint8_t*>(&outbuffer[0]), size); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
bool RawData::getLine(std::string& buffer) { |
| 155 |
if (getCurrentIndex() >= getDataLength()) |
| 156 |
return false; |
| 157 |
|
| 158 |
buffer = ""; |
| 159 |
char c; |
| 160 |
while (getCurrentIndex() < getDataLength() && (c = read8()) != '\n') |
| 161 |
buffer += c; |
| 162 |
|
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
bool RawData::littleEndian() { |
| 167 |
static int32_t endian = 2; |
| 168 |
if (endian == 2) { |
| 169 |
uint32_t value = 0x01; |
| 170 |
endian = reinterpret_cast<uint8_t*>(&value)[0]; |
| 171 |
FL_LOG(_log, LMsg("RawData") << "we are on a " << (endian == 1 ? "little endian" : "big endian") << " machine"); |
| 172 |
} |
| 173 |
|
| 174 |
return endian == 1; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
}//FIFE |