#include "file64.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifndef WIN32
#include <sys/stat.h>
#endif


#include <stdarg.h>
#include "vsnprintf.h"

char *nice_num_big(bigint d, char *tmp)
{
	sprintf(tmp,"%d",(int) d);
	if (d>5000) sprintf(tmp,"%dk",(int) d/1024);
	if (d>5000000) sprintf(tmp,"%dmb",(int) d/1024/1024);
	return tmp;
}

char* lib_itoa_bigint(char *s, bigint i)
{
	if (i/(1024*1024*1024)>20)		sprintf(s,"%dgig",(int) (i/(1024*1024*1024)));
	else if (i/(1024*1024*1024)>0)	sprintf(s,"%.1fgig",(double) (i/(1024.0*1024*1024)));
	else if (i/(1024*1024)>20)		sprintf(s,"%dmb",(int)(i/(1024*1024)));
	else if (i/(1024*1024)>0)		sprintf(s,"%.1fmb",(double) (i/(1024.0*1024)));
	else if (i/(1024)>20)			sprintf(s,"%dkb",(int)(i/(1024)));
	else if (i/(1024)>0)			sprintf(s,"%.1fkb",(double) (i/(1024.0)));
	else sprintf(s,"%d",(int) i);
	return s;
}


char *bigint_to_str(bigint x, char *tmp)
{
	if (tmp==NULL) return "";
#ifdef WIN32
	snprintf(tmp,99,"%I64d",x);
#else
	snprintf(tmp,99,"%lld",x);
#endif
	return tmp;
}
char *bigint_to_strx(bigint x, char *tmp)
{
	if (tmp==NULL) return "";
#ifdef WIN32
	snprintf(tmp,99,"%016I64x",x);
#else
	snprintf(tmp,99,"%016llx",x);
#endif
	return tmp;
}
bigint str_to_bigint(char *tmp)
{
#ifdef WIN32
	return _atoi64(tmp);
#else
	return atoll(tmp);
#endif
}
// chrisp expecting this to fail to build on linux, need to ifdef it to make it work right.
// define both, and make sure you rebuild the SAM modulese for this!!! _LARGEFILE_SOURCE and _LARGEFILE64_SOURCE
bigint bigint_file_size(char *fname)
{
#ifdef _LARGEFILE_SOURCE
	struct stat64 bf;
	if (stat64(fname, &bf)!=0) return -1;
	return bf.st_size;
#else
	#ifdef WIN32
		struct _stati64 bf;
		if (stat64(fname, &bf)!=0) return -1;
		return bf.st_size;
	#else
		struct stat bf;
		if (stat(fname, &bf)!=0) return -1;
		return bf.st_size;
	#endif
#endif
}
