Fellow C pushers:
Deliberately avoiding a look at Jack Klein's bulletproof code for a
robust use of strtol(), I respectfully submit the following for review
and vigorous critique, desiring unapologetic pedantry (in the *very*
best sense of that word).
In the way of background, I may as well be a C newbie since the nature
of my job has not involved day to day use of the language for maybe a
decade. Although I am entering my ``golden years'', I decided to put
aside my AARP magazine and try to regain my youth^wC skills by
re-reading some good books, the clc-faq, n1256.pdf and getting some
hearty laughs from reading you know who.
No...I don't plan to post such toy code on a regular basis but feedback
from the experts in c.l.c may help me get my shaky C legs back.
All comments were elided for brevity in this attempt to ``convert''
argument(s) to double(s) with a fair amount of validation:
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
static int is_valid_double (const char *s, const char *endptr,
double result, int save_errno, char **what)
{
int status = 0;
if(result == 0 && save_errno == ERANGE)
*what = "underflow" ;
else if((result == HUGE_VAL || result == -HUGE_VAL) &&
save_errno == ERANGE)
*what = "overflow";
else if(s == endptr)
*what = "no characters in subject sequence";
else {
int is_recognized = 1;
for(; *endptr; ++endptr) {
if(!isspace(*en dptr)) {
is_recognized = 0;
break;
}
}
if(is_recognize d)
status = 1;
else
*what = "unrecogniz ed character(s) in final string";
}
return status;
}
static void maybe_convert_t o_double(const char *s)
{
double result;
char *endptr, *what;
const char *indication = "failed";
int save_errno, valid;
result = strtod(s, &endptr);
save_errno = errno;
if((valid = is_valid_double (s, endptr, result, save_errno, &what)) != 0)
indication = "successful ";
printf("result: %g, conversion %s", result, indication);
if(valid)
putchar('\n');
else
printf(" due to %s\n", what);
}
int main(int argc, char **argv)
{
int i;
for(i = 1; i < argc; ++i)
maybe_convert_t o_double(argv[i]);
return EXIT_SUCCESS;
}
Deliberately avoiding a look at Jack Klein's bulletproof code for a
robust use of strtol(), I respectfully submit the following for review
and vigorous critique, desiring unapologetic pedantry (in the *very*
best sense of that word).
In the way of background, I may as well be a C newbie since the nature
of my job has not involved day to day use of the language for maybe a
decade. Although I am entering my ``golden years'', I decided to put
aside my AARP magazine and try to regain my youth^wC skills by
re-reading some good books, the clc-faq, n1256.pdf and getting some
hearty laughs from reading you know who.
No...I don't plan to post such toy code on a regular basis but feedback
from the experts in c.l.c may help me get my shaky C legs back.
All comments were elided for brevity in this attempt to ``convert''
argument(s) to double(s) with a fair amount of validation:
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
static int is_valid_double (const char *s, const char *endptr,
double result, int save_errno, char **what)
{
int status = 0;
if(result == 0 && save_errno == ERANGE)
*what = "underflow" ;
else if((result == HUGE_VAL || result == -HUGE_VAL) &&
save_errno == ERANGE)
*what = "overflow";
else if(s == endptr)
*what = "no characters in subject sequence";
else {
int is_recognized = 1;
for(; *endptr; ++endptr) {
if(!isspace(*en dptr)) {
is_recognized = 0;
break;
}
}
if(is_recognize d)
status = 1;
else
*what = "unrecogniz ed character(s) in final string";
}
return status;
}
static void maybe_convert_t o_double(const char *s)
{
double result;
char *endptr, *what;
const char *indication = "failed";
int save_errno, valid;
result = strtod(s, &endptr);
save_errno = errno;
if((valid = is_valid_double (s, endptr, result, save_errno, &what)) != 0)
indication = "successful ";
printf("result: %g, conversion %s", result, indication);
if(valid)
putchar('\n');
else
printf(" due to %s\n", what);
}
int main(int argc, char **argv)
{
int i;
for(i = 1; i < argc; ++i)
maybe_convert_t o_double(argv[i]);
return EXIT_SUCCESS;
}
Comment