Repository navigation
Expand file tree
/
Copy pathFileUtil.cpp
More file actions
55 lines (46 loc) · 1.58 KB
/
Copy pathFileUtil.cpp
File metadata and controls
55 lines (46 loc) · 1.58 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
//-----------------------------------------------------------------------------
// This file is part of the the QParser2 project. You can find the complete
// project here: https://github.com/robdunn4/QSpice/
//-----------------------------------------------------------------------------
#include "FileUtil.h"
#include "StrList.h"
#include <stdexcept>
namespace FileUtil {
std::ifstream openInput(const std::string &path) {
std::ifstream stream(path, std::ios::binary); // binary?
if (!stream)
throw std::runtime_error("FileUtil::openInput: cannot open '" + path + "'");
return stream;
}
std::ofstream openOutput(const std::string &path) {
std::ofstream stream(path, std::ios::binary); // binary?
if (!stream)
throw std::runtime_error("FileUtil::openOutput: cannot open '" + path +
"'");
return stream;
}
StrList readLines(std::istream &stream) {
StrList lines;
std::string line;
while (std::getline(stream, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
lines.push_back(std::move(line));
}
return lines;
}
void writeLines(std::ostream &stream, const StrList &strList, bool useCRLF) {
for (const auto &line : strList) {
stream << line;
if (useCRLF) stream.put('\r');
stream.put('\n');
}
}
StrList readLines(const std::string &path) {
std::ifstream stream = openInput(path);
return readLines(stream);
}
void writeLines(const std::string &path, const StrList &strList, bool useCRLF) {
std::ofstream stream = openOutput(path);
writeLines(stream, strList, useCRLF);
}
} // namespace FileUtil