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
|
#ifndef LINEFILEUTILITIES_H
#define LINEFILEUTILITIES_H
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;
// 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, 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());
}
}
// tokenize into a list of integers
inline
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());
}
}
#endif /* LINEFILEUTILITIES_H */
|