rawdata.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <algorithm>
00024 #include <vector>
00025 #include <string>
00026 
00027 // 3rd party library includes
00028 
00029 // FIFE includes
00030 // These includes are split up in two parts, separated by one empty line
00031 // First block: files included from the FIFE root src directory
00032 // Second block: files included from the same folder
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         std::vector<uint8_t> target;
00051         uint32_t size = getDataLength();
00052         uint8_t* array = new uint8_t[size];
00053         readInto(array, size);
00054         for (uint32_t i = 0; i < size; i++) {
00055             target.push_back(array[i]);
00056         }
00057         delete array;
00058         return target;
00059     }
00060 
00061     std::vector<std::string> RawData::getDataInLines() {
00062         std::vector<std::string> target;
00063 
00064         std::string line;
00065         while (getLine(line)) {
00066             target.push_back(line);
00067         }
00068         return target;
00069     }
00070 
00071     unsigned int RawData::getDataLength() const {
00072         return m_datasource->getSize();
00073     }
00074 
00075     unsigned int RawData::getCurrentIndex() const {
00076         return m_index_current;
00077     }
00078 
00079     void RawData::setIndex(unsigned int index) {
00080         if (index > getDataLength())
00081             throw IndexOverflow(__FUNCTION__);
00082 
00083         m_index_current = index;
00084     }
00085 
00086     void RawData::moveIndex(int offset) {
00087         setIndex(getCurrentIndex() + offset);
00088     }
00089 
00090     void RawData::readInto(uint8_t* buffer, size_t len) {
00091         if (m_index_current + len > getDataLength()) {
00092             FL_LOG(_log, LMsg("RawData") << m_index_current << " : " << len << " : " << getDataLength());
00093             throw IndexOverflow(__FUNCTION__);
00094         }
00095 
00096         m_datasource->readInto(buffer, m_index_current, len);
00097         m_index_current += len;
00098     }
00099 
00100     uint8_t RawData::read8() {
00101         return readSingle<uint8_t>();
00102     }
00103 
00104     uint16_t RawData::read16Little() {
00105         uint16_t val = readSingle<uint16_t>();
00106         return littleToHost(val);
00107     }
00108 
00109     uint32_t RawData::read32Little() {
00110         uint32_t val = readSingle<uint32_t>();
00111         return littleToHost(val);
00112     }
00113 
00114     uint16_t RawData::read16Big() {
00115         uint16_t val = readSingle<uint16_t>();
00116         return bigToHost(val);
00117     }
00118 
00119     uint32_t RawData::read32Big() {
00120         uint32_t val = readSingle<uint32_t>();
00121         return bigToHost(val);
00122     }
00123 
00124     std::string RawData::readString(size_t len) {
00125         char* str = new char[len+1];
00126         readInto(reinterpret_cast<uint8_t*>(str), len);
00127         str[len] = 0x00;
00128         std::string ret = str;
00129         delete [] str;
00130         return ret;
00131     }
00132 
00133     void RawData::read(std::string& outbuffer, int size) {
00134         if ((size < 0) || ((size + m_index_current + 1) > getDataLength())) {
00135             size = getDataLength() - m_index_current - 1;
00136         }
00137         if (size == 0) {
00138             outbuffer = "";
00139             return;
00140         }
00141         uint8_t* array = new uint8_t[size + 1];
00142         m_datasource->readInto(array, m_index_current, size);
00143         array[size] = 0x00;
00144         outbuffer = reinterpret_cast<char*>(array);
00145         delete[] array;
00146         m_index_current += size;
00147     }
00148     
00149 
00150     bool RawData::getLine(std::string& buffer) {
00151         if (getCurrentIndex() >= getDataLength())
00152             return false;
00153 
00154         buffer = "";
00155         char c;
00156         while (getCurrentIndex() < getDataLength() && (c = read8()) != '\n')
00157             buffer += c;
00158 
00159         return true;
00160     }
00161 
00162     bool RawData::littleEndian() {
00163         static int endian = 2;
00164         if (endian == 2) {
00165             uint32_t value = 0x01;
00166             endian = reinterpret_cast<uint8_t*>(&value)[0];
00167             FL_LOG(_log, LMsg("RawData") << "we are on a " << (endian == 1 ? "little endian" : "big endian") << " machine");
00168         }
00169 
00170         return endian == 1;
00171     }
00172 
00173 
00174 
00175 }//FIFE
Generated by  doxygen 1.6.2-20100208