2013-10-29 12:18:22 +00:00
|
|
|
|
|
|
|
#include "infocollection.hpp"
|
|
|
|
|
2013-11-08 11:16:41 +00:00
|
|
|
#include <stdexcept>
|
2013-11-09 10:42:19 +00:00
|
|
|
#include <iterator>
|
2013-11-08 11:16:41 +00:00
|
|
|
|
2013-10-29 12:18:22 +00:00
|
|
|
#include <components/esm/esmreader.hpp>
|
2013-11-01 16:43:45 +00:00
|
|
|
#include <components/esm/loaddial.hpp>
|
2013-10-29 12:18:22 +00:00
|
|
|
|
2013-11-08 10:52:30 +00:00
|
|
|
#include <components/misc/stringops.hpp>
|
|
|
|
|
2013-11-01 16:43:45 +00:00
|
|
|
void CSMWorld::InfoCollection::load (const Info& record, bool base)
|
2013-10-29 12:18:22 +00:00
|
|
|
{
|
|
|
|
int index = searchId (record.mId);
|
|
|
|
|
|
|
|
if (index==-1)
|
|
|
|
{
|
|
|
|
// new record
|
2013-11-01 16:43:45 +00:00
|
|
|
Record<Info> record2;
|
2013-10-29 12:18:22 +00:00
|
|
|
record2.mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly;
|
|
|
|
(base ? record2.mBase : record2.mModified) = record;
|
|
|
|
|
2013-11-09 10:42:19 +00:00
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
std::string topic = Misc::StringUtils::lowerCase (record2.get().mTopicId);
|
|
|
|
|
|
|
|
if (!record2.get().mPrev.empty())
|
|
|
|
{
|
|
|
|
index = getIndex (record2.get().mPrev, topic);
|
|
|
|
|
|
|
|
if (index!=-1)
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index==-1 && !record2.get().mNext.empty())
|
|
|
|
{
|
|
|
|
index = getIndex (record2.get().mNext, topic);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index==-1)
|
2013-11-10 11:09:49 +00:00
|
|
|
{
|
2013-11-10 12:00:46 +00:00
|
|
|
Range range = getTopicRange (topic);
|
2013-11-10 11:09:49 +00:00
|
|
|
|
2013-11-10 12:00:46 +00:00
|
|
|
index = std::distance (getRecords().begin(), range.second);
|
2013-11-10 11:09:49 +00:00
|
|
|
}
|
2013-11-09 10:42:19 +00:00
|
|
|
|
|
|
|
insertRecord (record2, index);
|
2013-10-29 12:18:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// old record
|
2013-11-01 16:43:45 +00:00
|
|
|
Record<Info> record2 = getRecord (index);
|
2013-10-29 12:18:22 +00:00
|
|
|
|
|
|
|
if (base)
|
|
|
|
record2.mBase = record;
|
|
|
|
else
|
|
|
|
record2.setModified (record);
|
|
|
|
|
|
|
|
setRecord (index, record2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-09 10:42:19 +00:00
|
|
|
int CSMWorld::InfoCollection::getIndex (const std::string& id, const std::string& topic) const
|
|
|
|
{
|
|
|
|
std::string fullId = Misc::StringUtils::lowerCase (topic) + "#" + id;
|
|
|
|
|
2013-11-10 12:00:46 +00:00
|
|
|
std::pair<RecordConstIterator, RecordConstIterator> range = getTopicRange (topic);
|
2013-11-09 10:42:19 +00:00
|
|
|
|
|
|
|
for (; range.first!=range.second; ++range.first)
|
2013-11-10 12:00:46 +00:00
|
|
|
if (Misc::StringUtils::lowerCase (range.first->get().mId)==fullId)
|
|
|
|
return std::distance (getRecords().begin(), range.first);
|
2013-11-09 10:42:19 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-08 11:16:41 +00:00
|
|
|
int CSMWorld::InfoCollection::getAppendIndex (const std::string& id, UniversalId::Type type) const
|
|
|
|
{
|
|
|
|
std::string::size_type separator = id.find_last_of ('#');
|
|
|
|
|
|
|
|
if (separator==std::string::npos)
|
|
|
|
throw std::runtime_error ("invalid info ID: " + id);
|
|
|
|
|
2013-11-10 12:00:46 +00:00
|
|
|
std::pair<RecordConstIterator, RecordConstIterator> range = getTopicRange (id.substr (0, separator));
|
2013-11-08 11:16:41 +00:00
|
|
|
|
|
|
|
if (range.first==range.second)
|
|
|
|
return Collection<Info, IdAccessor<Info> >::getAppendIndex (id, type);
|
|
|
|
|
2013-11-10 12:00:46 +00:00
|
|
|
return std::distance (getRecords().begin(), range.second);
|
2013-11-08 11:16:41 +00:00
|
|
|
}
|
|
|
|
|
2013-10-31 11:16:45 +00:00
|
|
|
void CSMWorld::InfoCollection::load (ESM::ESMReader& reader, bool base, const ESM::Dialogue& dialogue)
|
2013-10-29 12:18:22 +00:00
|
|
|
{
|
2013-11-08 10:52:30 +00:00
|
|
|
std::string id = Misc::StringUtils::lowerCase (dialogue.mId) + "#" +
|
|
|
|
reader.getHNOString ("INAM");
|
2013-10-29 12:18:22 +00:00
|
|
|
|
|
|
|
if (reader.isNextSub ("DELE"))
|
|
|
|
{
|
|
|
|
int index = searchId (id);
|
|
|
|
|
|
|
|
reader.skipRecord();
|
|
|
|
|
|
|
|
if (index==-1)
|
|
|
|
{
|
|
|
|
// deleting a record that does not exist
|
|
|
|
|
|
|
|
// ignore it for now
|
|
|
|
|
|
|
|
/// \todo report the problem to the user
|
|
|
|
}
|
|
|
|
else if (base)
|
|
|
|
{
|
|
|
|
removeRows (index, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-01 16:43:45 +00:00
|
|
|
Record<Info> record = getRecord (index);
|
2013-10-29 12:18:22 +00:00
|
|
|
record.mState = RecordBase::State_Deleted;
|
|
|
|
setRecord (index, record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-01 16:43:45 +00:00
|
|
|
Info record;
|
|
|
|
record.mTopicId = dialogue.mId;
|
2013-10-29 12:18:22 +00:00
|
|
|
record.mId = id;
|
|
|
|
record.load (reader);
|
|
|
|
|
|
|
|
load (record, base);
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 10:52:30 +00:00
|
|
|
|
2013-11-10 12:00:46 +00:00
|
|
|
CSMWorld::InfoCollection::Range CSMWorld::InfoCollection::getTopicRange (const std::string& topic)
|
|
|
|
const
|
2013-11-08 10:52:30 +00:00
|
|
|
{
|
|
|
|
std::string topic2 = Misc::StringUtils::lowerCase (topic);
|
|
|
|
|
2013-11-10 12:00:46 +00:00
|
|
|
std::map<std::string, int>::const_iterator iter = getIdMap().lower_bound (topic2);
|
2013-11-08 10:52:30 +00:00
|
|
|
|
|
|
|
// Skip invalid records: The beginning of a topic string could be identical to another topic
|
|
|
|
// string.
|
2013-11-10 12:00:46 +00:00
|
|
|
for (; iter!=getIdMap().end(); ++iter)
|
|
|
|
{
|
|
|
|
std::string testTopicId =
|
|
|
|
Misc::StringUtils::lowerCase (getRecord (iter->second).get().mTopicId);
|
|
|
|
|
|
|
|
if (testTopicId==topic2)
|
2013-11-08 10:52:30 +00:00
|
|
|
break;
|
|
|
|
|
2013-11-10 12:00:46 +00:00
|
|
|
std::size_t size = topic2.size();
|
|
|
|
|
|
|
|
if (testTopicId.size()<size || testTopicId.substr (0, size)!=topic2)
|
|
|
|
return Range (getRecords().end(), getRecords().end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter==getIdMap().end())
|
|
|
|
return Range (getRecords().end(), getRecords().end());
|
|
|
|
|
|
|
|
RecordConstIterator begin = getRecords().begin()+iter->second;
|
|
|
|
|
2013-11-08 10:52:30 +00:00
|
|
|
// Find end
|
2013-11-10 12:00:46 +00:00
|
|
|
RecordConstIterator end = begin;
|
2013-11-08 10:52:30 +00:00
|
|
|
|
2013-11-10 12:00:46 +00:00
|
|
|
for (; end!=getRecords().end(); ++end)
|
|
|
|
if (Misc::StringUtils::lowerCase (end->get().mTopicId)!=topic2)
|
2013-11-08 10:52:30 +00:00
|
|
|
break;
|
|
|
|
|
2013-11-10 12:00:46 +00:00
|
|
|
return Range (begin, end);
|
2013-11-08 10:52:30 +00:00
|
|
|
}
|