-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
218 lines (194 loc) · 4.18 KB
/
util.cpp
File metadata and controls
218 lines (194 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* util.cpp
*
* utility functions
*
* Copyright (c) 2003 by Martin Trautmann (martintrautmann@gmx.de)
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#include "util.hpp"
#include <iostream>
#ifndef __OLD_GCC__
#include <sstream>
#else
#include <strstream>
#endif
#include <ctype.h>
namespace holtz
{
void randomize()
{
unsigned int seed = (unsigned int)time(0);
std::cout << "Initial random seed is: " << seed << std::endl;
srand(seed);
}
unsigned next_prime( unsigned start )
{
if( !(start & 1) )
++start;
unsigned i;
bool found = false;
for( i = start; true; i += 2 )
{
found = true;
unsigned add = 5, factor_square = 9;
for( unsigned factor = 3; factor_square <= i; ++factor, factor_square += add )
{
if( i % factor == 0 )
{
found = false;
break;
}
add += 2;
}
if( found )
break;
}
return i;
}
std::string long_to_string( long l )
{
std::string str;
#ifndef __OLD_GCC__
std::ostringstream os;
#else
char buf[4096];
std::ostrstream os(buf,4096);
#endif
os << l;
str = os.str();
return str;
}
std::pair<long,unsigned /*digits*/> string_to_long( std::string str, int base )
{
const char *start = str.c_str();
char *end;
long num = strtol( start, &end, base );
return std::pair<long,unsigned /*digits*/>( num, end - start );
}
std::string double_to_string( double number, int post_point_digits,
int assert_max_pre_point_digits )
{
std::string str;
#ifndef __OLD_GCC__
std::ostringstream os;
#else
char buf[4096];
std::ostrstream os(buf,4096);
#endif
if( post_point_digits >= 0 )
{
int pre_point_digits=0;
double compare=1;
while( compare < number )
{
compare *= 10;
++pre_point_digits;
}
if( assert_max_pre_point_digits >= 0 )
assert( pre_point_digits <= assert_max_pre_point_digits );
os.precision(post_point_digits + pre_point_digits);
}
os << number;
str = os.str();
return str;
}
}
namespace std
{
#ifdef __OLD_GCC__
istringstream::istringstream( std::string str )
: istrstream((this->str=str).c_str(), str.size())
{
}
#endif
// returns an escaped string for transmission over streams
string escape( const string str )
{
string ret = str;
bool escape = false;
if( (ret.size() == 0) || (ret[0] == '\"') )
escape = true;
else
{
for( unsigned i=0; i<ret.size(); ++i )
if( isspace( ret[i] ) )
{
escape = true;
break;
}
}
if( escape )
{
replace( ret, "\\", "\\\\" );
replace( ret, "\"", "\\\"" );
ret = '\"' + ret + '\"';
}
return ret + ' ';
}
Escaped_String::Escaped_String( string &str )
: str(str), error(false)
{
}
istream &operator>>( istream &is, Escaped_String &estr )
{
bool ok = false;
string &dest = estr.str;
char c;
is >> c;
if( c == '"' )
{
dest = "";
while(is.good())
{
is.get(c);
if( c == '"' )
{
ok = true;
break;
}
if( c == '\\' )
is.get(c);
dest += c;
}
}
else
{
is.putback(c);
static_cast<istringstream&>(is) >> dest;
ok = true;
}
if( !ok )
estr.set_error();
return is;
}
escape_istream::escape_istream( istream &is ) : is(is), error(false)
{
}
escape_ostream::escape_ostream( ostream &os ) : os(os)
{
}
// replace pattern with <replace> in str and return number of occurances
int replace( string &str, string pattern, string replace,
string::size_type from, string::size_type to )
{
int replaces = 0;
string::size_type pos,last_pos=from;
for(;;)
{
pos = str.find(pattern, last_pos);
if( (pos == string::npos) || ((to != string::npos) && (pos >= to)) ) break;
str.replace( pos, pattern.size(), replace );
last_pos = pos + replace.size();
replaces++;
}
return replaces;
}
}