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