home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 9.ddi / IOSTRSR1.ZIP / OSTINT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.5 KB  |  164 lines

  1. /*[]------------------------------------------------------------[]*/
  2. /*|                                                              |*/
  3. /*|     ostint.cpp                                               |*/
  4. /*|                                                              |*/
  5. /*|     Class ostream                                            |*/
  6. /*|          ostream& ostream::operator<< (long l)               |*/
  7. /*|          ostream& ostream::operator<< (unsigned long ul)     |*/
  8. /*|                                                              |*/
  9. /*[]------------------------------------------------------------[]*/
  10.  
  11. /*
  12.  *      C/C++ Run Time Library - Version 5.0
  13.  *
  14.  *      Copyright (c) 1990, 1992 by Borland International
  15.  *      All Rights Reserved.
  16.  *
  17.  */
  18.  
  19. #include <ioconfig.h>
  20. #include <iostream.h>
  21.  
  22. /*
  23.  * Convert val>=0 to ascii in buffer b, with implied numeric base.
  24.  * Digits are acquired in reverse order, so we start at the end of the
  25.  * buffer and work forward.
  26.  * 'b' initially points to the end of the buffer, and is assumed big enough.
  27.  * Return a pointer to the beginning address of the null-terminated string.
  28.  */
  29.  
  30. // decimal conversion
  31.  
  32. static char* todec(char *b, unsigned long val)
  33. {
  34.     *b = '\0';
  35.     do  {
  36.         *--b = ((int) (val % 10)) + '0';
  37.         val /= 10;
  38.         } while( val );
  39.     return b;
  40. }
  41.  
  42. // octal conversion
  43.  
  44. static char* tooct(char *b, unsigned long val)
  45. {
  46.     *b = '\0';
  47.     do  {
  48.         *--b = (val & 7) + '0';
  49.         val /= 8;   // let compiler make this a shift if appropriate
  50.         } while( val );
  51.     return b;
  52. }
  53.  
  54. // hex conversion, with indicator for uppercase or lowercase letters
  55.  
  56. static char *tohex(char *b, unsigned long val, int upper)
  57. {
  58.     static char digits[2][16] = {
  59.     {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'},
  60.     {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}
  61.     };
  62.     char *d = upper ? &digits[1][0] : &digits[0][0];
  63.     *b = '\0';
  64.     do  {
  65.         *--b = d[(int)val & 0xf];
  66.         val /= 16;  // let compiler make this a shift if appropriate
  67.         } while( val );
  68.     return b;
  69. }
  70.  
  71.  
  72. // format and insert a signed long
  73.  
  74. ostream _FAR & ostream::operator<< (long l)
  75. {
  76.     char buf[MaxCharsInLong];   // result of conversion
  77.     char *prefix = 0;           // displayed numeric prefix string
  78.     char *p;
  79.  
  80.     // find conversion base
  81.     int base = (flags() & ios::hex) ? 16 : ((flags() & ios::oct) ? 8 : 10);
  82.  
  83.     // do we treat this as negative?  (oct and hex are unsigned)
  84.     int neg = base == 10  &&  l < 0;
  85.  
  86.     // value to use, exclusive of sign
  87.     unsigned long ul = neg ? -l : l;
  88.  
  89.     if( base == 10 )
  90.         {
  91.         p = todec(buf + MaxCharsInLong - 1, ul);
  92.  
  93.         // compute any sign prefix
  94.         if( ul )
  95.             if( neg )
  96.                 prefix = "-";
  97.             else if( flags() & ios::showpos )
  98.                 prefix = "+";
  99.         }
  100.     else if( base == 16 )
  101.         {
  102.         int upper = (flags() & ios::uppercase) != 0;
  103.         p = tohex(buf + MaxCharsInLong - 1, ul, upper);
  104.  
  105.         // compute any base prefix
  106.         if( flags() & ios::showbase )
  107.             prefix = upper ? "0X" : "0x";
  108.         }
  109.     else /* base == 8 */
  110.         {
  111.         p = tooct(buf + MaxCharsInLong - 1, ul);
  112.  
  113.         // compute any base prefix
  114.         if( flags() & ios::showbase )
  115.             prefix = "0";
  116.         }
  117.  
  118.     // now we have a formatted string for output, to be possibly padded
  119.     outstr((char*)p, prefix);
  120.     return *this;
  121. }
  122.  
  123.  
  124. // format and insert an unsigned long
  125. ostream _FAR & ostream::operator<< (unsigned long ul)
  126. {
  127.     char buf[MaxCharsInLong];
  128.     char *prefix = 0;       // displayed numeric prefix string
  129.     char *p;
  130.  
  131.  
  132.     if( flags() & ios::hex )
  133.         {
  134.         int upper = (flags() & ios::uppercase) != 0;
  135.         p = tohex(buf + MaxCharsInLong - 1, ul, upper);
  136.  
  137.         // compute any base prefix
  138.         if( flags() & ios::showbase )
  139.             prefix = upper ? "0X" : "0x";
  140.         }
  141.     else if( flags() & ios::oct )
  142.         {
  143.         p = tooct(buf + MaxCharsInLong - 1, ul);
  144.  
  145.         // compute any base prefix
  146.         if( flags() & ios::showbase )
  147.             prefix = "0";
  148.         }
  149.     else
  150.         {
  151.         p = todec(buf + MaxCharsInLong - 1, ul);
  152.  
  153.         // compute any sign prefix
  154.         if( ul  &&  (flags() & ios::showpos) )
  155.             prefix = "+";
  156.         }
  157.  
  158.     // now we have a formatted string for output, to be possibly padded
  159.     outstr((char*)p, prefix);
  160.     return *this;
  161. }
  162.  
  163.  
  164.