github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpf/inp_str.c (about)

     1  /* mpf_inp_str(dest_float, stream, base) -- Input a number in base
     2     BASE from stdio stream STREAM and store the result in DEST_FLOAT.
     3  
     4  Copyright 1996, 2000-2002, 2005 Free Software Foundation, Inc.
     5  
     6  This file is part of the GNU MP Library.
     7  
     8  The GNU MP Library is free software; you can redistribute it and/or modify
     9  it under the terms of either:
    10  
    11    * the GNU Lesser General Public License as published by the Free
    12      Software Foundation; either version 3 of the License, or (at your
    13      option) any later version.
    14  
    15  or
    16  
    17    * the GNU General Public License as published by the Free Software
    18      Foundation; either version 2 of the License, or (at your option) any
    19      later version.
    20  
    21  or both in parallel, as here.
    22  
    23  The GNU MP Library is distributed in the hope that it will be useful, but
    24  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    25  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    26  for more details.
    27  
    28  You should have received copies of the GNU General Public License and the
    29  GNU Lesser General Public License along with the GNU MP Library.  If not,
    30  see https://www.gnu.org/licenses/.  */
    31  
    32  #include <stdio.h>
    33  #include <ctype.h>
    34  #include "gmp.h"
    35  #include "gmp-impl.h"
    36  
    37  size_t
    38  mpf_inp_str (mpf_ptr rop, FILE *stream, int base)
    39  {
    40    char *str;
    41    size_t alloc_size, str_size;
    42    int c;
    43    int res;
    44    size_t nread;
    45  
    46    if (stream == 0)
    47      stream = stdin;
    48  
    49    alloc_size = 100;
    50    str = (char *) (*__gmp_allocate_func) (alloc_size);
    51    str_size = 0;
    52    nread = 0;
    53  
    54    /* Skip whitespace.  */
    55    do
    56      {
    57        c = getc (stream);
    58        nread++;
    59      }
    60    while (isspace (c));
    61  
    62    for (;;)
    63      {
    64        if (str_size >= alloc_size)
    65  	{
    66  	  size_t old_alloc_size = alloc_size;
    67  	  alloc_size = alloc_size * 3 / 2;
    68  	  str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
    69  	}
    70        if (c == EOF || isspace (c))
    71  	break;
    72        str[str_size++] = c;
    73        c = getc (stream);
    74      }
    75    ungetc (c, stream);
    76    nread--;
    77  
    78    if (str_size >= alloc_size)
    79      {
    80        size_t old_alloc_size = alloc_size;
    81        alloc_size = alloc_size * 3 / 2;
    82        str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
    83      }
    84    str[str_size] = 0;
    85  
    86    res = mpf_set_str (rop, str, base);
    87    (*__gmp_free_func) (str, alloc_size);
    88  
    89    if (res == -1)
    90      return 0;			/* error */
    91  
    92    return str_size + nread;
    93  }