-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathlineFileUtilities.h
More file actions
99 lines (85 loc) · 2.6 KB
/
lineFileUtilities.h
File metadata and controls
99 lines (85 loc) · 2.6 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
#ifndef LINEFILEUTILITIES_H
#define LINEFILEUTILITIES_H
#include <vector>
#include <string>
#include <cstring>
#include <stdint.h>
#include <cstdlib>
#include <sstream>
#include <iostream>
using namespace std;
typedef int64_t CHRPOS;
// templated function to convert objects to strings
template <typename T>
inline
std::string ToString(const T & value) {
std::stringstream ss;
ss << value;
return ss.str();
}
// tokenize into a list of strings.
inline
void Tokenize(const string &str, vector<string> &elems, char delimiter = '\t')
{
// http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c/236803#236803
// NOTE: this approach intentionally allows consecutive delimiters
std::stringstream ss(str);
std::string item;
while(getline(ss, item, delimiter)) {
elems.push_back(item);
}
}
// tokenize into a list of integers
inline
void Tokenize(const string &str, vector<CHRPOS> &elems, char delimiter = '\t')
{
// http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c/236803#236803
// NOTE: this approach intentionally allows consecutive delimiters
std::stringstream ss(str);
std::string item;
while(getline(ss, item, delimiter)) {
elems.push_back(stoll(item.c_str()));
}
}
// tokenize into a list of integers
inline
void Tokenize(const string &str, vector<int> &elems, char delimiter = '\t')
{
// http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c/236803#236803
// NOTE: this approach intentionally allows consecutive delimiters
std::stringstream ss(str);
std::string item;
while(getline(ss, item, delimiter)) {
elems.push_back(atoi(item.c_str()));
}
}
// tokenize a column string into a list of integers.
inline
void TokenizeColumns(const string &str, vector<CHRPOS> &elems)
{
vector<string> col_sets;
Tokenize(str, col_sets, ',');
for( size_t i = 0; i < col_sets.size(); i++ ) {
string col_set = col_sets[i];
if( string::npos == col_set.find("-") ){
elems.push_back(stoll(col_set.c_str()));
}
else {
vector<string> ends;
Tokenize(col_set, ends, '-');
CHRPOS start = stoll(ends[0].c_str());
CHRPOS end = stoll(ends[1].c_str());
if(start <= end){
for(CHRPOS i = start; i <= end; i++){
elems.push_back(i);
}
}
else {
for(CHRPOS i = start; i >= end; i--){
elems.push_back(i);
}
}
}
}
}
#endif /* LINEFILEUTILITIES_H */