-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.cpp
91 lines (72 loc) · 1.87 KB
/
util.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <cerrno>
#include <cstring>
#include <sstream>
#include <iostream>
#include <filesystem>
#include "util.hpp"
void
__throw_err(const std::string &err_expr,
const std::string &err_append,
const std::string &fnc_name,
int sc_line)
{
std::ostringstream errbuf;
errbuf << '[' << fnc_name << ':' << sc_line << ']';
errbuf << " - ";
errbuf << '[' << err_expr << "]:[" << err_append << ']';
if (errno) {
errbuf << " - " << std::strerror(errno);
}
throw std::runtime_error(errbuf.str());
}
void
usage(std::initializer_list<std::string> example, std::initializer_list<std::string> help)
{
std::ostream &os = std::cerr;
os << "Usage:";
for (auto &e_arg : example) {
os << ' ' << e_arg;
}
os << std::endl;
os << "Help:" << std::endl;
for (auto &e_opt : help) {
os << ' ' << e_opt << std::endl;
}
std::exit(EXIT_FAILURE);
}
std::string
FilePathOnFS(const std::string &dir, const std::string &base)
{
std::filesystem::path p;
p = std::filesystem::path(base);
p = std::filesystem::path(dir) /= p.relative_path();
return p.make_preferred().string();
};
std::fstream
FileOpen(const std::string &fname, std::ios::openmode m)
{
std::fstream fd(fname, m);
if (!fd.is_open()) {
throw_err("!fd.is_open()", fname);
}
if (!std::filesystem::is_regular_file(fname)) {
throw_err("!is_regular_file(fname)", fname);
}
return fd;
}
std::string
FileRead(std::fstream fd)
{
const size_t sz = fd.seekg(0, std::ios::end).tellg();
fd.seekg(std::ios::beg);
std::string buf(sz, '\0');
if (!fd.read(buf.data(), buf.size())) {
throw_err("Read file", std::to_string(buf.size()));
}
return buf;
}
std::string
FileRead(const std::string &fname, std::ios::openmode m)
{
return FileRead(FileOpen(fname, m));
}