From f5124de32783600d66333d1863d8ae697967268d Mon Sep 17 00:00:00 2001 From: DennisOSRM Date: Tue, 9 Jul 2013 14:25:32 +0200 Subject: [PATCH] Partially fixes #646. Values were incorrectly casted to int32_t and not uint32_t --- Extractor/XMLParser.cpp | 12 ++++----- Extractor/XMLParser.h | 14 ++++++----- Util/StringUtil.h | 54 +++++++++++++++++++++++++++++++++++------ 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/Extractor/XMLParser.cpp b/Extractor/XMLParser.cpp index d9ab60411..90590e5e9 100644 --- a/Extractor/XMLParser.cpp +++ b/Extractor/XMLParser.cpp @@ -129,13 +129,13 @@ _RawRestrictionContainer XMLParser::_ReadXMLRestriction() { xmlChar * type = xmlTextReaderGetAttribute( inputReader, ( const xmlChar* ) "type" ); if(xmlStrEqual(role, (const xmlChar *) "to") && xmlStrEqual(type, (const xmlChar *) "way")) { - restriction.toWay = atoi((const char*) ref); + restriction.toWay = stringToUint((const char*) ref); } if(xmlStrEqual(role, (const xmlChar *) "from") && xmlStrEqual(type, (const xmlChar *) "way")) { - restriction.fromWay = atoi((const char*) ref); + restriction.fromWay = stringToUint((const char*) ref); } if(xmlStrEqual(role, (const xmlChar *) "via") && xmlStrEqual(type, (const xmlChar *) "node")) { - restriction.restriction.viaNode = atoi((const char*) ref); + restriction.restriction.viaNode = stringToUint((const char*) ref); } if(NULL != type) { @@ -176,7 +176,7 @@ ExtractionWay XMLParser::_ReadXMLWay() { if ( depth == childDepth && childType == 15 && xmlStrEqual( childName, ( const xmlChar* ) "way" ) == 1 ) { xmlChar* id = xmlTextReaderGetAttribute( inputReader, ( const xmlChar* ) "id" ); - way.id = atoi((char*)id); + way.id = stringToUint((char*)id); xmlFree(id); xmlFree( childName ); break; @@ -202,7 +202,7 @@ ExtractionWay XMLParser::_ReadXMLWay() { } else if ( xmlStrEqual( childName, ( const xmlChar* ) "nd" ) == 1 ) { xmlChar* ref = xmlTextReaderGetAttribute( inputReader, ( const xmlChar* ) "ref" ); if ( ref != NULL ) { - way.path.push_back( atoi(( const char* ) ref ) ); + way.path.push_back( stringToUint(( const char* ) ref ) ); xmlFree( ref ); } } @@ -227,7 +227,7 @@ ImportNode XMLParser::_ReadXMLNode() { } attribute = xmlTextReaderGetAttribute( inputReader, ( const xmlChar* ) "id" ); if ( attribute != NULL ) { - node.id = atoi(( const char* ) attribute ); + node.id = stringToUint(( const char* ) attribute ); xmlFree( attribute ); } diff --git a/Extractor/XMLParser.h b/Extractor/XMLParser.h index a2af2fb86..d17230dfe 100644 --- a/Extractor/XMLParser.h +++ b/Extractor/XMLParser.h @@ -1,17 +1,17 @@ /* open source routing machine Copyright (C) Dennis Luxen, others 2010 - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU AFFERO General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA @@ -21,17 +21,19 @@ #ifndef XMLPARSER_H_ #define XMLPARSER_H_ +#include "BaseParser.h" +#include "../Util/StringUtil.h" +#include "../typedefs.h" + #include -#include "../typedefs.h" -#include "BaseParser.h" class XMLParser : public BaseParser { public: XMLParser(const char* filename, ExtractorCallbacks* ec, ScriptingEnvironment& se); bool ReadHeader(); bool Parse(); - + private: _RawRestrictionContainer _ReadXMLRestriction(); ExtractionWay _ReadXMLWay(); diff --git a/Util/StringUtil.h b/Util/StringUtil.h index 3c1b590f1..6945575f4 100644 --- a/Util/StringUtil.h +++ b/Util/StringUtil.h @@ -62,23 +62,63 @@ static inline char* printInt( char* buffer, int value ) { } static inline void intToString(const int value, std::string & output) { - // The largest 32-bit integer is 4294967295, that is 10 chars - // On the safe side, add 1 for sign, and 1 for trailing zero output.clear(); std::back_insert_iterator sink(output); boost::spirit::karma::generate(sink, boost::spirit::karma::int_, value); } +static inline void int64ToString(const int64_t value, std::string & output) { + output.clear(); + std::back_insert_iterator sink(output); + boost::spirit::karma::generate(sink, boost::spirit::karma::long_long, value); +} + static inline int stringToInt(const std::string& input) { - std::string::const_iterator realBeginOfNumber = input.begin(); + std::string::const_iterator first_digit = input.begin(); //Delete any trailing white-spaces - while(realBeginOfNumber != input.end() && std::isspace(*realBeginOfNumber)) - ++realBeginOfNumber; - int value = 0; // 2 - boost::spirit::qi::parse(realBeginOfNumber, input.end(), boost::spirit::int_, value); // 3 + while(first_digit != input.end() && std::isspace(*first_digit)) { + ++first_digit; + } + int value = 0; + boost::spirit::qi::parse( + first_digit, + input.end(), + boost::spirit::int_, value + ); return value; } +static inline unsigned stringToUint(const std::string& input) { + std::string::const_iterator first_digit = input.begin(); + //Delete any trailing white-spaces + while(first_digit != input.end() && std::isspace(*first_digit)) { + ++first_digit; + } + int value = 0; + boost::spirit::qi::parse( + first_digit, + input.end(), + boost::spirit::uint_, value + ); + return value; +} + +static inline uint64_t stringToInt64(const std::string& input) { + std::string::const_iterator first_digit = input.begin(); + //Delete any trailing white-spaces + while(first_digit != input.end() && std::isspace(*first_digit)) { + ++first_digit; + } + uint64_t value = 0; + boost::spirit::qi::parse( + first_digit, + input.end(), + boost::spirit::long_long, value + ); + return value; +} + + static inline void doubleToString(const double value, std::string & output){ output.clear(); std::back_insert_iterator sink(output);