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         // get the total file size
00051         uint32_t size = getDataLength();
00052 
00053         // create output vector
00054         std::vector<uint8_t> target;
00055 
00056         // resize vector to file size
00057         target.resize(size);
00058 
00059         // read bytes directly into vector
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         // read directly into string
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 }//FIFE