home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff319.lzh / CNewsSrc / cnews.src.lzh / libcnews / ltoza.c < prev    next >
C/C++ Source or Header  |  1989-07-03  |  1KB  |  58 lines

  1. /*
  2.  * ltoza, ltozan - long to zero-padded ascii conversions
  3.  *
  4.  * These functions exist only because there is no portable way
  5.  * to do this with printf and there may be no way do it at all
  6.  * with printf on V7, due to a bug in V7's printf.
  7.  */
  8. #include <stdlib.h>
  9.  
  10. #define RADIX 10
  11.  
  12. /*
  13.  * convert value to at most width characters in outstr, padding with
  14.  * zeros on the left (after any sign); do not terminate with a NUL.
  15.  * returns true iff the value fits in width characters.
  16.  */
  17. int                    /* boolean */
  18. ltozan(outstr, value, width)
  19. char *outstr;
  20. long value;
  21. int width;
  22. {
  23.     register char *op = outstr;
  24.     register long wval = value;
  25.     register int wwid = width;
  26.  
  27.     if (wval < 0 && wwid > 0) {
  28.         *op++ = '-';
  29.         --wwid;
  30.         wval = -wval;        /* fails on smallest int; tough */
  31.     }
  32.     op += wwid - 1;            /* find right end */
  33.     while (wwid-- > 0) {        /* generate "wwid" digits */
  34.         register ldiv_t result;
  35.  
  36.         result = ldiv(wval, (long)RADIX);    /* shades of V6! */
  37.         wval = result.quot;
  38.         *op-- = result.rem + '0';
  39.     }
  40.     return wval == 0;
  41. }
  42.  
  43. /*
  44.  * convert value to at most width characters in outstr, padding with
  45.  * zeros on the left (after any sign); terminate with a NUL.
  46.  */
  47. int                    /* boolean */
  48. ltoza(outstr, value, width)
  49. register char *outstr;            /* char outstr[width+1]; */
  50. long value;
  51. register int width;
  52. {
  53.     register int fits = ltozan(outstr, value, width);
  54.  
  55.     outstr[width] = '\0';
  56.     return fits;
  57. }
  58.