Changeset 154
- Timestamp:
 - May 26, 2005, 4:16:18 AM (20 years ago)
 - File:
 - 
      
- 1 edited
 
- 
          
  trunk/dll/commafmt.c (modified) (2 diffs)
 
 
Legend:
- Unmodified
 - Added
 - Removed
 
- 
      
trunk/dll/commafmt.c
r98 r154 7 7 8 8 Copyright (c) 1993-98 M. Kimes 9 Copyright (c) 2004 Steven H.Levine9 Copyright (c) 2004, 2005 Steven H. Levine 10 10 11 Revisions 06 Jan 04 SHL - Drop hundfmt, clean commafmt 11 06 Jan 04 SHL Disable hundfmt, clean commafmt 12 25 May 05 SHL Drop hundfmt 13 25 May 05 SHL Add CommaFmtULL, CommaFmtUL 12 14 13 15 ***********************************************************************/ 14 16 15 17 /* 16 ** COMMAFMT.C17 ** Public domain by Bob Stout18 **19 ** Notes: 1. Use static buffer to eliminate error checks on buffer overflow20 ** and reduce code size.21 ** 2. By making the numeric argument a long and prototyping it before22 ** use, passed numeric arguments will be implicitly cast to longs23 ** thereby avoiding int overflow.24 ** 3. Use the thousands grouping and thousands separator from the25 ** ANSI locale to make this more robust.26 */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 */ 27 29 28 #pragma alloc_text(MISC8,commafmt,hundfmt) 30 #pragma alloc_text(MISC8,commafmt,CommaFmtU64) 31 32 #define INCL_LONGLONG 33 #include <os2.h> 29 34 30 35 #include <stdlib.h> … … 32 37 #include <string.h> 33 38 34 size_t commafmt(char *buf, /* Buffer for formatted string */35 int bufsize, /* Size of buffer */36 long N) /* Number to convert */39 size_t commafmt(char *pszBuf, // Output buffer 40 UINT cBufSize, // Buffer size, including nul 41 long lNumber) // Number to convert 37 42 { 38 int len = 1; 39 int posn = 1; 40 intsign = 1;43 UINT cChars = 1; // Number of characters generated (excluding nul) 44 UINT cDigits = 1; // For commas 45 INT sign = 1; 41 46 42 char *ptr = buf + bufsize - 1;47 char *pch = pszBuf + cBufSize - 1; 43 48 44 if (bufsize < 2) 45 { 46 ABORT: *buf = 0; 47 return 0; 48 } 49 if (cBufSize < 2) 50 goto ABORT; 49 51 50 *ptr-- = 0; 51 --bufsize;52 if (N< 0)53 {54 sign = -1;55 N = -N;56 }52 *pch-- = 0; // Stuff terminator 53 --cBufSize; 54 if (lNumber < 0) 55 { 56 sign = -1; 57 lNumber = -lNumber; 58 } 57 59 58 for ( ; len <= bufsize; ++len, ++posn)59 {60 *ptr-- = (char)((N % 10L)+ '0');61 N/= 10;62 if (!N)63 break;64 if (posn% 3 == 0)65 {66 *ptr-- = ',';67 ++len;68 }69 if (len >= bufsize)70 goto ABORT;71 } 60 for (; cChars <= cBufSize; ++cChars, ++cDigits) 61 { 62 *pch-- = (CHAR)(lNumber % 10 + '0'); 63 lNumber /= 10; 64 if (!lNumber) 65 break; 66 if (cDigits % 3 == 0) 67 { 68 *pch-- = ','; 69 ++cChars; 70 } 71 if (cChars >= cBufSize) 72 goto ABORT; 73 } // for 72 74 73 if (sign < 0)74 {75 if (bufsize == 0)76 goto ABORT;77 *ptr-- = '-';78 ++len;79 }75 if (sign < 0) 76 { 77 if (cBufSize == 0) 78 goto ABORT; 79 *pch-- = '-'; 80 ++cChars; 81 } 80 82 81 strcpy(buf, ++ptr); // Left align 82 return len; 83 strcpy(pszBuf, ++pch); // Left align 84 85 return cChars; 86 87 ABORT: 88 *pszBuf = 0; 89 return 0; 90 } 91 92 //=== CommaFmtULL: format long long number with commas and SI unit suffix === 93 94 size_t CommaFmtULL(char *pszBuf, // Output buffer 95 UINT cBufSize, // Buffer size, including nul 96 ULONGLONG ullNumber, // Number to convert 97 CHAR chPreferred) // Preferred suffix, blank, K, M 98 { 99 CHAR chSuffix = ' '; 100 size_t c; 101 if (ullNumber >= 1ULL << 31 || 102 (chPreferred != ' ' && ullNumber >= 1024)) 103 { 104 ullNumber = (ullNumber + 1023) >> 10; 105 chSuffix = 'K'; 106 if (ullNumber >= 1ULL << 31 || 107 (chPreferred == 'M' && ullNumber >= 1024)) 108 { 109 ullNumber = (ullNumber + 1023) >> 10; 110 chSuffix = 'M'; 111 } 112 } 113 114 c = commafmt(pszBuf, cBufSize, (LONG)ullNumber); 115 116 if (chSuffix != ' ') { 117 if (c + 4 > cBufSize) 118 { 119 *pszBuf = 0; 120 c = 0; 121 } 122 else 123 { 124 pszBuf += c; 125 *pszBuf++ = chSuffix; 126 *pszBuf++ = 'i'; 127 *pszBuf++ = 'B'; 128 c += 3; 129 *pszBuf = 0; 130 } 131 } 132 return c; 133 } 134 135 //=== CommaFmtUL: format unsigned long number with commas and SI unit suffix === 136 137 size_t CommaFmtUL(char *pszBuf, // Output buffer 138 UINT cBufSize, // Buffer size, including nul 139 ULONG ulNumber, // Number to convert 140 CHAR chPreferred) // Preferred suffix, blank, K, M 141 { 142 CHAR chSuffix = ' '; 143 size_t c; 144 if (ulNumber >= 1ULL << 31 || 145 (chPreferred != ' ' && ulNumber >= 1024)) 146 { 147 ulNumber = (ulNumber + 1023) >> 10; 148 chSuffix = 'K'; 149 if (ulNumber >= 1ULL << 31 || 150 (chPreferred == 'M' && ulNumber >= 1024)) 151 { 152 ulNumber = (ulNumber + 1023) >> 10; 153 chSuffix = 'M'; 154 } 155 } 156 157 c = commafmt(pszBuf, cBufSize, ulNumber); 158 159 if (chSuffix != ' ') { 160 if (c + 4 > cBufSize) 161 { 162 *pszBuf = 0; 163 c = 0; 164 } 165 else 166 { 167 pszBuf += c; 168 *pszBuf++ = chSuffix; 169 *pszBuf++ = 'i'; 170 *pszBuf++ = 'B'; 171 c += 3; 172 *pszBuf = 0; 173 } 174 } 175 return c; 83 176 } 84 177 85 178 86 #if 0 // fixme87 179 88 size_t hundfmt (char *buf,int bufsize,unsigned long N) {89 180 90 char tbuf[34];91 register char *pt,*p;92 register int len;93 181 94 sprintf(tbuf,"%02lu",N);95 len = strlen(tbuf);96 pt = tbuf;97 p = buf;98 bufsize--;99 while(*pt && (p - buf) < bufsize) {100 if(len == 2) {101 *p = '.';102 p++;103 }104 *p = *pt;105 p++;106 pt++;107 len--;108 }109 *p = 0;110 return p - buf;111 }112 113 #endif 0 // fixme 
  Note:
 See   TracChangeset
 for help on using the changeset viewer.
  