Back to index

php5  5.3.10
Classes | Typedefs | Functions | Variables
output.c File Reference
#include <config.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include "bcmath.h"
#include "private.h"

Go to the source code of this file.

Classes

struct  stk_rec

Typedefs

typedef struct stk_rec stk_rec

Functions

void bc_out_long (long val, int size, int space, void(*out_char)())
void bc_out_num (bc_num num, int o_base, void(*out_char)(), int leading_zero TSRMLS_DC)

Variables

static char ref_str [] = "0123456789ABCDEF"

Class Documentation

struct stk_rec

Definition at line 46 of file output.c.

Collaboration diagram for stk_rec:
Class Members
long digit
struct stk_rec * next

Typedef Documentation

typedef struct stk_rec stk_rec

Function Documentation

void bc_out_long ( long  val,
int  size,
int  space,
void (*)()  out_char 
)

Definition at line 61 of file output.c.

{
  char digits[40];
  int len, ix;

  if (space) (*out_char) (' ');
  snprintf(digits, sizeof(digits), "%ld", val);
  len = strlen (digits);
  while (size > len)
    {
      (*out_char) ('0');
      size--;
    }
  for (ix=0; ix < len; ix++)
    (*out_char) (digits[ix]);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void bc_out_num ( bc_num  num,
int  o_base,
void(*)()  out_char,
int leading_zero  TSRMLS_DC 
)

Definition at line 92 of file output.c.

{
  char *nptr;
  int  index, fdigit, pre_space;
  stk_rec *digits, *temp;
  bc_num int_part, frac_part, base, cur_dig, t_num, max_o_digit;

  /* The negative sign if needed. */
  if (num->n_sign == MINUS) (*out_char) ('-');

  /* Output the number. */
  if (bc_is_zero (num TSRMLS_CC))
    (*out_char) ('0');
  else
    if (o_base == 10)
      {
       /* The number is in base 10, do it the fast way. */
       nptr = num->n_value;
       if (num->n_len > 1 || *nptr != 0)
         for (index=num->n_len; index>0; index--)
           (*out_char) (BCD_CHAR(*nptr++));
       else
         nptr++;

       if (leading_zero && bc_is_zero (num TSRMLS_CC))
         (*out_char) ('0');

       /* Now the fraction. */
       if (num->n_scale > 0)
         {
           (*out_char) ('.');
           for (index=0; index<num->n_scale; index++)
             (*out_char) (BCD_CHAR(*nptr++));
         }
      }
    else
      {
       /* special case ... */
       if (leading_zero && bc_is_zero (num TSRMLS_CC))
         (*out_char) ('0');

       /* The number is some other base. */
       digits = NULL;
       bc_init_num (&int_part TSRMLS_CC);
       bc_divide (num, BCG(_one_), &int_part, 0 TSRMLS_CC);
       bc_init_num (&frac_part TSRMLS_CC);
       bc_init_num (&cur_dig TSRMLS_CC);
       bc_init_num (&base TSRMLS_CC);
       bc_sub (num, int_part, &frac_part, 0);
       /* Make the INT_PART and FRAC_PART positive. */
       int_part->n_sign = PLUS;
       frac_part->n_sign = PLUS;
       bc_int2num (&base, o_base);
       bc_init_num (&max_o_digit TSRMLS_CC);
       bc_int2num (&max_o_digit, o_base-1);


       /* Get the digits of the integer part and push them on a stack. */
       while (!bc_is_zero (int_part TSRMLS_CC))
         {
           bc_modulo (int_part, base, &cur_dig, 0 TSRMLS_CC);
              /* PHP Change:  malloc() -> emalloc() */
           temp = (stk_rec *) emalloc (sizeof(stk_rec));
           if (temp == NULL) bc_out_of_memory();
           temp->digit = bc_num2long (cur_dig);
           temp->next = digits;
           digits = temp;
           bc_divide (int_part, base, &int_part, 0 TSRMLS_CC);
         }

       /* Print the digits on the stack. */
       if (digits != NULL)
         {
           /* Output the digits. */
           while (digits != NULL)
             {
              temp = digits;
              digits = digits->next;
              if (o_base <= 16)
                (*out_char) (ref_str[ (int) temp->digit]);
              else
                bc_out_long (temp->digit, max_o_digit->n_len, 1, out_char);
              efree (temp);
             }
         }

       /* Get and print the digits of the fraction part. */
       if (num->n_scale > 0)
         {
           (*out_char) ('.');
           pre_space = 0;
           t_num = bc_copy_num (BCG(_one_));
           while (t_num->n_len <= num->n_scale) {
             bc_multiply (frac_part, base, &frac_part, num->n_scale TSRMLS_CC);
             fdigit = bc_num2long (frac_part);
             bc_int2num (&int_part, fdigit);
             bc_sub (frac_part, int_part, &frac_part, 0);
             if (o_base <= 16)
              (*out_char) (ref_str[fdigit]);
             else {
              bc_out_long (fdigit, max_o_digit->n_len, pre_space, out_char);
              pre_space = 1;
             }
             bc_multiply (t_num, base, &t_num, 0 TSRMLS_CC);
           }
           bc_free_num (&t_num);
         }

       /* Clean up. */
       bc_free_num (&int_part);
       bc_free_num (&frac_part);
       bc_free_num (&base);
       bc_free_num (&cur_dig);
       bc_free_num (&max_o_digit);
      }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char ref_str[] = "0123456789ABCDEF" [static]

Definition at line 52 of file output.c.