[comp.sources.bugs] engformat in sc 6.9

wilber@alice.att.com (rew) (02/17/91)

sc 6.9 calls a routine called "engformat" in several places, which is
not defined anywhere in the sc code.  Is this supposed to be a
standard library routine?  (It's not on my machine.)  What's it
supposed to do?

Bob Wilber   wilber@homxb.att.com

jik@athena.mit.edu (Jonathan I. Kamens) (02/18/91)

In article <11884@alice.att.com>, wilber@alice.att.com (rew) writes:
|> sc 6.9 calls a routine called "engformat" in several places, which is
|> not defined anywhere in the sc code.  Is this supposed to be a
|> standard library routine?  (It's not on my machine.)  What's it
|> supposed to do?

  Um, actually, I believe version 6.9 doesn't call engformat, 6.10 does.  The
patches to bring sc up to 6.10 included references to engformat, but forgot to
include the file format.c which defines engformat.

  I've included at the end of this message the message from comp.sources.bugs
which announced this and provided format.c.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

Article: 2763 of comp.sources.bugs
Path: bloom-beacon!mintaka!olivea!samsung!uunet!tut.cis.ohio-state.edu!purdue!tippy!sawmill!buhrt
From: buhrt@sawmill.uucp (Jeffery A Buhrt)
Newsgroups: comp.sources.bugs
Subject: Sc6.10 missing function format.c
Message-ID: <1990Oct29.132848.16331@sawmill.uucp>
Date: 29 Oct 90 13:28:48 GMT
Organization: Grauel Enterprises
Lines: 581

Sorry, format.c was not included in the patch set.

						-Jeff Buhrt
						317-477-6000
					sequent!sawmill!buhrt
#!/bin/sh
# This is a shell archive (shar 3.20)
# made 10/29/1990 13:28 UTC by buhrt@sawmill
# Source directory /users/buhrt/src/sc
#
# existing files WILL be overwritten
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#  13822 -r--r--r-- format.c
#
if touch 2>&1 | fgrep '[-amc]' > /dev/null
 then TOUCH=touch
 else TOUCH=true
fi
# ============= format.c ==============
echo "x - extracting format.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > format.c &&
X/*****************************************************************************
X *
X * Mark Nagel <nagel@ics.uci.edu>
X * 20 July 1989
X *
X * $Revision: 6.10 $
X *
X * bool
X * format(fmt, num, buf, buflen)
X *  char *fmt;
X *  double num;
X *  char buf[];
X *  int buflen;
X *
X * The format function will produce a string representation of a number
X * given a _format_ (described below) and a double value.  The result is
X * written into the passed buffer -- if the resulting string is too
X * long to fit into the passed buffer, the function returns false.
X * Otherwise the function returns true.
X *
X * The fmt parameter contains the format to use to convert the number.
X *
X *  #	Digit placeholder.  If the number has fewer digits on either
X *      side of the decimal point than  there are '#' characters in
X *      the format, the extra '#' characters are ignored.  The number
X *      is rounded to the number of digit placeholders as there are
X *      to the right of the decimal point.  If there are more digits
X *      in the number than there are digit placeholders on the left
X *      side of the decimal point, then those digits are displayed.
X *
X *  0	Digit placeholder.  Same as for '#' except that the number
X *      is padded with zeroes on either side of the decimal point.
X *      The number of zeroes used in padding is determined by the
X *      number of digit placeholders after the '0' for digits on
X *      the left side of the decimal point and by the number of
X *      digit placeholders before the '0' for digits on the right
X *      side of the decimal point.
X *
X *  .	Decimal point.  Determines how many digits are placed on
X *      the right and left sides of the decimal point in the number.
X *      Note that numbers smaller than 1 will begin with a decimal
X *      point if the left side of the decimal point contains only
X *      a '#' digit placeholder.  Use a '0' placeholder to get a
X *      leading zero in decimal formats.
X *
X *  %	Percentage.  For each '%' character in the format, the actual
X *      number gets multiplied by 100 (only for purposes of formatting
X *      -- the original number is left unmodified) and the '%' character
X *      is placed in the same position as it is in the format.
X *
X *  ,	Thousands separator.  The presence of a ',' in the format
X *      (multiple commas are treated as one) will cause the number
X *      to be formatted with a ',' separating each set of three digits
X *      in the integer part of the number with numbering beginning
X *      from the right end of the integer.
X *
X *  \	Quote.  This character causes the next character to be
X *      inserted into the formatted string directly with no
X *      special interpretation.
X *
X *  E- E+ e- e+
X *	Scientific format.  Causes the number to formatted in scientific
X *	notation.  The case of the 'E' or 'e' given is preserved.  If
X *      the format uses a '+', then the sign is always given for the
X *	exponent value.  If the format uses a '-', then the sign is
X *	only given when the exponent value is negative.  Note that if
X *	there is no digit placeholder following the '+' or '-', then
X *	that part of the formatted number is left out.  In general,
X *	there should be one or more digit placeholders after the '+'
X *	or '-'.
X *
X *  ;	Format selector.  Use this character to separate the format
X *	into two distinct formats.  The format to the left of the
X *	';' character will be used if the number given is zero or
X *	positive.  The format to the right of the ';' character is
X *      used if the number given is negative.
X *    
X *  Any
X *	Self insert.  Any other character will be inserted directly
X *	into the formatted number with no change made to the actual
X *      number.
X *
X *****************************************************************************/
X
X/*****************************************************************************/
X
X#include <stdio.h>
X
X#define bool	int
X#define true	1
X#define false	0
X#define EOS	'\0'
X#define MAXBUF	256
X
Xextern char
X  *strcpy();
X
Xstatic char
X  *fmt_int(),
X  *fmt_frac(),
X  *fmt_exp();
X
Xstatic void
X  reverse();
X
X/*****************************************************************************/
X
Xbool
Xformat(fmt, val, buf, buflen)
X  char *fmt;
X  double val;
X  char *buf;
X  int buflen;
X{
X  register char *cp;
X  char *tmp, *tp, *tmpfmt;
X  bool comma = false, negative = false;
X  char *integer = NULL, *decimal = NULL;
X  char *exponent = NULL;
X  int exp_val, width;
X  char prtfmt[32];
X  char *mantissa;
X  char *fraction = NULL;
X  int zero_pad = 0;
X
X  if (fmt == NULL)
X    return(true);
X
X  fmt = tmpfmt = strcpy((char *) xmalloc(strlen(fmt) + 1), fmt);
X  mantissa = (char *) xmalloc(buflen + 1);
X
X  /*
X   * select positive or negative format if necessary
X   */
X  for (cp = fmt; *cp != ';' && *cp != EOS; cp++)
X  {
X    if (*cp == '\\')
X      cp++;
X  }
X  if (*cp == ';')
X  {
X    if (val < 0.0)
X    {
X      val = -val;     /* format should provide sign if desired */
X      fmt = cp + 1;
X    }
X    else
X    {
X      *cp = EOS;
X    }
X  }
X  
X  /*
X   * extract other information from format and produce new
X   * malloc'ed format string
X   */
X  tmp = (char *) xmalloc(strlen(fmt) + 1);
X  for (cp = fmt, tp = tmp; *cp != EOS; cp++)
X  {
X    switch (*cp)
X    {
X      case '\\':
X        *tp++ = *cp++;
X        *tp++ = *cp;
X	break;
X
X      case ',':
X        comma = true;
X	break;
X
X      case '.':
X        if (decimal == NULL)
X	  decimal = tp;
X	*tp++ = *cp;
X	break;
X	
X      case '%':
X        val *= 100.0;
X	*tp++ = *cp;
X	break;
X	
X      default:
X        *tp++ = *cp;
X	break;
X    }
X  }
X  *tp = EOS;
X  xfree(tmpfmt);
X  fmt = tmp;
X  
X  /*
X   * extract the exponent from the format if present
X   */
X  for (cp = fmt; *cp != EOS; cp++)
X  {
X    if (*cp == '\\')
X    {
X      cp++;
X    }
X    else if (*cp == 'e' || *cp == 'E')
X    {
X      if (cp[1] == '+' || cp[1] == '-')
X      {
X        tmp = (char *) xmalloc(strlen(cp) + 1);
X	exponent = strcpy(tmp, cp);
X	*cp = EOS;
X	exp_val = 0;
X	while (val < 1.0)
X	{
X          val *= 10.0;
X	  exp_val--;
X	}
X	while (val >= 10.0)
X	{
X	  val /= 10.0;
X	  exp_val++;
X	}
X	break;
X      }
X    }
X  }
X
X  /*
X   * determine maximum decimal places and use sprintf
X   * to build initial character form of formatted value.
X   */
X  width = 0;
X  if (decimal)
X  {
X    *decimal++ = EOS;
X    for (cp = decimal; *cp != EOS; cp++)
X    {
X      switch (*cp)
X      {
X        case '\\':
X          cp++;
X	  break;
X
X        case '#':
X          width++;
X	  break;
X
X	case '0':
X	  zero_pad = ++width;
X	  break;
X      }
X    }
X    zero_pad = strlen(decimal) - zero_pad;
X  }
X  if (val < 0.0)
X  {
X    negative = true;
X    val = -val;
X  }
X  sprintf(prtfmt, "%%.%dlf", width);
X  sprintf(mantissa, prtfmt, val);
X  for (cp = integer = mantissa; *cp != '.' && *cp != EOS; cp++)
X  {
X    if (*integer == '0')
X      integer++;
X  }
X  if (*cp == '.')
X  {
X    fraction = cp + 1;
X    *cp = EOS;
X    cp = fraction + strlen(fraction) - 1;
X    for (; zero_pad > 0; zero_pad--, cp--)
X    {
X      if (*cp == '0')
X        *cp = EOS;
X    }
X  }
X
X  /*
X   * format the puppy
X   */
X  {
X    char *ci, *cf, *ce;
X    int len_ci, len_cf, len_ce;
X    bool ret = false;
X    
X    ci = fmt_int(integer, fmt, comma, negative);
X    ci = strcpy((char *)xmalloc((len_ci = strlen(ci)) + 1), ci);
X    cf = (fraction) ? fmt_frac(fraction, decimal) : "";
X    cf = strcpy((char *)xmalloc((len_cf = strlen(cf)) + 1), cf);
X    ce = (exponent) ? fmt_exp(exp_val, exponent) : "";
X    ce = strcpy((char *)xmalloc((len_ce = strlen(ce)) + 1), ce);
X    if (len_ci + len_cf + len_ce < buflen)
X    {
X      sprintf(buf, "%s%s%s", ci, cf, ce);
X      ret = true;
X    }
X
X    /*
X     * free up malloc'ed memory
X     */
X    xfree(mantissa);
X    xfree(fmt);
X    if (exponent) xfree(exponent);
X    xfree(ci);
X    xfree(cf);
X    xfree(ce);
X
X    return (ret);
X  }
X}
X
X/*****************************************************************************/
X
Xstatic char *
Xfmt_int(val, fmt, comma, negative)
X  char *val;	    /* integer part of the value to be formatted */
X  char *fmt;	    /* integer part of the format */
X  bool comma;	    /* true if we should comma-ify the value */
X  bool negative;    /* true if the value is actually negative */
X{
X  int digit, f, v;
X  int thousands = 0;
X  char *cp;
X  static char buf[MAXBUF];
X  char *bufptr = buf;
X
X  /*
X   * locate the leftmost digit placeholder
X   */
X  for (cp = fmt; *cp != EOS; cp++)
X  {
X    if (*cp == '\\')
X      cp++;
X    else if (*cp == '#' || *cp == '0')
X      break;
X  }
X  digit = (*cp == EOS) ? -1 : cp - fmt;
X
X  /*
X   * format the value
X   */
X  f = strlen(fmt) - 1;
X  v = (digit >= 0) ? strlen(val) - 1 : -1;
X  while (f >= 0 || v >= 0)
X  {
X    if (f > 0 && fmt[f-1] == '\\')
X    {
X      *bufptr++ = fmt[f--];
X    }
X    else if (f >= 0 && (fmt[f] == '#' || fmt[f] == '0'))
X    {
X      if (v >= 0 || fmt[f] == '0')
X      {
X        *bufptr++ = v < 0 ? '0' : val[v];
X	if (comma && (thousands = (thousands + 1) % 3) == 0 && v > 0)
X	{
X	  *bufptr++ = ',';
X	}
X	v--;
X      }
X    }
X    else if (f >= 0)
X    {
X      *bufptr++ = fmt[f];
X    }
X    if (v >= 0 && f == digit)
X    {
X      continue;
X    }
X    f--;
X  }
X    
X  if (negative && digit >= 0)
X    *bufptr++ = '-';
X  *bufptr = EOS;
X  reverse(buf);
X
X  return (buf);
X}
X
X/*****************************************************************************/
X
Xstatic char *
Xfmt_frac(val, fmt)
X  char *val;	    /* fractional part of the value to be formatted */
X  char *fmt;	    /* fractional portion of format */
X{
X  static char buf[MAXBUF];
X  register char *bufptr = buf;
X  register char *fmtptr = fmt, *valptr = val;
X
X  *bufptr++ = '.';
X  while (*fmtptr != EOS)
X  {
X    if (*fmtptr == '\\')
X    {
X      *bufptr++ = *++fmtptr;
X    }
X    else if (*fmtptr == '#' || *fmtptr == '0')
X    {
X      if (*valptr != EOS || *fmtptr == '0')
X      {
X        *bufptr++ = (*valptr != EOS) ? *valptr++ : *fmtptr;
X      }
X    }
X    else
X    {
X      *bufptr++ = *fmtptr;
X    }
X    fmtptr++;
X  }
X  *bufptr = EOS;
X
X  return (buf);
X}
X
X/*****************************************************************************/
X
Xstatic char *
Xfmt_exp(val, fmt)
X  int val;	    /* value of the exponent */
X  char *fmt;	    /* exponent part of the format */
X{
X  static char buf[MAXBUF];
X  register char *bufptr = buf;
X  char valbuf[64];
X  bool negative = false;
X  
X  *bufptr++ = *fmt++;
X  if (*fmt++ == '+')
X    *bufptr++ = (val < 0) ? '-' : '+';
X  else if (val < 0)
X    *bufptr++ = '-';
X  *bufptr = EOS;
X
X  if (val < 0)
X  {
X    val = -val;
X    negative = true;
X  }
X  sprintf(valbuf, "%d", val);
X  
X  strcat(buf, fmt_int(valbuf, fmt, false, negative));
X  return (buf);
X}
X
X/*****************************************************************************/
X
Xstatic void
Xreverse(buf)
X  register char *buf;
X{
X  register char *cp = buf + strlen(buf) - 1;
X  register char tmp;
X
X  while (buf < cp)
X  {
X    tmp = *cp;
X    *cp-- = *buf;
X    *buf++ = tmp;
X  }
X}
X
X/*****************************************************************************/
X/*  
X * Tom Anderson    <toma@hpsad.hp.com>
X * 10/14/90
X *
X * This routine takes a value and formats it using fixed, scientific,
X * or engineering notation.  The format command 'f' determines which
X * format is used.  The formats are:         example
X *    0:   Fixed point (default)             0.00010
X *    1:   Scientific                        1.00E-04
X *    2:   Engineering                       100.00u
X *
X * The format command 'f' now uses three values.  The first two are the
X * width and precision, and the last one is the format value 0, 1, or 2 as
X * described above.  The format value is passed in the variable fmt.
X *
X * This formatted value is written into the passed buffer.  if the
X * resulting string is too long to fit into the passed buffer, the
X * function returns false.  Otherwise the function returns true.
X *
X * When a number is formatted as engineering and is outside of the range
X * of typically used engineering exponents, the format reverts to
X * scientific.
X *
X * To preserve compatability with old spreadsheet files, the third value
X * may be missing, and the default will be fixed point (format 0).
X *
X * When an old style sheet is saved, the third value will be stored.
X *
X */
X
X#define REFMTFIX 0
X#define REFMTFLT 1
X#define REFMTENG 2
Xchar engmult[] = "afpnum kMGT";
X
Xbool
Xengformat(fmt, width, precision, val, buf, buflen)
Xint fmt; 
Xint width; 
Xint precision;
Xdouble val;
Xchar *buf;
Xint buflen;
X{
X  int engind = 0;
X  double engmant, pow(), engabs, engexp;
X  if (buflen < width) return (false);
X  if (fmt == REFMTFIX)
X  (void)sprintf(buf,"%*.*f", width, precision, val);
X  if (fmt == REFMTFLT)
X  (void)sprintf(buf,"%*.*E", width, precision, val);
X  if (fmt == REFMTENG)
X  {
X    if (val == 0e0)    /* Hack to get zeroes to line up in engr fmt */
X    {
X      (void)sprintf((buf-1),"%*.*f ", width, precision, val);
X    }
X    else
X    {
X      engabs=(val);
X      if (engabs < 0e0) engabs= -engabs;
X      if ((engabs >= 1e-18) && (engabs < 1e-15 )) engind=0;
X      if ((engabs >= 1e-15) && (engabs < 1e-12 )) engind=1;
X      if ((engabs >= 1e-12) && (engabs < 1e-9 )) engind=2;
X      if ((engabs >= 1e-9) && (engabs < 1e-6 )) engind=3;
X      if ((engabs >= 1e-6) && (engabs < 1e-3 )) engind=4;
X      if ((engabs >= 1e-3) && (engabs < 1 )) engind=5;
X      if ((engabs >= 1) && (engabs < 1e3 )) engind=6;
X      if ((engabs >= 1e3) && (engabs < 1e6 )) engind=7;
X      if ((engabs >= 1e6) && (engabs < 1e9 )) engind=8;
X      if ((engabs >= 1e9) && (engabs < 1e12 )) engind=9;
X      if ((engabs >= 1e12) && (engabs < 1e15 )) engind=10;
X      if ((engabs <1e-18) || (engabs >=1e15))
X      {
X      /* Revert to floating point */
X        (void)sprintf(buf,"%*.*E", width, precision, val);
X      }
X      else
X      {
X        engexp= (double) (engind-6)*3;
X        engmant= val/pow(10.0e0,engexp);
X        (void)sprintf(buf,"%*.*f%c", width-1,
X                      precision, engmant, engmult[engind]);
X      }
X    }
X  }
X  return (true);
X}
SHAR_EOF
$TOUCH -am 1026165790 format.c &&
chmod 0444 format.c ||
echo "restore of format.c failed"
set `wc -c format.c`;Wc_c=$1
if test "$Wc_c" != "13822"; then
	echo original size 13822, current size $Wc_c
fi
exit 0

jpd@pc.usl.edu (Dugal James P.) (02/19/91)

Instead of porting/patching 6.9, why not start with 6.12 instead?
You can ftp it from pc.usl.edu as pub/unix/sc6.12.tar.Z

Regards,
-- 
-- James Dugal,	N5KNX		Internet: jpd@usl.edu
Associate Director		Ham packet: n5knx@k5arh
Computing Center		US Mail: PO Box 42770  Lafayette, LA  70504
University of Southwestern LA.	Tel. 318-231-6417	U.S.A.

lowell@tc.fluke.COM (Lowell Skoog) (02/20/91)

In article <25419@rouge.usl.edu> jpd@pc.usl.edu (Dugal James P.) writes:
>Instead of porting/patching 6.9, why not start with 6.12 instead?
>You can ftp it from pc.usl.edu as pub/unix/sc6.12.tar.Z

Question:  Is Jeff Buhrt no longer supporting SC?  This is the first
time I've heard about version 6.12.  I'd like to track the most
"official" copy available.

----------------------------------------------------------------------
Lowell Skoog  M/S 223B                             lowell@tc.fluke.COM
John Fluke Mfg. Co. Inc.        {uw-beaver,microsoft,sun}!fluke!lowell
P.O. Box 9090
Everett, WA, USA  98206-9090                            (206) 356-5283

jik@athena.mit.edu (Jonathan I. Kamens) (02/20/91)

In article <1991Feb19.224208.10633@tc.fluke.COM>, lowell@tc.fluke.COM (Lowell Skoog) writes:
|> Question:  Is Jeff Buhrt no longer supporting SC?  This is the first
|> time I've heard about version 6.12.  I'd like to track the most
|> "official" copy available.

  He is supporting it.  However, I don't think he posts every patchlevel to
the net.  If he does, he's been sending it to Rich Salz, and there's a good
chance that Rich has several patchlevels worth of patches that he hasn't
posted yet.

  I say that because I'm still waiting for him to post a patch to one of my
packages which he told me on December 5 in E-mail he would post "tomorrow
morning."  And that was after he'd already been sitting on it since October
15.  I'm just about ready to give up on comp.sources.unix and repost the whole
package, at the most recent patch level, to comp.sources.misc or alt.sources. 
Sigh.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

jpd@pc.usl.edu (Dugal James P.) (02/21/91)

In article <1991Feb19.224208.10633@tc.fluke.COM> lowell@tc.fluke.COM (Lowell Skoog) writes:
>Question:  Is Jeff Buhrt no longer supporting SC?  This is the first
>time I've heard about version 6.12.  I'd like to track the most
>"official" copy available.
>

pc.usl.edu is the "official" home of the latest ftp-able SC.
Jeff sends me updates periodically so I can make them available to
the Internet.  He has also posted a list of UUCP SC archivers.

-- 
-- James Dugal,	N5KNX		Internet: jpd@usl.edu
Associate Director		Ham packet: n5knx@k5arh
Computing Center		US Mail: PO Box 42770  Lafayette, LA  70504
University of Southwestern LA.	Tel. 318-231-6417	U.S.A.