Esempio n. 1
0
File: format.c Progetto: dkastner/sc
static char *
fmt_exp(int val,	/* value of the exponent */
    char *fmt)		/* exponent part of the format */
{
    static char buf[MAXBUF];
    register char *bufptr = buf;
    char valbuf[64];
    bool negative = false;
  
    *bufptr++ = *fmt++;
    if (*fmt == '+')
	*bufptr++ = (val < 0) ? '-' : '+';
    else if (val < 0)
	*bufptr++ = '-';
    fmt++;
    *bufptr = EOS;

    if (val < 0) {
	val = -val;
	negative = false;
    }
    (void) sprintf(valbuf, "%d", val);
  
    (void) strcat(buf, fmt_int(valbuf, fmt, false, negative));
    return (buf);
}
Esempio n. 2
0
void dump_entries(array * entries)
{
	int i, len;
	struct nentry *tmp;
	char efmt[FMT_ULONG];

	len = array_length(entries, sizeof(struct nentry));
	efmt[fmt_int(efmt, len)] = 0;
	sprintm("Found ", efmt, " entries\n");
	for (i = 0; i < len; i++) {
		tmp = (struct nentry *)array_get(entries, sizeof(struct nentry),
			i);
		entry_dump(tmp);
	}
}
Esempio n. 3
0
STRING
prim_GenericOS_errorName(int errnum)
{
	unsigned int i;
	const char *name = NULL;

	for (i = 0; i < arraysize(sys_errors); i++) {
		if (sys_errors[i].errnum == errnum) {
			name = sys_errors[i].name;
			break;
		}
	}

	if (name == NULL)
		return fmt_int(errnum, 10);
	else
		return sml_str_new(name);
}
Esempio n. 4
0
STRING
prim_Int_toString(int value)
{
	return fmt_int(value, 10);
}
Esempio n. 5
0
File: format.c Progetto: dkastner/sc
bool
format(char *fmt, int lprecision, double val, char *buf, int buflen)
{
    register char *cp;
    char *tmp, *tp;
    bool comma = false, negative = false;
    char *integer = NULL, *decimal = NULL;
    char *exponent = NULL;
    int exp_val = 0;
    int width;
    char prtfmt[32];
    static char		*mantissa = NULL;
    static char		*tmpfmt1 = NULL, *tmpfmt2 = NULL, *exptmp = NULL;
    static unsigned	mantlen = 0, fmtlen = 0;
    char *fraction = NULL;
    int zero_pad = 0;

    if (fmt == NULL)
	return(true);

    if (strlen(fmt) + 1 > fmtlen) {
	fmtlen = strlen(fmt) + 40;
	tmpfmt1 = scxrealloc(tmpfmt1, fmtlen);
	tmpfmt2 = scxrealloc(tmpfmt2, fmtlen);
	exptmp = scxrealloc(exptmp, fmtlen);
    }
    fmt = strcpy(tmpfmt1, fmt);
    if (buflen + 1 > mantlen) {
    	mantlen = buflen + 40;
	mantissa = scxrealloc(mantissa, mantlen);
    }

/*
 * select positive or negative format if necessary
 */
    for (cp = fmt; *cp != ';' && *cp != EOS; cp++) {
	if (*cp == '\\')
	    cp++;
    }
    if (*cp == ';') {
	if (val < 0.0) {
	    val = -val;     /* format should provide sign if desired */
	    fmt = cp + 1;
	} else
	    *cp = EOS;
    }
  
/*
 * extract other information from format and produce a
 * format string stored in tmpfmt2 also scxmalloc()'d above
 */
    tmp = tmpfmt2;
    for (cp = fmt, tp = tmp; *cp != EOS; cp++) {
	switch (*cp) {
	    case '\\':
		*tp++ = *cp++;
		*tp++ = *cp;
		break;

	    case ',':
		comma = true;
		break;

	    case '.':
		if (decimal == NULL)
		    decimal = tp;
		*tp++ = *cp;
		break;
	
	    case '%':
		val *= 100.0;
		*tp++ = *cp;
		break;
	
	    default:
		*tp++ = *cp;
		break;
	}
    }
    *tp = EOS;
    fmt = tmpfmt2;

/* The following line was necessary due to problems with the gcc
 * compiler and val being a negative zero.  Thanks to Mike Novack for
 * the suggestion. - CRM
 */
    val = (val + 1.0) - 1.0;
    if (val < 0.0) {
  	negative = true;
	val = -val;
    }
/*
 * extract the exponent from the format if present
 */
    for (cp = fmt; *cp != EOS; cp++) {
	if (*cp == '\\')
	    cp++;
	else if (*cp == 'e' || *cp == 'E') {
	    if (cp[1] == '+' || cp[1] == '-') {
		exponent = strcpy(exptmp, cp);
		*cp = EOS;
		if (val != 0.0) {
		    while (val < 1.0) {
			val *= 10.0;
			exp_val--;
		    }
		    while (val >= 10.0) {
			val /= 10.0;
			exp_val++;
		    }
		}
		break;
	    }
	}
    }

/*
 * determine maximum decimal places and use sprintf
 * to build initial character form of formatted value.
 */
    width = 0;
    if (decimal) {
	*decimal++ = EOS;
	for (cp = decimal; *cp != EOS; cp++) {
	    switch (*cp) {
		case '\\':
		    cp++;
		    break;

		case '#':
		    width++;
		    break;

		case '0':
		    zero_pad = ++width;
		    break;

		case '&':
		    width += lprecision;
		    zero_pad = width;
		    break;
	    }
	}
	zero_pad = strlen(decimal) - zero_pad;
    }
    (void) sprintf(prtfmt, "%%.%dlf", width);
    (void) sprintf(mantissa, prtfmt, val);
    for (cp = integer = mantissa; *cp != dpoint && *cp != EOS; cp++) {
	if (*integer == '0')
	    integer++;
    }
    if (*cp == dpoint) {
	fraction = cp + 1;
	*cp = EOS;
	cp = fraction + strlen(fraction) - 1;
	for (; zero_pad > 0; zero_pad--, cp--) {
	    if (*cp == '0')
		*cp = EOS;
	    else
		break;
	}
    } else
	fraction = "";

/*
 * format the puppy
 */
    {
    static	char *citmp = NULL, *cftmp = NULL;
    static	unsigned cilen = 0, cflen = 0;
    char *ci, *cf, *ce;
    int len_ci, len_cf, len_ce;
    bool ret = false;
    
    ci = fmt_int(integer, fmt, comma, negative);
    len_ci = strlen(ci);
    if (len_ci >= cilen) {
    	cilen = len_ci + 40;
	citmp = scxrealloc(citmp, cilen);
    }
    ci = strcpy(citmp, ci);

    cf = decimal ? fmt_frac(fraction, decimal, lprecision) : "";
    len_cf = strlen(cf);
    if (len_cf >= cflen) {
    	cflen = len_cf + 40;
	cftmp = scxrealloc(cftmp, cilen);
    }
    cf = strcpy(cftmp, cf);

    ce = (exponent) ? fmt_exp(exp_val, exponent) : "";
    len_ce = strlen(ce);
/*
 * Skip copy assuming sprintf doesn't call our format functions
 *   ce = strcpy(scxmalloc((unsigned)((len_ce = strlen(ce)) + 1)), ce);
 */
    if (len_ci + len_cf + len_ce < buflen) {
	(void) sprintf(buf, "%s%s%s", ci, cf, ce);
	ret = true;
    }

    return (ret);
    }
}