| 1 | 
 | 
|---|
| 2 | /***********************************************************************
 | 
|---|
| 3 | 
 | 
|---|
| 4 |   $Id: commafmt.c 859 2007-11-05 22:32:39Z gyoung $
 | 
|---|
| 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 |   20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
 | 
|---|
| 15 | 
 | 
|---|
| 16 | ***********************************************************************/
 | 
|---|
| 17 | 
 | 
|---|
| 18 | /*
 | 
|---|
| 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 |  */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #define INCL_LONGLONG
 | 
|---|
| 32 | #include <os2.h>
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #include <stdlib.h>
 | 
|---|
| 35 | #include <stdio.h>
 | 
|---|
| 36 | #include <string.h>
 | 
|---|
| 37 | 
 | 
|---|
| 38 | size_t commafmt(char *pszBuf,   // Output buffer
 | 
|---|
| 39 |                 UINT cBufSize,  // Buffer size, including nul
 | 
|---|
| 40 |                 long lNumber)   // Number to convert
 | 
|---|
| 41 | {
 | 
|---|
| 42 |   UINT cChars = 1;              // Number of characters generated (excluding nul)
 | 
|---|
| 43 |   UINT cDigits = 1;             // For commas
 | 
|---|
| 44 |   INT sign = 1;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   char *pch = pszBuf + cBufSize - 1;
 | 
|---|
| 47 | 
 | 
|---|
| 48 |   if (cBufSize < 2)
 | 
|---|
| 49 |     goto ABORT;
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   *pch-- = 0;                           // Stuff terminator
 | 
|---|
| 52 |   --cBufSize;
 | 
|---|
| 53 |   if (lNumber < 0) {
 | 
|---|
| 54 |     sign = -1;
 | 
|---|
| 55 |     lNumber = -lNumber;
 | 
|---|
| 56 |   }
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   for (; cChars <= cBufSize; ++cChars, ++cDigits) {
 | 
|---|
| 59 |     *pch-- = (CHAR) (lNumber % 10 + '0');
 | 
|---|
| 60 |     lNumber /= 10;
 | 
|---|
| 61 |     if (!lNumber)
 | 
|---|
| 62 |       break;
 | 
|---|
| 63 |     if (cDigits % 3 == 0) {
 | 
|---|
| 64 |       *pch-- = ',';
 | 
|---|
| 65 |       ++cChars;
 | 
|---|
| 66 |     }
 | 
|---|
| 67 |     if (cChars >= cBufSize)
 | 
|---|
| 68 |       goto ABORT;
 | 
|---|
| 69 |   }                                     // for
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   if (sign < 0) {
 | 
|---|
| 72 |     if (cBufSize == 0)
 | 
|---|
| 73 |       goto ABORT;
 | 
|---|
| 74 |     *pch-- = '-';
 | 
|---|
| 75 |     ++cChars;
 | 
|---|
| 76 |   }
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   strcpy(pszBuf, ++pch);                // Left align
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   return cChars;
 | 
|---|
| 81 | 
 | 
|---|
| 82 | ABORT:
 | 
|---|
| 83 |   *pszBuf = 0;
 | 
|---|
| 84 |   return 0;
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | //=== CommaFmtULL: format long long number with commas and SI unit suffix ===
 | 
|---|
| 88 | 
 | 
|---|
| 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, G
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   CHAR chSuffix = ' ';
 | 
|---|
| 95 |   size_t c;
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   if (ullNumber >= 1ULL << 31 || (chPreferred != ' ' && ullNumber >= 1024)) {
 | 
|---|
| 98 |     ullNumber = (ullNumber + 1023) >> 10;
 | 
|---|
| 99 |     chSuffix = 'K';
 | 
|---|
| 100 |     if (ullNumber >= 1ULL << 31 || (chPreferred == 'M' && ullNumber >= 1024) ||
 | 
|---|
| 101 |         (chPreferred == 'G' && ullNumber >= 1024)) {
 | 
|---|
| 102 |       ullNumber = (ullNumber + 1023) >> 10;
 | 
|---|
| 103 |       chSuffix = 'M';
 | 
|---|
| 104 |       if (ullNumber >= 1ULL << 31 || (chPreferred == 'G' && ullNumber >= 1024)) {
 | 
|---|
| 105 |         ullNumber = (ullNumber + 1023) >> 10;
 | 
|---|
| 106 |         chSuffix = 'G';
 | 
|---|
| 107 |       }
 | 
|---|
| 108 |     }
 | 
|---|
| 109 |   }
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   c = commafmt(pszBuf, cBufSize, (LONG) ullNumber);
 | 
|---|
| 112 | 
 | 
|---|
| 113 |   if (chSuffix != ' ') {
 | 
|---|
| 114 |     if (c + 4 > cBufSize) {
 | 
|---|
| 115 |       *pszBuf = 0;
 | 
|---|
| 116 |       c = 0;
 | 
|---|
| 117 |     }
 | 
|---|
| 118 |     else {
 | 
|---|
| 119 |       pszBuf += c;
 | 
|---|
| 120 |       *pszBuf++ = chSuffix;
 | 
|---|
| 121 |       *pszBuf++ = 'i';
 | 
|---|
| 122 |       *pszBuf++ = 'B';
 | 
|---|
| 123 |       c += 3;
 | 
|---|
| 124 |       *pszBuf = 0;
 | 
|---|
| 125 |     }
 | 
|---|
| 126 |   }
 | 
|---|
| 127 |   return c;
 | 
|---|
| 128 | }
 | 
|---|
| 129 | 
 | 
|---|
| 130 | //=== CommaFmtUL: format unsigned long number with commas and SI unit suffix ===
 | 
|---|
| 131 | 
 | 
|---|
| 132 | size_t CommaFmtUL(char *pszBuf, // Output buffer
 | 
|---|
| 133 |                   UINT cBufSize,        // Buffer size, including nul
 | 
|---|
| 134 |                   ULONG ulNumber,       // Number to convert
 | 
|---|
| 135 |                   CHAR chPreferred)     // Preferred suffix, blank, K, M
 | 
|---|
| 136 | {
 | 
|---|
| 137 |   CHAR chSuffix = ' ';
 | 
|---|
| 138 |   size_t c;
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   if (ulNumber >= 1ULL << 31 || (chPreferred != ' ' && ulNumber >= 1024)) {
 | 
|---|
| 141 |     ulNumber = (ulNumber + 1023) >> 10;
 | 
|---|
| 142 |     chSuffix = 'K';
 | 
|---|
| 143 |     if (ulNumber >= 1ULL << 31 || (chPreferred == 'M' && ulNumber >= 1024)) {
 | 
|---|
| 144 |       ulNumber = (ulNumber + 1023) >> 10;
 | 
|---|
| 145 |       chSuffix = 'M';
 | 
|---|
| 146 |     }
 | 
|---|
| 147 |   }
 | 
|---|
| 148 | 
 | 
|---|
| 149 |   c = commafmt(pszBuf, cBufSize, ulNumber);
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   if (chSuffix != ' ') {
 | 
|---|
| 152 |     if (c + 4 > cBufSize) {
 | 
|---|
| 153 |       *pszBuf = 0;
 | 
|---|
| 154 |       c = 0;
 | 
|---|
| 155 |     }
 | 
|---|
| 156 |     else {
 | 
|---|
| 157 |       pszBuf += c;
 | 
|---|
| 158 |       *pszBuf++ = chSuffix;
 | 
|---|
| 159 |       *pszBuf++ = 'i';
 | 
|---|
| 160 |       *pszBuf++ = 'B';
 | 
|---|
| 161 |       c += 3;
 | 
|---|
| 162 |       *pszBuf = 0;
 | 
|---|
| 163 |     }
 | 
|---|
| 164 |   }
 | 
|---|
| 165 |   return c;
 | 
|---|
| 166 | }
 | 
|---|
| 167 | 
 | 
|---|
| 168 | #pragma alloc_text(MISC8,commafmt,CommaFmtU64)
 | 
|---|