123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- #pragma once
- #include "openmpt/all/BuildSettings.hpp"
- #if defined(MPT_ENABLE_FILEIO)
- #include "mpt/io_read/filecursor_memory.hpp"
- #include "mpt/io_read/filecursor_stdstream.hpp"
- #include "../common/mptString.h"
- #include "../common/mptPathString.h"
- #include "../common/FileReaderFwd.h"
- #if defined(MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR)
- #if MPT_GCC_AT_LEAST(9,1,0)
- #include <filesystem>
- #endif
- #endif
- #include <fstream>
- #include <ios>
- #include <ostream>
- #include <streambuf>
- #include <utility>
- #if MPT_COMPILER_MSVC
- #include <cstdio>
- #endif
- #ifdef MODPLUG_TRACKER
- #if MPT_OS_WINDOWS
- #include <windows.h>
- #endif
- #endif
- #endif
- OPENMPT_NAMESPACE_BEGIN
- #if defined(MPT_ENABLE_FILEIO)
- #ifdef MODPLUG_TRACKER
- #if MPT_OS_WINDOWS
- bool SetFilesystemCompression(HANDLE hFile);
- bool SetFilesystemCompression(int fd);
- bool SetFilesystemCompression(const mpt::PathString &filename);
- #endif
- #endif
- namespace mpt
- {
- namespace detail
- {
- template<typename Tbase>
- inline void fstream_open(Tbase & base, const mpt::PathString & filename, std::ios_base::openmode mode)
- {
- #if defined(MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR)
- #if MPT_GCC_AT_LEAST(9,1,0)
- base.open(static_cast<std::filesystem::path>(filename.AsNative()), mode);
- #else
-
- base.open(mpt::ToCharset(mpt::Charset::Locale, filename.AsNative()).c_str(), mode);
- #endif
- #else
- base.open(filename.AsNativePrefixed().c_str(), mode);
- #endif
- }
- }
- class fstream
- : public std::fstream
- {
- private:
- typedef std::fstream Tbase;
- public:
- fstream() {}
- fstream(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
- {
- detail::fstream_open<Tbase>(*this, filename, mode);
- }
- #if MPT_COMPILER_MSVC
- protected:
- fstream(std::FILE * file)
- : std::fstream(file)
- {
- }
- #endif
- public:
- void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
- {
- detail::fstream_open<Tbase>(*this, filename, mode);
- }
- void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
- void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
- #if MPT_OS_WINDOWS
- void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
- void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
- #endif
- };
- class ifstream
- : public std::ifstream
- {
- private:
- typedef std::ifstream Tbase;
- public:
- ifstream() {}
- ifstream(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in)
- {
- detail::fstream_open<Tbase>(*this, filename, mode);
- }
- #if MPT_COMPILER_MSVC
- protected:
- ifstream(std::FILE * file)
- : std::ifstream(file)
- {
- }
- #endif
- public:
- void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in)
- {
- detail::fstream_open<Tbase>(*this, filename, mode);
- }
- void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
- void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
- #if MPT_OS_WINDOWS
- void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
- void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
- #endif
- };
- class ofstream
- : public std::ofstream
- {
- private:
- typedef std::ofstream Tbase;
- public:
- ofstream() {}
- ofstream(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::out)
- {
- detail::fstream_open<Tbase>(*this, filename, mode);
- }
- #if MPT_COMPILER_MSVC
- protected:
- ofstream(std::FILE * file)
- : std::ofstream(file)
- {
- }
- #endif
- public:
- void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::out)
- {
- detail::fstream_open<Tbase>(*this, filename, mode);
- }
- void open(const char * filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
- void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
- #if MPT_OS_WINDOWS
- void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
- void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
- #endif
- };
- enum class FlushMode
- {
- None = 0,
- Single = 1,
- Full = 2,
- };
- inline FlushMode FlushModeFromBool(bool flush)
- {
- return flush ? FlushMode::Full : FlushMode::None;
- }
- #ifdef MODPLUG_TRACKER
- class SafeOutputFile
- {
- private:
- FlushMode m_FlushMode;
- #if MPT_COMPILER_MSVC
- std::FILE *m_f = nullptr;
- #else
- mpt::ofstream m_s;
- #endif
- #if MPT_COMPILER_MSVC
- class FILEostream
- : public mpt::ofstream
- {
- public:
- FILEostream(std::FILE * file)
- : mpt::ofstream(file)
- {
- return;
- }
- };
- FILEostream m_s;
- static mpt::tstring convert_mode(std::ios_base::openmode mode, FlushMode flushMode);
- std::FILE * internal_fopen(const mpt::PathString &filename, std::ios_base::openmode mode, FlushMode flushMode);
- #endif
- public:
- SafeOutputFile() = delete;
- explicit SafeOutputFile(const mpt::PathString &filename, std::ios_base::openmode mode = std::ios_base::out, FlushMode flushMode = FlushMode::Full)
- : m_FlushMode(flushMode)
- #if MPT_COMPILER_MSVC
- , m_s(internal_fopen(filename, mode | std::ios_base::out, flushMode))
- #else
- , m_s(filename, mode)
- #endif
- {
- if(!stream().is_open())
- {
- stream().setstate(mpt::ofstream::failbit);
- }
- }
- mpt::ofstream& stream()
- {
- return m_s;
- }
- operator mpt::ofstream& ()
- {
- return stream();
- }
- const mpt::ofstream& stream() const
- {
- return m_s;
- }
- operator const mpt::ofstream& () const
- {
- return stream();
- }
- operator bool() const
- {
- return stream() ? true : false;
- }
- bool operator!() const
- {
- return stream().operator!();
- }
- ~SafeOutputFile() noexcept(false);
- };
- #endif
- #ifdef MODPLUG_TRACKER
- class LazyFileRef {
- private:
- const mpt::PathString m_Filename;
- public:
- LazyFileRef(const mpt::PathString &filename)
- : m_Filename(filename)
- {
- return;
- }
- public:
- LazyFileRef & operator = (const std::vector<std::byte> &data);
- LazyFileRef & operator = (const std::vector<char> &data);
- LazyFileRef & operator = (const std::string &data);
- operator std::vector<std::byte> () const;
- operator std::vector<char> () const;
- operator std::string () const;
- };
- #endif
- }
- class InputFile
- {
- private:
- mpt::PathString m_Filename;
- mpt::ifstream m_File;
- bool m_IsValid;
- bool m_IsCached;
- std::vector<std::byte> m_Cache;
- public:
- InputFile(const mpt::PathString &filename, bool allowWholeFileCaching = false);
- ~InputFile();
- bool IsValid() const;
- bool IsCached() const;
- mpt::PathString GetFilename() const;
- std::istream& GetStream();
- mpt::const_byte_span GetCache();
- private:
- bool Open(const mpt::PathString &filename, bool allowWholeFileCaching = false);
- };
- template <typename Targ1>
- inline FileCursor make_FileCursor(Targ1 &&arg1)
- {
- return mpt::IO::make_FileCursor<mpt::PathString>(std::forward<Targ1>(arg1));
- }
- template <typename Targ1, typename Targ2>
- inline FileCursor make_FileCursor(Targ1 &&arg1, Targ2 &&arg2)
- {
- return mpt::IO::make_FileCursor<mpt::PathString>(std::forward<Targ1>(arg1), std::forward<Targ2>(arg2));
- }
- class InputFile;
- template <typename TInputFile, std::enable_if_t<std::is_same<TInputFile, InputFile>::value, bool> = true>
- inline FileCursor make_FileCursor(TInputFile &file)
- {
- if(!file.IsValid())
- {
- return FileCursor();
- }
- if(file.IsCached())
- {
- return mpt::IO::make_FileCursor<mpt::PathString>(file.GetCache(), std::make_shared<mpt::PathString>(file.GetFilename()));
- } else
- {
- return mpt::IO::make_FileCursor<mpt::PathString>(file.GetStream(), std::make_shared<mpt::PathString>(file.GetFilename()));
- }
- }
- template <typename Targ1>
- inline FileCursor GetFileReader(Targ1 &&arg1)
- {
- return make_FileCursor(std::forward<Targ1>(arg1));
- }
- template <typename Targ1, typename Targ2>
- inline FileCursor GetFileReader(Targ1 &&arg1, Targ2 &&arg2)
- {
- return make_FileCursor(std::forward<Targ1>(arg1), std::forward<Targ2>(arg2));
- }
- #if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS
- class OnDiskFileWrapper
- {
- private:
- mpt::PathString m_Filename;
- bool m_IsTempFile;
- public:
- OnDiskFileWrapper(FileCursor& file, const mpt::PathString& fileNameExtension = P_("tmp"));
- ~OnDiskFileWrapper();
- public:
- bool IsValid() const;
- mpt::PathString GetFilename() const;
- };
- #endif
- #endif
- OPENMPT_NAMESPACE_END
|