[comp.sys.amiga.programmer] looking for strtol

rtrevino@fornax.apsys.cirt.unm.edu (Roy) (05/10/91)

Howdy,

Has anyone out there written/ran across a PD version of the
Un*x strtol(char *nptr, char **eptr, int base) function?  This
is the one that returns a long and an end pointer pointer based
on a string and a number base.

It's not a terribly complicated function, but I'd rather not
reinvent it.  Surely someone has written one and/or can let me know 
where to find this nifty beast?...

Thanks in advance!
Roy Trevino
rtrevino@fornax.unm.edu

dillon@overload.Berkeley.CA.US (Matthew Dillon) (05/11/91)

In article <1991May10.061244.18920@ariel.unm.edu> rtrevino@fornax.apsys.cirt.unm.edu (Roy) writes:
>Howdy,
>
>Has anyone out there written/ran across a PD version of the
>Un*x strtol(char *nptr, char **eptr, int base) function?  This
>is the one that returns a long and an end pointer pointer based
>on a string and a number base.
>
>It's not a terribly complicated function, but I'd rather not
>reinvent it.  Surely someone has written one and/or can let me know
>where to find this nifty beast?...
>
>Thanks in advance!
>Roy Trevino
>rtrevino@fornax.unm.edu

    This is out of the DICE libs... since I wrote DICE, I can put
    selected parts of it in the public domain by posting source
    modules :-)  (but registered users: please not that *you* cannot!)

				    -Matt

/*
 *  STRTOL.C
 */

#include <stdio.h>
#include <string.h>

#ifndef HYPER
#define HYPER(x) x
#endif

long
HYPER(strtol)(ptr, tail, base)
const char *ptr;
char **tail;
int base;
{
    long v = 0;
    short ishex = 0;
    short c;
    short neg = 0;

    while (*ptr == ' ' || *ptr == '\t')
	++ptr;
    if (*ptr == '-') {
	neg = 1;
	++ptr;
    } else if (*ptr == '+')
	++ptr;

    if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
	ishex = 1;

    if (base == 0) {
	base = 10;
	if (ptr[0] == '0') {
	    base = 8;
	    if (ishex)
		base = 16;
	}
    }
    if (base == 16 && ishex)
	ptr += 2;
    for (;;) {
	c = *ptr;
	if (c >= '0' && c <= '9')
	    c -= '0';
	else if (c >= 'a' && c <= 'z')
	    c -= ('a' - 10);
	else if (c >= 'A' && c <= 'Z')
	    c -= ('A' - 10);
	else
	    break;
	if (c >= base)
	    break;
	v = v * base + c;
	++ptr;
    }
    if (tail)
	*tail = ptr;
    if (neg)
	v = -v;
    return(v);
}



--

    Matthew Dillon	    dillon@Overload.Berkeley.CA.US
    891 Regal Rd.	    uunet.uu.net!overload!dillon
    Berkeley, Ca. 94708
    USA