-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathurl.h
272 lines (246 loc) · 9.84 KB
/
url.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Pheniqs : PHilology ENcoder wIth Quality Statistics
Copyright (C) 2018 Lior Galanti
NYU Center for Genetics and System Biology
Author: Lior Galanti <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PHENIQS_URL_H
#define PHENIQS_URL_H
#include "include.h"
#include "json.h"
#define STANDARD_STREAM_ALIAS "-"
#define CANONICAL_STDIN_PATH "/dev/stdin"
#define CANONICAL_STDOUT_PATH "/dev/stdout"
#define CANONICAL_STDERR_PATH "/dev/stderr"
#define CANONICAL_NULL_DEVICE_PATH "/dev/null"
enum class IoDirection : uint8_t {
IN,
OUT,
UNKNOWN,
};
string to_string(const IoDirection& value);
bool from_string(const char* value, IoDirection& result);
bool from_string(const string& value, IoDirection& result);
ostream& operator<<(ostream& o, const IoDirection& direction);
void encode_key_value(const string& key, const IoDirection& value, Value& container, Document& document);
enum class FormatType : uint8_t {
UNKNOWN,
NONE,
FASTQ,
SAM,
BAM,
BAI,
CRAM,
CRAI,
VCF,
BCF,
CSI,
GZI,
TBI,
BED,
JSON,
};
string to_string(const FormatType& value);
bool from_string(const char* value, FormatType& result);
bool from_string(const string& value, FormatType& result);
ostream& operator<<(ostream& o, const FormatType& value);
void encode_key_value(const string& key, const FormatType& value, Value& container, Document& document);
template<> bool decode_value_by_key< FormatType >(const Value::Ch* key, FormatType& value, const Value& container);
template<> FormatType decode_value_by_key< FormatType >(const Value::Ch* key, const Value& container);
enum class FormatCompression : uint8_t {
UNKNOWN,
NONE,
GZIP,
BGZF,
BZ2,
XZ,
};
string to_string(const FormatCompression& value);
bool from_string(const char* value, FormatCompression& result);
bool from_string(const string& value, FormatCompression& result);
ostream& operator<<(ostream& o, const FormatCompression& value);
void encode_key_value(const string& key, const FormatCompression& value, Value& container, Document& document);
template<> bool decode_value_by_key< FormatCompression >(const Value::Ch* key, FormatCompression& value, const Value& container);
template<> FormatCompression decode_value_by_key< FormatCompression >(const Value::Ch* key, const Value& container);
enum class CompressionLevel : uint8_t {
LEVEL_0,
LEVEL_1,
LEVEL_2,
LEVEL_3,
LEVEL_4,
LEVEL_5,
LEVEL_6,
LEVEL_7,
LEVEL_8,
LEVEL_9,
UNKNOWN,
};
string to_string(const CompressionLevel& value);
bool from_string(const char* value, CompressionLevel& result);
bool from_string(const string& value, CompressionLevel& result);
ostream& operator<<(ostream& o, const CompressionLevel& value);
void encode_key_value(const string& key, const CompressionLevel& value, Value& container, Document& document);
template<> bool decode_value_by_key< CompressionLevel >(const Value::Ch* key, CompressionLevel& value, const Value& container);
template<> CompressionLevel decode_value_by_key< CompressionLevel >(const Value::Ch* key, const Value& container);
enum class URLQueryParameter : uint8_t {
UNKNOWN,
FORMAT_TYPE,
FORMAT_COMPRESSION,
COMPRESSION_LEVEL,
};
string to_string(const URLQueryParameter& value);
bool from_string(const char* value, URLQueryParameter& result);
bool from_string(const string& value, URLQueryParameter& result);
ostream& operator<<(ostream& o, const URLQueryParameter& value);
string& expand_shell(string& expression);
void normalize_standard_stream(string& path, const IoDirection& direction);
class URL {
friend ostream& operator<<(ostream& o, const URL& url);
friend bool operator<(const URL& lhs, const URL& rhs);
public:
URL();
URL(const URL& other);
URL(const string& encoded);
void parse(const string& encoded);
void set_type(const FormatType type);
void set_compression(const FormatCompression& compression);
void set_compression_level(const CompressionLevel& level);
void override_query(const URL& other);
void relocate_child(const URL& base);
void relocate_sibling(const URL& base);
inline const string& encoded() const {
return _encoded;
};
inline const string& path() const {
return _path;
};
inline const string& basename() const {
return _basename;
};
inline const string& dirname() const {
return _dirname;
};
inline const string& query() const {
return _query;
};
inline const FormatType& type() const {
return _format_type;
};
inline const FormatCompression& implicit_compression() const {
return _implicit_compression;
};
inline const FormatCompression& explicit_compression() const {
return _explicit_compression;
};
inline const FormatCompression& compression() const {
return _explicit_compression != FormatCompression::UNKNOWN ? _explicit_compression: _implicit_compression;
};
inline const CompressionLevel& compression_level() const {
return _compression_level;
};
inline bool empty() const {
return _encoded.empty();
};
inline bool is_file() const {
return !_basename.empty();
};
inline bool is_directory() const {
return _basename.empty() && !_dirname.empty();
};
inline bool is_stdin() const {
return _path == CANONICAL_STDIN_PATH;
};
inline bool is_stdout() const {
return _path == CANONICAL_STDOUT_PATH;
};
inline bool is_stderr() const {
return _path == CANONICAL_STDERR_PATH;
};
inline bool is_dev_null() const {
return _path == CANONICAL_NULL_DEVICE_PATH;
};
inline bool is_standard_stream() const {
return is_stdin() || is_stdout() || is_stderr() || is_dev_null();
};
inline bool is_absolute() const {
return !_dirname.empty() && _dirname[0] == '/';
};
inline const char* const hfile_name() const {
if(is_file()) {
if(is_stdout() || is_stdin()) {
return STANDARD_STREAM_ALIAS;
} else if(is_stderr()) {
return NULL;
} else if(is_dev_null()) {
return NULL;
} else {
return _path.c_str();
}
} else {
return NULL;
}
};
inline void clear() {
_encoded.clear();
_path.clear();
_basename.clear();
_dirname.clear();
_query.clear();
_format_type = FormatType::UNKNOWN;
_explicit_compression = FormatCompression::UNKNOWN;
_implicit_compression = FormatCompression::UNKNOWN;
_compression_level = CompressionLevel::UNKNOWN;
};
bool is_readable() const;
bool is_writable() const;
bool operator==(const URL& other) const;
URL& operator=(const URL& other);
operator string() const;
string description() const;
private:
string _encoded;
string _path;
string _basename;
string _dirname;
string _query;
FormatType _format_type;
FormatCompression _implicit_compression;
FormatCompression _explicit_compression;
CompressionLevel _compression_level;
void refresh();
void parse_query();
void apply_query_parameter(const string& key, const string& value);
void append_query_parameter(const URLQueryParameter& name, const string& value);
};
bool operator<(const URL& lhs, const URL& rhs);
namespace std {
template <> struct hash< URL > {
size_t operator()(const URL& url) const {
return hash< string >()(url.path());
};
};
};
template<> URL decode_value(const Value& container);
template<> URL decode_value_by_key(const Value::Ch* key, const Value& container);
template<> bool decode_value< URL >(URL& value, const Value& container);
template<> list< URL > decode_value_by_key(const Value::Ch* key, const Value& container);
template<> bool decode_value_by_key< URL >(const Value::Ch* key, URL& value, const Value& container);
template<> bool decode_value_by_key< list< URL > >(const Value::Ch* key, list< URL >& value, const Value& container);
void encode_value(const URL& value, Value& container, Document& document);
bool encode_key_value(const string& key, const URL& value, Value& container, Document& document);
bool encode_key_value(const string& key, const list< URL >& value, Value& container, Document& document);
void standardize_url_value_by_key(const Value::Ch* key, Value& container, Document& document, const IoDirection& direction);
void standardize_url_array_by_key(const Value::Ch* key, Value& container, Document& document, const IoDirection& direction);
void relocate_url_by_key(const Value::Ch* key, Value& container, Document& document, const URL& base);
void relocate_url_array_by_key(const Value::Ch* key, Value& container, Document& document, const URL& base);
#endif /* PHENIQS_URL_H */