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