I'm not very experienced with C string conversion functions, but from the references it seems that strtoul can already tell you that:
Code:
char* end;
num = strtoul(str, &end, 10);
//if *end is not 0 then there are unconsumed characters in str
if (*end != '\0')
Another thing is detecting for under- and over-flow. Overflow seems possible with strtoul, but apparently underflow needs to be detected manually.
So altogether the conversion function might look like this:
Code:
int smart_to_ulong(const char* str, unsigned long* num)
{
char* end = str;
//strtoul apparently doesn't report errors on negative values
if (!isdigit(str[0])) {
return 0;
}
errno = 0;
*num = strtoul(str, &end, 10);
//not read to the end
if (*end != '\0') {
return 0;
}
//over-flow
if (errno == ERANGE) {
return 0;
}
return 1;
}