|
4 | 4 | #include <fstream>
|
5 | 5 | #include <cstring>
|
6 | 6 | #include <string>
|
| 7 | +#include <vector> |
7 | 8 |
|
8 | 9 | /**
|
9 | 10 | * This namespace defines wrappers for std::ifstream, std::ofstream, and
|
@@ -33,22 +34,28 @@ namespace strict_fstream
|
33 | 34 |
|
34 | 35 | // Non-gnu variants of strerror_* don't necessarily null-terminate if
|
35 | 36 | // truncating, so we have to do things manually.
|
36 |
| -inline std::string &trim_to_null(std::string &buff) |
| 37 | +inline std::string trim_to_null(const std::vector<char> &buff) |
37 | 38 | {
|
38 |
| - const std::string::size_type pos = buff.find('\0'); |
| 39 | + std::string ret(buff.begin(), buff.end()); |
| 40 | + |
| 41 | + const std::string::size_type pos = ret.find('\0'); |
39 | 42 | if (pos == std::string::npos) {
|
40 |
| - buff += " [...]"; // it has been truncated |
| 43 | + ret += " [...]"; // it has been truncated |
41 | 44 | } else {
|
42 |
| - buff.resize(pos); |
| 45 | + ret.resize(pos); |
43 | 46 | }
|
44 |
| - return buff; |
| 47 | + return ret; |
45 | 48 | }
|
46 | 49 |
|
47 |
| -/// Overload of error-reporting function, to enable use with VS. |
48 |
| -/// Ref: http://stackoverflow.com/a/901316/717706 |
| 50 | +/// Overload of error-reporting function, to enable use with VS and non-GNU |
| 51 | +/// POSIX libc's |
| 52 | +/// Ref: |
| 53 | +/// - http://stackoverflow.com/a/901316/717706 |
49 | 54 | static std::string strerror()
|
50 | 55 | {
|
51 |
| - std::string buff(256, '\0'); |
| 56 | + // Can't use std::string since we're pre-C++17 |
| 57 | + std::vector<char> buff(256, '\0'); |
| 58 | + |
52 | 59 | #ifdef _WIN32
|
53 | 60 | // Since strerror_s might set errno itself, we need to store it.
|
54 | 61 | const int err_num = errno;
|
|
0 commit comments