- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
template<class T, class U> bool convertStrToInt(const char* str, int base, T &res, T def, U (*strto)(const char *, char **, int ))
{
char *endptr = NULL;
errno = 0;//man wants it
if ( ! str || *str == '\0' )
{
res = def;
return false;
}
U result = strto(str, &endptr, base);
if (errno == ERANGE || (*endptr != '\0') || ! *str) {
std::stringstream errorStr;
errorStr << "convertStrToInt failed ; string = '" << str << "' result ='" << result << "' endptr = '" << endptr << "' errno = '" << errno << "'";
res = def;
log_error("%s", errorStr.str().c_str());
return false;
}
res = static_cast<T>(result);
return true;
}