summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/File.cpp
diff options
context:
space:
mode:
authorTiger Wang <ziwei.tiger@outlook.com>2021-01-11 17:39:43 +0100
committerGitHub <noreply@github.com>2021-01-11 17:39:43 +0100
commiteeb63b8901a9c049f1bb594abb9ce9b4a9c47620 (patch)
treeb07daae788f918b83eeb0bdbd51e49292f1c8d88 /src/OSSupport/File.cpp
parentFixed switch-ups regarding some slab and stair recipes (#5099) (diff)
downloadcuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar
cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.gz
cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.bz2
cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.lz
cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.xz
cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.zst
cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.zip
Diffstat (limited to 'src/OSSupport/File.cpp')
-rw-r--r--src/OSSupport/File.cpp67
1 files changed, 58 insertions, 9 deletions
diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp
index 8c5eb92a4..618463bd6 100644
--- a/src/OSSupport/File.cpp
+++ b/src/OSSupport/File.cpp
@@ -157,19 +157,18 @@ int cFile::Read (void * a_Buffer, size_t a_NumBytes)
-AString cFile::Read(size_t a_NumBytes)
+ContiguousByteBuffer cFile::Read(size_t a_NumBytes)
{
ASSERT(IsOpen());
if (!IsOpen())
{
- return AString();
+ return {};
}
- // HACK: This depends on the knowledge that AString::data() returns the internal buffer, rather than a copy of it.
- AString res;
- res.resize(a_NumBytes);
- auto newSize = fread(const_cast<char *>(res.data()), 1, a_NumBytes, m_File);
+ ContiguousByteBuffer res;
+ res.resize(a_NumBytes); // TODO: investigate if worth hacking around std::string internals to avoid initialisation
+ auto newSize = fread(res.data(), sizeof(std::byte), a_NumBytes, m_File);
res.resize(newSize);
return res;
}
@@ -284,9 +283,8 @@ int cFile::ReadRestOfFile(AString & a_Contents)
auto DataSize = static_cast<size_t>(TotalSize - Position);
- // HACK: This depends on the internal knowledge that AString's data() function returns the internal buffer directly
- a_Contents.assign(DataSize, '\0');
- return Read(static_cast<void *>(const_cast<char *>(a_Contents.data())), DataSize);
+ a_Contents.resize(DataSize); // TODO: investigate if worth hacking around std::string internals to avoid initialisation
+ return Read(a_Contents.data(), DataSize);
}
@@ -709,3 +707,54 @@ void cFile::Flush(void)
{
fflush(m_File);
}
+
+
+
+
+
+template <class StreamType>
+FileStream<StreamType>::FileStream(const std::string & Path)
+{
+ // Except on failbit, which is what open sets on failure:
+ FileStream::exceptions(FileStream::failbit | FileStream::badbit);
+
+ // Open the file:
+ FileStream::open(Path);
+
+ // Only subsequently except on serious errors, and not on conditions like EOF or malformed input:
+ FileStream::exceptions(FileStream::badbit);
+}
+
+
+
+
+
+template <class StreamType>
+FileStream<StreamType>::FileStream(const std::string & Path, const typename FileStream::openmode Mode)
+{
+ // Except on failbit, which is what open sets on failure:
+ FileStream::exceptions(FileStream::failbit | FileStream::badbit);
+
+ // Open the file:
+ FileStream::open(Path, Mode);
+
+ // Only subsequently except on serious errors, and not on conditions like EOF or malformed input:
+ FileStream::exceptions(FileStream::badbit);
+}
+
+
+
+
+
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wweak-template-vtables" // http://bugs.llvm.org/show_bug.cgi?id=18733
+#endif
+
+// Instantiate the templated wrapper for input and output:
+template class FileStream<std::ifstream>;
+template class FileStream<std::ofstream>;
+
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif