1 /* Auxiliary functions for C++-style input of GMP types.
2 
3 Copyright 2001 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library.
6 
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it 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 
14 or
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 
20 or both in parallel, as here.
21 
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
25 for more details.
26 
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/.  */
30 
31 #include <cctype>
32 #include <iostream>
33 #include <string>
34 #include "gmp-impl.h"
35 
36 using namespace std;
37 
38 
39 int
__gmp_istream_set_base(istream & i,char & c,bool & zero,bool & showbase)40 __gmp_istream_set_base (istream &i, char &c, bool &zero, bool &showbase)
41 {
42   int base;
43 
44   zero = showbase = false;
45   switch (i.flags() & ios::basefield)
46     {
47     case ios::dec:
48       base = 10;
49       break;
50     case ios::hex:
51       base = 16;
52       break;
53     case ios::oct:
54       base = 8;
55       break;
56     default:
57       showbase = true; // look for initial "0" or "0x" or "0X"
58       if (c == '0')
59 	{
60 	  if (! i.get(c))
61 	    c = 0; // reset or we might loop indefinitely
62 
63 	  if (c == 'x' || c == 'X')
64 	    {
65 	      base = 16;
66 	      i.get(c);
67 	    }
68 	  else
69 	    {
70 	      base = 8;
71 	      zero = true; // if no other digit is read, the "0" counts
72 	    }
73 	}
74       else
75 	base = 10;
76       break;
77     }
78 
79   return base;
80 }
81 
82 void
__gmp_istream_set_digits(string & s,istream & i,char & c,bool & ok,int base)83 __gmp_istream_set_digits (string &s, istream &i, char &c, bool &ok, int base)
84 {
85   switch (base)
86     {
87     case 10:
88       while (isdigit(c))
89 	{
90 	  ok = true; // at least a valid digit was read
91 	  s += c;
92 	  if (! i.get(c))
93 	    break;
94 	}
95       break;
96     case 8:
97       while (isdigit(c) && c != '8' && c != '9')
98 	{
99 	  ok = true; // at least a valid digit was read
100 	  s += c;
101 	  if (! i.get(c))
102 	    break;
103 	}
104       break;
105     case 16:
106       while (isxdigit(c))
107 	{
108 	  ok = true; // at least a valid digit was read
109 	  s += c;
110 	  if (! i.get(c))
111 	    break;
112 	}
113       break;
114     }
115 }
116