| [98] | 1 | 
 | 
|---|
 | 2 | /***********************************************************************
 | 
|---|
 | 3 | 
 | 
|---|
 | 4 |   $Id: commafmt.c 907 2008-01-06 07:26:17Z stevenhl $
 | 
|---|
 | 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
 | 
|---|
| [860] | 15 |   05 Nov 07 GKY Use commafmtULL to display file sizes for large file support
 | 
|---|
 | 16 |   10 Nov 07 GKY Get thousands separator from country info for file sizes.
 | 
|---|
| [98] | 17 | 
 | 
|---|
 | 18 | ***********************************************************************/
 | 
|---|
 | 19 | 
 | 
|---|
| [2] | 20 | /*
 | 
|---|
| [860] | 21 |  * COMMAFMT.C
 | 
|---|
 | 22 |  * Adapted from public domain code by Bob Stout
 | 
|---|
 | 23 |  *
 | 
|---|
 | 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.
 | 
|---|
| [154] | 29 |  */
 | 
|---|
| [2] | 30 | 
 | 
|---|
| [907] | 31 | #include <string.h>
 | 
|---|
| [154] | 32 | 
 | 
|---|
| [860] | 33 | #include "fm3dll.h"
 | 
|---|
| [2] | 34 | 
 | 
|---|
| [154] | 35 | size_t commafmt(char *pszBuf,   // Output buffer
 | 
|---|
 | 36 |                 UINT cBufSize,  // Buffer size, including nul
 | 
|---|
 | 37 |                 long lNumber)   // Number to convert
 | 
|---|
| [2] | 38 | {
 | 
|---|
| [154] | 39 |   UINT cChars = 1;              // Number of characters generated (excluding nul)
 | 
|---|
 | 40 |   UINT cDigits = 1;             // For commas
 | 
|---|
 | 41 |   INT sign = 1;
 | 
|---|
| [2] | 42 | 
 | 
|---|
| [154] | 43 |   char *pch = pszBuf + cBufSize - 1;
 | 
|---|
| [98] | 44 | 
 | 
|---|
| [154] | 45 |   if (cBufSize < 2)
 | 
|---|
| [551] | 46 |     goto ABORT;
 | 
|---|
| [2] | 47 | 
 | 
|---|
| [551] | 48 |   *pch-- = 0;                           // Stuff terminator
 | 
|---|
| [154] | 49 |   --cBufSize;
 | 
|---|
| [551] | 50 |   if (lNumber < 0) {
 | 
|---|
| [154] | 51 |     sign = -1;
 | 
|---|
 | 52 |     lNumber = -lNumber;
 | 
|---|
 | 53 |   }
 | 
|---|
| [2] | 54 | 
 | 
|---|
| [551] | 55 |   for (; cChars <= cBufSize; ++cChars, ++cDigits) {
 | 
|---|
| [860] | 56 |     *pch-- = (CHAR)(lNumber % 10 + '0');
 | 
|---|
| [154] | 57 |     lNumber /= 10;
 | 
|---|
 | 58 |     if (!lNumber)
 | 
|---|
 | 59 |       break;
 | 
|---|
| [551] | 60 |     if (cDigits % 3 == 0) {
 | 
|---|
| [860] | 61 |       *pch-- = *ThousandsSeparator;
 | 
|---|
| [154] | 62 |       ++cChars;
 | 
|---|
 | 63 |     }
 | 
|---|
 | 64 |     if (cChars >= cBufSize)
 | 
|---|
 | 65 |       goto ABORT;
 | 
|---|
| [551] | 66 |   }                                     // for
 | 
|---|
| [2] | 67 | 
 | 
|---|
| [551] | 68 |   if (sign < 0) {
 | 
|---|
| [154] | 69 |     if (cBufSize == 0)
 | 
|---|
 | 70 |       goto ABORT;
 | 
|---|
 | 71 |     *pch-- = '-';
 | 
|---|
 | 72 |     ++cChars;
 | 
|---|
 | 73 |   }
 | 
|---|
| [2] | 74 | 
 | 
|---|
| [154] | 75 |   strcpy(pszBuf, ++pch);                // Left align
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |   return cChars;
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 | ABORT:
 | 
|---|
| [551] | 80 |   *pszBuf = 0;
 | 
|---|
 | 81 |   return 0;
 | 
|---|
| [2] | 82 | }
 | 
|---|
 | 83 | 
 | 
|---|
| [860] | 84 | /**
 | 
|---|
 | 85 |  * Format long long number with commas and SI unit suffix
 | 
|---|
 | 86 |  * @param pszBuf Points to output buffer
 | 
|---|
 | 87 |  * @param cBufSize Buffer size including space for terminating nul
 | 
|---|
 | 88 |  * @param ullNumber Number to convert
 | 
|---|
 | 89 |  * @param chPreferred Preferred suffix, blank, K, M
 | 
|---|
 | 90 |  * @return size of formatting string excluding terminating nul
 | 
|---|
 | 91 |  */
 | 
|---|
| [2] | 92 | 
 | 
|---|
| [154] | 93 | size_t CommaFmtULL(char *pszBuf,        // Output buffer
 | 
|---|
 | 94 |                    UINT cBufSize,       // Buffer size, including nul
 | 
|---|
 | 95 |                    ULONGLONG ullNumber, // Number to convert
 | 
|---|
| [857] | 96 |                    CHAR chPreferred)    // Preferred suffix, blank, K, M, G
 | 
|---|
| [154] | 97 | {
 | 
|---|
 | 98 |   CHAR chSuffix = ' ';
 | 
|---|
 | 99 |   size_t c;
 | 
|---|
| [551] | 100 | 
 | 
|---|
 | 101 |   if (ullNumber >= 1ULL << 31 || (chPreferred != ' ' && ullNumber >= 1024)) {
 | 
|---|
| [154] | 102 |     ullNumber = (ullNumber + 1023) >> 10;
 | 
|---|
 | 103 |     chSuffix = 'K';
 | 
|---|
| [859] | 104 |     if (ullNumber >= 1ULL << 31 || (chPreferred == 'M' && ullNumber >= 1024) ||
 | 
|---|
 | 105 |         (chPreferred == 'G' && ullNumber >= 1024)) {
 | 
|---|
| [154] | 106 |       ullNumber = (ullNumber + 1023) >> 10;
 | 
|---|
 | 107 |       chSuffix = 'M';
 | 
|---|
| [857] | 108 |       if (ullNumber >= 1ULL << 31 || (chPreferred == 'G' && ullNumber >= 1024)) {
 | 
|---|
 | 109 |         ullNumber = (ullNumber + 1023) >> 10;
 | 
|---|
 | 110 |         chSuffix = 'G';
 | 
|---|
 | 111 |       }
 | 
|---|
| [154] | 112 |     }
 | 
|---|
 | 113 |   }
 | 
|---|
| [98] | 114 | 
 | 
|---|
| [860] | 115 |   c = commafmt(pszBuf, cBufSize, (LONG)ullNumber);
 | 
|---|
| [2] | 116 | 
 | 
|---|
| [154] | 117 |   if (chSuffix != ' ') {
 | 
|---|
| [551] | 118 |     if (c + 4 > cBufSize) {
 | 
|---|
| [154] | 119 |       *pszBuf = 0;
 | 
|---|
 | 120 |       c = 0;
 | 
|---|
 | 121 |     }
 | 
|---|
| [551] | 122 |     else {
 | 
|---|
| [154] | 123 |       pszBuf += c;
 | 
|---|
 | 124 |       *pszBuf++ = chSuffix;
 | 
|---|
 | 125 |       *pszBuf++ = 'i';
 | 
|---|
 | 126 |       *pszBuf++ = 'B';
 | 
|---|
 | 127 |       c += 3;
 | 
|---|
 | 128 |       *pszBuf = 0;
 | 
|---|
 | 129 |     }
 | 
|---|
 | 130 |   }
 | 
|---|
 | 131 |   return c;
 | 
|---|
 | 132 | }
 | 
|---|
| [2] | 133 | 
 | 
|---|
| [154] | 134 | //=== CommaFmtUL: format unsigned long number with commas and SI unit suffix ===
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 | size_t CommaFmtUL(char *pszBuf, // Output buffer
 | 
|---|
 | 137 |                   UINT cBufSize,        // Buffer size, including nul
 | 
|---|
 | 138 |                   ULONG ulNumber,       // Number to convert
 | 
|---|
 | 139 |                   CHAR chPreferred)     // Preferred suffix, blank, K, M
 | 
|---|
 | 140 | {
 | 
|---|
 | 141 |   CHAR chSuffix = ' ';
 | 
|---|
 | 142 |   size_t c;
 | 
|---|
| [551] | 143 | 
 | 
|---|
 | 144 |   if (ulNumber >= 1ULL << 31 || (chPreferred != ' ' && ulNumber >= 1024)) {
 | 
|---|
| [154] | 145 |     ulNumber = (ulNumber + 1023) >> 10;
 | 
|---|
 | 146 |     chSuffix = 'K';
 | 
|---|
| [551] | 147 |     if (ulNumber >= 1ULL << 31 || (chPreferred == 'M' && ulNumber >= 1024)) {
 | 
|---|
| [154] | 148 |       ulNumber = (ulNumber + 1023) >> 10;
 | 
|---|
 | 149 |       chSuffix = 'M';
 | 
|---|
| [2] | 150 |     }
 | 
|---|
 | 151 |   }
 | 
|---|
| [154] | 152 | 
 | 
|---|
 | 153 |   c = commafmt(pszBuf, cBufSize, ulNumber);
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 |   if (chSuffix != ' ') {
 | 
|---|
| [551] | 156 |     if (c + 4 > cBufSize) {
 | 
|---|
| [154] | 157 |       *pszBuf = 0;
 | 
|---|
 | 158 |       c = 0;
 | 
|---|
 | 159 |     }
 | 
|---|
| [551] | 160 |     else {
 | 
|---|
| [154] | 161 |       pszBuf += c;
 | 
|---|
 | 162 |       *pszBuf++ = chSuffix;
 | 
|---|
 | 163 |       *pszBuf++ = 'i';
 | 
|---|
 | 164 |       *pszBuf++ = 'B';
 | 
|---|
 | 165 |       c += 3;
 | 
|---|
 | 166 |       *pszBuf = 0;
 | 
|---|
 | 167 |     }
 | 
|---|
 | 168 |   }
 | 
|---|
 | 169 |   return c;
 | 
|---|
| [2] | 170 | }
 | 
|---|
| [793] | 171 | 
 | 
|---|
 | 172 | #pragma alloc_text(MISC8,commafmt,CommaFmtU64)
 | 
|---|