aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2022-06-10 14:52:24 +0200
committerStefan Boberg <[email protected]>2022-06-10 14:52:24 +0200
commitc798a8878066e1078add1fbd03ee433e1133878b (patch)
treecbfc572f0c540f6f4fbcc3eba65e196c4f203b5e
parenthttp: added some more content-type aliases/suffixes (diff)
downloadzen-c798a8878066e1078add1fbd03ee433e1133878b.tar.xz
zen-c798a8878066e1078add1fbd03ee433e1133878b.zip
core: added ReadStdIn
implements a simple wrapper around reading stdin until EOF
-rw-r--r--zencore/filesystem.cpp20
-rw-r--r--zencore/include/zencore/filesystem.h1
2 files changed, 21 insertions, 0 deletions
diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp
index bd85f5a11..01997daae 100644
--- a/zencore/filesystem.cpp
+++ b/zencore/filesystem.cpp
@@ -6,6 +6,7 @@
#include <zencore/fmtutils.h>
#include <zencore/iobuffer.h>
#include <zencore/logging.h>
+#include <zencore/stream.h>
#include <zencore/string.h>
#include <zencore/testing.h>
@@ -637,6 +638,25 @@ FileContents::Flatten()
}
FileContents
+ReadStdIn()
+{
+ BinaryWriter Writer;
+
+ do
+ {
+ uint8_t ReadBuffer[1024];
+
+ size_t BytesRead = fread(ReadBuffer, 1, sizeof ReadBuffer, stdin);
+ Writer.Write(ReadBuffer, BytesRead);
+ } while (!feof(stdin));
+
+ FileContents Contents;
+ Contents.Data.emplace_back(IoBuffer(IoBuffer::Clone, Writer.GetData(), Writer.GetSize()));
+
+ return Contents;
+}
+
+FileContents
ReadFile(std::filesystem::path Path)
{
uint64_t FileSizeBytes;
diff --git a/zencore/include/zencore/filesystem.h b/zencore/include/zencore/filesystem.h
index 6d07a79b4..f49135687 100644
--- a/zencore/include/zencore/filesystem.h
+++ b/zencore/include/zencore/filesystem.h
@@ -46,6 +46,7 @@ struct FileContents
IoBuffer Flatten();
};
+ZENCORE_API FileContents ReadStdIn();
ZENCORE_API FileContents ReadFile(std::filesystem::path Path);
ZENCORE_API bool ScanFile(std::filesystem::path Path, uint64_t ChunkSize, std::function<void(const void* Data, size_t Size)>&& ProcessFunc);
ZENCORE_API void WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t BufferCount);