source: trunk/dll/commafmt.c@ 857

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

Free strdup memory for large file size formatting Add GiB label to CommaFmtULL

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