utils.hpp
1.79 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
/*
* File: utils.hpp
* Author: mlenart
*
* Created on 24 październik 2013, 17:56
*/
#ifndef UTILS_HPP
#define UTILS_HPP
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#ifdef DEBUG_BUILD
#define DEBUG(x) do { std::cerr << x << std::endl; } while (0)
#else
#define DEBUG(x)
#endif
namespace morfeusz {
inline void validate(const bool cond, const std::string& msg) {
if (!cond) {
std::cerr << msg << std::endl;
throw msg;
}
}
inline std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
inline std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
//string &rtrim(string &s) {
// s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end());
// return s;
//}
template <class T>
inline T* readFile(const char* fname) {
std::ifstream ifs;
ifs.open(fname, std::ios::in | std::ios::binary | std::ios::ate);
if (ifs.is_open()) {
long size = ifs.tellg();
T* memblock = new T[size];
ifs.seekg(0, std::ios::beg);
ifs.read(reinterpret_cast<char*> (memblock), size);
ifs.close();
return memblock;
}
else {
std::stringstream ss;
ss << "Failed to open file: " << fname;
throw std::ios_base::failure(ss.str());
}
}
}
#endif /* UTILS_HPP */