📄 atopkge.c
字号:
/*********************** self documentation **********************/
/***************************************************************************
ATOPKGE - convert ascii to arithmetic and with error checking
eatoh ascii to short
eatou ascii to unsigned short
eatoi ascii to int
eatop ascii to unsigned
eatol ascii to long
eatov ascii to unsigned long
eatof ascii to float
eatod ascii to double
****************************************************************************
Function Prototypes:
short eatoh(char *s);
unsigned short eatou(char *s);
int eatoi(char *s);
unsigned int eatop(char *s);
long eatol(char *s);
unsigned long eatov(char *s);
float eatof(char *s);
double eatod(char *s);
****************************************************************************
Input:
s string
Returned: type indicated
****************************************************************************
Notes:
Each of these routines acts like atoi, but has error checking:
This is a major revision of the tedious code required before
vendors implemented the ANSI C strtol, strtoul and strtod.
/**************** end self doc ********************************/
#include <errno.h>
/* eatoh - convert string s to short integer {SHRT_MIN:SHRT_MAX} */
short eatoh(char *s)
{
long n = strtol(s, NULL, 10);
if ( (n > SHRT_MAX) || (n < SHRT_MIN) || (errno == ERANGE) )
printf("%s: eatoh: overflow", __FILE__);
return (short) n;
}
/* eatou - convert string s to unsigned short integer {0:USHRT_MAX} */
unsigned short eatou(char *s)
{
unsigned long n = strtoul(s, NULL, 10);
if ( (n > USHRT_MAX) || (errno == ERANGE) )
printf("%s: eatou: overflow", __FILE__);
return (unsigned short) n;
}
/* eatoi - convert string s to integer {INT_MIN:INT_MAX} */
int eatoi(char *s)
{
long n = strtol(s, NULL, 10);
if ( (n > INT_MAX) || (n < INT_MIN) || (errno == ERANGE) )
printf("%s: eatoi: overflow", __FILE__);
return (int) n;
}
/* eatop - convert string s to unsigned integer {0:UINT_MAX} */
unsigned int eatop(char *s)
{
unsigned long n = strtoul(s, NULL, 10);
if ( (n > UINT_MAX) || (errno == ERANGE) )
printf("%s: eatop: overflow", __FILE__);
return (unsigned int) n;
}
/* eatol - convert string s to long integer {LONG_MIN:LONG_MAX} */
long eatol(char *s)
{
long n = strtol(s, NULL, 10);
if (errno == ERANGE)
printf("%s: eatol: overflow", __FILE__);
return n;
}
/* eatov - convert string s to unsigned long {0:ULONG_MAX} */
unsigned long eatov(char *s)
{
unsigned long n = strtoul(s, NULL, 10);
if (errno == ERANGE)
printf("%s: eatov: overflow", __FILE__);
return n;
}
/* eatof - convert string s to float {-FLT_MAX:FLT_MAX} */
float eatof(char *s)
{
float x = strtod(s, NULL);
if ( (x > FLT_MAX) || (x < -FLT_MAX) || (errno == ERANGE) )
printf("%s: eatof: overflow", __FILE__);
return (float) x;
}
/* eatod - convert string s to double {-DBL_MAX:DBL_MAX} */
double eatod(char *s)
{
double x = strtod(s, NULL);
/* errno == ERANGE suffices if compiler sets errno on overflow */
if ( (errno == ERANGE) || (x > DBL_MAX) || (x < -DBL_MAX) )
printf("%s: eatod: overflow", __FILE__);
return x;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -