home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / tostring.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  4KB  |  196 lines

  1.  
  2. /*
  3.  * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  4.  *
  5.  * This software may be distributed freely as long as the following conditions
  6.  * are met:
  7.  *         * the distribution, or any derivative thereof, may not be
  8.  *          included as part of a commercial product
  9.  *        * full source code is provided including this copyright
  10.  *        * there is no charge for the software itself (there may be
  11.  *          a minimal charge for the copying or distribution effort)
  12.  *        * this copyright notice is not modified or removed from any
  13.  *          source file
  14.  */
  15. #include "tostring.h"
  16. #include "mallocin.h"
  17.  
  18. /*
  19.  * Function:    tostring()
  20.  *
  21.  * Purpose:    to convert an integer to an ascii display string
  22.  *
  23.  * Arguments:    buf    - place to put the 
  24.  *        val    - integer to convert
  25.  *        len    - length of output field (0 if just enough to hold data)
  26.  *        base    - base for number conversion (only works for base <= 16)
  27.  *        fill    - fill char when len > # digits
  28.  *
  29.  * Returns:    length of string
  30.  *
  31.  * Narrative:    IF fill character is non-blank
  32.  *            Determine base
  33.  *                If base is HEX
  34.  *                    add "0x" to begining of string
  35.  *                IF base is OCTAL
  36.  *                    add "0" to begining of string
  37.  *
  38.  *        While value is greater than zero
  39.  *            use val % base as index into xlation str to get cur char
  40.  *            divide val by base
  41.  *
  42.  *        Determine fill-in length
  43.  *
  44.  *        Fill in fill chars
  45.  *
  46.  *        Copy in number
  47.  *        
  48.  *
  49.  * Mod History:    
  50.  *   90/01/24    cpcahil        Initial revision.
  51.  */
  52.  
  53. #ifndef lint
  54. static
  55. char rcs_hdr[] = "$Id: tostring.c,v 1.9 1992/08/22 16:27:13 cpcahil Exp $";
  56. #endif
  57.  
  58. #define T_LEN 15
  59.  
  60. int
  61. tostring(buf,val,len,base,fill)
  62.     char        * buf;
  63.     unsigned long      val;
  64.     int          len;
  65.     int          base;
  66.     char          fill;
  67.     
  68. {
  69.     char        * bufstart = buf;
  70.     int          filled = 0;
  71.     int          i = T_LEN;
  72.     CONST char    * xbuf = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  73.     char          tbuf[T_LEN];
  74.  
  75.     /*
  76.      * if we are filling with non-blanks, make sure the
  77.      * proper start string is added
  78.      */
  79.     if( fill != ' ' )
  80.     {
  81.         switch(base)
  82.         {
  83.             case B_HEX:
  84.                 if( (len == 0) ||  (len > 2) )
  85.                 {
  86.                     filled = 2;
  87.                     *(buf++) = '0';
  88.                     *(buf++) = 'x';
  89.                     if( len )
  90.                     {
  91.                         len -= 2;
  92.                     }
  93.                 }
  94.                 break;
  95.             case B_OCTAL:
  96.                 *(buf++) = fill;
  97.                 if( len )
  98.                 {
  99.                     len--;
  100.                 }
  101.                 break;
  102.             default:
  103.                 break;
  104.         }
  105.     }
  106.  
  107.     /*
  108.      * convert the value to a string
  109.      */
  110.     do
  111.     {
  112.         tbuf[--i] = xbuf[val % (unsigned long)base];
  113.         val = val / (unsigned long)base;
  114.     }
  115.     while( val > 0 );
  116.  
  117.  
  118.     if( len )
  119.     {
  120.         len -= (T_LEN - i);
  121.  
  122.         /*
  123.          * if we are out of room and we already filled in some of the
  124.          * output buffer (usually 0x for hex output), strip the pre-fill
  125.          * so that we get as much data as we can.
  126.          */
  127.         if( (len < 0) && filled )
  128.         {
  129.             len += filled;
  130.             buf -= filled;
  131.         }
  132.  
  133.         if( len > 0 )
  134.         {
  135.             while(len-- > 0)
  136.             {
  137.                 *(buf++) = fill;
  138.             }
  139.         }
  140.         else
  141.         {
  142.             /* 
  143.              * string is too long so we must truncate
  144.              * off some characters.  We do this the easiest
  145.              * way by just incrementing i.  This means the
  146.              * most significant digits are lost.
  147.              */
  148.             while( len++ < 0 )
  149.             {
  150.                 i++;
  151.             }
  152.         }
  153.     }
  154.  
  155.     while( i < T_LEN )
  156.     {
  157.         *(buf++) = tbuf[i++];
  158.     }
  159.  
  160.     return( (int) (buf - bufstart) );
  161.  
  162. } /* tostring(... */
  163.  
  164. /*
  165.  * $Log: tostring.c,v $
  166.  * Revision 1.9  1992/08/22  16:27:13  cpcahil
  167.  * final changes for pl14
  168.  *
  169.  * Revision 1.8  1992/05/08  02:30:35  cpcahil
  170.  * minor cleanups from minix/atari port
  171.  *
  172.  * Revision 1.7  1992/04/14  02:27:30  cpcahil
  173.  * adjusted output of pointes so that values that had the high bit
  174.  * set would print correctly.
  175.  *
  176.  * Revision 1.6  1992/04/13  03:06:33  cpcahil
  177.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  178.  *
  179.  * Revision 1.5  1991/11/25  14:42:06  cpcahil
  180.  * Final changes in preparation for patch 4 release
  181.  *
  182.  * Revision 1.4  90/05/11  00:13:11  cpcahil
  183.  * added copyright statment
  184.  * 
  185.  * Revision 1.3  90/02/24  21:50:33  cpcahil
  186.  * lots of lint fixes
  187.  * 
  188.  * Revision 1.2  90/02/24  17:29:42  cpcahil
  189.  * changed $Header to $Id so full path wouldnt be included as part of rcs 
  190.  * id string
  191.  * 
  192.  * Revision 1.1  90/02/22  23:17:44  cpcahil
  193.  * Initial revision
  194.  * 
  195.  */
  196.