-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneralUtils.cpp
More file actions
140 lines (124 loc) · 3.39 KB
/
Copy pathgeneralUtils.cpp
File metadata and controls
140 lines (124 loc) · 3.39 KB
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
//
// generalUtils.cpp
// Hi-reComb
//
// Created by Milan Malinsky on 02.09.22.
//
#include "generalUtils.hpp"
void split(const string &s, char delim, vector<string> &elems) {
std::stringstream ss(s);
string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
}
void splitToDouble(const string &s, char delim, vector<double> &elems) {
std::stringstream ss(s);
string item;
while (std::getline(ss, item, delim)) {
elems.push_back(stringToDouble(item));
}
}
vector<double> splitToDouble(const string &s, char delim) {
vector<double> elems;
splitToDouble(s, delim, elems);
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
int safeStringToInt(const string& s) {
int value;
std::stringstream ss(s); //turn the string into a stream
ss >> value; //convert
if(ss.fail()) {
std::cerr << "Error: could not convert '" << s << "' to an integer\n";
exit(EXIT_FAILURE);
}
return value;
}
double stringToDouble(const string& s) {
double d;
std::stringstream ss(s); //turn the string into a stream
ss >> d; //convert
return d;
}
// Ensure a filehandle is open
void assertFileOpen(std::ifstream& fh, const string& fn)
{
if(!fh.is_open())
{
std::cerr << "Error: could not open " << fn << " for read\n";
exit(EXIT_FAILURE);
}
}
// Ensure a filehandle is open
void assertFileOpen(std::ofstream& fh, const string& fn)
{
if(!fh.is_open())
{
std::cerr << "Error: could not open " << fn << " for write\n";
exit(EXIT_FAILURE);
}
}
//
void assertGZOpen(gzstreambase& gh, const string& fn)
{
if(!gh.good())
{
std::cerr << "Error: could not open " << fn << std::endl;
exit(EXIT_FAILURE);
}
}
string suffix(const string& seq, size_t len)
{
assert(seq.length() >= len);
return seq.substr(seq.length() - len);
}
// Returns true if the filename has an extension indicating it is compressed
bool isGzip(const string& filename)
{
size_t suffix_length = sizeof(GZIP_EXT) - 1;
// Assume files without an extension are not compressed
if(filename.length() < suffix_length)
return false;
string extension = suffix(filename, suffix_length);
return extension == GZIP_EXT;
}
// Open a file that may or may not be gzipped for reading
// The caller is responsible for freeing the handle
std::istream* createReader(const string& filename, std::ios_base::openmode mode)
{
if(isGzip(filename))
{
igzstream* pGZ = new igzstream(filename.c_str(), mode);
assertGZOpen(*pGZ, filename);
return pGZ;
}
else
{
std::ifstream* pReader = new std::ifstream(filename.c_str(), mode);
assertFileOpen(*pReader, filename);
return pReader;
}
}
// Open a file that may or may not be gzipped for writing
// The caller is responsible for freeing the handle
std::ostream* createWriter(const string& filename,
std::ios_base::openmode mode)
{
if(isGzip(filename))
{
ogzstream* pGZ = new ogzstream(filename.c_str(), mode);
assertGZOpen(*pGZ, filename);
return pGZ;
}
else
{
std::ofstream* pWriter = new std::ofstream(filename.c_str(), mode);
assertFileOpen(*pWriter, filename);
return pWriter;
}
}