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_regex.hpp>
#include <boost/regex.hpp>
#include <climits>
#include "../Util/StringUtil.h"
//TODO: Move into LUA
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( ":" ) ) ;
bool matched = regex_match(s, e);
if(matched) {
hours = (result.size()== 2) ? atoi(result[0].c_str()) : 0;
minutes = (result.size()== 2) ? atoi(result[1].c_str()) : atoi(result[0].c_str());
hours = (result.size()== 2) ? stringToInt(result[0]) : 0;
minutes = (result.size()== 2) ? stringToInt(result[1]) : stringToInt(result[0]);
return 600*(hours*60+minutes);
}
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.
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) {
n = (n*1609)/1000;
}

View File

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

View File

@ -26,7 +26,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../typedefs.h"
// 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);
if(!boost::filesystem::exists(fileToTest)) {
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 <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/qi.hpp>
#include "../DataStructures/Coordinate.h"
#include "../typedefs.h"
// precision: position after decimal point
// 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);
}
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) {
char buffer[100];
buffer[10] = 0; // Nullterminierung