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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <algorithm>
#include <cctype>
#include <limits.h>
#include <stdexcept>
#include <sstream>
#include <boost/scoped_array.hpp>
#include "System/mmgr.h"
#include "System/Util.h"
#include "System/TdfParser.h"
#include "tdf_grammar.h"
#include "System/FileSystem/FileHandler.h"
#include "System/Log/ILog.h"
TdfParser::parse_error::parse_error(size_t l, size_t c, std::string const& f) throw()
: content_error("Parse error in " + f + " at line " + IntToString(l) + " column " + IntToString(c) + ".")
, line(l)
, column(c)
, filename(f)
{}
TdfParser::parse_error::parse_error(std::string const& line_of_error, size_t l, size_t c, std::string const& f) throw()
: content_error("Parse error in " + f + " at line " + IntToString(l) + " column " + IntToString(c) +" near\n"+ line_of_error)
, line(l)
, column(c)
, filename(f)
{}
TdfParser::parse_error::parse_error(std::string const& message, std::string const& line_of_error, size_t l, size_t c, std::string const& f)
throw()
: content_error("Parse error '" + message + "' in " + f + " at line " + IntToString(l) + " column " + IntToString(c) +" near\n"+ line_of_error)
, line(l)
, column(c)
, filename(f)
{}
TdfParser::parse_error::~parse_error() throw() {}
size_t TdfParser::parse_error::get_line() const { return line; }
size_t TdfParser::parse_error::get_column() const { return column; }
std::string const& TdfParser::parse_error::get_filename() const { return filename; }
void TdfParser::TdfSection::print(std::ostream & out) const
{
for (std::map<std::string,TdfSection*>::const_iterator it = sections.begin(), e=sections.end(); it != e; ++it) {
out << "[" << it->first << "]\n{\n";
it->second->print(out);
out << "}\n";
}
for (std::map<std::string,std::string>::const_iterator it = values.begin(), e=values.end(); it != e; ++it) {
out << it->first << "=" << it->second << ";\n";
}
}
TdfParser::TdfSection* TdfParser::TdfSection::construct_subsection(const std::string& name)
{
std::string lowerd_name = StringToLower(name);
std::map<std::string,TdfSection*>::iterator it = sections.find(lowerd_name);
if (it != sections.end()) {
return it->second;
} else {
TdfSection* ret = new TdfSection;
sections[lowerd_name] = ret;
return ret;
}
}
bool TdfParser::TdfSection::remove(const std::string& key)
{
valueMap_t::iterator it = values.find(key);
if (it != values.end()) {
values.erase(it);
return true;
} else {
return false;
}
}
void TdfParser::TdfSection::add_name_value(const std::string& name, const std::string& value)
{
std::string lowerd_name = StringToLower(name);
values[lowerd_name] = value;
}
TdfParser::TdfSection::~TdfSection()
{
for (std::map<std::string,TdfSection*>::iterator it = sections.begin(), e=sections.end(); it != e; ++it) {
delete it->second;
}
}
TdfParser::TdfParser(char const* buf, size_t size)
{
LoadBuffer(buf, size);
}
TdfParser::TdfParser(std::string const& filename)
{
LoadFile(filename);
}
TdfParser::~TdfParser()
{
}
void TdfParser::print(std::ostream & out) const {
root_section.print(out);
}
void TdfParser::parse_buffer(char const* buf, size_t size) {
std::list<std::string> junk_data;
tdf_grammar grammar(&root_section, &junk_data);
parse_info<char const*> info;
std::string message;
typedef position_iterator2<char const*> iterator_t;
iterator_t error_it(buf, buf + size);
try {
info = parse(
buf
, buf + size
, grammar
, space_p
| comment_p("/*", "*/") // rule for C-comments
| comment_p("//")
);
} catch (const parser_error<tdf_grammar::Errors, char const*>& ex) { // thrown by assertion parsers in tdf_grammar
switch(ex.descriptor) {
case tdf_grammar::semicolon_expected: message = "semicolon expected"; break;
case tdf_grammar::equals_sign_expected: message = "equals sign in name value pair expected"; break;
case tdf_grammar::square_bracket_expected: message = "square bracket to close section name expected"; break;
case tdf_grammar::brace_expected: message = "brace or further name value pairs expected"; break;
default: message = "unknown boost::spirit::parser_error exception"; break;
};
std::ptrdiff_t target_pos = ex.where - buf;
for (int i = 1; i < target_pos; ++i) {
++error_it;
if (error_it != (iterator_t(buf + i, buf + size))) {
++i;
}
}
}
for (std::list<std::string>::const_iterator it = junk_data.begin(), e = junk_data.end(); it !=e ; ++it) {
std::string temp = StringTrim(*it);
if (!temp.empty()) {
LOG_L(L_WARNING, "TdfParser: Junk in %s: %s",
filename.c_str(), temp.c_str());
}
}
if (!message.empty()) {
throw parse_error(message, error_it.get_currentline(), error_it.get_position().line, error_it.get_position().column, filename);
}
// a different error might have happened:
if (!info.full) {
std::ptrdiff_t target_pos = info.stop - buf;
for (int i = 1; i < target_pos; ++i) {
++error_it;
if (error_it != (iterator_t(buf + i, buf + size))) {
++i;
}
}
throw parse_error(error_it.get_currentline(), error_it.get_position().line, error_it.get_position().column, filename);
}
}
void TdfParser::LoadBuffer(char const* buf, size_t size)
{
this->filename = "buffer";
parse_buffer(buf, size);
}
void TdfParser::LoadFile(std::string const& filename)
{
this->filename = filename;
CFileHandler file(filename);
if (!file.FileExists()) {
throw content_error(("file " + filename + " not found").c_str());
}
const size_t fileBuf_size = file.FileSize();
//char* fileBuf = new char[fileBuf_size];
boost::scoped_array<char> fileBuf(new char[fileBuf_size]);
file.Read(fileBuf.get(), file.FileSize());
parse_buffer(fileBuf.get(), fileBuf_size);
//delete[] fileBuf;
}
std::string TdfParser::SGetValueDef(std::string const& defaultValue, std::string const& location) const
{
std::string lowerd = StringToLower(location);
std::string value;
bool found = SGetValue(value, lowerd);
if (!found) {
value = defaultValue;
}
return value;
}
bool TdfParser::SGetValue(std::string &value, std::string const& location) const
{
std::string lowerd = StringToLower(location);
std::string searchpath; // for error-messages
// split the location string
const std::vector<std::string>& loclist = GetLocationVector(lowerd);
sectionsMap_t::const_iterator sit = root_section.sections.find(loclist[0]);
if (sit == root_section.sections.end()) {
value = "Section " + loclist[0] + " missing in file " + filename;
return false;
}
TdfSection* sectionptr = sit->second;
searchpath = loclist[0];
for (unsigned int i=1; i < loclist.size()-1; ++i) {
//const char *arg = loclist[i].c_str();
searchpath += '\\';
searchpath += loclist[i];
sit = sectionptr->sections.find(loclist[i]);
if (sit == sectionptr->sections.end()) {
value = "Section " + searchpath + " missing in file " + filename;
return false;
}
sectionptr = sit->second;
}
searchpath += '\\';
searchpath += loclist[loclist.size()-1];
valueMap_t::const_iterator vit =
sectionptr->values.find(loclist[loclist.size()-1]);
if (vit == sectionptr->values.end()) {
value = "Value " + searchpath + " missing in file " + filename;
return false;
}
std::string svalue = vit->second;
value = svalue;
return true;
}
bool TdfParser::GetValue(bool& val, const std::string& location) const
{
std::string buf;
if (SGetValue(buf, location)) {
int tempval;
std::istringstream stream(buf);
stream >> tempval;
if (tempval == 0) {
val = false;
} else {
val = true;
}
return true;
} else {
return false;
}
}
const TdfParser::valueMap_t& TdfParser::GetAllValues(std::string const& location) const
{
static valueMap_t emptymap;
std::string lowerd = StringToLower(location);
const std::vector<std::string>& loclist = GetLocationVector(lowerd);
sectionsMap_t::const_iterator sit = root_section.sections.find(loclist[0]);
if (sit == root_section.sections.end()) {
LOG_L(L_WARNING, "Section %s missing in file %s",
loclist[0].c_str(), filename.c_str());
return emptymap;
}
TdfSection* sectionptr = sit->second;
std::string searchpath = loclist[0]; // for error-messages
for (unsigned int i=1; i < loclist.size(); i++) {
searchpath += '\\';
searchpath += loclist[i];
sit = sectionptr->sections.find(loclist[i]);
if (sit == sectionptr->sections.end()) {
LOG_L(L_WARNING, "Section %s missing in file %s",
searchpath.c_str(), filename.c_str());
return emptymap;
}
sectionptr = sit->second;
}
return sectionptr->values;
}
std::vector<std::string> TdfParser::GetSectionList(std::string const& location) const
{
std::string lowerd = StringToLower(location);
const std::vector<std::string>& loclist = GetLocationVector(lowerd);
std::vector<std::string> returnvec;
const sectionsMap_t* sectionsptr = &root_section.sections;
if (!loclist[0].empty()) {
std::string searchpath;
for (unsigned int i = 0; i < loclist.size(); i++) {
searchpath += loclist[i];
if (sectionsptr->find(loclist[i]) == sectionsptr->end()) {
LOG_L(L_WARNING, "Section %s missing in file %s",
searchpath.c_str(), filename.c_str());
return returnvec;
}
sectionsptr = §ionsptr->find(loclist[i])->second->sections;
searchpath += '\\';
}
}
std::map<std::string,TdfSection*>::const_iterator it;
for (it = sectionsptr->begin(); it != sectionsptr->end(); ++it) {
returnvec.push_back(it->first);
StringToLowerInPlace(returnvec.back());
}
return returnvec;
}
bool TdfParser::SectionExist(std::string const& location) const
{
std::string lowerd = StringToLower(location);
const std::vector<std::string>& loclist = GetLocationVector(lowerd);
sectionsMap_t::const_iterator sit = root_section.sections.find(loclist[0]);
if (sit == root_section.sections.end()) {
return false;
}
TdfSection* sectionptr = sit->second;
for (unsigned int i = 1; i < loclist.size(); i++) {
sit = sectionptr->sections.find(loclist[i]);
if (sit == sectionptr->sections.end()) {
return false;
}
sectionptr = sectionptr->sections[loclist[i]];
}
return true;
}
std::vector<std::string> TdfParser::GetLocationVector(std::string const& location) const
{
std::string lowerd = StringToLower(location);
std::vector<std::string> loclist;
std::string::size_type start = 0;
std::string::size_type next = 0;
while ((next = lowerd.find_first_of("\\", start)) != std::string::npos) {
loclist.push_back(lowerd.substr(start, next-start));
start = next + 1;
}
loclist.push_back(lowerd.substr(start));
return loclist;
}
float3 TdfParser::GetFloat3(float3 def, std::string const& location) const
{
std::string s = SGetValueDef("", location);
if (s.empty()) {
return def;
}
float3 ret;
ParseArray(s, &ret.x, 3);
return ret;
}
|