FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
rawdata.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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 
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 
76  return m_datasource->getSize();
77  }
78 
80  return m_index_current;
81  }
82 
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 
105  return readSingle<uint8_t>();
106  }
107 
109  uint16_t val = readSingle<uint16_t>();
110  return littleToHost(val);
111  }
112 
114  uint32_t val = readSingle<uint32_t>();
115  return littleToHost(val);
116  }
117 
119  uint16_t val = readSingle<uint16_t>();
120  return bigToHost(val);
121  }
122 
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 
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
uint16_t read16Big()
reads a uint16_t bigEndian and converts them to the host-byteorder
Definition: rawdata.cpp:118
uint32_t getDataLength() const
get the complete datalength
Definition: rawdata.cpp:75
bool getLine(std::string &buffer)
reads until a \n is encountered or no more data is available
Definition: rawdata.cpp:154
void moveIndex(int32_t offset)
move the current index
Definition: rawdata.cpp:90
std::vector< std::string > getDataInLines()
get the data in distinct lines
Definition: rawdata.cpp:65
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
Abstract baseclass - provides data for RawData.
Definition: rawdatasource.h:44
size_t m_index_current
Definition: rawdata.h:158
std::vector< uint8_t > getDataInBytes()
get the data as a vector of bytes This does not append a null terminator to the end ...
Definition: rawdata.cpp:49
RawDataSource * m_datasource
Definition: rawdata.h:157
virtual ~RawData()
Definition: rawdata.cpp:45
T bigToHost(T value) const
Definition: rawdata.h:167
static Logger _log(LM_AUDIO)
virtual void readInto(uint8_t *buffer, uint32_t start, uint32_t length)=0
read data from the source
T littleToHost(T value) const
Definition: rawdata.h:160
uint32_t getCurrentIndex() const
get the current index
Definition: rawdata.cpp:79
uint8_t read8()
reads 1 byte
Definition: rawdata.cpp:104
unsigned char uint8_t
Definition: core.h:38
virtual uint32_t getSize() const =0
get the complete datasize
uint32_t read32Big()
reads a uint16_t bigEndian and converts them to the host-byteorder
Definition: rawdata.cpp:123
unsigned short uint16_t
Definition: core.h:39
#define FL_LOG(logger, msg)
Definition: logger.h:71
std::string readString(size_t len)
read a string with len bytes, not assuming a terminating 0 Appends a null terminator character to the...
Definition: rawdata.cpp:128
uint32_t read32Little()
reads a uint16_t littleEndian and converts them to the host-byteorder
Definition: rawdata.cpp:113
Definition: modules.h:59
void readInto(uint8_t *buffer, size_t len)
read len bytes into buffer
Definition: rawdata.cpp:94
RawData(RawDataSource *datasource)
Definition: rawdata.cpp:41
uint16_t read16Little()
reads a uint16_t littleEndian and converts them to the host-byteorder
Definition: rawdata.cpp:108
void setIndex(uint32_t index)
set the current index
Definition: rawdata.cpp:83
unsigned int uint32_t
Definition: core.h:40
static bool littleEndian()
Definition: rawdata.cpp:166
void read(std::string &outbuffer, int32_t size=-1)
Reads all data into the buffer This does not append a null terminator to the end Created to especiall...
Definition: rawdata.cpp:138