Home | History | Annotate | Line # | Download | only in mpq
inp_str.c revision 1.1
      1  1.1  mrg /* mpq_inp_str -- read an mpq from a FILE.
      2  1.1  mrg 
      3  1.1  mrg Copyright 2001 Free Software Foundation, Inc.
      4  1.1  mrg 
      5  1.1  mrg This file is part of the GNU MP Library.
      6  1.1  mrg 
      7  1.1  mrg The GNU MP Library is free software; you can redistribute it and/or modify
      8  1.1  mrg it under the terms of the GNU Lesser General Public License as published by
      9  1.1  mrg the Free Software Foundation; either version 3 of the License, or (at your
     10  1.1  mrg option) any later version.
     11  1.1  mrg 
     12  1.1  mrg The GNU MP Library is distributed in the hope that it will be useful, but
     13  1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     14  1.1  mrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     15  1.1  mrg License for more details.
     16  1.1  mrg 
     17  1.1  mrg You should have received a copy of the GNU Lesser General Public License
     18  1.1  mrg along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
     19  1.1  mrg 
     20  1.1  mrg #include <stdio.h>
     21  1.1  mrg #include <ctype.h>
     22  1.1  mrg #include "gmp.h"
     23  1.1  mrg #include "gmp-impl.h"
     24  1.1  mrg 
     25  1.1  mrg 
     26  1.1  mrg size_t
     27  1.1  mrg mpq_inp_str (mpq_ptr q, FILE *fp, int base)
     28  1.1  mrg {
     29  1.1  mrg   size_t  nread;
     30  1.1  mrg   int     c;
     31  1.1  mrg 
     32  1.1  mrg   if (fp == NULL)
     33  1.1  mrg     fp = stdin;
     34  1.1  mrg 
     35  1.1  mrg   q->_mp_den._mp_size = 1;
     36  1.1  mrg   q->_mp_den._mp_d[0] = 1;
     37  1.1  mrg 
     38  1.1  mrg   nread = mpz_inp_str (mpq_numref(q), fp, base);
     39  1.1  mrg   if (nread == 0)
     40  1.1  mrg     return 0;
     41  1.1  mrg 
     42  1.1  mrg   c = getc (fp);
     43  1.1  mrg   nread++;
     44  1.1  mrg 
     45  1.1  mrg   if (c == '/')
     46  1.1  mrg     {
     47  1.1  mrg       c = getc (fp);
     48  1.1  mrg       nread++;
     49  1.1  mrg 
     50  1.1  mrg       nread = mpz_inp_str_nowhite (mpq_denref(q), fp, base, c, nread);
     51  1.1  mrg       if (nread == 0)
     52  1.1  mrg         {
     53  1.1  mrg           q->_mp_num._mp_size = 0;
     54  1.1  mrg           q->_mp_den._mp_size = 1;
     55  1.1  mrg           q->_mp_den._mp_d[0] = 1;
     56  1.1  mrg         }
     57  1.1  mrg     }
     58  1.1  mrg   else
     59  1.1  mrg     {
     60  1.1  mrg       ungetc (c, fp);
     61  1.1  mrg       nread--;
     62  1.1  mrg     }
     63  1.1  mrg 
     64  1.1  mrg   return nread;
     65  1.1  mrg }
     66