diff --git a/Descriptors/DescriptionFactory.h b/Descriptors/DescriptionFactory.h index e38fb10fb..52adc2713 100644 --- a/Descriptors/DescriptionFactory.h +++ b/Descriptors/DescriptionFactory.h @@ -53,13 +53,9 @@ public: _RouteSummary() : lengthString("0"), durationString("0"), startName(0), destName(0) {} void BuildDurationAndLengthStrings(const double distance, const unsigned time) { //compute distance/duration for route summary - std::ostringstream s; - s << round(distance); - lengthString = s.str(); + intToString(round(distance), lengthString); int travelTime = time/10 + 1; - s.str(""); - s << travelTime; - durationString = s.str(); + intToString(travelTime, durationString); } } summary; diff --git a/Server/BasicDatastructures.h b/Server/BasicDatastructures.h index c01d5d34a..cf3aa133f 100644 --- a/Server/BasicDatastructures.h +++ b/Server/BasicDatastructures.h @@ -21,8 +21,11 @@ or see http://www.gnu.org/licenses/agpl.txt. #ifndef BASIC_DATASTRUCTURES_H #define BASIC_DATASTRUCTURES_H #include +#include #include +#include "../Util/StringUtil.h" + namespace http { const std::string okString = "HTTP/1.0 200 OK\r\n"; @@ -70,15 +73,13 @@ struct Reply { std::vector HeaderstoBuffers(); std::string content; static Reply stockReply(status_type status); - void setSize(unsigned size) { - for (std::size_t i = 0; i < headers.size(); ++i) { - Header& h = headers[i]; - if("Content-Length" == h.name) { - std::stringstream sizeString; - sizeString << size; - h.value = sizeString.str(); - } - } + void setSize(const unsigned size) { + BOOST_FOREACH (const Header& h, headers) { + if("Content-Length" == h.name) { + std::string sizeString; + intToString(size,h.value ); + } + } } }; diff --git a/Server/RequestHandler.h b/Server/RequestHandler.h index 35036a6ba..c801b7971 100644 --- a/Server/RequestHandler.h +++ b/Server/RequestHandler.h @@ -33,6 +33,7 @@ or see http://www.gnu.org/licenses/agpl.txt. #include "../DataStructures/HashTable.h" #include "../Plugins/BasePlugin.h" #include "../Plugins/RouteParameters.h" +#include "../Util/StringUtil.h" #include "../typedefs.h" namespace http { @@ -71,16 +72,17 @@ public: bool result = boost::spirit::qi::parse(it, request.end(), apiParser); // returns true if successful if (!result || (it != request.end()) ) { rep = http::Reply::stockReply(http::Reply::badRequest); - std::stringstream content; int position = std::distance(request.begin(), it); - content << "Input seems to be malformed close to position " << position << "
"; - content << "
";
-                content << req.uri << "
"; + std::string tmp_position_string; + intToString(position, tmp_position_string); + rep.content += "Input seems to be malformed close to position "; + rep.content += "
";
+                rep.content += request;
+                rep.content += tmp_position_string;
+                rep.content += "
"; for(unsigned i = 0, end = std::distance(request.begin(), it); i < end; ++i) - content << " "; - content << "^" << "
"; - content << "
"; - rep.content += content.str(); + rep.content += " "; + rep.content += "^
"; } else { //Finished parsing, lets call the right plugin to handle the request if(pluginMap.Holds(routeParameters.service)) { diff --git a/Util/StringUtil.h b/Util/StringUtil.h index 32e147c1f..0dc6df007 100644 --- a/Util/StringUtil.h +++ b/Util/StringUtil.h @@ -21,14 +21,14 @@ or see http://www.gnu.org/licenses/agpl.txt. #ifndef STRINGUTIL_H_ #define STRINGUTIL_H_ -#include -#include #include -#include +#include #include #include +#include + #include "../DataStructures/Coordinate.h" #include "../typedefs.h" @@ -80,6 +80,20 @@ static inline int stringToInt(const std::string& input) { return value; } +static inline void doubleToString(const double value, std::string & output){ + output.clear(); + std::back_insert_iterator sink(output); + boost::spirit::karma::generate(sink, boost::spirit::karma::double_, value); +} + +static inline void doubleToStringWithTwoDigitsBehindComma(const double 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 + char buffer[12] ; + sprintf(buffer, "%g", value) ; + output = buffer ; +} + static inline void convertInternalLatLonToString(const int value, std::string & output) { char buffer[100]; buffer[10] = 0; // Nullterminierung @@ -106,70 +120,37 @@ static inline void convertInternalReversedCoordinateToString(const _Coordinate & output += " "; } -static inline void doubleToString(const double 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 - char buffer[12] ; - sprintf(buffer, "%f", value) ; - output = buffer ; -} - -static inline void doubleToStringWithTwoDigitsBehindComma(const double 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 - char buffer[12] ; - sprintf(buffer, "%g", value) ; - output = buffer ; -} - -inline std::string & replaceAll(std::string &s, const std::string &sub, const std::string &other) { - assert(!sub.empty()); - size_t b = 0; - for (;;) { - b = s.find(sub, b); - if (b == s.npos) break; - s.replace(b, sub.size(), other); - b += other.size(); - } - return s; +inline void replaceAll(std::string &s, const std::string &sub, const std::string &other) { + boost::replace_all(s, sub, other); } inline void stringSplit(const std::string &s, const char delim, std::vector& result) { - std::stringstream ss(s); - std::string item; - while(std::getline(ss, item, delim)) { - if(item.size() > 0) - result.push_back(item); - } + boost::split(result, s, boost::is_any_of(std::string(&delim))); } - static std::string originals[] = {"&", "\"", "<", ">", "'", "[", "]", "\\"}; static std::string entities[] = {"&", """, "<", ">", "'", "&91;", "&93;", " \" }; -inline std::string HTMLEntitize( std::string result) { - for(unsigned i = 0; i < sizeof(originals)/sizeof(std::string); i++) { - result = replaceAll(result, originals[i], entities[i]); +inline std::string HTMLEntitize( std::string & result) { + for(unsigned i = 0; i < sizeof(originals)/sizeof(std::string); ++i) { + replaceAll(result, originals[i], entities[i]); } return result; } -inline std::string HTMLDeEntitize( std::string result) { - for(unsigned i = 0; i < sizeof(originals)/sizeof(std::string); i++) { - result = replaceAll(result, entities[i], originals[i]); +inline std::string HTMLDeEntitize( std::string & result) { + for(unsigned i = 0; i < sizeof(originals)/sizeof(std::string); ++i) { + replaceAll(result, entities[i], originals[i]); } return result; } -inline bool StringStartsWith(std::string & input, std::string & prefix) { - return (input.find(prefix) == 0); +inline bool StringStartsWith(const std::string & input, const std::string & prefix) { + return boost::starts_with(input, prefix); } - -/* - * Function returns a 'random' filename in temporary directors. - * May not be platform independent. - */ +// Function returns a 'random' filename in temporary directors. +// May not be platform independent. inline void GetTemporaryFileName(std::string & filename) { char buffer[L_tmpnam]; char * retPointer = tmpnam (buffer);