1 /* mpc_inp_str -- Input a complex number from a given stream.
3 Copyright (C) 2009, 2010, 2011 INRIA
5 This file is part of GNU MPC.
7 GNU MPC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/ .
21 #include <stdio.h> /* for FILE */
27 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);
112 /* (n-char-sequence) only after a NaN */
114 || tolower ((unsigned char) (str
[0])) != 'n'
115 || tolower ((unsigned char) (str
[1])) != 'a'
116 || tolower ((unsigned char) (str
[2])) != 'n')
119 || tolower ((unsigned char) (str
[1])) != 'n'
120 || tolower ((unsigned char) (str
[2])) != 'a'
121 || tolower ((unsigned char) (str
[3])) != 'n'
127 suffix
= extract_suffix (stream
);
128 nread
+= strlen (suffix
) + 1;
129 if (nread
>= strsize
) {
130 str
= mpc_realloc_str (str
, strsize
, nread
+ 1);
134 /* Warning: the sprintf does not allow overlap between arguments. */
135 ret
= sprintf (str
+ lenstr
, "(%s", suffix
);
136 MPC_ASSERT (ret
>= 0);
137 n
= lenstr
+ (size_t) ret
;
138 MPC_ASSERT (n
== nread
);
142 str
= mpc_realloc_str (str
, strsize
, nread
+ 2);
144 str
[nread
] = (char) c
;
145 str
[nread
+1] = '\0';
151 mpc_free_str (suffix
);
161 mpc_inp_str (mpc_ptr rop
, FILE *stream
, size_t *read
, int base
,
164 size_t white
, nread
= 0;
172 white
= skip_whitespace (stream
);
181 nread
++; /* the opening parenthesis */
182 white
= skip_whitespace (stream
);
183 real_str
= extract_string (stream
);
184 nread
+= strlen(real_str
);
187 if (!isspace ((unsigned int) c
)) {
190 mpc_free_str (real_str
);
196 white
+= skip_whitespace (stream
);
197 imag_str
= extract_string (stream
);
198 nread
+= strlen (imag_str
);
200 str
= mpc_alloc_str (nread
+ 2);
201 ret
= sprintf (str
, "(%s %s", real_str
, imag_str
);
202 MPC_ASSERT (ret
>= 0);
204 MPC_ASSERT (n
== nread
+ 1);
205 mpc_free_str (real_str
);
206 mpc_free_str (imag_str
);
208 white
+= skip_whitespace (stream
);
211 str
= mpc_realloc_str (str
, nread
+2, nread
+ 3);
212 str
[nread
+1] = (char) c
;
213 str
[nread
+2] = '\0';
222 str
= extract_string (stream
);
223 nread
+= strlen (str
);
226 inex
= mpc_set_str (rop
, str
, base
, rnd_mode
);
233 mpfr_set_nan (mpc_realref(rop
));
234 mpfr_set_nan (mpc_imagref(rop
));
237 *read
= white
+ nread
;