Refactoring in the data loading classes.

Lots of junk code removed, the architecture is now much simpler.
AsciiMapSource works as before.
This commit is contained in:
King_DuckZ 2015-05-25 01:37:20 +02:00
parent 1c5cb6299c
commit ea71478948
18 changed files with 134 additions and 427 deletions

View file

@ -8,21 +8,12 @@
#include <sstream>
namespace dkh {
namespace {
char extractByte ( AsciiMapSource::MapTileType parVal, std::size_t parByte ) a_pure;
char extractByte (AsciiMapSource::MapTileType parVal, std::size_t parByte) {
return static_cast<char>(
(parVal >> (parByte * 8)) & 0xFF
);
}
} //unnamed namespace
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (const char* parFilename, const coords& parSize) :
AsciiMapSource::AsciiMapSource (const char* parFilename, const coords& parSize, dk::MapTypes parMapType) :
m_mapSize(parSize),
m_bytepos(0)
m_bytepos(0),
m_mapType(parMapType)
{
std::ifstream src(parFilename);
parse_map_data(src);
@ -30,9 +21,10 @@ namespace dkh {
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (const std::string& parFilename, const coords& parSize) :
AsciiMapSource::AsciiMapSource (const std::string& parFilename, const coords& parSize, dk::MapTypes parMapType) :
m_mapSize(parSize),
m_bytepos(0)
m_bytepos(0),
m_mapType(parMapType)
{
std::ifstream src(parFilename);
parse_map_data(src);
@ -40,53 +32,14 @@ namespace dkh {
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (std::istream& parData, const coords& parSize) :
AsciiMapSource::AsciiMapSource (std::istream& parData, const coords& parSize, dk::MapTypes parMapType) :
m_mapSize(parSize),
m_bytepos(0)
m_bytepos(0),
m_mapType(parMapType)
{
parse_map_data(parData);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::int_type AsciiMapSource::underflow() {
if (m_bytepos == m_wholedata.size() * DataSize)
return traits_type::eof();
const auto index = m_bytepos / DataSize;
const auto byte = m_bytepos % DataSize;
return traits_type::to_int_type(extractByte(m_wholedata[index], byte));
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::int_type AsciiMapSource::uflow() {
if (m_bytepos == m_wholedata.size() * DataSize)
return traits_type::eof();
const auto retval = underflow();
++m_bytepos;
return retval;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::int_type AsciiMapSource::pbackfail (int_type parCh) {
if (0 == m_bytepos or (parCh != traits_type::eof() and parCh != traits_type::to_int_type(extractByte(m_wholedata[m_bytepos / DataSize], m_bytepos % DataSize))))
return traits_type::eof();
--m_bytepos;
DK_ASSERT(underflow() == traits_type::to_int_type(parCh));
return traits_type::to_int_type(parCh);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
std::streamsize AsciiMapSource::showmanyc() {
DK_ASSERT(m_bytepos <= m_wholedata.size() * DataSize);
return m_wholedata.size() * DataSize - m_bytepos;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
void AsciiMapSource::parse_map_data (std::istream& parSrc) {
@ -128,65 +81,23 @@ namespace dkh {
}
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::pos_type AsciiMapSource::seekoff (off_type parOff, std::ios_base::seekdir parDir, std::ios_base::openmode parWhich) {
using std::ios;
if (parWhich & ios::out)
return traits_type::eof();
const std::size_t dataLength = DataSize * m_wholedata.size();
if (parWhich & ios::in) {
std::size_t desiredPos;
switch (parDir) {
case ios::beg:
desiredPos = static_cast<std::size_t>(parOff);
break;
case ios::cur:
desiredPos = static_cast<std::size_t>(m_bytepos + parOff);
break;
case ios::end:
desiredPos = static_cast<std::size_t>(dataLength - parOff);
break;
default:
m_bytepos = static_cast<std::size_t>(dataLength);
return traits_type::eof();
}
if (desiredPos >= dataLength) {
m_bytepos = dataLength;
return traits_type::eof();
}
else {
m_bytepos = desiredPos;
return static_cast<pos_type>(m_bytepos);
}
}
m_bytepos = dataLength;
return traits_type::eof();
const AsciiMapSource::coords& AsciiMapSource::mapSize() const {
return m_mapSize;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::pos_type AsciiMapSource::seekpos(pos_type parPos, std::ios_base::openmode parWhich) {
using std::ios;
void AsciiMapSource::fetch (std::vector<MapTileType>& parOut, const coords& parFrom, const coords& parTo) {
const std::size_t from = parFrom.x() + parFrom.y() * m_mapSize.x();
const std::size_t to = parTo.x() + parTo.y() * m_mapSize.x() + 1;
DK_ASSERT(to >= from);
parOut.resize(to - from);
std::copy(m_wholedata.begin() + from, m_wholedata.begin() + to, parOut.begin());
}
if (parWhich & ios::out)
return traits_type::eof();
dk::MapTypes AsciiMapSource::mapType() const {
return m_mapType;
}
const std::size_t dataLength = DataSize * m_wholedata.size();
if (parWhich & ios::in) {
if (static_cast<std::size_t>(parPos) >= dataLength) {
m_bytepos = dataLength;
return traits_type::eof();
}
else {
m_bytepos = static_cast<std::size_t>(parPos);
return static_cast<pos_type>(m_bytepos);
}
}
m_bytepos = static_cast<std::size_t>(parPos);
return static_cast<pos_type>(m_bytepos);
int AsciiMapSource::layersCount() const {
return 1;
}
} //namespace dkh