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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/*
* CLegacyConfigParser.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CLegacyConfigParser.h"
#include "TextOperations.h"
#include "Languages.h"
#include "../VCMI_Lib.h"
#include "filesystem/Filesystem.h"
#include "../modding/CModHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
//Helper for string -> float conversion
class LocaleWithComma: public std::numpunct<char>
{
protected:
char do_decimal_point() const override
{
return ',';
}
};
CLegacyConfigParser::CLegacyConfigParser(const TextPath & resource)
{
auto input = CResourceHandler::get()->load(resource);
fileEncoding = VLC->modh->findResourceEncoding(resource);
data.reset(new char[input->getSize()]);
input->read(reinterpret_cast<uint8_t*>(data.get()), input->getSize());
curr = data.get();
end = curr + input->getSize();
}
std::string CLegacyConfigParser::extractQuotedPart()
{
assert(*curr == '\"');
curr++; // skip quote
const char * begin = curr;
while (curr != end && *curr != '\"' && *curr != '\t')
curr++;
return std::string(begin, curr++); //increment curr to close quote
}
std::string CLegacyConfigParser::extractQuotedString()
{
assert(*curr == '\"');
std::string ret;
while (true)
{
ret += extractQuotedPart();
// double quote - add it to string and continue quoted part
if (curr < end && *curr == '\"')
{
ret += '\"';
}
//extract normal part
else if(curr < end && *curr != '\t' && *curr != '\r')
{
const char * begin = curr;
while (curr < end && *curr != '\t' && *curr != '\r' && *curr != '\"')//find end of string or next quoted part start
curr++;
ret += std::string(begin, curr);
if(curr>=end || *curr != '\"')
return ret;
}
else // end of string
return ret;
}
}
std::string CLegacyConfigParser::extractNormalString()
{
const char * begin = curr;
while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
curr++;
return std::string(begin, curr);
}
std::string CLegacyConfigParser::readRawString()
{
if (curr >= end || *curr == '\n')
return "";
std::string ret;
if (*curr == '\"')
ret = extractQuotedString();// quoted text - find closing quote
else
ret = extractNormalString();//string without quotes - copy till \t or \r
curr++;
return ret;
}
std::string CLegacyConfigParser::readString()
{
// do not convert strings that are already in ASCII - this will only slow down loading process
std::string str = readRawString();
if (TextOperations::isValidASCII(str))
return str;
return TextOperations::toUnicode(str, fileEncoding);
}
float CLegacyConfigParser::readNumber()
{
std::string input = readRawString();
std::istringstream stream(input);
if(input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
stream.imbue(std::locale(std::locale(), new LocaleWithComma()));
float result;
if ( !(stream >> result) )
return 0;
return result;
}
bool CLegacyConfigParser::isNextEntryEmpty() const
{
const char * nextSymbol = curr;
while (nextSymbol < end && *nextSymbol == ' ')
nextSymbol++; //find next meaningful symbol
return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
}
bool CLegacyConfigParser::endLine()
{
while (curr < end && *curr != '\n')
readString();
curr++;
return curr < end;
}
VCMI_LIB_NAMESPACE_END
|