示例#1
0
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;
}
示例#2
0
To lexical_cast(From a)
{
	To res;
	std::stringstream str;

	if(!(str << a && str >> res)) {
		throw bad_lexical_cast();
	} else {
		return res;
示例#3
0
//#################### 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;
}
示例#4
0
	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;
	}
示例#7
0
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;
	}
}
示例#8
0
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;
	}
}
示例#9
0
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;
	}
}
示例#10
0
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;
	}
}
示例#11
0
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;
	}
}
示例#12
0
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;
	}
}
示例#13
0
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);
}