lineFileUtilities.h 1.14 KB
Newer Older
1
2
3
4
5
#ifndef LINEFILEUTILITIES_H
#define LINEFILEUTILITIES_H

#include <vector>
#include <string>
Aaron's avatar
Aaron committed
6
#include <cstring>
Aaron's avatar
Aaron committed
7
#include <sstream>
Aaron's avatar
Aaron committed
8

9
10
using namespace std;

Aaron's avatar
Aaron committed
11
12
13
14
// templated function to convert objects to strings
template <typename T>
inline
std::string ToString(const T & value) {
15
16
17
    std::stringstream ss;
    ss << value;
    return ss.str();
Aaron's avatar
Aaron committed
18
19
}

Aaron's avatar
Aaron committed
20
// tokenize into a list of strings.
Aaron's avatar
Aaron committed
21
inline
Aaron's avatar
Aaron committed
22
23
24
25
26
27
28
29
30
31
void Tokenize(const string &str, vector<string> &elems, const string &delimiter = "\t") 
{
    char* tok;
    char cchars [str.size()+1];
    char* cstr = &cchars[0];
    strcpy(cstr, str.c_str());
    tok = strtok(cstr, delimiter.c_str());
    while (tok != NULL) {
        elems.push_back(tok);
        tok = strtok(NULL, delimiter.c_str());
32
    }
Aaron's avatar
Aaron committed
33
34
}

Aaron's avatar
Aaron committed
35
// tokenize into a list of integers
Aaron's avatar
Aaron committed
36
inline
Aaron's avatar
Aaron committed
37
38
39
40
41
42
43
44
45
46
void Tokenize(const string &str, vector<int> &elems, const string &delimiter = "\t") 
{
    char* tok;
    char cchars [str.size()+1];
    char* cstr = &cchars[0];
    strcpy(cstr, str.c_str());
    tok = strtok(cstr, delimiter.c_str());
    while (tok != NULL) {
        elems.push_back(atoi(tok));
        tok = strtok(NULL, delimiter.c_str());
Aaron's avatar
Aaron committed
47
48
    }
}
49
50

#endif /* LINEFILEUTILITIES_H */
Aaron's avatar
Aaron committed
51