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