Back to index

php5  5.3.10
str2num.c
Go to the documentation of this file.
00001 /* str2num.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 /* Convert strings to bc numbers.  Base 10 only.*/
00042 
00043 void
00044 bc_str2num (bc_num *num, char *str, int scale TSRMLS_DC)
00045 {
00046   int digits, strscale;
00047   char *ptr, *nptr;
00048   char zero_int;
00049 
00050   /* Prepare num. */
00051   bc_free_num (num);
00052 
00053   /* Check for valid number and count digits. */
00054   ptr = str;
00055   digits = 0;
00056   strscale = 0;
00057   zero_int = FALSE;
00058   if ( (*ptr == '+') || (*ptr == '-'))  ptr++;  /* Sign */
00059   while (*ptr == '0') ptr++;                     /* Skip leading zeros. */
00060   while (isdigit((int)*ptr)) ptr++, digits++;    /* digits */
00061   if (*ptr == '.') ptr++;                 /* decimal point */
00062   while (isdigit((int)*ptr)) ptr++, strscale++;  /* digits */
00063   if ((*ptr != '\0') || (digits+strscale == 0))
00064     {
00065       *num = bc_copy_num (BCG(_zero_));
00066       return;
00067     }
00068 
00069   /* Adjust numbers and allocate storage and initialize fields. */
00070   strscale = MIN(strscale, scale);
00071   if (digits == 0)
00072     {
00073       zero_int = TRUE;
00074       digits = 1;
00075     }
00076   *num = bc_new_num (digits, strscale);
00077 
00078   /* Build the whole number. */
00079   ptr = str;
00080   if (*ptr == '-')
00081     {
00082       (*num)->n_sign = MINUS;
00083       ptr++;
00084     }
00085   else
00086     {
00087       (*num)->n_sign = PLUS;
00088       if (*ptr == '+') ptr++;
00089     }
00090   while (*ptr == '0') ptr++;                     /* Skip leading zeros. */
00091   nptr = (*num)->n_value;
00092   if (zero_int)
00093     {
00094       *nptr++ = 0;
00095       digits = 0;
00096     }
00097   for (;digits > 0; digits--)
00098     *nptr++ = CH_VAL(*ptr++);
00099 
00100 
00101   /* Build the fractional part. */
00102   if (strscale > 0)
00103     {
00104       ptr++;  /* skip the decimal point! */
00105       for (;strscale > 0; strscale--)
00106        *nptr++ = CH_VAL(*ptr++);
00107     }
00108 }
00109