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 372 373
|
// SunParser.cpp: implementation of the TdfParser class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include <algorithm>
#include <cctype>
#include <limits.h>
#include <stdexcept>
#include <boost/algorithm/string/trim.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/scoped_array.hpp>
#include "mmgr.h"
#include "Util.h"
#include "TdfParser.h"
#include "tdf_grammar.hpp"
#include "FileSystem/FileHandler.h"
#include "LogOutput.h"
TdfParser::parse_error::parse_error( std::size_t l, std::size_t c, std::string const& f) throw()
: content_error ( "Parse error in " + f + " at line " + boost::lexical_cast<std::string>(l) + " column " + boost::lexical_cast<std::string>(c) +".")
, line(l)
, column(c)
, filename(f)
{}
TdfParser::parse_error::parse_error( std::string const& line_of_error, std::size_t l, std::size_t c, std::string const& f) throw()
: content_error ( "Parse error in " + f + " at line " + boost::lexical_cast<std::string>(l) + " column " + boost::lexical_cast<std::string>(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, std::size_t l, std::size_t c, std::string const& f)
throw()
: content_error( "Parse error '" + message + "' in " + f + " at line " + boost::lexical_cast<std::string>(l) + " column " + boost::lexical_cast<std::string>(c) +" near\n"+ line_of_error)
, line(l)
, column(c)
, filename(f)
{}
TdfParser::parse_error::~parse_error() throw() {}
std::size_t TdfParser::parse_error::get_line() const {return line;}
std::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::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::~TdfParser()
{
}
TdfParser::TdfSection::~TdfSection()
{
for( std::map<std::string,TdfSection*>::iterator it = sections.begin(), e=sections.end(); it != e; ++it )
delete it->second;
}
void TdfParser::print( std::ostream & out ) const {
root_section.print( out );
}
void TdfParser::parse_buffer( char const* buf, std::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( parser_error<tdf_grammar::Errors, char const*> & e ) { // thrown by assertion parsers in tdf_grammar
switch(e.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 = e.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 = boost::trim_copy( *it );
if( ! temp.empty( ) ) {
::logOutput.Print( "TdfParser: Junk in "+ filename + ": " + temp );
}
}
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 );
}
}
TdfParser::TdfParser( char const* buf, std::size_t size) {
LoadBuffer( buf, size );
}
void TdfParser::LoadBuffer( char const* buf, std::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() );
int size = file.FileSize();
boost::scoped_array<char> filebuf( new char[size] );
file.Read( filebuf.get(), file.FileSize());
parse_buffer(filebuf.get(), size);
}
TdfParser::TdfParser(std::string const& filename) {
LoadFile(filename);
}
//find value, return default value if no such value found
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;
}
//finds a value in the file , if not found returns false, and errormessages is returned in value
//location of value is sent as a string "section\section\value"
bool TdfParser::SGetValue(std::string &value, std::string const& location) const
{
std::string lowerd = StringToLower(location);
std::string searchpath; //for errormessages
//split the location string
std::vector<std::string> loclist = GetLocationVector(lowerd);
std::map<std::string, TdfSection*>::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];
std::map<std::string, std::string>::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;
}
//return a map with all values in section
const std::map<std::string, std::string>& TdfParser::GetAllValues(std::string const& location) const
{
static std::map<std::string, std::string> emptymap;
std::string lowerd = StringToLower(location);
std::string searchpath; //for errormessages
std::vector<std::string> loclist = GetLocationVector(lowerd);
std::map<std::string, TdfSection*>::const_iterator sit =
root_section.sections.find(loclist[0]);
if(sit == root_section.sections.end())
{
logOutput.Print ("Section " + loclist[0] + " missing in file " + filename);
return emptymap;
}
TdfSection *sectionptr = sit->second;
searchpath = loclist[0];
for(unsigned int i=1; i<loclist.size(); i++)
{
searchpath += '\\';
searchpath += loclist[i];
sit = sectionptr->sections.find(loclist[i]);
if(sit == sectionptr->sections.end())
{
logOutput.Print ("Section " + searchpath + " missing in file " + filename);
return emptymap;
}
sectionptr = sit->second;
}
return sectionptr->values;
}
//return vector with all section names in it
std::vector<std::string> TdfParser::GetSectionList(std::string const& location) const
{
std::string lowerd = StringToLower(location);
std::vector<std::string> loclist = GetLocationVector(lowerd);
std::vector<std::string> returnvec;
const std::map<std::string, TdfSection*>* sectionsptr = &root_section.sections;
if(loclist[0].compare("")!=0)
{
std::string searchpath;// = loclist[0];
for(unsigned int i=0; i<loclist.size(); i++)
{
searchpath += loclist[i];
if(sectionsptr->find(loclist[i]) == sectionsptr->end())
{
logOutput.Print ("Section " + searchpath + " missing in file " + filename);
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);
std::vector<std::string> loclist = GetLocationVector(lowerd);
std::map<std::string, TdfSection*>::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;
}
|