AsciiMapReader works on raw data, so it needs to know

the map size (2D only) from the outside.
This commit is contained in:
King_DuckZ 2015-05-21 09:52:45 +02:00
parent eaca77c2de
commit 2d61a84c13
3 changed files with 29 additions and 15 deletions

View file

@ -5,6 +5,7 @@
#include <iomanip>
#include <stdexcept>
#include <algorithm>
#include <sstream>
namespace dkh {
namespace {
@ -19,24 +20,30 @@ namespace dkh {
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (const char* parFilename) {
m_bytepos = 0;
AsciiMapSource::AsciiMapSource (const char* parFilename, const coords& parSize) :
m_mapSize(parSize),
m_bytepos(0)
{
std::ifstream src(parFilename);
parse_map_data(src);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (const std::string& parFilename) {
m_bytepos = 0;
AsciiMapSource::AsciiMapSource (const std::string& parFilename, const coords& parSize) :
m_mapSize(parSize),
m_bytepos(0)
{
std::ifstream src(parFilename);
parse_map_data(src);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (std::istream& parData) {
m_bytepos = 0;
AsciiMapSource::AsciiMapSource (std::istream& parData, const coords& parSize) :
m_mapSize(parSize),
m_bytepos(0)
{
parse_map_data(parData);
}
@ -102,16 +109,23 @@ namespace dkh {
throw std::runtime_error("Invalid data: can't parse file");
}
coords map_size;
if (grammar.lengths.empty()) {
m_mapSize.x() = m_mapSize.y() = 0;
map_size.x() = map_size.y() = 0;
throw std::runtime_error("Invalid data: can't guess map size");
}
//Only look at the front. Besides a bug in spirit makes the other counts
//invalid, see
//http://boost.2283326.n4.nabble.com/Possible-bug-in-line-pos-iterator-td4636592.html
m_mapSize.x() = grammar.lengths.front();
m_mapSize.y() = m_wholedata.size() / m_mapSize.x();
map_size.x() = grammar.lengths.front();
map_size.y() = m_wholedata.size() / map_size.x();
if (map_size != m_mapSize) {
std::ostringstream oss;
oss << "Calculated map size is " << map_size <<
" which is different than the expected size " << m_mapSize;
throw std::runtime_error(oss.str());
}
}
///-------------------------------------------------------------------------