xref: /netbsd-src/external/lgpl3/gmp/dist/cxx/isfuns.cc (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
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.h"
35 #include "gmp-impl.h"
36 
37 using namespace std;
38 
39 
40 int
41 __gmp_istream_set_base (istream &i, char &c, bool &zero, bool &showbase)
42 {
43   int base;
44 
45   zero = showbase = false;
46   switch (i.flags() & ios::basefield)
47     {
48     case ios::dec:
49       base = 10;
50       break;
51     case ios::hex:
52       base = 16;
53       break;
54     case ios::oct:
55       base = 8;
56       break;
57     default:
58       showbase = true; // look for initial "0" or "0x" or "0X"
59       if (c == '0')
60 	{
61 	  if (! i.get(c))
62 	    c = 0; // reset or we might loop indefinitely
63 
64 	  if (c == 'x' || c == 'X')
65 	    {
66 	      base = 16;
67 	      i.get(c);
68 	    }
69 	  else
70 	    {
71 	      base = 8;
72 	      zero = true; // if no other digit is read, the "0" counts
73 	    }
74 	}
75       else
76 	base = 10;
77       break;
78     }
79 
80   return base;
81 }
82 
83 void
84 __gmp_istream_set_digits (string &s, istream &i, char &c, bool &ok, int base)
85 {
86   switch (base)
87     {
88     case 10:
89       while (isdigit(c))
90 	{
91 	  ok = true; // at least a valid digit was read
92 	  s += c;
93 	  if (! i.get(c))
94 	    break;
95 	}
96       break;
97     case 8:
98       while (isdigit(c) && c != '8' && c != '9')
99 	{
100 	  ok = true; // at least a valid digit was read
101 	  s += c;
102 	  if (! i.get(c))
103 	    break;
104 	}
105       break;
106     case 16:
107       while (isxdigit(c))
108 	{
109 	  ok = true; // at least a valid digit was read
110 	  s += c;
111 	  if (! i.get(c))
112 	    break;
113 	}
114       break;
115     }
116 }
117