initial commit <3
This commit is contained in:
31
strtoui.h
Normal file
31
strtoui.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#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 > INT_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
|
||||
Reference in New Issue
Block a user