string compat functions throw

This commit is contained in:
badaix 2016-04-25 19:27:21 +02:00
parent 244202674b
commit f11a7055a1
12 changed files with 36 additions and 18 deletions

View file

@ -19,7 +19,7 @@
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include "clientConnection.h" #include "clientConnection.h"
#include "common/compat.h" #include "common/strCompat.h"
#include "common/snapException.h" #include "common/snapException.h"
#include "message/hello.h" #include "message/hello.h"
#include "common/log.h" #include "common/log.h"

View file

@ -32,7 +32,7 @@
#endif #endif
#include "common/log.h" #include "common/log.h"
#include "common/signalHandler.h" #include "common/signalHandler.h"
#include "common/compat.h" #include "common/strCompat.h"
using namespace std; using namespace std;

View file

@ -21,7 +21,7 @@
#include <sstream> #include <sstream>
#include "sampleFormat.h" #include "sampleFormat.h"
#include "common/compat.h" #include "common/strCompat.h"
#include "common/utils.h" #include "common/utils.h"
#include "common/log.h" #include "common/log.h"

View file

@ -7,6 +7,10 @@
#ifdef NO_CPP11_STRING #ifdef NO_CPP11_STRING
#include <sstream> #include <sstream>
#include <cstdlib> #include <cstdlib>
#include <cmath>
#include <climits>
#include <stdexcept>
#include <cerrno>
#endif #endif
@ -24,19 +28,26 @@ namespace cpt
#endif #endif
} }
static long stoul(const std::string& s) static long stoul(const std::string& str)
{ {
#ifdef NO_CPP11_STRING #ifdef NO_CPP11_STRING
return atol(s.c_str()); errno = 0;
char *temp;
long val = strtol(str.c_str(), &temp, 10);
if (temp == str.c_str() || *temp != '\0')
throw std::invalid_argument("stoi");
if (((val == LONG_MIN) || (val == LONG_MAX)) && (errno == ERANGE))
throw std::out_of_range("stoi");
return val;
#else #else
return std::stoul(s); return std::stoul(str);
#endif #endif
} }
static int stoi(const std::string& str) static int stoi(const std::string& str)
{ {
#ifdef NO_CPP11_STRING #ifdef NO_CPP11_STRING
return strtol(str.c_str(), 0, 10); return cpt::stoul(str);
#else #else
return std::stoi(str); return std::stoi(str);
#endif #endif
@ -45,7 +56,14 @@ namespace cpt
static double stod(const std::string& str) static double stod(const std::string& str)
{ {
#ifdef NO_CPP11_STRING #ifdef NO_CPP11_STRING
return strtod(str.c_str(), NULL); errno = 0;
char *temp;
double val = strtod(str.c_str(), &temp);
if (temp == str.c_str() || *temp != '\0')
throw std::invalid_argument("stod");
if ((val == HUGE_VAL) && (errno == ERANGE))
throw std::out_of_range("stod");
return val;
#else #else
return std::stod(str.c_str()); return std::stod(str.c_str());
#endif #endif
@ -54,7 +72,7 @@ namespace cpt
static long double strtold(const char* str, char** endptr) static long double strtold(const char* str, char** endptr)
{ {
#ifdef NO_CPP11_STRING #ifdef NO_CPP11_STRING
return strtod(str, endptr); return cpt::stod(str);
#else #else
return std::strtold(str, endptr); return std::strtold(str, endptr);
#endif #endif
@ -63,7 +81,7 @@ namespace cpt
static float strtof(const char* str, char** endptr) static float strtof(const char* str, char** endptr)
{ {
#ifdef NO_CPP11_STRING #ifdef NO_CPP11_STRING
return (float)strtod(str, endptr); return (float)cpt::stod(str);
#else #else
return std::strtof(str, endptr); return std::strtof(str, endptr);
#endif #endif

2
externals/json.hpp vendored
View file

@ -61,7 +61,7 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation.
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "common/compat.h" #include "common/strCompat.h"
// enable ssize_t on MinGW // enable ssize_t on MinGW
#ifdef __GNUC__ #ifdef __GNUC__

View file

@ -22,7 +22,7 @@
#include <fstream> #include <fstream>
#include <cerrno> #include <cerrno>
#include "common/snapException.h" #include "common/snapException.h"
#include "common/compat.h" #include "common/strCompat.h"
#include "common/log.h" #include "common/log.h"
using namespace std; using namespace std;

View file

@ -19,7 +19,7 @@
#include <iostream> #include <iostream>
#include "flacEncoder.h" #include "flacEncoder.h"
#include "common/compat.h" #include "common/strCompat.h"
#include "common/snapException.h" #include "common/snapException.h"
#include "common/log.h" #include "common/log.h"

View file

@ -21,7 +21,7 @@
#include "oggEncoder.h" #include "oggEncoder.h"
#include "common/snapException.h" #include "common/snapException.h"
#include "common/compat.h" #include "common/strCompat.h"
#include "common/utils.h" #include "common/utils.h"
#include "common/log.h" #include "common/log.h"

View file

@ -23,7 +23,7 @@
#include "encoder/encoderFactory.h" #include "encoder/encoderFactory.h"
#include "common/snapException.h" #include "common/snapException.h"
#include "common/compat.h" #include "common/strCompat.h"
#include "pcmStream.h" #include "pcmStream.h"
#include "common/log.h" #include "common/log.h"

View file

@ -26,7 +26,7 @@
#include "encoder/encoderFactory.h" #include "encoder/encoderFactory.h"
#include "common/log.h" #include "common/log.h"
#include "common/snapException.h" #include "common/snapException.h"
#include "common/compat.h" #include "common/strCompat.h"
using namespace std; using namespace std;

View file

@ -20,7 +20,7 @@
#include "pipeStream.h" #include "pipeStream.h"
#include "fileStream.h" #include "fileStream.h"
#include "common/utils.h" #include "common/utils.h"
#include "common/compat.h" #include "common/strCompat.h"
#include "common/log.h" #include "common/log.h"
#include "common/snapException.h" #include "common/snapException.h"

View file

@ -18,7 +18,7 @@
#include <common/utils.h> #include <common/utils.h>
#include <common/compat.h> #include <common/strCompat.h>
#include <common/log.h> #include <common/log.h>
#include "streamUri.h" #include "streamUri.h"