1 /* mpc_inp_str -- Input a complex number from a given stream.
3 Copyright (C) 2009 Andreas Enge, Philippe Th\'eveny, Paul Zimmermann
5 This file is part of the MPC Library.
7 The MPC Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
12 The MPC Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the MPC Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
22 #include <stdio.h> /* for FILE */
28 skip_whitespace (FILE *stream) {
29 int c = getc (stream);
31 while (c != EOF && isspace ((unsigned char) c)) {
40 /* Extract from stream the longest string made up of alphanumeric char and
41 '_' (i.e. n-char-sequence).
42 The user must free the returned string. */
44 extract_suffix (FILE *stream)
49 char *str = mpc_alloc_str (strsize);
52 while (isalnum ((unsigned char) c) || c == '_') {
53 str [nread] = (char) c;
55 if (nread == strsize) {
56 str = mpc_realloc_str (str, strsize, 2 * strsize);
62 str = mpc_realloc_str (str, strsize, nread + 1);
72 /* Extract from the stream the longest string of characters which are neither
73 whitespace nor brackets (except for an optional bracketed n-char_sequence
74 directly following nan or @nan@ independently of case).
75 The user must free the returned string. */
77 extract_string (FILE *stream)
82 char *str = mpc_alloc_str (strsize);
86 while (c != EOF && c != '\n'
87 && !isspace ((unsigned char) c)
88 && c != '(' && c != ')') {
89 str [nread] = (char) c;
91 if (nread == strsize) {
92 str = mpc_realloc_str (str, strsize, 2 * strsize);
98 str = mpc_realloc_str (str, strsize, nread + 1);
111 /* (n-char-sequence) only after a NaN */
113 || tolower ((unsigned char) (str[0])) != 'n'
114 || tolower ((unsigned char) (str[1])) != 'a'
115 || tolower ((unsigned char) (str[2])) != 'n')
118 || tolower ((unsigned char) (str[1])) != 'n'
119 || tolower ((unsigned char) (str[2])) != 'a'
120 || tolower ((unsigned char) (str[3])) != 'n'
126 suffix = extract_suffix (stream);
127 nread += strlen (suffix) + 1;
128 if (nread >= strsize) {
129 str = mpc_realloc_str (str, strsize, nread + 1);
133 /* Warning: the sprintf does not allow overlap between arguments. */
134 n = lenstr + sprintf (str + lenstr, "(%s", suffix);
135 MPC_ASSERT (n == nread);
139 str = mpc_realloc_str (str, strsize, nread + 2);
142 str [nread+1] = '\0';
148 mpc_free_str (suffix);
158 mpc_inp_str (mpc_ptr rop, FILE *stream, size_t *read, int base,
161 size_t white, nread = 0;
169 white = skip_whitespace (stream);
177 nread++; /* the opening parenthesis */
178 white = skip_whitespace (stream);
179 real_str = extract_string (stream);
180 nread += strlen(real_str);
183 if (!isspace ((unsigned int) c)) {
186 mpc_free_str (real_str);
192 white += skip_whitespace (stream);
193 imag_str = extract_string (stream);
194 nread += strlen (imag_str);
196 str = mpc_alloc_str (nread + 2);
197 n = sprintf (str, "(%s %s", real_str, imag_str);
198 MPC_ASSERT (n == nread + 1);
199 mpc_free_str (real_str);
200 mpc_free_str (imag_str);
202 white += skip_whitespace (stream);
205 str = mpc_realloc_str (str, nread +2, nread + 3);
207 str [nread+2] = '\0';
216 str = extract_string (stream);
217 nread += strlen (str);
220 inex = mpc_set_str (rop, str, base, rnd_mode);
227 mpfr_set_nan (MPC_RE(rop));
228 mpfr_set_nan (MPC_IM(rop));
231 *read = white + nread;