[98] | 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
|
---|
[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
|
---|
[857] | 92 | CHAR chPreferred) // Preferred suffix, blank, K, M, G
|
---|
[154] | 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';
|
---|
[859] | 100 | if (ullNumber >= 1ULL << 31 || (chPreferred == 'M' && ullNumber >= 1024) ||
|
---|
| 101 | (chPreferred == 'G' && ullNumber >= 1024)) {
|
---|
[154] | 102 | ullNumber = (ullNumber + 1023) >> 10;
|
---|
| 103 | chSuffix = 'M';
|
---|
[857] | 104 | if (ullNumber >= 1ULL << 31 || (chPreferred == 'G' && ullNumber >= 1024)) {
|
---|
| 105 | ullNumber = (ullNumber + 1023) >> 10;
|
---|
| 106 | chSuffix = 'G';
|
---|
| 107 | }
|
---|
[154] | 108 | }
|
---|
| 109 | }
|
---|
[98] | 110 |
|
---|
[551] | 111 | c = commafmt(pszBuf, cBufSize, (LONG) ullNumber);
|
---|
[2] | 112 |
|
---|
[154] | 113 | if (chSuffix != ' ') {
|
---|
[551] | 114 | if (c + 4 > cBufSize) {
|
---|
[154] | 115 | *pszBuf = 0;
|
---|
| 116 | c = 0;
|
---|
| 117 | }
|
---|
[551] | 118 | else {
|
---|
[154] | 119 | pszBuf += c;
|
---|
| 120 | *pszBuf++ = chSuffix;
|
---|
| 121 | *pszBuf++ = 'i';
|
---|
| 122 | *pszBuf++ = 'B';
|
---|
| 123 | c += 3;
|
---|
| 124 | *pszBuf = 0;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | return c;
|
---|
| 128 | }
|
---|
[2] | 129 |
|
---|
[154] | 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;
|
---|
[551] | 139 |
|
---|
| 140 | if (ulNumber >= 1ULL << 31 || (chPreferred != ' ' && ulNumber >= 1024)) {
|
---|
[154] | 141 | ulNumber = (ulNumber + 1023) >> 10;
|
---|
| 142 | chSuffix = 'K';
|
---|
[551] | 143 | if (ulNumber >= 1ULL << 31 || (chPreferred == 'M' && ulNumber >= 1024)) {
|
---|
[154] | 144 | ulNumber = (ulNumber + 1023) >> 10;
|
---|
| 145 | chSuffix = 'M';
|
---|
[2] | 146 | }
|
---|
| 147 | }
|
---|
[154] | 148 |
|
---|
| 149 | c = commafmt(pszBuf, cBufSize, ulNumber);
|
---|
| 150 |
|
---|
| 151 | if (chSuffix != ' ') {
|
---|
[551] | 152 | if (c + 4 > cBufSize) {
|
---|
[154] | 153 | *pszBuf = 0;
|
---|
| 154 | c = 0;
|
---|
| 155 | }
|
---|
[551] | 156 | else {
|
---|
[154] | 157 | pszBuf += c;
|
---|
| 158 | *pszBuf++ = chSuffix;
|
---|
| 159 | *pszBuf++ = 'i';
|
---|
| 160 | *pszBuf++ = 'B';
|
---|
| 161 | c += 3;
|
---|
| 162 | *pszBuf = 0;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | return c;
|
---|
[2] | 166 | }
|
---|
[793] | 167 |
|
---|
| 168 | #pragma alloc_text(MISC8,commafmt,CommaFmtU64)
|
---|