-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshared.hh
192 lines (142 loc) · 3.15 KB
/
shared.hh
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
/*
* shared: basic shared utilities and structures for tblutils
*
* Copyright(c) 2008-2010 Yuri D'Elia <[email protected]>
* Copyright(c) 2008-2010 EURAC, Institute of Genetic Medicine
*/
#pragma once
/*
* Headers
*/
// base headers
#include <iostream>
using std::cerr;
using std::cout;
#include <fstream>
using std::ifstream;
using std::ofstream;
using std::istream;
using std::ostream;
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <stdexcept>
using std::runtime_error;
// c system headers
#include <string.h>
using std::string;
/*
* Generics
*/
#define ARRAY_LENGTH(X) (sizeof(X) / sizeof(*X))
#define foreach(T, I, C) for(T::iterator (I) = (C).begin(); I != (C).end(); ++(I))
#define foreach_ro(T, I, C) for(T::const_iterator (I) = (C).begin(); I != (C).end(); ++(I))
string
sprintf2(const char* fmt, ...);
vector<string>&
tokenize(vector<string>& dst, const string& buf,
const string& sep = "\t", bool coalesce = false);
class Progress
{
long max;
const char* type;
bool clean;
bool tty;
public:
Progress(long max, const char* type);
~Progress() throw()
{
cleanup();
}
void
remax(long newMax)
{
max = newMax;
}
void
operator()(long v)
{
if(!tty) return;
cerr << v << ' ';
if(type) cerr << type;
cerr << ' ' << (max? (v * 100 / max): 100) << "%\r";
}
void
cleanup()
{
if(clean || !tty) return;
clean = true;
operator()(max);
cerr << '\n';
}
};
/*
* I/O helpers
*/
template<class T>
class named_stream: public T
{
const char* const file_;
public:
named_stream(const char* file)
: T(file), file_(file)
{}
const char*
file() const
{ return file_; }
};
typedef named_stream<istream> named_istream;
typedef named_stream<ostream> named_ostream;
typedef named_stream<ifstream> named_ifstream;
typedef named_stream<ofstream> named_ofstream;
class namedio_error: public runtime_error
{
public:
namedio_error(const char* file, const char* what)
: runtime_error(sprintf2("%s:%lu: %s", file, what))
{}
namedio_error(const char* file, int line, const char* what)
: runtime_error(sprintf2("%s:%lu: %s", file, line, what))
{}
template<class T>
namedio_error(const named_stream<T>& fd, const char* what)
: runtime_error(sprintf2("%s: %s", fd.file(), what))
{}
template<class T>
namedio_error(const named_stream<T>& fd, int line, const char* what)
: runtime_error(sprintf2("%s:%lu: %s", fd.file(), line, what))
{}
};
class fix_string
{
const char* addr;
size_t len;
public:
explicit
fix_string(const char* addr, const size_t len)
: addr(addr), len(len)
{}
operator string() const
{ return string(addr, len); }
const char*
data() const
{ return addr; }
size_t
size() const
{ return len; }
bool
operator !=(const fix_string& r) const
{
return ((len != r.len) || memcmp(addr, r.addr, len));
}
};
inline ostream&
operator <<(ostream& buf, const fix_string& r)
{
buf.write(r.data(), r.size());
return buf;
}
typedef vector<vector<fix_string> > fix_string_matrix;
fix_string_matrix*
mapFixStringMatrix(const char** addr, const char* file, const char sep, int* fd = NULL);