lineFileUtilities.h 1.16 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>
daler's avatar
daler committed
8
#include <cstdlib>
Aaron's avatar
Aaron committed
9

10
11
using namespace std;

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

Aaron's avatar
Aaron committed
21
// tokenize into a list of strings.
Aaron's avatar
Aaron committed
22
inline
Aaron's avatar
Aaron committed
23
24
25
26
27
28
29
30
31
32
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());
33
    }
Aaron's avatar
Aaron committed
34
35
}

Aaron's avatar
Aaron committed
36
// tokenize into a list of integers
Aaron's avatar
Aaron committed
37
inline
Aaron's avatar
Aaron committed
38
39
40
41
42
43
44
45
46
47
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
48
49
    }
}
50
51

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