-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsputil.h
130 lines (100 loc) · 2.64 KB
/
sputil.h
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
#ifndef SPUTIL_H
#define SPUTIL_H
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <exception>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#define STRFY(s) #s
#define TOSTR(s) STRFY(s)
#define ERR_LOC __FILE__ ":" TOSTR(__LINE__)
#define VERIFY(expr) verify((expr), ERR_LOC ":\t assertion '" TOSTR(expr) "' failed!")
#define VERIFY_MSG(expr, msg) verify((expr), ERR_LOC ":\t", msg)
#ifdef S_DEBUG
#define ASSERT(expr) VERIFY(expr)
#else
#define ASSERT(expr) (void(0))
#endif
using boost::lexical_cast;
/*************************************************************************/
inline double pow_2(double x)
{
return (double) x*(double)x;
}
/*************************************************************************/
inline void get_time_short(std::string & smess)
/*get the current time and put only the day number and time in smess*/
{
smess = lexical_cast<std::string>(time(NULL));
}
inline void error(const std::string & msg)
{
throw std::runtime_error(msg);
//std::cerr << "\n" << msg << std::endl;
//exit(1);
}
inline void verify(bool cond, const std::string & msg, const std::string & msg2 = "")
{
if (!cond)
error(msg + msg2);
}
// split string into two halves at position pos and cast results to T
template<typename T>
void splitStr(const std::string & str, int pos, T & x1, T & x2)
{
x1 = lexical_cast<T>(str.substr(0, pos));
x2 = lexical_cast<T>(str.substr(pos+1, str.size()-pos-1));
}
// split string at separator sep and feed pieces into iterator iter
template<typename ITER>
void splitStr(const std::string & str, char sep, ITER & iter)
{
typedef typename ITER::container_type::value_type value_type;
size_t last = 0;
for (size_t i=0; i<=str.size(); i++)
if (i==str.size() || str[i]==sep)
{
*iter = lexical_cast<value_type>(str.substr(last, i-last));
last = i + 1;
}
}
// read next non-blank line
inline void skip_space(std::istream & inp, std::string & str)
{
while(getline(inp, str) && boost::all(str, boost::is_space()));
}
template<typename T>
bool get_value (std::istream & inp_file, T & value)
{
static std::string str;
skip_space(inp_file, str);
if (!inp_file) return false;
std::istringstream tmp_s;
tmp_s.str(str);
tmp_s >> value;
if (tmp_s.fail()) return false;
return true;
}
class SPIOException : public std::exception
{
protected:
std::string _msg;
public:
SPIOException(const std::string & msg)
: _msg(msg)
{}
const char * what() const throw()
{
return _msg.c_str();
}
void print(std::ostream & out) const
{
out << _msg;
}
virtual ~SPIOException() throw()
{}
};
#endif // SPUTIL_H