-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
167 lines (132 loc) · 4.95 KB
/
util.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
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
/*************************************************
* Publicly released by Rhoban System, August 2012
* www.rhoban-system.fr
*
* Freely usable for non-commercial purposes
*
* Licence Creative Commons *CC BY-NC-SA
* http://creativecommons.org/licenses/by-nc-sa/3.0
*************************************************/
#ifndef UTIL_H
#define UTIL_H
typedef unsigned char ui8;
typedef unsigned short ui16;
typedef unsigned int ui32;
typedef unsigned int uint;
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <list>
#include <stdexcept>
#include <ctime>
using namespace std;
/**
* Return the content of the whole file 'effectively'
* cf: http://insanecoding.blogspot.jp/2011/11/how-to-read-in-file-in-c.html
*/
std::string slurpFile(const std::string& path);
std::string to_string(double val, int precision);
bool is_in_vector_string(vector<string> vector, string testval);
template <typename T>
bool is_in(vector<T> lst, T x) {
for (int i=0; i < (int) lst.size(); i++)
if (x==lst[i]) return true;
return false;
}
/* extract the subvector of lst regarding indexes [i0,i1] included */
template <typename T>
vector<T> v_extract_subvector(vector<T> & lst, int i0, int i1) {
vector<T> result;
if (i0 < 0 || i1 < 0 ||
i0 > ((int) lst.size()-1) ||
i1 > ((int) lst.size()-1) ||
i0 > i1) return result;
return vector<T>(lst.begin() + i0, lst.begin() + i1);
}
inline string my_itoa(int i)
{
ostringstream os;
os << i;
return os.str();
}
int char_to_int(char c);
int hex_to_int(char * argv);
int string_to_int(char * argv);
//TODO: eliminer my_trunc
#define my_trunc(x,a,b) { if ((x) < (a)) x=a; if ((x) > (b)) x=b; }
// Truncate the value x and return the new value
#define interval_trunc(x,a,b) ( ((x)<(a)) ? x=a : ( ((x)>(b))? x=b : x ) )
#define abs_value(x) (((x)>=0)?(x):-(x))
/*****************************************************************************/
#define BOT_VERBOSE_MODE 1
#define BOT_VERBOSE(x,...) do { \
if ( BOT_VERBOSE_MODE ) { printf(x,...); fflush(stdout); } \
} while(0);
#define BOT_ERROR(x,...) do { \
printf("Bot Error: "); printf(x,...); fflush(stdout); \
} while(0);
/*****************************************************************************/
#define check_opt(option) (argc>1 && strcmp(argv[1], option)==0)
/*****************************************************************************/
int is_parameter(string param_name, int argc, char * argv[]);
pair<bool,string> get_parameter(string param_name, int argc, char * argv[]);
pair<bool,int> get_int_parameter(string param_name, int argc, char * argv[]);
bool get_int_parameter(string param_name, int * arg1, int argc, char * argv[]);
bool get_int_int_parameter(string param_name, int * arg1, int * arg2, int argc, char * argv[]);
bool get_int_int_int_parameter(string param_name, int * arg1, int * arg2, int * arg3, int argc, char * argv[]);
/*****************************************************************************/
/*! \name Tools (\todo: to be put anywhere else !
*****************************************************************************/
/*@{*/
//Hug the following lines may conflict with some C++ libraries I suspect
#define my_min(a,b) (((a)<(b))? (a) : (b))
#define my_max(a,b) (((a)>(b))? (a) : (b))
template<typename T>
void list_concat(std::list<T> & dest, const std::list<T> src) {
for (typename std::list<T>::const_iterator elt = src.begin(); elt != src.end(); elt++)
dest.push_back(*elt);
}
template<typename T>
bool list_contains(std::list<T> l, T elt) {
for (typename std::list<T>::iterator e = l.begin(); e != l.end(); e++)
if ((*e) == elt) return true;
return false;
}
template<typename T>
std::list<T> vector_to_list(std::vector<T> & V) {
std::list<T> L;
for (int i=0; i<(int)V.size(); i++)
L.push_back(V[i]);
return L;
}
vector<string> &split(const string &s, char delim, vector<string> &elems);
list<string> &split_list(const string &s, char delim, list<string> &elems);
template<typename T>
inline string join(const T &tab, string delimiter) {
ostringstream oss;
typename T::const_iterator it;
bool notOver;
it = tab.begin();
do {
oss << (*it);
it++;
notOver = (it != tab.end());
if (notOver) {
oss << delimiter;
}
} while (notOver);
return oss.str();
}
string system_time();
bool endsWith(std::string const &fullString, std::string const &ending);
vector<string> getLines(const string &header);
string today();
std::string camelize(std::string input);
std::string file_get_contents(std::string path);
void file_put_contents(std::string path, std::string contents);
void replaceAll(std::string& str, const std::string& from, const std::string& to);
bool strContains(const std::string &str, const std::string &other);
bool file_exists (const std::string& name);
/*@}*/
#endif