1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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);
	}
}