| 1 |  | 
|---|
| 2 | /*********************************************************************** | 
|---|
| 3 |  | 
|---|
| 4 | $Id: commafmt.c 551 2007-02-28 01:33:51Z 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 |  | 
|---|
| 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 | sign = -1; | 
|---|
| 56 | lNumber = -lNumber; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | for (; cChars <= cBufSize; ++cChars, ++cDigits) { | 
|---|
| 60 | *pch-- = (CHAR) (lNumber % 10 + '0'); | 
|---|
| 61 | lNumber /= 10; | 
|---|
| 62 | if (!lNumber) | 
|---|
| 63 | break; | 
|---|
| 64 | if (cDigits % 3 == 0) { | 
|---|
| 65 | *pch-- = ','; | 
|---|
| 66 | ++cChars; | 
|---|
| 67 | } | 
|---|
| 68 | if (cChars >= cBufSize) | 
|---|
| 69 | goto ABORT; | 
|---|
| 70 | }                                     // for | 
|---|
| 71 |  | 
|---|
| 72 | if (sign < 0) { | 
|---|
| 73 | if (cBufSize == 0) | 
|---|
| 74 | goto ABORT; | 
|---|
| 75 | *pch-- = '-'; | 
|---|
| 76 | ++cChars; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | strcpy(pszBuf, ++pch);                // Left align | 
|---|
| 80 |  | 
|---|
| 81 | return cChars; | 
|---|
| 82 |  | 
|---|
| 83 | ABORT: | 
|---|
| 84 | *pszBuf = 0; | 
|---|
| 85 | return 0; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | //=== CommaFmtULL: format long long number with commas and SI unit suffix === | 
|---|
| 89 |  | 
|---|
| 90 | size_t CommaFmtULL(char *pszBuf,        // Output buffer | 
|---|
| 91 | UINT cBufSize,       // Buffer size, including nul | 
|---|
| 92 | ULONGLONG ullNumber, // Number to convert | 
|---|
| 93 | CHAR chPreferred)    // Preferred suffix, blank, K, M | 
|---|
| 94 | { | 
|---|
| 95 | CHAR chSuffix = ' '; | 
|---|
| 96 | size_t c; | 
|---|
| 97 |  | 
|---|
| 98 | if (ullNumber >= 1ULL << 31 || (chPreferred != ' ' && ullNumber >= 1024)) { | 
|---|
| 99 | ullNumber = (ullNumber + 1023) >> 10; | 
|---|
| 100 | chSuffix = 'K'; | 
|---|
| 101 | if (ullNumber >= 1ULL << 31 || (chPreferred == 'M' && ullNumber >= 1024)) { | 
|---|
| 102 | ullNumber = (ullNumber + 1023) >> 10; | 
|---|
| 103 | chSuffix = 'M'; | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | c = commafmt(pszBuf, cBufSize, (LONG) ullNumber); | 
|---|
| 108 |  | 
|---|
| 109 | if (chSuffix != ' ') { | 
|---|
| 110 | if (c + 4 > cBufSize) { | 
|---|
| 111 | *pszBuf = 0; | 
|---|
| 112 | c = 0; | 
|---|
| 113 | } | 
|---|
| 114 | else { | 
|---|
| 115 | pszBuf += c; | 
|---|
| 116 | *pszBuf++ = chSuffix; | 
|---|
| 117 | *pszBuf++ = 'i'; | 
|---|
| 118 | *pszBuf++ = 'B'; | 
|---|
| 119 | c += 3; | 
|---|
| 120 | *pszBuf = 0; | 
|---|
| 121 | } | 
|---|
| 122 | } | 
|---|
| 123 | return c; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | //=== CommaFmtUL: format unsigned long number with commas and SI unit suffix === | 
|---|
| 127 |  | 
|---|
| 128 | size_t CommaFmtUL(char *pszBuf, // Output buffer | 
|---|
| 129 | UINT cBufSize,        // Buffer size, including nul | 
|---|
| 130 | ULONG ulNumber,       // Number to convert | 
|---|
| 131 | CHAR chPreferred)     // Preferred suffix, blank, K, M | 
|---|
| 132 | { | 
|---|
| 133 | CHAR chSuffix = ' '; | 
|---|
| 134 | size_t c; | 
|---|
| 135 |  | 
|---|
| 136 | if (ulNumber >= 1ULL << 31 || (chPreferred != ' ' && ulNumber >= 1024)) { | 
|---|
| 137 | ulNumber = (ulNumber + 1023) >> 10; | 
|---|
| 138 | chSuffix = 'K'; | 
|---|
| 139 | if (ulNumber >= 1ULL << 31 || (chPreferred == 'M' && ulNumber >= 1024)) { | 
|---|
| 140 | ulNumber = (ulNumber + 1023) >> 10; | 
|---|
| 141 | chSuffix = 'M'; | 
|---|
| 142 | } | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | c = commafmt(pszBuf, cBufSize, ulNumber); | 
|---|
| 146 |  | 
|---|
| 147 | if (chSuffix != ' ') { | 
|---|
| 148 | if (c + 4 > cBufSize) { | 
|---|
| 149 | *pszBuf = 0; | 
|---|
| 150 | c = 0; | 
|---|
| 151 | } | 
|---|
| 152 | else { | 
|---|
| 153 | pszBuf += c; | 
|---|
| 154 | *pszBuf++ = chSuffix; | 
|---|
| 155 | *pszBuf++ = 'i'; | 
|---|
| 156 | *pszBuf++ = 'B'; | 
|---|
| 157 | c += 3; | 
|---|
| 158 | *pszBuf = 0; | 
|---|
| 159 | } | 
|---|
| 160 | } | 
|---|
| 161 | return c; | 
|---|
| 162 | } | 
|---|