summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTiger Wang <ziwei.tiger@outlook.com>2020-08-19 00:36:05 +0200
committerTiger Wang <ziwei.tiger@outlook.com>2020-08-19 21:45:27 +0200
commitf23402dacdf32549d3b13384af7a8a7c097ff420 (patch)
tree57a9519da77f0e1c399a04e29b123b2675358404 /src
parentAdd statistics palette for 1.15 & 1.16 (diff)
downloadcuberite-f23402dacdf32549d3b13384af7a8a7c097ff420.tar
cuberite-f23402dacdf32549d3b13384af7a8a7c097ff420.tar.gz
cuberite-f23402dacdf32549d3b13384af7a8a7c097ff420.tar.bz2
cuberite-f23402dacdf32549d3b13384af7a8a7c097ff420.tar.lz
cuberite-f23402dacdf32549d3b13384af7a8a7c097ff420.tar.xz
cuberite-f23402dacdf32549d3b13384af7a8a7c097ff420.tar.zst
cuberite-f23402dacdf32549d3b13384af7a8a7c097ff420.zip
Diffstat (limited to '')
-rw-r--r--src/Globals.h1
-rw-r--r--src/OSSupport/File.cpp27
-rw-r--r--src/OSSupport/File.h16
3 files changed, 43 insertions, 1 deletions
diff --git a/src/Globals.h b/src/Globals.h
index c29f3200a..c2f117e23 100644
--- a/src/Globals.h
+++ b/src/Globals.h
@@ -173,6 +173,7 @@ template class SizeChecker<UInt8, 1>;
#include <chrono>
#include <condition_variable>
#include <deque>
+#include <fstream>
#include <limits>
#include <list>
#include <map>
diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp
index f860c37ca..113acc3a5 100644
--- a/src/OSSupport/File.cpp
+++ b/src/OSSupport/File.cpp
@@ -6,7 +6,6 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "File.h"
-#include <fstream>
#include <sys/stat.h>
#ifdef _WIN32
#include <share.h> // for _SH_DENYWRITE
@@ -710,3 +709,29 @@ void cFile::Flush(void)
{
fflush(m_File);
}
+
+
+
+
+
+template <class StreamType>
+FileStream<StreamType>::FileStream(const std::string & Path) :
+ StreamType()
+{
+ // 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);
+}
+
+
+
+
+
+// Instantiate the templated wrapper for input and output:
+template class FileStream<std::ifstream>;
+template class FileStream<std::ofstream>;
diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h
index 9987c41c7..b2099e9fd 100644
--- a/src/OSSupport/File.h
+++ b/src/OSSupport/File.h
@@ -185,3 +185,19 @@ private:
+
+/** A wrapper for file streams that enables exceptions. */
+template <class StreamType>
+class FileStream final : public StreamType
+{
+public:
+
+ FileStream(const std::string & Path);
+};
+
+
+
+
+
+using InputFileStream = FileStream<std::ifstream>;
+using OutputFileStream = FileStream<std::ofstream>;