1 #ifndef LINEFILEUTILITIES_H
2 #define LINEFILEUTILITIES_H
3 
4 #include <vector>
5 #include <string>
6 #include <cstring>
7 #include <cstdlib>
8 #include <sstream>
9 #include <iostream>
10 
11 using namespace std;
12 
13 typedef int64_t CHRPOS;
14 
15 // templated function to convert objects to strings
16 template <typename T>
17 inline
ToString(const T & value)18 std::string ToString(const T & value) {
19     std::stringstream ss;
20     ss << value;
21     return ss.str();
22 }
23 
24 // tokenize into a list of strings.
25 inline
26 void Tokenize(const string &str, vector<string> &elems, char delimiter = '\t')
27 {
28     // http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c/236803#236803
29     // NOTE: this approach intentionally allows consecutive delimiters
30     std::stringstream ss(str);
31     std::string item;
32     while(getline(ss, item, delimiter)) {
33         elems.push_back(item);
34     }
35 }
36 
37 // tokenize into a list of integers
38 inline
39 void Tokenize(const string &str, vector<CHRPOS> &elems, char delimiter = '\t')
40 {
41 
42     // http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c/236803#236803
43     // NOTE: this approach intentionally allows consecutive delimiters
44     std::stringstream ss(str);
45     std::string item;
46     while(getline(ss, item, delimiter)) {
47         elems.push_back(stoll(item.c_str()));
48     }
49 }
50 
51 // tokenize into a list of integers
52 inline
53 void Tokenize(const string &str, vector<int> &elems, char delimiter = '\t')
54 {
55 
56     // http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c/236803#236803
57     // NOTE: this approach intentionally allows consecutive delimiters
58     std::stringstream ss(str);
59     std::string item;
60     while(getline(ss, item, delimiter)) {
61         elems.push_back(atoi(item.c_str()));
62     }
63 }
64 
65 // tokenize a column string into a list of integers.
66 inline
TokenizeColumns(const string & str,vector<CHRPOS> & elems)67 void TokenizeColumns(const string &str, vector<CHRPOS> &elems)
68 {
69     vector<string> col_sets;
70     Tokenize(str, col_sets, ',');
71 
72     for( size_t i = 0; i < col_sets.size(); i++ ) {
73         string col_set = col_sets[i];
74         if( string::npos == col_set.find("-") ){
75             elems.push_back(stoll(col_set.c_str()));
76         }
77         else {
78             vector<string> ends;
79             Tokenize(col_set, ends, '-');
80             CHRPOS start = stoll(ends[0].c_str());
81             CHRPOS end = stoll(ends[1].c_str());
82             if(start <= end){
83                 for(CHRPOS i = start; i <= end; i++){
84                     elems.push_back(i);
85                 }
86             }
87             else {
88                 for(CHRPOS i = start; i >= end; i--){
89                     elems.push_back(i);
90                 }
91             }
92         }
93     }
94 }
95 
96 
97 #endif /* LINEFILEUTILITIES_H */
98 
99