1 /* operator>> -- C++-style input of mpf_t.
3 Copyright 2001, 2003 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
34 #include <clocale> // for localeconv
42 // For g++ libstdc++ parsing see num_get<chartype,initer>::_M_extract_float
43 // in include/bits/locale_facets.tcc.
45 // There are no plans to accept hex or octal floats, not unless the standard
46 // C++ library does so. Although such formats might be of use, it's
47 // considered more important to be compatible with what the normal
48 // operator>> does on "double"s etc.
51 operator>> (istream &i, mpf_ptr f)
58 // C decimal point, as expected by mpf_set_str
59 const char *lconv_point = GMP_DECIMAL_POINT;
63 const locale& loc = i.getloc();
64 char point_char = use_facet< numpunct<char> >(loc).decimal_point();
66 const char *point = lconv_point;
67 char point_char = *point;
70 i.get(c); // start reading
72 if (i.flags() & ios::skipws) // skip initial whitespace
76 const ctype<char>& ct = use_facet< ctype<char> >(loc);
77 #define cxx_isspace(c) (ct.is(ctype_base::space,(c)))
79 #define cxx_isspace(c) isspace(c)
82 while (cxx_isspace(c) && i.get(c))
86 if (c == '-' || c == '+') // sign
94 __gmp_istream_set_digits(s, i, c, ok, base); // read the number
96 // look for the C++ radix point, but put the C one in for mpf_set_str
101 #else // lconv point can be multi-char
113 __gmp_istream_set_digits(s, i, c, ok, base); // read the mantissa
116 if (ok && (c == 'e' || c == 'E')) // exponent
120 ok = false; // exponent is mandatory
122 if (c == '-' || c == '+') // sign
128 __gmp_istream_set_digits(s, i, c, ok, base); // read the exponent
131 if (i.good()) // last character read was non-numeric
133 else if (i.eof() && ok) // stopped just before eof
134 i.clear(ios::eofbit);
137 ASSERT_NOCARRY (mpf_set_str(f, s.c_str(), base)); // extract the number
141 i.setstate(ios::failbit); // read failed