| 1 | 
 | 
|---|
| 2 | /***********************************************************************
 | 
|---|
| 3 | 
 | 
|---|
| 4 |   $Id: commafmt.c 154 2005-05-26 02:16:18Z root $
 | 
|---|
| 5 | 
 | 
|---|
| 6 |   Command formatting tools
 | 
|---|
| 7 | 
 | 
|---|
| 8 |   Copyright (c) 1993-98 M. Kimes
 | 
|---|
| 9 |   Copyright (c) 2004, 2005 Steven H. Levine
 | 
|---|
| 10 | 
 | 
|---|
| 11 |   06 Jan 04 SHL Disable hundfmt, clean commafmt
 | 
|---|
| 12 |   25 May 05 SHL Drop hundfmt
 | 
|---|
| 13 |   25 May 05 SHL Add CommaFmtULL, CommaFmtUL
 | 
|---|
| 14 | 
 | 
|---|
| 15 | ***********************************************************************/
 | 
|---|
| 16 | 
 | 
|---|
| 17 | /*
 | 
|---|
| 18 |    **  COMMAFMT.C
 | 
|---|
| 19 |    **  Public domain by Bob Stout
 | 
|---|
| 20 |    **
 | 
|---|
| 21 |    **  Notes:  1. Use static buffer to eliminate error checks on buffer overflow
 | 
|---|
| 22 |    **             and reduce code size.
 | 
|---|
| 23 |    **          2. By making the numeric argument a long and prototyping it before
 | 
|---|
| 24 |    **             use, passed numeric arguments will be implicitly cast to longs
 | 
|---|
| 25 |    **             thereby avoiding int overflow.
 | 
|---|
| 26 |    **          3. Use the thousands grouping and thousands separator from the
 | 
|---|
| 27 |    **             ANSI locale to make this more robust.
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | #pragma alloc_text(MISC8,commafmt,CommaFmtU64)
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #define INCL_LONGLONG
 | 
|---|
| 33 | #include <os2.h>
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include <stdlib.h>
 | 
|---|
| 36 | #include <stdio.h>
 | 
|---|
| 37 | #include <string.h>
 | 
|---|
| 38 | 
 | 
|---|
| 39 | size_t commafmt(char *pszBuf,   // Output buffer
 | 
|---|
| 40 |                 UINT cBufSize,  // Buffer size, including nul
 | 
|---|
| 41 |                 long lNumber)   // Number to convert
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   UINT cChars = 1;              // Number of characters generated (excluding nul)
 | 
|---|
| 44 |   UINT cDigits = 1;             // For commas
 | 
|---|
| 45 |   INT sign = 1;
 | 
|---|
| 46 | 
 | 
|---|
| 47 |   char *pch = pszBuf + cBufSize - 1;
 | 
|---|
| 48 | 
 | 
|---|
| 49 |   if (cBufSize < 2)
 | 
|---|
| 50 |       goto ABORT;
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   *pch-- = 0;                   // Stuff terminator
 | 
|---|
| 53 |   --cBufSize;
 | 
|---|
| 54 |   if (lNumber < 0)
 | 
|---|
| 55 |   {
 | 
|---|
| 56 |     sign = -1;
 | 
|---|
| 57 |     lNumber = -lNumber;
 | 
|---|
| 58 |   }
 | 
|---|
| 59 | 
 | 
|---|
| 60 |   for (; cChars <= cBufSize; ++cChars, ++cDigits)
 | 
|---|
| 61 |   {
 | 
|---|
| 62 |     *pch-- = (CHAR)(lNumber % 10 + '0');
 | 
|---|
| 63 |     lNumber /= 10;
 | 
|---|
| 64 |     if (!lNumber)
 | 
|---|
| 65 |       break;
 | 
|---|
| 66 |     if (cDigits % 3 == 0)
 | 
|---|
| 67 |     {
 | 
|---|
| 68 |       *pch-- = ',';
 | 
|---|
| 69 |       ++cChars;
 | 
|---|
| 70 |     }
 | 
|---|
| 71 |     if (cChars >= cBufSize)
 | 
|---|
| 72 |       goto ABORT;
 | 
|---|
| 73 |   } // for
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   if (sign < 0)
 | 
|---|
| 76 |   {
 | 
|---|
| 77 |     if (cBufSize == 0)
 | 
|---|
| 78 |       goto ABORT;
 | 
|---|
| 79 |     *pch-- = '-';
 | 
|---|
| 80 |     ++cChars;
 | 
|---|
| 81 |   }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   strcpy(pszBuf, ++pch);                // Left align
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   return cChars;
 | 
|---|
| 86 | 
 | 
|---|
| 87 | ABORT:
 | 
|---|
| 88 |    *pszBuf = 0;
 | 
|---|
| 89 |     return 0;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | //=== CommaFmtULL: format long long number with commas and SI unit suffix ===
 | 
|---|
| 93 | 
 | 
|---|
| 94 | size_t CommaFmtULL(char *pszBuf,        // Output buffer
 | 
|---|
| 95 |                    UINT cBufSize,       // Buffer size, including nul
 | 
|---|
| 96 |                    ULONGLONG ullNumber, // Number to convert
 | 
|---|
| 97 |                    CHAR chPreferred)    // Preferred suffix, blank, K, M
 | 
|---|
| 98 | {
 | 
|---|
| 99 |   CHAR chSuffix = ' ';
 | 
|---|
| 100 |   size_t c;
 | 
|---|
| 101 |   if (ullNumber >= 1ULL << 31 ||
 | 
|---|
| 102 |       (chPreferred != ' ' && ullNumber >= 1024))
 | 
|---|
| 103 |   {
 | 
|---|
| 104 |     ullNumber = (ullNumber + 1023) >> 10;
 | 
|---|
| 105 |     chSuffix = 'K';
 | 
|---|
| 106 |     if (ullNumber >= 1ULL << 31 ||
 | 
|---|
| 107 |         (chPreferred == 'M' && ullNumber >= 1024))
 | 
|---|
| 108 |     {
 | 
|---|
| 109 |       ullNumber = (ullNumber + 1023) >> 10;
 | 
|---|
| 110 |       chSuffix = 'M';
 | 
|---|
| 111 |     }
 | 
|---|
| 112 |   }
 | 
|---|
| 113 | 
 | 
|---|
| 114 |   c = commafmt(pszBuf, cBufSize, (LONG)ullNumber);
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   if (chSuffix != ' ') {
 | 
|---|
| 117 |     if (c + 4 > cBufSize)
 | 
|---|
| 118 |     {
 | 
|---|
| 119 |       *pszBuf = 0;
 | 
|---|
| 120 |       c = 0;
 | 
|---|
| 121 |     }
 | 
|---|
| 122 |     else
 | 
|---|
| 123 |     {
 | 
|---|
| 124 |       pszBuf += c;
 | 
|---|
| 125 |       *pszBuf++ = chSuffix;
 | 
|---|
| 126 |       *pszBuf++ = 'i';
 | 
|---|
| 127 |       *pszBuf++ = 'B';
 | 
|---|
| 128 |       c += 3;
 | 
|---|
| 129 |       *pszBuf = 0;
 | 
|---|
| 130 |     }
 | 
|---|
| 131 |   }
 | 
|---|
| 132 |   return c;
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | //=== CommaFmtUL: format unsigned long number with commas and SI unit suffix ===
 | 
|---|
| 136 | 
 | 
|---|
| 137 | size_t CommaFmtUL(char *pszBuf, // Output buffer
 | 
|---|
| 138 |                   UINT cBufSize,        // Buffer size, including nul
 | 
|---|
| 139 |                   ULONG ulNumber,       // Number to convert
 | 
|---|
| 140 |                   CHAR chPreferred)     // Preferred suffix, blank, K, M
 | 
|---|
| 141 | {
 | 
|---|
| 142 |   CHAR chSuffix = ' ';
 | 
|---|
| 143 |   size_t c;
 | 
|---|
| 144 |   if (ulNumber >= 1ULL << 31 ||
 | 
|---|
| 145 |       (chPreferred != ' ' && ulNumber >= 1024))
 | 
|---|
| 146 |   {
 | 
|---|
| 147 |     ulNumber = (ulNumber + 1023) >> 10;
 | 
|---|
| 148 |     chSuffix = 'K';
 | 
|---|
| 149 |     if (ulNumber >= 1ULL << 31 ||
 | 
|---|
| 150 |         (chPreferred == 'M' && ulNumber >= 1024))
 | 
|---|
| 151 |     {
 | 
|---|
| 152 |       ulNumber = (ulNumber + 1023) >> 10;
 | 
|---|
| 153 |       chSuffix = 'M';
 | 
|---|
| 154 |     }
 | 
|---|
| 155 |   }
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   c = commafmt(pszBuf, cBufSize, ulNumber);
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   if (chSuffix != ' ') {
 | 
|---|
| 160 |     if (c + 4 > cBufSize)
 | 
|---|
| 161 |     {
 | 
|---|
| 162 |       *pszBuf = 0;
 | 
|---|
| 163 |       c = 0;
 | 
|---|
| 164 |     }
 | 
|---|
| 165 |     else
 | 
|---|
| 166 |     {
 | 
|---|
| 167 |       pszBuf += c;
 | 
|---|
| 168 |       *pszBuf++ = chSuffix;
 | 
|---|
| 169 |       *pszBuf++ = 'i';
 | 
|---|
| 170 |       *pszBuf++ = 'B';
 | 
|---|
| 171 |       c += 3;
 | 
|---|
| 172 |       *pszBuf = 0;
 | 
|---|
| 173 |     }
 | 
|---|
| 174 |   }
 | 
|---|
| 175 |   return c;
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | 
 | 
|---|
| 179 | 
 | 
|---|
| 180 | 
 | 
|---|
| 181 | 
 | 
|---|