static long getNum(const char *fname, const char *arg, int flags, const char *name) { long res; char *endptr; int base; if (arg == NULL || *arg == '\0') gnFail(fname, "null or empty string", arg, name); base = (flags & GN_ANY_BASE) ? 0 : (flags & GN_BASE_8) ? 8 : (flags & GN_BASE_16) ? 16 : 10; errno = 0; res = strtol(arg, &endptr, base); if (errno != 0) gnFail(fname, "strtol() failed", arg, name); if (*endptr != '\0') gnFail(fname, "nonnumeric characters", arg, name); if ((flags & GN_NONNEG) && res < 0) gnFail(fname, "negative value not allowed", arg, name); if ((flags & GN_GT_0) && res <= 0) gnFail(fname, "value must be > 0", arg, name); return res; }
unsigned long long getUnsigned(const char *fname, const char *arg, /*#{{{*/ register int32_t flags, const char *name) { register unsigned long long res = 0; register int32_t base = 0; char *endptr = NULL; if(arg == NULL || *arg == '\0'){ if(flags & GN_NOEXIT_){ fprintf(stderr,"\nNull or empty string.\n"); errno = EINVAL; return 0;} else{ gnFail(fname, "null or empty string", arg, name);} } /* determine the base to be passed to strtoul. */ base = (flags & GN_BASE_10) ? 10 : (flags & GN_BASE_16) ? 16 : (flags & GN_BASE_8) ? 8 : 0; errno = 0; /* just in case errno was set from who knows where */ res = strtoull(arg, &endptr, base); /* convert string to base */ /* error check result */ if(errno != 0){ if(flags & GN_NOEXIT_){ fprintf(stderr,"\nConversion failed.\n"); errno = EINVAL; return 0;} else{ gnFail(fname, "strtoll() failed", arg, name);} } if(*endptr != '\0'){ if(flags & GN_NOEXIT_){ fprintf(stderr,"\nNonnumeric characters.\n"); errno = EINVAL; return 0;} else{ gnFail(fname, "nonnumeric characters", arg, name);} } if((flags & GN_GT_O) && res <= 0){ if(flags & GN_NOEXIT_){ fprintf(stderr,"\nValue must be > 0.\n"); errno = EINVAL; return 0;} else{ gnFail(fname, "value must be > 0", arg, name);} } return res; } /* end getUnsigned #}}} */
int getInt(const char *arg, int flags, const char *name) { long res; res = getNum("getInt", arg, flags, name); if(res > INT_MAX || res < INT_MIN) gnFail("getInt", "integer out of range", arg, name); return (int) res; }
int getInt(const char *arg, int32_t flags, const char *varName)/*#{{{*/ { register long long res = 0; res = getNum("getInt", arg, flags, varName); /* make sure the converted number is in the range of an int */ if(res > INT_MAX || res < INT_MIN){ if(flags & GN_NOEXIT_){ fprintf(stderr,"\nInteger was out of range\n"); return 0;} else{ gnFail("getInt", "integer out of range", arg, varName);} } return (int) res; } /* end getInt #}}}*/
uint64_t getu64_t(const char *arg, int32_t flags, const char *varName)/*#{{{*/ { register unsigned long long res = 0; res = getUnsigned("getu64_t", arg, flags, varName); /* make sure the converted number is in the range of an uint64_t */ if(res > UINT64_MAX){ /* checks for unsigned in getNum */ if(flags & GN_NOEXIT_){ fprintf(stderr,"\nInteger was out of range\n"); errno = EINVAL; return 0;} else{ gnFail("getUnsigned", "integer out of range", arg, varName);} } return (uint64_t) res; } /* end get64_t #}}} */
static long getNum(const char *fname, const char *arg, int flags, const char *name) { long res; int base; char *endptr=NULL; if(arg == NULL || *arg == '\0') { gnFail(fname, "null or empty string", arg, name); } base= (flags & GN_ANY_BASE)? 0 : (flags & GN_BASE_) ? 8 : (flags & GN_BASE_16) ? 16:10; errno = 0; res = strtol(arg, &endptr, base); }