32 lines
715 B
C
32 lines
715 B
C
#ifndef STRTOUI_H
|
|
#define STRTOUI_H
|
|
|
|
#include "stdlib.h"
|
|
#include "ctype.h"
|
|
#include "limits.h"
|
|
#include "errno.h"
|
|
|
|
enum strtoui_error{
|
|
STRTOUI_SUCCESS,
|
|
STRTOUI_OVERFLOW,
|
|
STRTOUI_UNDERFLOW,
|
|
STRTOUI_INCONVERTIBLE
|
|
};
|
|
|
|
enum strtoui_error strtoui(unsigned int *out, char *s, int base) {
|
|
char *end;
|
|
if(s[0] == '\0' || isspace(s[0]))
|
|
return STRTOUI_INCONVERTIBLE;
|
|
long l = strtol(s, &end, base);
|
|
if(l > UINT_MAX || (errno == ERANGE && l == LONG_MAX))
|
|
return STRTOUI_OVERFLOW;
|
|
if(l < 0|| (errno == ERANGE && l == LONG_MIN))
|
|
return STRTOUI_UNDERFLOW;
|
|
if(*end != '\0')
|
|
return STRTOUI_INCONVERTIBLE;
|
|
*out = l;
|
|
return STRTOUI_SUCCESS;
|
|
}
|
|
|
|
#endif
|