source: trunk/dll/commafmt.c@ 860

Last change on this file since 860 was 860, checked in by Gregg Young, 18 years ago

Use the thousand separator from NLS Country info for file sizes. Repalce dir.ico & pmap.bmp with new versions from David

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
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
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 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.
17
18***********************************************************************/
19
20/*
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.
29 */
30
31#define INCL_WIN
32#define INCL_LONGLONG
33#include <os2.h>
34
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include "fm3dll.h"
39
40size_t commafmt(char *pszBuf, // Output buffer
41 UINT cBufSize, // Buffer size, including nul
42 long lNumber) // Number to convert
43{
44 UINT cChars = 1; // Number of characters generated (excluding nul)
45 UINT cDigits = 1; // For commas
46 INT sign = 1;
47
48 char *pch = pszBuf + cBufSize - 1;
49
50 if (cBufSize < 2)
51 goto ABORT;
52
53 *pch-- = 0; // Stuff terminator
54 --cBufSize;
55 if (lNumber < 0) {
56 sign = -1;
57 lNumber = -lNumber;
58 }
59
60 for (; cChars <= cBufSize; ++cChars, ++cDigits) {
61 *pch-- = (CHAR)(lNumber % 10 + '0');
62 lNumber /= 10;
63 if (!lNumber)
64 break;
65 if (cDigits % 3 == 0) {
66 *pch-- = *ThousandsSeparator;
67 ++cChars;
68 }
69 if (cChars >= cBufSize)
70 goto ABORT;
71 } // for
72
73 if (sign < 0) {
74 if (cBufSize == 0)
75 goto ABORT;
76 *pch-- = '-';
77 ++cChars;
78 }
79
80 strcpy(pszBuf, ++pch); // Left align
81
82 return cChars;
83
84ABORT:
85 *pszBuf = 0;
86 return 0;
87}
88
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 */
97
98size_t CommaFmtULL(char *pszBuf, // Output buffer
99 UINT cBufSize, // Buffer size, including nul
100 ULONGLONG ullNumber, // Number to convert
101 CHAR chPreferred) // Preferred suffix, blank, K, M, G
102{
103 CHAR chSuffix = ' ';
104 size_t c;
105
106 if (ullNumber >= 1ULL << 31 || (chPreferred != ' ' && ullNumber >= 1024)) {
107 ullNumber = (ullNumber + 1023) >> 10;
108 chSuffix = 'K';
109 if (ullNumber >= 1ULL << 31 || (chPreferred == 'M' && ullNumber >= 1024) ||
110 (chPreferred == 'G' && ullNumber >= 1024)) {
111 ullNumber = (ullNumber + 1023) >> 10;
112 chSuffix = 'M';
113 if (ullNumber >= 1ULL << 31 || (chPreferred == 'G' && ullNumber >= 1024)) {
114 ullNumber = (ullNumber + 1023) >> 10;
115 chSuffix = 'G';
116 }
117 }
118 }
119
120 c = commafmt(pszBuf, cBufSize, (LONG)ullNumber);
121
122 if (chSuffix != ' ') {
123 if (c + 4 > cBufSize) {
124 *pszBuf = 0;
125 c = 0;
126 }
127 else {
128 pszBuf += c;
129 *pszBuf++ = chSuffix;
130 *pszBuf++ = 'i';
131 *pszBuf++ = 'B';
132 c += 3;
133 *pszBuf = 0;
134 }
135 }
136 return c;
137}
138
139//=== CommaFmtUL: format unsigned long number with commas and SI unit suffix ===
140
141size_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;
148
149 if (ulNumber >= 1ULL << 31 || (chPreferred != ' ' && ulNumber >= 1024)) {
150 ulNumber = (ulNumber + 1023) >> 10;
151 chSuffix = 'K';
152 if (ulNumber >= 1ULL << 31 || (chPreferred == 'M' && ulNumber >= 1024)) {
153 ulNumber = (ulNumber + 1023) >> 10;
154 chSuffix = 'M';
155 }
156 }
157
158 c = commafmt(pszBuf, cBufSize, ulNumber);
159
160 if (chSuffix != ' ') {
161 if (c + 4 > cBufSize) {
162 *pszBuf = 0;
163 c = 0;
164 }
165 else {
166 pszBuf += c;
167 *pszBuf++ = chSuffix;
168 *pszBuf++ = 'i';
169 *pszBuf++ = 'B';
170 c += 3;
171 *pszBuf = 0;
172 }
173 }
174 return c;
175}
176
177#pragma alloc_text(MISC8,commafmt,CommaFmtU64)
Note: See TracBrowser for help on using the repository browser.