diff options
author | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2000-05-25 15:28:50 +0000 |
---|---|---|
committer | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2000-05-25 15:28:50 +0000 |
commit | 80f6cfc62657e7e4c5df6128476546503e615fbc (patch) | |
tree | f6f09a36bce5e7c1a330f3bb27be2e3b34042e2e /inp_str.c | |
parent | f523fc19ff465108328380c9b5b6fcf73e432e3f (diff) | |
download | mpfr-80f6cfc62657e7e4c5df6128476546503e615fbc.tar.gz |
initial version
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@560 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'inp_str.c')
-rw-r--r-- | inp_str.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/inp_str.c b/inp_str.c new file mode 100644 index 000000000..92627f9d5 --- /dev/null +++ b/inp_str.c @@ -0,0 +1,91 @@ +/* mpf_inp_str(dest_float, stream, base) -- Input a number in base + BASE from stdio stream STREAM and store the result in DEST_FLOAT. + +Copied from GMP, file mpf/inp_str.c. + +This file is part of the MPFR Library. + +The MPFR Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Library General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at your +option) any later version. + +The MPFR Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public +License for more details. + +You should have received a copy of the GNU Library General Public License +along with the MPFR Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include <stdio.h> +#include <ctype.h> +#include "gmp.h" +#include "gmp-impl.h" +#include "mpfr.h" + +size_t +#if __STDC__ +mpfr_inp_str (mpfr_ptr rop, FILE *stream, int base, mp_rnd_t rnd_mode) +#else +mpfr_inp_str (rop, stream, base, rnd_mode) + mpfr_ptr rop; + FILE *stream; + int base; + mp_rnd_t rnd_mode; +#endif +{ + char *str; + size_t alloc_size, str_size; + int c; + size_t retval; + size_t nread; + + if (stream == 0) + stream = stdin; + + alloc_size = 100; + str = (char *) (*_mp_allocate_func) (alloc_size); + str_size = 0; + nread = 0; + + /* Skip whitespace. */ + do + { + c = getc (stream); + nread++; + } + while (isspace (c)); + + for (;;) + { + if (str_size >= alloc_size) + { + size_t old_alloc_size = alloc_size; + alloc_size = alloc_size * 3 / 2; + str = (char *) (*_mp_reallocate_func) (str, old_alloc_size, alloc_size); + } + if (c == EOF || isspace (c)) + break; + str[str_size++] = c; + c = getc (stream); + } + ungetc (c, stream); + + if (str_size >= alloc_size) + { + size_t old_alloc_size = alloc_size; + alloc_size = alloc_size * 3 / 2; + str = (char *) (*_mp_reallocate_func) (str, old_alloc_size, alloc_size); + } + str[str_size] = 0; + + retval = mpfr_set_str (rop, str, base, rnd_mode); + if (retval == -1) + return 0; /* error */ + + (*_mp_free_func) (str, alloc_size); + return str_size + nread; +} |