1/* mpq_inp_str -- read an mpq from a FILE.
2
3Copyright 2001, 2018 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of either:
9
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.
13
14or
15
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
18    later version.
19
20or both in parallel, as here.
21
22The GNU MP Library is distributed in the hope that it will be useful, but
23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25for more details.
26
27You should have received copies of the GNU General Public License and the
28GNU Lesser General Public License along with the GNU MP Library.  If not,
29see https://www.gnu.org/licenses/.  */
30
31#include <stdio.h>
32#include <ctype.h>
33#include "gmp-impl.h"
34
35
36size_t
37mpq_inp_str (mpq_ptr q, FILE *fp, int base)
38{
39  size_t  nread;
40  int     c;
41
42  if (fp == NULL)
43    fp = stdin;
44
45  SIZ(DEN(q)) = 1;
46  MPZ_NEWALLOC (DEN(q), 1)[0] = 1;
47
48  nread = mpz_inp_str (mpq_numref(q), fp, base);
49  if (nread == 0)
50    return 0;
51
52  c = getc (fp);
53  nread++;
54
55  if (c == '/')
56    {
57      c = getc (fp);
58      nread++;
59
60      nread = mpz_inp_str_nowhite (mpq_denref(q), fp, base, c, nread);
61      if (nread == 0)
62	{
63	  SIZ(NUM(q)) = 0;
64	  SIZ(DEN(q)) = 1;
65	  PTR(DEN(q))[0] = 1;
66	}
67    }
68  else
69    {
70      ungetc (c, fp);
71      nread--;
72    }
73
74  return nread;
75}
76