Frobby
0.9.0
src
FrobbyStringStream.cpp
Go to the documentation of this file.
1
/* Frobby: Software for monomial ideal computations.
2
Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
3
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see http://www.gnu.org/licenses/.
16
*/
17
#include "
stdinc.h
"
18
#include "
FrobbyStringStream.h
"
19
20
#include <algorithm>
21
#include <exception>
22
23
// This is a replacement for stringstream, which may seem weird since
24
// stringstream should work perfectly fine for any purpose where
25
// FrobbyStringStream could be used. Unfortunately this is not the
26
// case.
27
//
28
// The sad truth is that stringstream is unusable due to its behavior
29
// when running out of memory - it just stops what it is doing and
30
// then returns without propagating the bad_alloc exception. Setting
31
// exception flags with the exception() method does not change this
32
// behavior. This is for GCC v. 3.4.4 and on GCC 4.1.2 I have tested
33
// that at least without setting exception() it fails silently, though
34
// not whether that can be changed by using exception().
35
//
36
// The advantage of FrobbyStringStream is that it does not try to be
37
// clever in any way, and so it avoids these issues.
38
39
FrobbyStringStream
&
FrobbyStringStream::operator<<
(
char
character) {
40
_str
+= character;
41
return
*
this
;
42
}
43
44
FrobbyStringStream
&
FrobbyStringStream::operator<<
(
unsigned
long
integer) {
45
appendIntegerToString
(
_str
, integer);
46
return
*
this
;
47
}
48
49
FrobbyStringStream
&
FrobbyStringStream::operator<<
(
unsigned
int
integer) {
50
appendIntegerToString
(
_str
, integer);
51
return
*
this
;
52
}
53
54
FrobbyStringStream
&
FrobbyStringStream::operator<<
(
const
mpz_class& integer) {
55
appendIntegerToString
(
_str
, integer);
56
return
*
this
;
57
}
58
59
FrobbyStringStream
&
FrobbyStringStream::operator<<
(
const
string
& text) {
60
_str
+= text;
61
return
*
this
;
62
}
63
64
FrobbyStringStream
&
FrobbyStringStream::operator<<
(
const
char
* text) {
65
_str
+= text;
66
return
*
this
;
67
}
68
69
string
&
FrobbyStringStream::str
() {
70
return
_str
;
71
}
72
73
const
string
&
FrobbyStringStream::str
()
const
{
74
return
_str
;
75
}
76
77
FrobbyStringStream::operator
const
string
&()
const
{
78
return
_str;
79
}
80
81
void
FrobbyStringStream::appendIntegerToString
(
string
& str,
82
unsigned
long
integer) {
83
unsigned
long
initialLength =
str
.size();
84
85
// Append string representation of integer with digits in reverse
86
// order.
87
do
{
88
unsigned
long
quotient = integer / 10;
89
unsigned
long
remainder = integer - quotient * 10;
// faster than %
90
91
char
digit =
static_cast<
char
>
(remainder +
'0'
);
92
str
+= digit;
93
94
integer = quotient;
95
96
// condition at end so that zero maps to "0" rather than "".
97
}
while
(integer != 0);
98
99
// Reverse the digits (and only the digits) to get the correct
100
// order.
101
reverse(
str
.begin() + initialLength,
str
.end());
102
}
103
104
void
FrobbyStringStream::appendIntegerToString
(
string
& str,
105
const
mpz_class& integer) {
106
str
+= integer.get_str();
107
}
108
109
void
FrobbyStringStream::parseInteger
(mpz_class& integer,
const
string
& str) {
110
if
(integer.set_str(
str
, 10) != 0)
111
throw
NotAnIntegerException
112
(
"Argument to FrobbyStringStream::parseInteger not a valid integer."
);
113
}
114
115
FrobbyStringStream::NotAnIntegerException::NotAnIntegerException
116
(
const
string
& str):
117
runtime_error(str) {
118
}
FrobbyStringStream::_str
string _str
Definition:
FrobbyStringStream.h:57
stdinc.h
FrobbyStringStream::appendIntegerToString
static void appendIntegerToString(string &str, unsigned long integer)
Definition:
FrobbyStringStream.cpp:81
FrobbyStringStream::parseInteger
static void parseInteger(mpz_class &integer, const string &str)
Throws NotAnIntegerException if str is not the string representation of an integer.
Definition:
FrobbyStringStream.cpp:109
FrobbyStringStream::NotAnIntegerException::NotAnIntegerException
NotAnIntegerException(const string &)
Definition:
FrobbyStringStream.cpp:116
FrobbyStringStream.h
FrobbyStringStream
A replacement for stringstream.
Definition:
FrobbyStringStream.h:26
FrobbyStringStream::str
string & str()
Definition:
FrobbyStringStream.cpp:69
FrobbyStringStream::NotAnIntegerException
Definition:
FrobbyStringStream.h:51
FrobbyStringStream::operator<<
FrobbyStringStream & operator<<(unsigned long integer)
Definition:
FrobbyStringStream.cpp:44
Generated by
1.8.17