source: trunk/third/gmp/cxx/isfuns.cc @ 18191

Revision 18191, 2.3 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Auxiliary functions for C++-style input of GMP types.
2
3Copyright 2001 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 the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA. */
21
22#include <cctype>
23#include <iostream>
24#include <string>
25#include "gmp.h"
26#include "gmp-impl.h"
27
28using namespace std;
29
30
31int
32__gmp_istream_set_base (istream &i, char &c, bool &zero, bool &showbase)
33{
34  int base;
35
36  zero = showbase = false;
37  switch (i.flags() & ios::basefield)
38    {
39    case ios::dec:
40      base = 10;
41      break;
42    case ios::hex:
43      base = 16;
44      break;
45    case ios::oct:
46      base = 8;
47      break;
48    default:
49      showbase = true; // look for initial "0" or "0x" or "0X"
50      if (c == '0')
51        {
52          if (! i.get(c))
53            c = 0; // reset or we might loop indefinitely
54
55          if (c == 'x' || c == 'X')
56            {
57              base = 16;
58              i.get(c);
59            }
60          else
61            {
62              base = 8;
63              zero = true; // if no other digit is read, the "0" counts
64            }
65        }
66      else
67        base = 10;
68      break;
69    }
70
71  return base;
72}
73
74void
75__gmp_istream_set_digits (string &s, istream &i, char &c, bool &ok, int base)
76{
77  switch (base)
78    {
79    case 10:
80      while (isdigit(c))
81        {
82          ok = true; // at least a valid digit was read
83          s += c;
84          if (! i.get(c))
85            break;
86        }
87      break;
88    case 8:
89      while (isdigit(c) && c != '8' && c != '9')
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 16:
98      while (isxdigit(c))
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    }
107}
Note: See TracBrowser for help on using the repository browser.