FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
rawdata.cpp
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 *
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  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 
105  uint16_t val = readSingle<uint16_t>();
106  return littleToHost(val);
107  }
108 
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