#include <conf.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#ifdef	HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef	HAVE_SYS_TIME_H
#ifdef	TIME_WITH_SYS_TIME
#include <time.h>
#endif
#endif
#include <x_types.h>
#include <utils.h>

UChar *
Real64_to_intstr(Real64 d, UChar * retstr)
{
  UChar		str[70];

  UChar		*cptr;
  Real64	p10, d_10;
  int		c;

  cptr = (retstr ? retstr : str);

  if(d >= 1.0e+40){
    sprintf(cptr, T_("%lg <integer representation not possible>"),
				(double) d);
    return(retstr ? retstr : (UChar *) strdup(str));
  }

  d += 0.5;
  if(d < 1.0){
    strcpy(cptr, "0");
    return(retstr ? retstr : (UChar *) strdup(str));
  }

  d_10 = d / 10.0;
  for(p10 = 1.0; p10 <= d_10; p10 *= 10.0);

  while(p10 >= 1.0){
    c = (int)(d / p10);
    *(cptr++) = '0' + c;
    d -= (Real64) c * p10;
    p10 /= 10.0;
  }

  *cptr = '\0';

  return(retstr ? retstr : (UChar *) strdup(str));
}

UChar *
time_t_to_intstr(time_t d, UChar * retstr)
{
  UChar		str[70];

  UChar		*cptr;
  time_t	p10, d_10;
  int		c, n;
  Flag		neg = NO;

  if(d < 0){
    d = -d;
    neg = YES;
  }

  p10 = (time_t) 1;
  d_10 = d / 10;
  for(n = 0; p10 <= d_10; n++)
    p10 = (p10 << 3) + (p10 << 1);

  cptr = (retstr ? retstr : str);
  if(neg)
    *(cptr++) = '-';

  while(p10 >= (time_t) 1){
    c = d / p10;
    *(cptr++) = '0' + c;
    d -= (time_t) c * p10;
    p10 /= 10;
  }

  *cptr = '\0';

  return(retstr ? retstr : (UChar *) strdup(str));
}

UChar *
size_t_to_intstr(size_t d, UChar * retstr)
{
  UChar		str[70];

  UChar		*cptr;
  size_t	p10, d_10;
  int		c, n;

  p10 = (size_t) 1;
  d_10 = d / 10;
  for(n = 0; p10 <= d_10; n++)
    p10 = (p10 << 3) + (p10 << 1);

  cptr = (retstr ? retstr : str);
  while(p10 >= (size_t) 1){
    c = d / p10;
    *(cptr++) = '0' + c;
    d -= (size_t) c * p10;
    p10 /= 10;
  }

  *cptr = '\0';

  return(retstr ? retstr : (UChar *) strdup(str));
}

UChar *
off_t_to_intstr(off_t d, UChar * retstr)
{
  UChar		str[70];

  UChar		*cptr;
  off_t		p10, d_10;
  int		c, n;

  p10 = (off_t) 1;
  d_10 = d / 10;
  for(n = 0; p10 <= d_10; n++)
    p10 = (p10 << 3) + (p10 << 1);

  cptr = (retstr ? retstr : str);
  while(p10 >= (off_t) 1){
    c = d / p10;
    *(cptr++) = '0' + c;
    d -= (off_t) c * p10;
    p10 /= 10;
  }

  *cptr = '\0';

  return(retstr ? retstr : (UChar *) strdup(str));
}

UChar *
ino_t_to_intstr(ino_t d, UChar * retstr)
{
  UChar		str[70];

  UChar		*cptr;
  ino_t		p10, d_10;
  int		c, n;

  p10 = (ino_t) 1;
  d_10 = d / 10;
  for(n = 0; p10 <= d_10; n++)
    p10 = (p10 << 3) + (p10 << 1);

  cptr = (retstr ? retstr : str);
  while(p10 >= (ino_t) 1){
    c = d / p10;
    *(cptr++) = '0' + c;
    d -= (ino_t) c * p10;
    p10 /= 10;
  }

  *cptr = '\0';

  return(retstr ? retstr : (UChar *) strdup(str));
}
