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 the GNU Lesser General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or (at your 10 option) any later version. 11 12 The GNU MP Library is distributed in the hope that it will be useful, but 13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15 License for more details. 16 17 You should have received a copy of the GNU Lesser General Public License 18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 19 20 #include <cctype> 21 #include <iostream> 22 #include <string> 23 #include "gmp.h" 24 #include "gmp-impl.h" 25 26 using namespace std; 27 28 29 int 30 __gmp_istream_set_base (istream &i, char &c, bool &zero, bool &showbase) 31 { 32 int base; 33 34 zero = showbase = false; 35 switch (i.flags() & ios::basefield) 36 { 37 case ios::dec: 38 base = 10; 39 break; 40 case ios::hex: 41 base = 16; 42 break; 43 case ios::oct: 44 base = 8; 45 break; 46 default: 47 showbase = true; // look for initial "0" or "0x" or "0X" 48 if (c == '0') 49 { 50 if (! i.get(c)) 51 c = 0; // reset or we might loop indefinitely 52 53 if (c == 'x' || c == 'X') 54 { 55 base = 16; 56 i.get(c); 57 } 58 else 59 { 60 base = 8; 61 zero = true; // if no other digit is read, the "0" counts 62 } 63 } 64 else 65 base = 10; 66 break; 67 } 68 69 return base; 70 } 71 72 void 73 __gmp_istream_set_digits (string &s, istream &i, char &c, bool &ok, int base) 74 { 75 switch (base) 76 { 77 case 10: 78 while (isdigit(c)) 79 { 80 ok = true; // at least a valid digit was read 81 s += c; 82 if (! i.get(c)) 83 break; 84 } 85 break; 86 case 8: 87 while (isdigit(c) && c != '8' && c != '9') 88 { 89 ok = true; // at least a valid digit was read 90 s += c; 91 if (! i.get(c)) 92 break; 93 } 94 break; 95 case 16: 96 while (isxdigit(c)) 97 { 98 ok = true; // at least a valid digit was read 99 s += c; 100 if (! i.get(c)) 101 break; 102 } 103 break; 104 } 105 } 106