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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - format.cpp
  3.  * C++ output conversion functions
  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 <string.h>
  17. #include <stdarg.h>
  18. #include <stream.h>
  19.  
  20. #define SIZE BUFSIZ        // circular buffer size
  21. static char buf[SIZE];        // circular buffer
  22. static char *bufp = buf;    // next available location in buf
  23.  
  24.  
  25. // common conversion for dec, hex, oct
  26. static char * _Cdecl conv( char *fmt, long val, int width )
  27. {
  28.     if( bufp + (width ? width : 12) >= buf + (SIZE-1) )
  29.     bufp = buf;
  30.     char *ret = bufp;
  31.     *bufp = 0;
  32.     if( width == 0 )
  33.     bufp += sprintf(bufp, fmt, val);
  34.     else if( width > 11 ) {
  35.     char *p = new char[ width+1 ];
  36.     if( p != 0 ) {
  37.         (void) sprintf(p, fmt, val);
  38.         bufp += sprintf(bufp, "%*.*s", width, width, p);
  39.         delete p;
  40.     }
  41.     }
  42.     else {
  43.     char p[12];
  44.     (void) sprintf(p, fmt, val);
  45.     bufp += sprintf(bufp, "%*.*s", width, width, p);
  46.     }
  47.     bufp++;
  48.     return ret;
  49. }
  50.  
  51.  
  52. // internal to decimal text
  53. char * _Cdecl dec( long val, int width )
  54. {
  55.     return conv("%ld", val, width);
  56. }
  57.  
  58.  
  59. // internal to hex text
  60. char * _Cdecl hex( long val, int width )
  61. {
  62.     return conv("%lx", val, width);
  63. }
  64.  
  65.  
  66. // internal to octal text
  67. char * _Cdecl oct( long val, int width )
  68. {
  69.     return conv("%lo", val, width);
  70. }
  71.  
  72.  
  73. // char to string
  74. char * _Cdecl chr( int c, int width )
  75. {
  76.     if( width == 0 ) width = 1;
  77.     if( (bufp + width) >= (buf + SIZE - 1) )
  78.     bufp = buf;
  79.     char *ret = bufp;
  80.     while( --width > 0 )
  81.     *bufp++ = ' ';
  82.     *bufp++ = c;
  83.     *bufp++ = 0;
  84.     return ret;
  85. }
  86.  
  87. // make fixed-width string
  88. char * _Cdecl str( const char *s, int width )
  89. {
  90.     if( width == 0 ) width = strlen((char *)s);
  91.     if( (bufp + width) >= (buf + SIZE - 1) )
  92.     bufp = buf;
  93.     char *ret = bufp;
  94.     *bufp = 0;
  95.     bufp += sprintf(bufp, "%*.*s", width, width, s) + 1;
  96.     return ret;
  97. }
  98.  
  99.  
  100. // general formating
  101. char * _Cdecl form( char *format ... )
  102. {
  103.     va_list ap;
  104.     if( bufp >= (buf + SIZE/2) )    // ensure 1/2 buffer is left
  105.     bufp = buf;
  106.     char *ret = bufp;
  107.     *bufp = 0;
  108.     va_start(ap, format);
  109.     bufp += vsprintf(bufp, format, ap) + 1;
  110.     va_end(ap);
  111.     return ret;
  112. }
  113.