00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <algorithm>
00024 #include <vector>
00025 #include <string>
00026
00027
00028
00029
00030
00031
00032
00033 #include "util/base/exception.h"
00034 #include "util/log/logger.h"
00035
00036 #include "rawdata.h"
00037
00038 namespace FIFE {
00039 static Logger _log(LM_VFS);
00040
00041 RawData::RawData(RawDataSource* datasource) : m_datasource(datasource), m_index_current(0) {
00042
00043 }
00044
00045 RawData::~RawData() {
00046 delete m_datasource;
00047 }
00048
00049 std::vector<uint8_t> RawData::getDataInBytes() {
00050
00051 uint32_t size = getDataLength();
00052
00053
00054 std::vector<uint8_t> target;
00055
00056
00057 target.resize(size);
00058
00059
00060 readInto(&target[0], target.size());
00061
00062 return target;
00063 }
00064
00065 std::vector<std::string> RawData::getDataInLines() {
00066 std::vector<std::string> target;
00067
00068 std::string line;
00069 while (getLine(line)) {
00070 target.push_back(line);
00071 }
00072 return target;
00073 }
00074
00075 uint32_t RawData::getDataLength() const {
00076 return m_datasource->getSize();
00077 }
00078
00079 uint32_t RawData::getCurrentIndex() const {
00080 return m_index_current;
00081 }
00082
00083 void RawData::setIndex(uint32_t index) {
00084 if (index > getDataLength())
00085 throw IndexOverflow(__FUNCTION__);
00086
00087 m_index_current = index;
00088 }
00089
00090 void RawData::moveIndex(int32_t offset) {
00091 setIndex(getCurrentIndex() + offset);
00092 }
00093
00094 void RawData::readInto(uint8_t* buffer, size_t len) {
00095 if (m_index_current + len > getDataLength()) {
00096 FL_LOG(_log, LMsg("RawData") << m_index_current << " : " << len << " : " << getDataLength());
00097 throw IndexOverflow(__FUNCTION__);
00098 }
00099
00100 m_datasource->readInto(buffer, m_index_current, len);
00101 m_index_current += len;
00102 }
00103
00104 uint8_t RawData::read8() {
00105 return readSingle<uint8_t>();
00106 }
00107
00108 uint16_t RawData::read16Little() {
00109 uint16_t val = readSingle<uint16_t>();
00110 return littleToHost(val);
00111 }
00112
00113 uint32_t RawData::read32Little() {
00114 uint32_t val = readSingle<uint32_t>();
00115 return littleToHost(val);
00116 }
00117
00118 uint16_t RawData::read16Big() {
00119 uint16_t val = readSingle<uint16_t>();
00120 return bigToHost(val);
00121 }
00122
00123 uint32_t RawData::read32Big() {
00124 uint32_t val = readSingle<uint32_t>();
00125 return bigToHost(val);
00126 }
00127
00128 std::string RawData::readString(size_t len) {
00129 std::vector<uint8_t> strVector;
00130 strVector.resize(len);
00131 readInto(&strVector[0], len);
00132
00133 std::string ret(strVector.begin(), strVector.end());
00134
00135 return ret;
00136 }
00137
00138 void RawData::read(std::string& outbuffer, int32_t size) {
00139 if ((size < 0) || ((size + m_index_current) > getDataLength())) {
00140 size = getDataLength() - m_index_current;
00141 }
00142 if (size == 0) {
00143 outbuffer = "";
00144 return;
00145 }
00146
00147 outbuffer.resize(size);
00148
00149
00150 readInto(reinterpret_cast<uint8_t*>(&outbuffer[0]), size);
00151 }
00152
00153
00154 bool RawData::getLine(std::string& buffer) {
00155 if (getCurrentIndex() >= getDataLength())
00156 return false;
00157
00158 buffer = "";
00159 char c;
00160 while (getCurrentIndex() < getDataLength() && (c = read8()) != '\n')
00161 buffer += c;
00162
00163 return true;
00164 }
00165
00166 bool RawData::littleEndian() {
00167 static int32_t endian = 2;
00168 if (endian == 2) {
00169 uint32_t value = 0x01;
00170 endian = reinterpret_cast<uint8_t*>(&value)[0];
00171 FL_LOG(_log, LMsg("RawData") << "we are on a " << (endian == 1 ? "little endian" : "big endian") << " machine");
00172 }
00173
00174 return endian == 1;
00175 }
00176
00177
00178
00179 }