home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / OSTREAMI.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  4.5 KB  |  161 lines

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