| 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 | 
|---|
| 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 | 
|---|
| 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 | ullNumber = (ullNumber + 1023) >> 10; | 
|---|
| 102 | chSuffix = 'M'; | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | c = commafmt(pszBuf, cBufSize, (LONG) ullNumber); | 
|---|
| 107 |  | 
|---|
| 108 | if (chSuffix != ' ') { | 
|---|
| 109 | if (c + 4 > cBufSize) { | 
|---|
| 110 | *pszBuf = 0; | 
|---|
| 111 | c = 0; | 
|---|
| 112 | } | 
|---|
| 113 | else { | 
|---|
| 114 | pszBuf += c; | 
|---|
| 115 | *pszBuf++ = chSuffix; | 
|---|
| 116 | *pszBuf++ = 'i'; | 
|---|
| 117 | *pszBuf++ = 'B'; | 
|---|
| 118 | c += 3; | 
|---|
| 119 | *pszBuf = 0; | 
|---|
| 120 | } | 
|---|
| 121 | } | 
|---|
| 122 | return c; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 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; | 
|---|
| 134 |  | 
|---|
| 135 | if (ulNumber >= 1ULL << 31 || (chPreferred != ' ' && ulNumber >= 1024)) { | 
|---|
| 136 | ulNumber = (ulNumber + 1023) >> 10; | 
|---|
| 137 | chSuffix = 'K'; | 
|---|
| 138 | if (ulNumber >= 1ULL << 31 || (chPreferred == 'M' && ulNumber >= 1024)) { | 
|---|
| 139 | ulNumber = (ulNumber + 1023) >> 10; | 
|---|
| 140 | chSuffix = 'M'; | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | c = commafmt(pszBuf, cBufSize, ulNumber); | 
|---|
| 145 |  | 
|---|
| 146 | if (chSuffix != ' ') { | 
|---|
| 147 | if (c + 4 > cBufSize) { | 
|---|
| 148 | *pszBuf = 0; | 
|---|
| 149 | c = 0; | 
|---|
| 150 | } | 
|---|
| 151 | else { | 
|---|
| 152 | pszBuf += c; | 
|---|
| 153 | *pszBuf++ = chSuffix; | 
|---|
| 154 | *pszBuf++ = 'i'; | 
|---|
| 155 | *pszBuf++ = 'B'; | 
|---|
| 156 | c += 3; | 
|---|
| 157 | *pszBuf = 0; | 
|---|
| 158 | } | 
|---|
| 159 | } | 
|---|
| 160 | return c; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | #pragma alloc_text(MISC8,commafmt,CommaFmtU64) | 
|---|