str2num.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* str2num.c: bcmath library file. */
  2. /*
  3. Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
  4. Copyright (C) 2000 Philip A. Nelson
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details. (COPYING.LIB)
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to:
  15. The Free Software Foundation, Inc.
  16. 59 Temple Place, Suite 330
  17. Boston, MA 02111-1307 USA.
  18. You may contact the author by:
  19. e-mail: philnelson@acm.org
  20. us-mail: Philip A. Nelson
  21. Computer Science Department, 9062
  22. Western Washington University
  23. Bellingham, WA 98226-9062
  24. *************************************************************************/
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <assert.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #include <stdarg.h>
  31. #include "bcmath.h"
  32. #include "private.h"
  33. /* Convert strings to bc numbers. Base 10 only.*/
  34. void
  35. bc_str2num (bc_num *num, char *str, int scale TSRMLS_DC)
  36. {
  37. int digits, strscale;
  38. char *ptr, *nptr;
  39. char zero_int;
  40. /* Prepare num. */
  41. bc_free_num (num);
  42. /* Check for valid number and count digits. */
  43. ptr = str;
  44. digits = 0;
  45. strscale = 0;
  46. zero_int = FALSE;
  47. if ( (*ptr == '+') || (*ptr == '-')) ptr++; /* Sign */
  48. while (*ptr == '0') ptr++; /* Skip leading zeros. */
  49. while (isdigit((int)*ptr)) ptr++, digits++; /* digits */
  50. if (*ptr == '.') ptr++; /* decimal point */
  51. while (isdigit((int)*ptr)) ptr++, strscale++; /* digits */
  52. if ((*ptr != '\0') || (digits+strscale == 0))
  53. {
  54. *num = bc_copy_num (BCG(_zero_));
  55. return;
  56. }
  57. /* Adjust numbers and allocate storage and initialize fields. */
  58. strscale = MIN(strscale, scale);
  59. if (digits == 0)
  60. {
  61. zero_int = TRUE;
  62. digits = 1;
  63. }
  64. *num = bc_new_num (digits, strscale);
  65. /* Build the whole number. */
  66. ptr = str;
  67. if (*ptr == '-')
  68. {
  69. (*num)->n_sign = MINUS;
  70. ptr++;
  71. }
  72. else
  73. {
  74. (*num)->n_sign = PLUS;
  75. if (*ptr == '+') ptr++;
  76. }
  77. while (*ptr == '0') ptr++; /* Skip leading zeros. */
  78. nptr = (*num)->n_value;
  79. if (zero_int)
  80. {
  81. *nptr++ = 0;
  82. digits = 0;
  83. }
  84. for (;digits > 0; digits--)
  85. *nptr++ = CH_VAL(*ptr++);
  86. /* Build the fractional part. */
  87. if (strscale > 0)
  88. {
  89. ptr++; /* skip the decimal point! */
  90. for (;strscale > 0; strscale--)
  91. *nptr++ = CH_VAL(*ptr++);
  92. }
  93. }