home *** CD-ROM | disk | FTP | other *** search
- /* ==( bench/fmtf.c )== */
-
- /* ----------------------------------------------- */
- /* Pro-C Copyright (C) 1988 - 1990 Vestronix Inc. */
- /* Modification to this source is not supported */
- /* by Vestronix Inc. */
- /* All Rights Reserved */
- /* ----------------------------------------------- */
- /* Written Nig 1-Jan-87 */
- /* Modified Geo 11-Dec-89 See comments below */
- /* ----------------------------------------------- */
- /* %W% (%H% %T%) */
-
- /*
- * Modifications
- *
- * 11-Dec-89 Geo - V2 version
- * 25-Oct-89 Geo - 1.32 Merge
- */
-
- # include <stdio.h>
- # include <bench.h>
-
- /* Function prototypes */
- # ifdef ANSI
- static char fmtwd(char * ,char *);
- static void reverse(char *);
- # else
- static char fmtwd();
- static void reverse();
- # endif
-
- int negative = FALSE;
- static int sign_done;
-
- char *fmt_flt(n, mask)
- double n;
- char *mask;
- {
- return(fmt_dbl((double)n, mask));
- }
-
- char *fmt_dbl(num, msk)
- double num;
- char *msk;
- {
- char c, *p, dc, *dp;
- char numl[81], *numr;
- static char maskl[41];
- char *maskr;
- int overflow = FALSE;
-
- /* break the mask into two parts, one for each side of the decimal */
- strcpy(maskl, msk);
- for(dp = maskl; (dc = *dp) != '\0' && dc != '.'; ++dp)
- ;
- if(dc != '\0')
- *dp++ = '\0';
- maskr = dp;
-
- /* find out how many decimal places were requested */
- for(p = maskr; (c = *p) == '9' || c == 'z' || c == 'Z'; ++p)
- ;
-
- /* under TC, the following are true: -0 != 0, -(-0) == -0 */
- /* so, check for -0 here and set it to 0 to prevent trouble */
- if(num == -0.0)
- num = 0.0;
-
- /* convert number to non-negative and remember sign */
- if(negative = (num < 0.0))
- num = -num;
-
- /* let sprintf() do the work of actually converting it */
- sprintf(numl, "%.*lf", (int)(p-maskr), num);
-
- /* break the number into two parts, one for each side of the decimal */
- for(p = numl; (c = *p) != '\0' && c != '.'; ++p)
- ;
- if(c != '\0')
- *p++ = '\0';
- numr = p;
-
- /* remove any trailing 0s for now, format '9' will restore them */
- /*
- * This bit is obsolete and doesn't work.
- *
- for(p += strlen(p) - 1; *p == '0'; --p)
- ;
- *++p = '\0';
- */
-
- /* format each half separately, leaving the result in the mask argument */
- /* note that both parts are formatted from the decimal point outwards */
- sign_done = FALSE;
-
- /* the part to the right of the decimal CANNOT overflow */
- (void)fmtwd(numr, maskr);
-
- reverse(numl);
- reverse(maskl);
- overflow = (fmtwd(numl, maskl) != '\0');
- reverse(maskl);
-
- /* put the decimal point back in and return the result */
- if(dc != '\0')
- dp[-1] = '.';
-
- /* put in a sign if negative, there is none yet, and there is room */
- if(negative && !sign_done) {
- for(p = maskl; *p == ' '; ++p)
- ;
- if(p == maskl)
- overflow = TRUE;
- else
- *--p = '-';
- }
-
- /* set overflow indicator if overflow occurred */
- if(overflow)
- *maskl = '?';
-
- return(maskl);
- }
-
- static char fmtwd(num, mask)
- char *num, *mask;
- {
- char mc, nc, *root;
-
- root = mask;
- while((mc = *mask) != '\0') {
- switch (mc) {
- case '9':
- case 'Z':
- case 'z':
- /* copy in digit if any left, otherwise copy '0' or ' ' */
- if((nc = *num) != '\0') {
- ++num;
- *mask = nc;
- }
- else
- *mask = (mc == '9') ? '0' : ' ';
- break;
- case '+':
- case '-':
- /* treat as a sign only if this is the first one encountered */
- if (!sign_done
- /* next line allows non-sign + or - inside a num eg. ZZZ-ZZZZ */
- && ( *(mask-1) == '\0' || *(mask+1) == '\0' )
- ) {
- char *p;
-
- sign_done = TRUE;
- /* search back for a non-blank */
- for(p = mask; --p >= root && *p == ' '; )
- ;
- *mask = ' ';
- *++p = negative ? '-' : ((mc == '+') ? '+' : ' ');
- }
- break;
- case ',':
- /* don't want to delete comma if there is going to be a zero */
- if(*num == '\0' && (mask[1] == 'z' || mask[1] == 'Z'))
- *mask = ' ';
- break;
- default:
- /* simply leave the character as is */
- break;
- }
- ++mask;
- }
-
- /* return next character in number; this is used to detect overflow */
- return(*num);
- }
-
- /* not a very general function, but it's only used in this file */
- static void reverse(str)
- char *str;
- {
- char tmp, *estr;
- for (estr = str + strlen(str) - 1; estr > str; estr--, str++) {
- tmp = *estr;
- *estr = *str;
- *str = tmp;
- }
- }
-
-