Back to index

php5  5.3.10
output.c
Go to the documentation of this file.
00001 /* output.c: bcmath library file. */
00002 /*
00003     Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
00004     Copyright (C) 2000 Philip A. Nelson
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.  (COPYING.LIB)
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to:
00018 
00019       The Free Software Foundation, Inc.
00020       59 Temple Place, Suite 330
00021       Boston, MA 02111-1307 USA.
00022 
00023     You may contact the author by:
00024        e-mail:  philnelson@acm.org
00025       us-mail:  Philip A. Nelson
00026                 Computer Science Department, 9062
00027                 Western Washington University
00028                 Bellingham, WA 98226-9062
00029        
00030 *************************************************************************/
00031 
00032 #include <config.h>
00033 #include <stdio.h>
00034 #include <assert.h>
00035 #include <stdlib.h>
00036 #include <ctype.h>
00037 #include <stdarg.h>
00038 #include "bcmath.h"
00039 #include "private.h"
00040 
00041 
00042 /* The following routines provide output for bcd numbers package
00043    using the rules of POSIX bc for output. */
00044 
00045 /* This structure is used for saving digits in the conversion process. */
00046 typedef struct stk_rec {
00047        long  digit;
00048        struct stk_rec *next;
00049 } stk_rec;
00050 
00051 /* The reference string for digits. */
00052 static char ref_str[] = "0123456789ABCDEF";
00053 
00054 
00055 /* A special output routine for "multi-character digits."  Exactly
00056    SIZE characters must be output for the value VAL.  If SPACE is
00057    non-zero, we must output one space before the number.  OUT_CHAR
00058    is the actual routine for writing the characters. */
00059 
00060 void
00061 bc_out_long (val, size, space, out_char)
00062      long val;
00063      int size, space;
00064 #ifdef __STDC__
00065      void (*out_char)(int);
00066 #else
00067      void (*out_char)();
00068 #endif
00069 {
00070   char digits[40];
00071   int len, ix;
00072 
00073   if (space) (*out_char) (' ');
00074   snprintf(digits, sizeof(digits), "%ld", val);
00075   len = strlen (digits);
00076   while (size > len)
00077     {
00078       (*out_char) ('0');
00079       size--;
00080     }
00081   for (ix=0; ix < len; ix++)
00082     (*out_char) (digits[ix]);
00083 }
00084 
00085 /* Output of a bcd number.  NUM is written in base O_BASE using OUT_CHAR
00086    as the routine to do the actual output of the characters. */
00087 
00088 void
00089 #ifdef __STDC__
00090 bc_out_num (bc_num num, int o_base, void (*out_char)(int), int leading_zero TSRMLS_DC)
00091 #else
00092 bc_out_num (bc_num num, int o_base, void (*out_char)(), int leading_zero TSRMLS_DC)
00093 #endif
00094 {
00095   char *nptr;
00096   int  index, fdigit, pre_space;
00097   stk_rec *digits, *temp;
00098   bc_num int_part, frac_part, base, cur_dig, t_num, max_o_digit;
00099 
00100   /* The negative sign if needed. */
00101   if (num->n_sign == MINUS) (*out_char) ('-');
00102 
00103   /* Output the number. */
00104   if (bc_is_zero (num TSRMLS_CC))
00105     (*out_char) ('0');
00106   else
00107     if (o_base == 10)
00108       {
00109        /* The number is in base 10, do it the fast way. */
00110        nptr = num->n_value;
00111        if (num->n_len > 1 || *nptr != 0)
00112          for (index=num->n_len; index>0; index--)
00113            (*out_char) (BCD_CHAR(*nptr++));
00114        else
00115          nptr++;
00116 
00117        if (leading_zero && bc_is_zero (num TSRMLS_CC))
00118          (*out_char) ('0');
00119 
00120        /* Now the fraction. */
00121        if (num->n_scale > 0)
00122          {
00123            (*out_char) ('.');
00124            for (index=0; index<num->n_scale; index++)
00125              (*out_char) (BCD_CHAR(*nptr++));
00126          }
00127       }
00128     else
00129       {
00130        /* special case ... */
00131        if (leading_zero && bc_is_zero (num TSRMLS_CC))
00132          (*out_char) ('0');
00133 
00134        /* The number is some other base. */
00135        digits = NULL;
00136        bc_init_num (&int_part TSRMLS_CC);
00137        bc_divide (num, BCG(_one_), &int_part, 0 TSRMLS_CC);
00138        bc_init_num (&frac_part TSRMLS_CC);
00139        bc_init_num (&cur_dig TSRMLS_CC);
00140        bc_init_num (&base TSRMLS_CC);
00141        bc_sub (num, int_part, &frac_part, 0);
00142        /* Make the INT_PART and FRAC_PART positive. */
00143        int_part->n_sign = PLUS;
00144        frac_part->n_sign = PLUS;
00145        bc_int2num (&base, o_base);
00146        bc_init_num (&max_o_digit TSRMLS_CC);
00147        bc_int2num (&max_o_digit, o_base-1);
00148 
00149 
00150        /* Get the digits of the integer part and push them on a stack. */
00151        while (!bc_is_zero (int_part TSRMLS_CC))
00152          {
00153            bc_modulo (int_part, base, &cur_dig, 0 TSRMLS_CC);
00154               /* PHP Change:  malloc() -> emalloc() */
00155            temp = (stk_rec *) emalloc (sizeof(stk_rec));
00156            if (temp == NULL) bc_out_of_memory();
00157            temp->digit = bc_num2long (cur_dig);
00158            temp->next = digits;
00159            digits = temp;
00160            bc_divide (int_part, base, &int_part, 0 TSRMLS_CC);
00161          }
00162 
00163        /* Print the digits on the stack. */
00164        if (digits != NULL)
00165          {
00166            /* Output the digits. */
00167            while (digits != NULL)
00168              {
00169               temp = digits;
00170               digits = digits->next;
00171               if (o_base <= 16)
00172                 (*out_char) (ref_str[ (int) temp->digit]);
00173               else
00174                 bc_out_long (temp->digit, max_o_digit->n_len, 1, out_char);
00175               efree (temp);
00176              }
00177          }
00178 
00179        /* Get and print the digits of the fraction part. */
00180        if (num->n_scale > 0)
00181          {
00182            (*out_char) ('.');
00183            pre_space = 0;
00184            t_num = bc_copy_num (BCG(_one_));
00185            while (t_num->n_len <= num->n_scale) {
00186              bc_multiply (frac_part, base, &frac_part, num->n_scale TSRMLS_CC);
00187              fdigit = bc_num2long (frac_part);
00188              bc_int2num (&int_part, fdigit);
00189              bc_sub (frac_part, int_part, &frac_part, 0);
00190              if (o_base <= 16)
00191               (*out_char) (ref_str[fdigit]);
00192              else {
00193               bc_out_long (fdigit, max_o_digit->n_len, pre_space, out_char);
00194               pre_space = 1;
00195              }
00196              bc_multiply (t_num, base, &t_num, 0 TSRMLS_CC);
00197            }
00198            bc_free_num (&t_num);
00199          }
00200 
00201        /* Clean up. */
00202        bc_free_num (&int_part);
00203        bc_free_num (&frac_part);
00204        bc_free_num (&base);
00205        bc_free_num (&cur_dig);
00206        bc_free_num (&max_o_digit);
00207       }
00208 }