summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-04 16:14:49 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-04 16:14:49 +0100
commitbf9ada75b941b40f30dfb25b063e064be062b7d8 (patch)
tree80ca01f69e53dc3c078f2cb6169cfb1a2001616f
parent5c330068e75946286e8e0d3dc0bd381e419074a9 (diff)
Support reading from /proc
-rw-r--r--file.cpp8
-rw-r--r--tests/test-file.cpp3
2 files changed, 10 insertions, 1 deletions
diff --git a/file.cpp b/file.cpp
index 0c28511..4e10035 100644
--- a/file.cpp
+++ b/file.cpp
@@ -8,10 +8,16 @@ using namespace std::string_literals;
std::string Reichwein::File::getFile(const fs::path& filename)
{
- std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate);
+ std::ifstream file(filename.string(), std::ios::in | std::ios::binary);
if (file.is_open()) {
+ file.seekg(0, std::ios::end);
std::ifstream::pos_type fileSize = file.tellg();
+ if (fileSize < 0) { // on pseudo-files (e.g. /proc/*), this doesn't work. Use a slower workaround:
+ std::stringstream buffer;
+ buffer << file.rdbuf();
+ return buffer.str();
+ }
file.seekg(0, std::ios::beg);
std::string bytes(fileSize, ' ');
diff --git a/tests/test-file.cpp b/tests/test-file.cpp
new file mode 100644
index 0000000..13d0ded
--- /dev/null
+++ b/tests/test-file.cpp
@@ -0,0 +1,3 @@
+getFile
+
+getFile /proc