initial commit <3

This commit is contained in:
Daniel Cortés
2019-10-13 18:40:32 -03:00
commit 810965a9fb
4 changed files with 80 additions and 0 deletions

31
strtoui.h Normal file
View 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