Use numberToString() provided by c++utilities
This commit is contained in:
parent
19e1448577
commit
6576f88608
|
@ -5,13 +5,11 @@ set(HEADER_FILES
|
||||||
angle.h
|
angle.h
|
||||||
location.h
|
location.h
|
||||||
main.h
|
main.h
|
||||||
utils.h
|
|
||||||
)
|
)
|
||||||
set(SRC_FILES
|
set(SRC_FILES
|
||||||
angle.cpp
|
angle.cpp
|
||||||
location.cpp
|
location.cpp
|
||||||
main.cpp
|
main.cpp
|
||||||
utils.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
set(DOC_FILES
|
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_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_MAJOR 1)
|
||||||
set(META_VERSION_MINOR 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})
|
set(META_APP_VERSION ${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH})
|
||||||
|
|
||||||
# find c++utilities
|
# find c++utilities
|
||||||
|
|
15
angle.cpp
15
angle.cpp
|
@ -1,7 +1,7 @@
|
||||||
#include "./utils.h"
|
|
||||||
#include "./angle.h"
|
#include "./angle.h"
|
||||||
|
|
||||||
#include <c++utilities/application/failure.h>
|
#include <c++utilities/application/failure.h>
|
||||||
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace ApplicationUtilities;
|
using namespace ApplicationUtilities;
|
||||||
|
using namespace ConversionUtilities;
|
||||||
|
|
||||||
Angle::Angle() :
|
Angle::Angle() :
|
||||||
m_val(0)
|
m_val(0)
|
||||||
|
@ -38,26 +39,26 @@ Angle::Angle(const string &value, AngularMeasure measure) :
|
||||||
{
|
{
|
||||||
switch(measure) {
|
switch(measure) {
|
||||||
case AngularMeasure::Radian:
|
case AngularMeasure::Radian:
|
||||||
m_val += ConversionUtilities::numberFromString<double>(value);
|
m_val += stringToNumber<double>(value);
|
||||||
break;
|
break;
|
||||||
case AngularMeasure::Degree:
|
case AngularMeasure::Degree:
|
||||||
{
|
{
|
||||||
string::size_type mpos, spos = string::npos;
|
string::size_type mpos, spos = string::npos;
|
||||||
mpos = value.find(':');
|
mpos = value.find(':');
|
||||||
if(mpos == string::npos)
|
if(mpos == string::npos)
|
||||||
m_val += ConversionUtilities::numberFromString<double>(value);
|
m_val += stringToNumber<double>(value);
|
||||||
else if(mpos >= (value.length() - 1))
|
else if(mpos >= (value.length() - 1))
|
||||||
throw Failure("excepted minutes after ':' in " + value);
|
throw Failure("excepted minutes after ':' in " + value);
|
||||||
else {
|
else {
|
||||||
m_val += ConversionUtilities::numberFromString<double>(value.substr(0, mpos));
|
m_val += stringToNumber<double>(value.substr(0, mpos));
|
||||||
spos = value.find(':', mpos + 1);
|
spos = value.find(':', mpos + 1);
|
||||||
if(spos == string::npos)
|
if(spos == string::npos)
|
||||||
m_val += ConversionUtilities::numberFromString<double>(value.substr(mpos + 1)) / 60.0;
|
m_val += stringToNumber<double>(value.substr(mpos + 1)) / 60.0;
|
||||||
else if(spos >= (value.length() - 1))
|
else if(spos >= (value.length() - 1))
|
||||||
throw Failure("excepted seconds after second ':'' in " + value);
|
throw Failure("excepted seconds after second ':'' in " + value);
|
||||||
else
|
else
|
||||||
m_val += (ConversionUtilities::numberFromString<double>(value.substr(mpos + 1, spos - mpos - 1)) / 60.0)
|
m_val += (stringToNumber<double>(value.substr(mpos + 1, spos - mpos - 1)) / 60.0)
|
||||||
+ (ConversionUtilities::numberFromString<double>(value.substr(spos + 1)) / 3600.0);
|
+ (stringToNumber<double>(value.substr(spos + 1)) / 3600.0);
|
||||||
}
|
}
|
||||||
m_val = m_val * M_PI / 180.0;
|
m_val = m_val * M_PI / 180.0;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "./location.h"
|
#include "./location.h"
|
||||||
#include "./utils.h"
|
|
||||||
|
|
||||||
#include <c++utilities/application/failure.h>
|
#include <c++utilities/application/failure.h>
|
||||||
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace ApplicationUtilities;
|
using namespace ApplicationUtilities;
|
||||||
|
using namespace ConversionUtilities;
|
||||||
|
|
||||||
// WGS84 Parameters
|
// WGS84 Parameters
|
||||||
#define WGS84_A 6378137.0 // major axis
|
#define WGS84_A 6378137.0 // major axis
|
||||||
|
@ -258,10 +259,10 @@ void Location::setValueByProvidedUtmWgs4Coordinates(const string &utmWgs4Coordin
|
||||||
if(epos != 0 && epos != string::npos) {
|
if(epos != 0 && epos != string::npos) {
|
||||||
string::size_type npos = utmWgs4Coordinates.find('N', epos);
|
string::size_type npos = utmWgs4Coordinates.find('N', epos);
|
||||||
if(npos < (utmWgs4Coordinates.length() - 1) && npos != string::npos) {
|
if(npos < (utmWgs4Coordinates.length() - 1) && npos != string::npos) {
|
||||||
int zone = ConversionUtilities::numberFromString<int>(utmWgs4Coordinates.substr(0, epos - 1));
|
int zone = stringToNumber<int>(utmWgs4Coordinates.substr(0, epos - 1));
|
||||||
char zoneDesignator = utmWgs4Coordinates.at(epos - 1);
|
char zoneDesignator = utmWgs4Coordinates.at(epos - 1);
|
||||||
double east = ConversionUtilities::numberFromString<double>(utmWgs4Coordinates.substr(epos + 1, npos - epos - 1));
|
double east = stringToNumber<double>(utmWgs4Coordinates.substr(epos + 1, npos - epos - 1));
|
||||||
double north = ConversionUtilities::numberFromString<double>(utmWgs4Coordinates.substr(npos + 1));
|
double north = stringToNumber<double>(utmWgs4Coordinates.substr(npos + 1));
|
||||||
setValueByProvidedUtmWgs4Coordinates(zone, zoneDesignator, east, north);
|
setValueByProvidedUtmWgs4Coordinates(zone, zoneDesignator, east, north);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
10
main.cpp
10
main.cpp
|
@ -1,11 +1,11 @@
|
||||||
#include "./main.h"
|
#include "./main.h"
|
||||||
#include "./location.h"
|
#include "./location.h"
|
||||||
#include "./utils.h"
|
|
||||||
|
|
||||||
#include "resources/config.h"
|
#include "resources/config.h"
|
||||||
|
|
||||||
#include <c++utilities/application/argumentparser.h>
|
#include <c++utilities/application/argumentparser.h>
|
||||||
#include <c++utilities/application/failure.h>
|
#include <c++utilities/application/failure.h>
|
||||||
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
#include <c++utilities/io/catchiofailure.h>
|
#include <c++utilities/io/catchiofailure.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -173,8 +173,12 @@ int main(int argc, char *argv[])
|
||||||
} else {
|
} else {
|
||||||
cerr << "No arguments given. See --help for available commands.";
|
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) {
|
} 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;
|
cerr << endl;
|
||||||
printAngleFormatInfo(cerr);
|
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)
|
void printDestination(const string &locationstr, const string &distancestr, const string &bearingstr)
|
||||||
{
|
{
|
||||||
Location start = locationFromString(locationstr);
|
Location start = locationFromString(locationstr);
|
||||||
double distance = numberFromString<double>(distancestr);
|
double distance = stringToNumber<double>(distancestr);
|
||||||
Angle bearing(bearingstr, inputAngularMeasure);
|
Angle bearing(bearingstr, inputAngularMeasure);
|
||||||
printLocation(start.destination(distance, bearing));
|
printLocation(start.destination(distance, bearing));
|
||||||
}
|
}
|
||||||
|
|
22
utils.h
22
utils.h
|
@ -1,22 +0,0 @@
|
||||||
#ifndef UTILS_H
|
|
||||||
#define UTILS_H
|
|
||||||
|
|
||||||
#include <c++utilities/application/failure.h>
|
|
||||||
|
|
||||||
#include <iomanip>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
namespace ConversionUtilities
|
|
||||||
{
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
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
|
|
Loading…
Reference in New Issue