From: teodor Date: Thu, 16 Oct 2008 11:32:16 +0000 (+0000) Subject: Add 64-bit variant for xtostr and strtox X-Git-Url: http://sigaev.ru/git/gitweb.cgi?a=commitdiff_plain;h=bc1341fcd04288e65a7ef44db4f20ecaed85ab3e;p=tedtools.git Add 64-bit variant for xtostr and strtox --- diff --git a/tools.c b/tools.c index a6c0206..07c0e46 100644 --- a/tools.c +++ b/tools.c @@ -71,7 +71,23 @@ strtox(char *src, char **end) { return res; } -static char buf[10]; +u_int64_t +strtox64(char *src, char **end) { + u_int32_t res=0, i=0; + + while(src && *src && isXdigit(*src) && i<16) { + res = res << 4; + res |= str2hex[ *(unsigned char*)src ]; + src++; + i++; + } + + if ( end ) + *end=src; + return res; +} + +static char buf[32]; static char hex2str[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; char* @@ -89,6 +105,21 @@ xtostr(u_int32_t x) { return buf; } +char* +x64tostr(u_int64_t x) { + char *ptr=buf; + int i,wast=0; + for(i=60;i>=0;i-=4) { + *ptr = hex2str[ (x>>i) & 0x0f ]; + if ( wast || *ptr!='0' || i==0 ) { + ptr++; + wast=1; + } + } + *ptr='\0'; + return buf; +} + double timediff(struct timeval *begin, struct timeval *end) { return ((double)( end->tv_sec - begin->tv_sec )) + ( (double)( end->tv_usec-begin->tv_usec ) ) / 1.0e+6; diff --git a/tools.h b/tools.h index 473e21b..f4ee5b7 100644 --- a/tools.h +++ b/tools.h @@ -34,8 +34,10 @@ #include u_int32_t strtox(char *src, char **end); +u_int64_t strtox64(char *src, char **end); #define atox(x) strtox((x),NULL) char* xtostr(u_int32_t x); +char* x64tostr(u_int64_t x); double timediff(struct timeval *begin, struct timeval *end); double elapsedtime(struct timeval *begin);