[98] | 1 |
|
---|
| 2 | /***********************************************************************
|
---|
| 3 |
|
---|
| 4 | $Id: commafmt.c 98 2004-05-21 15:22:09Z root $
|
---|
| 5 |
|
---|
| 6 | Command formatting tools
|
---|
| 7 |
|
---|
| 8 | Copyright (c) 1993-98 M. Kimes
|
---|
| 9 | Copyright (c) 2004 Steven H.Levine
|
---|
| 10 |
|
---|
| 11 | Revisions 06 Jan 04 SHL - Drop hundfmt, clean commafmt
|
---|
| 12 |
|
---|
| 13 | ***********************************************************************/
|
---|
| 14 |
|
---|
[2] | 15 | /*
|
---|
| 16 | ** COMMAFMT.C
|
---|
| 17 | ** Public domain by Bob Stout
|
---|
| 18 | **
|
---|
| 19 | ** Notes: 1. Use static buffer to eliminate error checks on buffer overflow
|
---|
| 20 | ** and reduce code size.
|
---|
| 21 | ** 2. By making the numeric argument a long and prototyping it before
|
---|
| 22 | ** use, passed numeric arguments will be implicitly cast to longs
|
---|
| 23 | ** thereby avoiding int overflow.
|
---|
| 24 | ** 3. Use the thousands grouping and thousands separator from the
|
---|
| 25 | ** ANSI locale to make this more robust.
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | #pragma alloc_text(MISC8,commafmt,hundfmt)
|
---|
| 29 |
|
---|
| 30 | #include <stdlib.h>
|
---|
| 31 | #include <stdio.h>
|
---|
| 32 | #include <string.h>
|
---|
| 33 |
|
---|
| 34 | size_t commafmt(char *buf, /* Buffer for formatted string */
|
---|
[98] | 35 | int bufsize, /* Size of buffer */
|
---|
| 36 | long N) /* Number to convert */
|
---|
[2] | 37 | {
|
---|
[98] | 38 | int len = 1;
|
---|
| 39 | int posn = 1;
|
---|
| 40 | int sign = 1;
|
---|
[2] | 41 |
|
---|
[98] | 42 | char *ptr = buf + bufsize - 1;
|
---|
| 43 |
|
---|
| 44 | if (bufsize < 2)
|
---|
| 45 | {
|
---|
[2] | 46 | ABORT: *buf = 0;
|
---|
[98] | 47 | return 0;
|
---|
| 48 | }
|
---|
[2] | 49 |
|
---|
[98] | 50 | *ptr-- = 0;
|
---|
| 51 | --bufsize;
|
---|
| 52 | if (N < 0)
|
---|
| 53 | {
|
---|
| 54 | sign = -1;
|
---|
| 55 | N = -N;
|
---|
| 56 | }
|
---|
[2] | 57 |
|
---|
[98] | 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 | }
|
---|
[2] | 72 |
|
---|
[98] | 73 | if (sign < 0)
|
---|
| 74 | {
|
---|
| 75 | if (bufsize == 0)
|
---|
| 76 | goto ABORT;
|
---|
| 77 | *ptr-- = '-';
|
---|
| 78 | ++len;
|
---|
| 79 | }
|
---|
[2] | 80 |
|
---|
[98] | 81 | strcpy(buf, ++ptr); // Left align
|
---|
| 82 | return len;
|
---|
[2] | 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
[98] | 86 | #if 0 // fixme
|
---|
| 87 |
|
---|
[2] | 88 | size_t hundfmt (char *buf,int bufsize,unsigned long N) {
|
---|
| 89 |
|
---|
| 90 | char tbuf[34];
|
---|
| 91 | register char *pt,*p;
|
---|
| 92 | register int len;
|
---|
| 93 |
|
---|
| 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 | }
|
---|
[98] | 112 |
|
---|
| 113 | #endif 0 // fixme
|
---|