-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile_utils.hpp
42 lines (34 loc) · 1.03 KB
/
file_utils.hpp
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
#ifndef FILE_UTILS_INCLUDED__
#define FILE_UTILS_INCLUDED__
#include <fstream>
#include <string>
#include <vector>
inline std::string file_slurp (const std::string & filename)
{
std::string retval;
std::ifstream ifs(filename);
std::getline(ifs, retval, '\0');
return retval;
}
inline std::vector<std::string> line_break (const std::string & file)
{
std::vector<std::string> retval;
std::string::size_type prev_pos = 0;
while (true) {
std::string::size_type pos_1 = file.find("\r\n", prev_pos);
std::string::size_type pos_2 = file.find_first_of("\r\n", prev_pos);
std::string::size_type pos = pos_2;
std::string::size_type match_size = 1;
if (pos_1 != std::string::npos) {
pos = pos_1;
match_size = 2;
} else if (pos_2 == std::string::npos) {
break;
}
retval.push_back(file.substr(prev_pos, pos - prev_pos));
prev_pos = pos + match_size;
}
retval.push_back(file.substr(prev_pos));
return retval;
}
#endif