33 #include "util/base/exception.h"
34 #include "util/log/logger.h"
39 static Logger _log(LM_VFS);
41 RawData::RawData(RawDataSource* datasource) : m_datasource(datasource), m_index_current(0) {
49 std::vector<uint8_t> RawData::getDataInBytes() {
51 uint32_t size = getDataLength();
54 std::vector<uint8_t> target;
60 readInto(&target[0], target.size());
65 std::vector<std::string> RawData::getDataInLines() {
66 std::vector<std::string> target;
69 while (getLine(line)) {
70 target.push_back(line);
75 uint32_t RawData::getDataLength()
const {
76 return m_datasource->getSize();
79 uint32_t RawData::getCurrentIndex()
const {
80 return m_index_current;
83 void RawData::setIndex(uint32_t index) {
84 if (index > getDataLength())
85 throw IndexOverflow(__FUNCTION__);
87 m_index_current = index;
90 void RawData::moveIndex(int32_t offset) {
91 setIndex(getCurrentIndex() + offset);
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__);
100 m_datasource->readInto(buffer, m_index_current, len);
101 m_index_current += len;
104 uint8_t RawData::read8() {
105 return readSingle<uint8_t>();
108 uint16_t RawData::read16Little() {
109 uint16_t val = readSingle<uint16_t>();
110 return littleToHost(val);
113 uint32_t RawData::read32Little() {
114 uint32_t val = readSingle<uint32_t>();
115 return littleToHost(val);
118 uint16_t RawData::read16Big() {
119 uint16_t val = readSingle<uint16_t>();
120 return bigToHost(val);
123 uint32_t RawData::read32Big() {
124 uint32_t val = readSingle<uint32_t>();
125 return bigToHost(val);
128 std::string RawData::readString(
size_t len) {
129 std::vector<uint8_t> strVector;
130 strVector.resize(len);
131 readInto(&strVector[0], len);
133 std::string ret(strVector.begin(), strVector.end());
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;
147 outbuffer.resize(size);
150 readInto(reinterpret_cast<uint8_t*>(&outbuffer[0]), size);
154 bool RawData::getLine(std::string& buffer) {
155 if (getCurrentIndex() >= getDataLength())
160 while (getCurrentIndex() < getDataLength() && (c = read8()) !=
'\n')
166 bool RawData::littleEndian() {
167 static int32_t 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");
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...