/* Version 0.0
** From "Wayne's Little DSA Library" (DSA == Data Structures and
** Algorithms) Feel free to change, modify, or burn these sources, but if
** you modify them please don't distribute your changes without a clear
** indication that you've done so.  If you think your change is spiffy,
** send it to me and maybe I'll include it in the next release.
**
** Wayne Hayes, wayne@csri.utoronto.ca (preffered), or wayne@csri.toronto.edu
*/

#include <ctype.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "longlong.h"

char *lltoa(char *foo, long long n)
{
    char buf[MAX_LLTOA_BUFS][MAX_ASCII_LONG_LONG];
    static int whichBuf;
    int d = 0, i;
    int sign;
    if(!foo)
    {
	foo=buf[whichBuf];
	/* Cycle through a fixed list of buffers.
	 * This allows one to, for example, call lltoa
	 * twice in the same printf statement and
	 * correctly print out both longlongs.
	 */
	whichBuf = (whichBuf+1) % MAX_LLTOA_BUFS;
    }
    if(n < 0)
    {
	sign = -1;
	n = -n;
    }
    else
	sign = 1;

    if(n < 0)   /* if it was maximum negative number, it doesn't have a positive representation */
    {
	strcpy(foo, "-9223372036854775808");
	return foo;
    }
    
    /* generate characters in reverse order */
    do
    {
	foo[d++] = n % 10 + '0';
	n = n/10;
    } while(n);

    if(sign == -1)
	foo[d++] = '-';

    foo[d] = '\0';

    /* now reverse order */
    for(i=0; i < (d >> 1); i++)
    {   
	char temp = foo[i];
	foo[i] = foo[d-1-i];
	foo[d-1-i] = temp;
    }

    return foo;
}

long long atoll(char *p)
{
    long long n = 0;
    long long sign;     /* just the high bit */
    while(isspace(*p))
	++p;
    switch(*p)
    {
    case '-': sign = ((long long)1) << (LONG_LONG_BITS-1); ++p; break;
    case '+': sign = 0; ++p; break;
    default: sign = 0; break;
    }
    while(isdigit(*p))
    {
	n *= 10;
	n += *p++ - '0';
    }
    return (sign | n);
}

long long getll(FILE *fp)
{
    char word[22];
    fscanf(fp, "%s", word);
    return atoll(word);
}
