forked from tplinderoth/ngsParalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneralUtils.cpp
137 lines (122 loc) · 3.14 KB
/
generalUtils.cpp
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
// generalUtils.cpp
// version 0.0.8; 15 December 2014
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <sstream>
#include "generalUtils.h"
// getCString converts string to C-Style pointer string
char* getCString (std::string s)
{
size_t slen = s.length();
char* cstring = new char[slen + 1];
s.copy(cstring, slen, 0);
cstring[slen] = '\0';
return cstring;
}
// getFILE is a wrapper for getting files
bool getFILE(std::fstream &fp, const char* fname, const char* mode, int allow_overwrite)
{
int writeFile = 0;
if (strcmp(mode, "out") == 0)
{
writeFile = 1;
if(writeFile && fexists(fname) && !allow_overwrite)
{
fprintf(stderr,"File already exists (to allow overwriting using '-allow_overwrite 1'): %s\n",fname);
return false;
}
fp.open(fname, std::ios::out);
}
else if (strcmp(mode, "app") == 0)
fp.open(fname, std::ios::app);
else if (strcmp(mode, "in") == 0)
fp.open(fname, std::ios::in);
if( !fp )
{
fprintf(stderr,"Error opening FILE handle for file: %s\n",fname);
fp.close();
return false;
}
return true;
}
// fexists finds out if a file exists
int fexists(const char* str)
{
struct stat buffer;
return (stat(str, &buffer )==0 );
}
// readChunk chunks in a file line by line (not efficient since it doesn't actually read a chunk)
bool readChunk (std::vector<std::string>& datavec, unsigned int* chunk, int* end, std::istream& is)
{
unsigned int i = 0;
std::string(line);
size_t cap = 0;
if (!datavec.empty())
{
cap = datavec.capacity();
datavec.clear();
if (datavec.capacity() < cap)
datavec.reserve(cap);
}
while (i < *chunk)
{
if (getline(is, line))
{
datavec.push_back(line);
++i;
}
else if (is.eof())
{
datavec.resize(i); // remove empty elements
*end = 1;
*chunk = i; // number of lines processed upon returning
return(true);
}
else
{
fprintf(stderr, "Failed reading stream in readChunk function\n");
return(false);
}
}
return(true);
}
// split splits a string based on a delimiter
std::vector<std::string> split (const std::string& s, char delim)
{
std::vector<std::string> elems;
std::stringstream ss(s);
std::string sholder;
while (std::getline(ss, sholder, delim))
{
if (!sholder.empty())
elems.push_back(sholder);
}
return elems;
}
// draws random, bounded, decimal number
double decimalUnifBound (double min, double max )
{
return rand() / (static_cast<double>(RAND_MAX) + 1) * (max - min) + min;
}
AssertStyleException::AssertStyleException(const char* err)
: _error(err)
{}
const char* AssertStyleException::what() const throw()
{
std::stringstream message;
message << "Assert type exception occurred:\n";
if (_error) message << _error;
return message.str().c_str();
}
PreConditionException::PreConditionException(const char* err)
: AssertStyleException(err)
{}
const char* PreConditionException::what() const throw()
{
std::stringstream message;
message << "Precondition exception occurred:\n";
if (_error) message << _error;
return message.str().c_str();
}