#include "gfCWordSearchMinigameLoader.h"
#include <fsCore/fsIFile.h>
#include <fsCore/src/fsCResourceName.h>
gfCWordSearchMinigameLoader::gfCWordSearchMinigameLoader(fsCResourceName const& pTileFile)
{
std::map<fsStr, TCsvSectionDecoder> sectionDecoders;
sectionDecoders.insert(std::pair<fsStr, TCsvSectionDecoder>("<words>", &gfCWordSearchMinigameLoader::wordParse));
sectionDecoders.insert(std::pair<fsStr, TCsvSectionDecoder>("<grid>", &gfCWordSearchMinigameLoader::gridRowParse));
csvFileParse(pTileFile, sectionDecoders);
}
void gfCWordSearchMinigameLoader::csvFileParse(const fsCResourceName& pFileName,
std::map<fsStr, TCsvSectionDecoder>& pDecoders)
{
fsStr dataBuffer = fsIFile::strGetFromFile(pFileName);
TCsvSectionDecoder sectionDecoder = nullptr;
fsChar* curr = &dataBuffer[0];<--- Variable 'curr' can be declared as pointer to const
while (*curr != '\0')
{
fsStr line;
while (*curr != fsStr::mLineFeedChar
&& *curr != fsStr::mNewLineChar
&& *curr != '\0')
{
line += *curr;
++curr;
}
if (*curr == fsStr::mLineFeedChar)
{
++curr;
}
if (*curr == fsStr::mNewLineChar)
{
++curr;
}
if (fsStr::npos != line.find('<'))
{
line.erase(line.find('>') + 1);
sectionDecoder = (*pDecoders.find(line)).second;
}
else
{
(this->*sectionDecoder)(line);
}
}
}
void gfCWordSearchMinigameLoader::wordParse(fsStr& pLine)
{
if (!pLine.emptyGet())
{
if (fsStr::npos != pLine.find(','))
{
pLine.erase(pLine.find(','));
}
if (!pLine.emptyGet())
{
mHiddenWords.push_back(pLine);
}
}
}
void gfCWordSearchMinigameLoader::gridRowParse(fsStr& pLine)
{
if (pLine.emptyGet())
{
return;
}
std::vector<fsStr> row;
while (!pLine.emptyGet())
{
fsStr letter = pLine.chomp(",");
if (fsStr::npos != letter.find(fsStr::mNewLineChar))
{
letter.trailingWhitespaceTrim();
break;
}
if (!letter.emptyGet())
{
row.push_back(letter);
}
}
if (!row.empty())
{
mLetterGrid.push_back(row);
}
}