To lexical_cast(const From& val){ To output; bool except=false; std::stringstream ss,ss_c; std::stringstream checker_before, checker_after; ss<<val; ss>>output; checker_before<<val; checker_after<<output; if(checker_before.str().empty()) except=true; if(checker_after.str().empty()) except=true; if(checker_before.str().size()>1){ if(ischar(checker_before.str().back())) except=true; if(checker_before.str().back()==' ') except=true; } if(checker_before.str().size()>0){ if(checker_before.str()[0]==' ') except=true; } if(ss.bad()) except=true; { long long int a,b; checker_after>>a; checker_before>>b; if(a!=b) except=true; } if(except) throw bad_lexical_cast(); return output; }
To lexical_cast(From a) { To res; std::stringstream str; if(!(str << a && str >> res)) { throw bad_lexical_cast(); } else { return res;
//#################### PropReaderUtil - PUBLIC METHODS #################### std::string PropReaderUtil::next_sized_token(std::string& input) { size_t i = input.find_first_of(' '); if(i == std::string::npos) throw bad_lexical_cast(); std::string lenString = input.substr(1,i-2); int len = lexical_cast<int>(lenString); std::string tok = input.substr(i+1,len); input.erase(0,i+len+2); return tok; }
To operator()(From value) { DEBUG_THROW("generic"); To result; std::stringstream sstr; if(!(sstr << value && sstr >> result)) { throw bad_lexical_cast(); } else { return result;
inline unsigned long lexical_cast( const std::string& str ) { errno = 0; const char* stringToConvert = str.c_str(); const char* endOfConversion = stringToConvert; unsigned long value = strtoul( stringToConvert, const_cast<char**>(&endOfConversion), 0 ); if( ( value == 0ul && stringToConvert == endOfConversion ) || // error: conversion could not be performed errno != 0 ) // error: overflow or underflow throw bad_lexical_cast();//throw bad_lexical_cast( std::type_info( str ), std::type_info( value ) ); return value; }
inline float lexical_cast( const std::string& str ) { errno = 0; const char* stringToConvert = str.c_str(); const char* endOfConversion = stringToConvert; float value = (float) strtod( stringToConvert, const_cast<char**>(&endOfConversion) ); if( ( value == 0.0f && stringToConvert == endOfConversion ) || // error: conversion could not be performed errno != 0 ) // error: overflow or underflow throw bad_lexical_cast();//throw bad_lexical_cast( std::type_info( str ), std::type_info( value ) ); return value; }
int lexical_cast<int, const char*>(const char* a) { char* endptr; int res = strtol(a, &endptr, 10); if (*a == '\0' || *endptr != '\0') { throw bad_lexical_cast(); } else { return res; } }
int lexical_cast<int, const std::string&>(const std::string& a) { char* endptr; int res = strtol(a.c_str(), &endptr, 10); if (a.empty() || *endptr != '\0') { throw bad_lexical_cast(); } else { return res; } }
float lexical_cast<float, const char*>(const char* a) { char* endptr; float res = static_cast<float>(strtod(a, &endptr)); if (*a == '\0' || *endptr != '\0') { throw bad_lexical_cast(); } else { return res; } }
float lexical_cast<float, const std::string&>(const std::string& a) { char* endptr; float res = static_cast<float>(strtod(a.c_str(), &endptr)); if (a.empty() || *endptr != '\0') { throw bad_lexical_cast(); } else { return res; } }
double lexical_cast<double, const char*>(const char* a) { char* endptr; double res = strtod(a, &endptr); if (*a == '\0' || *endptr != '\0') { throw bad_lexical_cast(); } else { return res; } }
double lexical_cast<double, const std::string&>(const std::string& a) { char* endptr; double res = strtod(a.c_str(), &endptr); if (a.empty() || *endptr != '\0') { throw bad_lexical_cast(); } else { return res; } }
void read_locations(const config& cfg, std::vector<map_location>& locs) { const std::vector<std::string> xvals = utils::split(cfg["x"]); const std::vector<std::string> yvals = utils::split(cfg["y"]); if (xvals.size() != yvals.size()) { throw bad_lexical_cast(); } std::transform(xvals.begin(), xvals.end(), yvals.begin(), std::back_inserter(locs), &read_locations_helper); }