From 6576f886089e14802b8c79e12cc7022cd146a8f4 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 27 Jan 2017 21:28:01 +0100 Subject: [PATCH] Use numberToString() provided by c++utilities --- CMakeLists.txt | 4 +--- angle.cpp | 15 ++++++++------- location.cpp | 9 +++++---- main.cpp | 10 +++++++--- utils.cpp | 6 ------ utils.h | 22 ---------------------- 6 files changed, 21 insertions(+), 45 deletions(-) delete mode 100644 utils.cpp delete mode 100644 utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 05f51a6..d9aaa8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,13 +5,11 @@ set(HEADER_FILES angle.h location.h main.h - utils.h ) set(SRC_FILES angle.cpp location.cpp main.cpp - utils.cpp ) set(DOC_FILES @@ -27,7 +25,7 @@ set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}") set(META_APP_DESCRIPTION "Command line tool for basic calculations with geo coordinates such as format conversions and calculation of distance, bearing, mid point, destination and more") set(META_VERSION_MAJOR 1) set(META_VERSION_MINOR 1) -set(META_VERSION_PATCH 2) +set(META_VERSION_PATCH 3) set(META_APP_VERSION ${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH}) # find c++utilities diff --git a/angle.cpp b/angle.cpp index b28d5da..133b7f6 100644 --- a/angle.cpp +++ b/angle.cpp @@ -1,7 +1,7 @@ -#include "./utils.h" #include "./angle.h" #include +#include #include #include @@ -14,6 +14,7 @@ using namespace std; using namespace ApplicationUtilities; +using namespace ConversionUtilities; Angle::Angle() : m_val(0) @@ -38,26 +39,26 @@ Angle::Angle(const string &value, AngularMeasure measure) : { switch(measure) { case AngularMeasure::Radian: - m_val += ConversionUtilities::numberFromString(value); + m_val += stringToNumber(value); break; case AngularMeasure::Degree: { string::size_type mpos, spos = string::npos; mpos = value.find(':'); if(mpos == string::npos) - m_val += ConversionUtilities::numberFromString(value); + m_val += stringToNumber(value); else if(mpos >= (value.length() - 1)) throw Failure("excepted minutes after ':' in " + value); else { - m_val += ConversionUtilities::numberFromString(value.substr(0, mpos)); + m_val += stringToNumber(value.substr(0, mpos)); spos = value.find(':', mpos + 1); if(spos == string::npos) - m_val += ConversionUtilities::numberFromString(value.substr(mpos + 1)) / 60.0; + m_val += stringToNumber(value.substr(mpos + 1)) / 60.0; else if(spos >= (value.length() - 1)) throw Failure("excepted seconds after second ':'' in " + value); else - m_val += (ConversionUtilities::numberFromString(value.substr(mpos + 1, spos - mpos - 1)) / 60.0) - + (ConversionUtilities::numberFromString(value.substr(spos + 1)) / 3600.0); + m_val += (stringToNumber(value.substr(mpos + 1, spos - mpos - 1)) / 60.0) + + (stringToNumber(value.substr(spos + 1)) / 3600.0); } m_val = m_val * M_PI / 180.0; break; diff --git a/location.cpp b/location.cpp index 0a7c9eb..ddc19a2 100644 --- a/location.cpp +++ b/location.cpp @@ -1,7 +1,7 @@ #include "./location.h" -#include "./utils.h" #include +#include #include #include @@ -9,6 +9,7 @@ using namespace std; using namespace ApplicationUtilities; +using namespace ConversionUtilities; // WGS84 Parameters #define WGS84_A 6378137.0 // major axis @@ -258,10 +259,10 @@ void Location::setValueByProvidedUtmWgs4Coordinates(const string &utmWgs4Coordin if(epos != 0 && epos != string::npos) { string::size_type npos = utmWgs4Coordinates.find('N', epos); if(npos < (utmWgs4Coordinates.length() - 1) && npos != string::npos) { - int zone = ConversionUtilities::numberFromString(utmWgs4Coordinates.substr(0, epos - 1)); + int zone = stringToNumber(utmWgs4Coordinates.substr(0, epos - 1)); char zoneDesignator = utmWgs4Coordinates.at(epos - 1); - double east = ConversionUtilities::numberFromString(utmWgs4Coordinates.substr(epos + 1, npos - epos - 1)); - double north = ConversionUtilities::numberFromString(utmWgs4Coordinates.substr(npos + 1)); + double east = stringToNumber(utmWgs4Coordinates.substr(epos + 1, npos - epos - 1)); + double north = stringToNumber(utmWgs4Coordinates.substr(npos + 1)); setValueByProvidedUtmWgs4Coordinates(zone, zoneDesignator, east, north); return; } diff --git a/main.cpp b/main.cpp index ee9e6dd..380e18d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,11 @@ #include "./main.h" #include "./location.h" -#include "./utils.h" #include "resources/config.h" #include #include +#include #include #include @@ -173,8 +173,12 @@ int main(int argc, char *argv[]) } else { cerr << "No arguments given. See --help for available commands."; } + } catch(const ConversionException &) { + cerr << "The provided numbers couldn't be parsed correctly." << endl; + cerr << endl; + printAngleFormatInfo(cerr); } catch(const Failure &ex) { - cerr << "The provided locations/coordinates couldn't be parsed correctly. " << ex.what() << endl; + cerr << "The provided locations/coordinates couldn't be parsed correctly: " << ex.what() << endl; cerr << endl; printAngleFormatInfo(cerr); } @@ -290,7 +294,7 @@ void printMidpoint(const string &locationstr1, const string &locationstr2) void printDestination(const string &locationstr, const string &distancestr, const string &bearingstr) { Location start = locationFromString(locationstr); - double distance = numberFromString(distancestr); + double distance = stringToNumber(distancestr); Angle bearing(bearingstr, inputAngularMeasure); printLocation(start.destination(distance, bearing)); } diff --git a/utils.cpp b/utils.cpp deleted file mode 100644 index 1323be0..0000000 --- a/utils.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "./utils.h" - -namespace Utils -{ - -} diff --git a/utils.h b/utils.h deleted file mode 100644 index e406e6a..0000000 --- a/utils.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef UTILS_H -#define UTILS_H - -#include - -#include -#include - -namespace ConversionUtilities -{ - -template -T numberFromString(const std::string &value) { - T result; - std::stringstream ss(value, std::stringstream::in | std::stringstream::out); - if(ss >> result && ss.eof()) return result; - else throw ApplicationUtilities::Failure(value + " is no number"); -} - -} - -#endif // UTILS_H