| Last change
 on this file since 15 was             2, checked in by root, 23 years ago | 
        
          | 
Initial revision
 | 
        
          | 
              
Property                 svn:eol-style
 set to                 native
Property                 svn:keywords
 set to                 Author Date Id Revision | 
        
          | File size:
            2.1 KB | 
      
      
| Rev | Line |  | 
|---|
| [2] | 1 | /* | 
|---|
|  | 2 | **  COMMAFMT.C | 
|---|
|  | 3 | ** | 
|---|
|  | 4 | **  Public domain by Bob Stout | 
|---|
|  | 5 | ** | 
|---|
|  | 6 | **  Notes:  1. Use static buffer to eliminate error checks on buffer overflow | 
|---|
|  | 7 | **             and reduce code size. | 
|---|
|  | 8 | **          2. By making the numeric argument a long and prototyping it before | 
|---|
|  | 9 | **             use, passed numeric arguments will be implicitly cast to longs | 
|---|
|  | 10 | **             thereby avoiding int overflow. | 
|---|
|  | 11 | **          3. Use the thousands grouping and thousands separator from the | 
|---|
|  | 12 | **             ANSI locale to make this more robust. | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
|  | 15 | #pragma alloc_text(MISC8,commafmt,hundfmt) | 
|---|
|  | 16 |  | 
|---|
|  | 17 | #include <stdlib.h> | 
|---|
|  | 18 | #include <stdio.h> | 
|---|
|  | 19 | #include <string.h> | 
|---|
|  | 20 |  | 
|---|
|  | 21 | size_t commafmt(char   *buf,            /* Buffer for formatted string  */ | 
|---|
|  | 22 | int     bufsize,        /* Size of buffer               */ | 
|---|
|  | 23 | long    N)              /* Number to convert            */ | 
|---|
|  | 24 | { | 
|---|
|  | 25 | int len = 1, posn = 1, sign = 1; | 
|---|
|  | 26 | char *ptr = buf + bufsize - 1; | 
|---|
|  | 27 |  | 
|---|
|  | 28 | if (2 > bufsize) | 
|---|
|  | 29 | { | 
|---|
|  | 30 | ABORT:          *buf = 0; | 
|---|
|  | 31 | return 0; | 
|---|
|  | 32 | } | 
|---|
|  | 33 |  | 
|---|
|  | 34 | *ptr-- = 0; | 
|---|
|  | 35 | --bufsize; | 
|---|
|  | 36 | if (0L > N) | 
|---|
|  | 37 | { | 
|---|
|  | 38 | sign = -1; | 
|---|
|  | 39 | N = -N; | 
|---|
|  | 40 | } | 
|---|
|  | 41 |  | 
|---|
|  | 42 | for ( ; len <= bufsize; ++len, ++posn) | 
|---|
|  | 43 | { | 
|---|
|  | 44 | *ptr-- = (char)((N % 10L) + '0'); | 
|---|
|  | 45 | if (0L == (N /= 10L)) | 
|---|
|  | 46 | break; | 
|---|
|  | 47 | if (0 == (posn % 3)) | 
|---|
|  | 48 | { | 
|---|
|  | 49 | *ptr-- = ','; | 
|---|
|  | 50 | ++len; | 
|---|
|  | 51 | } | 
|---|
|  | 52 | if (len >= bufsize) | 
|---|
|  | 53 | goto ABORT; | 
|---|
|  | 54 | } | 
|---|
|  | 55 |  | 
|---|
|  | 56 | if (0 > sign) | 
|---|
|  | 57 | { | 
|---|
|  | 58 | if (0 == bufsize) | 
|---|
|  | 59 | goto ABORT; | 
|---|
|  | 60 | *ptr-- = '-'; | 
|---|
|  | 61 | ++len; | 
|---|
|  | 62 | } | 
|---|
|  | 63 |  | 
|---|
|  | 64 | strcpy(buf, ++ptr); | 
|---|
|  | 65 | return (size_t)len; | 
|---|
|  | 66 | } | 
|---|
|  | 67 |  | 
|---|
|  | 68 |  | 
|---|
|  | 69 | size_t hundfmt (char *buf,int bufsize,unsigned long N) { | 
|---|
|  | 70 |  | 
|---|
|  | 71 | char           tbuf[34]; | 
|---|
|  | 72 | register char *pt,*p; | 
|---|
|  | 73 | register int   len; | 
|---|
|  | 74 |  | 
|---|
|  | 75 | sprintf(tbuf,"%02lu",N); | 
|---|
|  | 76 | len = strlen(tbuf); | 
|---|
|  | 77 | pt = tbuf; | 
|---|
|  | 78 | p = buf; | 
|---|
|  | 79 | bufsize--; | 
|---|
|  | 80 | while(*pt && (p - buf) < bufsize) { | 
|---|
|  | 81 | if(len == 2) { | 
|---|
|  | 82 | *p = '.'; | 
|---|
|  | 83 | p++; | 
|---|
|  | 84 | } | 
|---|
|  | 85 | *p = *pt; | 
|---|
|  | 86 | p++; | 
|---|
|  | 87 | pt++; | 
|---|
|  | 88 | len--; | 
|---|
|  | 89 | } | 
|---|
|  | 90 | *p = 0; | 
|---|
|  | 91 | return p - buf; | 
|---|
|  | 92 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.