-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.h
executable file
·232 lines (190 loc) · 5.39 KB
/
misc.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef _MISC_H_
#define _MISC_H_
//---------------------------------------------------------------------------//
// misc.h: helper functions //
// date: 20190124 //
//---------------------------------------------------------------------------//
/* shorthand for for loops from 0 to N */
#define for0(i,n) for(i = 0; i < n; i++)
#define STR_MAX 16384
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
#include<iostream>
#include<limits.h>
#include<memory.h>
#include<pthread.h>
#include<algorithm>
//#include"ansicolor.h"
#include <algorithm>
#include <cctype>
#include <locale>
using namespace std;
#include <stdio.h> /* defines FILENAME_MAX */
#ifdef WINDOWS
#include <direct.h>
#define _cwd _getcwd
#else
#include <unistd.h>
#define _cwd getcwd
#endif
void rewind(ifstream &a);
#define str string
string cwd();
/* split a string (a-la python) */
vector<string> split(string s, char delim);
vector<string> split(string s); // comma
vector<string> split_special(string s); // comma with possible commas inside quotation marks!
string join(const char * delim, vector<string> s);
template<class T> std::ostream& operator << (std::ostream& os, const std::vector<T>& v){
os << "[";
for (typename std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii){
os << " '" << *ii << "'";
}
os << "]";
return os;
}
template<class T> std::ostream& operator << (std::ostream& os, const std::set<T>& v){
os << "{";
for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii){
os << " " << *ii;
}
os << "}";
return os;
}
template<class A, class B> std::ostream& operator << (std::ostream& os, const std::map<A, B>& v){
os << "{" << endl;
for (typename std::map<A, B>::const_iterator ii = v.begin(); ii != v.end(); ++ii){
os << ii->first << ":" << ii->second << ","; //endl;
}
os << "}" << endl;
return os;
}
void err(string msg);
void err(const char * msg);
/* allocate memory */
inline void * alloc(size_t nb){
void * d = malloc(nb);
if(!d){
printf("%zu\n", nb);
err("failed to allocate memory");
}
memset(d, '\0', nb);
return d;
}
int non_space(int ch);
//a trim from start (in place)
static inline void ltrim(std::string &s){
s.erase(s.begin(), std::find_if(s.begin(), s.end(), non_space));
}
// trim from end (in place)
static inline void rtrim(std::string &s){
s.erase(std::find_if(s.rbegin(), s.rend(), non_space).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s){
ltrim(s);
rtrim(s);
}
// trim from start (copying): not implemented properly
static inline std::string ltrim_copy(std::string s){
ltrim(s);
return s;
}
// trim from end (copying): not implemented properly
static inline std::string rtrim_copy(std::string s){
rtrim(s);
return s;
}
// trim from both ends (copying): not implemented properly
static inline std::string trim_copy(std::string s){
trim(s);
return s;
}
static inline void trim(std::string &s, char delim){
str ret("");
int end = s.size() - 1;
int start = 0;
while(s[start] == delim) start += 1;
while(s[end] == delim) end -= 1;
s = s.substr(start, 1 + end - start);
}
#define strip trim
/* convert to lower case */
static void inline lower(std::string & s){
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
}
static inline std::string lower_copy(std::string &s){
string r(s);
std::transform(r.begin(), r.end(), r.begin(), ::tolower);
return r;
}
/* get size of file pointer */
size_t size(FILE * f);
size_t fsize(string fn);
// in-memory reader (writer to be implemented)
// note this should be able to modulate between available protocols (like ifstream, ofstream, etc. , fwrite, fread, if available)
FILE * wopen(string fn);
class f_idx{
public: // float, index tuple object
float d;
unsigned int idx;
f_idx(float d_ = 0., unsigned int idx_ = 0){
d = d_;
idx = idx_;
}
f_idx(const f_idx &a){
d = a.d;
idx = a.idx;
}
};
bool operator<(const f_idx& a, const f_idx&b);
// read header file
void hread(str hfn, size_t & nrow, size_t & ncol, size_t & nband);
void hwrite(str hfn, size_t nrow, size_t ncol, size_t nband);
float * falloc(size_t nf);
// read binary file
float * bread(str bfn, size_t nrow, size_t ncol, size_t nband);
extern pthread_mutex_t print_mutex;
void cprint(str s);
/*
pthread_mutex_lock(&print_mutex);
cout << s << endl;
pthread_mutex_unlock(&print_mutex);
*/
int hsv_to_rgb(float *r, float *g, float *b, float h, float s, float v);
str hdr_fn(str fn); //create = false
str hdr_fn(str fn, bool create);
/* get size of file pointer */
size_t size(FILE * f);
size_t fsize(string fn);
bool exists(str fn);
#define KNRM "\x1B[0m" //normal
#define KBLK "\x1B[30m" //black
#define KRED "\x1B[31m" //red
#define KGRN "\x1B[32m" //green
#define KYEL "\x1B[33m" //yellow
#define KBLU "\x1B[34m" //blue
#define KMAG "\x1B[35m" //magenta
#define KCYN "\x1B[36m" //cyan
#define KWHT "\x1B[37m" //white
#define KBLD "\x1B[1m" //bold
#define KRES "\x1B[0m" //reset
#define KITA "\x1B[3m" //italics
#define KUND "\x1B[4m" //underline
#define KSTR "\x1B[9m" //strikethrough
#define KINV "\x1B[7m" //inverseON
#define BBLK "\x1B[30m" //black
#define BRED "\x1B[31m" //red
#define BGRN "\x1B[32m" //green
#define BYEL "\x1B[33m" //yellow
#define BBLU "\x1B[34m" //blue
#define BMAG "\x1B[35m" //magenta
#define BCYN "\x1B[36m" //cyan
#define BWHT "\x1B[37m" //white
#endif