Replacing atoi by boost::spirit::qi

This commit is contained in:
DennisOSRM 2012-11-20 12:15:40 +01:00
parent aed5848f5a
commit 870ed96c24
4 changed files with 34 additions and 23 deletions

View File

@ -26,9 +26,10 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/algorithm/string_regex.hpp> #include <boost/algorithm/string_regex.hpp>
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <climits> #include <climits>
#include "../Util/StringUtil.h"
//TODO: Move into LUA //TODO: Move into LUA
inline bool durationIsValid(const std::string &s) { inline bool durationIsValid(const std::string &s) {
@ -49,8 +50,8 @@ inline unsigned parseDuration(const std::string &s) {
boost::algorithm::split_regex( result, s, boost::regex( ":" ) ) ; boost::algorithm::split_regex( result, s, boost::regex( ":" ) ) ;
bool matched = regex_match(s, e); bool matched = regex_match(s, e);
if(matched) { if(matched) {
hours = (result.size()== 2) ? atoi(result[0].c_str()) : 0; hours = (result.size()== 2) ? stringToInt(result[0]) : 0;
minutes = (result.size()== 2) ? atoi(result[1].c_str()) : atoi(result[0].c_str()); minutes = (result.size()== 2) ? stringToInt(result[1]) : stringToInt(result[0]);
return 600*(hours*60+minutes); return 600*(hours*60+minutes);
} }
return UINT_MAX; return UINT_MAX;
@ -58,7 +59,7 @@ inline unsigned parseDuration(const std::string &s) {
inline int parseMaxspeed(std::string input) { //call-by-value on purpose. inline int parseMaxspeed(std::string input) { //call-by-value on purpose.
boost::algorithm::to_lower(input); boost::algorithm::to_lower(input);
int n = atoi(input.c_str()); int n = stringToInt(input);
if (input.find("mph") != std::string::npos || input.find("mp/h") != std::string::npos) { if (input.find("mph") != std::string::npos || input.find("mp/h") != std::string::npos) {
n = (n*1609)/1000; n = (n*1609)/1000;
} }

View File

@ -25,42 +25,40 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef SERVERFACTORY_H_ #ifndef SERVERFACTORY_H_
#define SERVERFACTORY_H_ #define SERVERFACTORY_H_
#include <cstdlib> #include <zlib.h>
#include "Server.h" #include "Server.h"
#include "ServerConfiguration.h" #include "ServerConfiguration.h"
#include "../Util/InputFileUtil.h" #include "../Util/InputFileUtil.h"
#include "../Util/OpenMPWrapper.h" #include "../Util/OpenMPWrapper.h"
#include "../Util/StringUtil.h"
#include "../typedefs.h"
typedef http::Server Server; typedef http::Server Server;
struct ServerFactory { struct ServerFactory {
static Server * CreateServer(ServerConfiguration& serverConfig) { static Server * CreateServer(ServerConfiguration& serverConfig) {
if(!testDataFile(serverConfig.GetParameter("nodesData").c_str())) { if(!testDataFile(serverConfig.GetParameter("nodesData"))) {
std::cerr << "[error] nodes file not found" << std::endl; ERR("nodes file not found");
exit(-1);
} }
if(!testDataFile(serverConfig.GetParameter("hsgrData").c_str())) { if(!testDataFile(serverConfig.GetParameter("hsgrData"))) {
std::cerr << "[error] hsgr file not found" << std::endl; ERR("hsgr file not found");
exit(-1);
} }
if(!testDataFile(serverConfig.GetParameter("namesData").c_str())) { if(!testDataFile(serverConfig.GetParameter("namesData"))) {
std::cerr << "[error] names file not found" << std::endl; ERR("names file not found");
exit(-1);
} }
if(!testDataFile(serverConfig.GetParameter("ramIndex").c_str())) { if(!testDataFile(serverConfig.GetParameter("ramIndex"))) {
std::cerr << "[error] ram index file not found" << std::endl; ERR("ram index file not found");
exit(-1);
} }
if(!testDataFile(serverConfig.GetParameter("fileIndex").c_str())) { if(!testDataFile(serverConfig.GetParameter("fileIndex"))) {
std::cerr << "[error] file index file not found" << std::endl; ERR("file index file not found");
exit(-1);
} }
unsigned threads = omp_get_num_procs(); unsigned threads = omp_get_num_procs();
@ -69,8 +67,8 @@ struct ServerFactory {
if(serverConfig.GetParameter("Port") == "") if(serverConfig.GetParameter("Port") == "")
serverConfig.SetParameter("Port", "5000"); serverConfig.SetParameter("Port", "5000");
if(atoi(serverConfig.GetParameter("Threads").c_str()) != 0 && (unsigned)atoi(serverConfig.GetParameter("Threads").c_str()) <= threads) if(stringToInt(serverConfig.GetParameter("Threads")) != 0 && stringToInt(serverConfig.GetParameter("Threads")) <= threads)
threads = atoi( serverConfig.GetParameter("Threads").c_str() ); threads = stringToInt( serverConfig.GetParameter("Threads") );
std::cout << "[server] http 1.1 compression handled by zlib version " << zlibVersion() << std::endl; std::cout << "[server] http 1.1 compression handled by zlib version " << zlibVersion() << std::endl;
Server * server = new Server(serverConfig.GetParameter("IP"), serverConfig.GetParameter("Port"), threads); Server * server = new Server(serverConfig.GetParameter("IP"), serverConfig.GetParameter("Port"), threads);

View File

@ -26,7 +26,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../typedefs.h" #include "../typedefs.h"
// Check if file exists and if it can be opened for reading with ifstream an object // Check if file exists and if it can be opened for reading with ifstream an object
inline bool testDataFile(const char *filename){ inline bool testDataFile(const std::string & filename){
boost::filesystem::path fileToTest(filename); boost::filesystem::path fileToTest(filename);
if(!boost::filesystem::exists(fileToTest)) { if(!boost::filesystem::exists(fileToTest)) {
WARN("Failed to open file " << filename << " for reading."); WARN("Failed to open file " << filename << " for reading.");

View File

@ -27,8 +27,10 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include <sstream> #include <sstream>
#include <boost/spirit/include/karma.hpp> #include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/qi.hpp>
#include "../DataStructures/Coordinate.h" #include "../DataStructures/Coordinate.h"
#include "../typedefs.h"
// precision: position after decimal point // precision: position after decimal point
// length: maximum number of digits including comma and decimals // length: maximum number of digits including comma and decimals
@ -68,6 +70,16 @@ static inline void intToString(const int value, std::string & output) {
boost::spirit::karma::generate(sink, boost::spirit::karma::int_, value); boost::spirit::karma::generate(sink, boost::spirit::karma::int_, value);
} }
static inline int stringToInt(const std::string& input) {
std::string::const_iterator realBeginOfNumber = 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
return value;
}
static inline void convertInternalLatLonToString(const int value, std::string & output) { static inline void convertInternalLatLonToString(const int value, std::string & output) {
char buffer[100]; char buffer[100];
buffer[10] = 0; // Nullterminierung buffer[10] = 0; // Nullterminierung