From: <sag...@us...> - 2014-10-25 15:27:31
|
Revision: 4489 http://sourceforge.net/p/modplug/code/4489 Author: saga-games Date: 2014-10-25 15:27:10 +0000 (Sat, 25 Oct 2014) Log Message: ----------- [Ref] Move file handling code around as outlined in r4487. [Ref] Move CSoundFile upgrade code to own file, UpgradeModule.cpp Revision Links: -------------- http://sourceforge.net/p/modplug/code/4487 Modified Paths: -------------- trunk/OpenMPT/build/autotools/Makefile.am trunk/OpenMPT/common/Logging.cpp trunk/OpenMPT/common/mptIO.cpp trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters trunk/OpenMPT/mptrack/Ctrl_ins.cpp trunk/OpenMPT/mptrack/Ctrl_smp.cpp trunk/OpenMPT/mptrack/MainFrm.cpp trunk/OpenMPT/mptrack/Moddoc.cpp trunk/OpenMPT/mptrack/View_tre.cpp trunk/OpenMPT/mptrack/Vstplug.cpp trunk/OpenMPT/mptrack/mptrack_08.vcproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters trunk/OpenMPT/soundlib/Dlsbank.cpp trunk/OpenMPT/soundlib/Sndfile.cpp trunk/OpenMPT/soundlib/Sndfile.h Added Paths: ----------- trunk/OpenMPT/common/mptFileIO.cpp trunk/OpenMPT/common/mptFileIO.h trunk/OpenMPT/soundlib/UpgradeModule.cpp Removed Paths: ------------- trunk/OpenMPT/common/mptFstream.h trunk/OpenMPT/mptrack/MemoryMappedFile.cpp trunk/OpenMPT/mptrack/MemoryMappedFile.h Modified: trunk/OpenMPT/build/autotools/Makefile.am =================================================================== --- trunk/OpenMPT/build/autotools/Makefile.am 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/build/autotools/Makefile.am 2014-10-25 15:27:10 UTC (rev 4489) @@ -87,7 +87,8 @@ libopenmpt_la_SOURCES += common/misc_util.cpp libopenmpt_la_SOURCES += common/misc_util.h libopenmpt_la_SOURCES += common/mptAtomic.h -libopenmpt_la_SOURCES += common/mptFstream.h +libopenmpt_la_SOURCES += common/mptFileIO.h +libopenmpt_la_SOURCES += common/mptFileIO.cpp libopenmpt_la_SOURCES += common/mptIO.cpp libopenmpt_la_SOURCES += common/mptIO.h libopenmpt_la_SOURCES += common/mptPathString.cpp @@ -211,6 +212,7 @@ libopenmpt_la_SOURCES += soundlib/tuningcollection.h libopenmpt_la_SOURCES += soundlib/tuning.cpp libopenmpt_la_SOURCES += soundlib/tuning.h +libopenmpt_la_SOURCES += soundlib/UpgradeModule.cpp libopenmpt_la_SOURCES += soundlib/Wav.h libopenmpt_la_SOURCES += soundlib/WAVTools.cpp libopenmpt_la_SOURCES += soundlib/WAVTools.h Modified: trunk/OpenMPT/common/Logging.cpp =================================================================== --- trunk/OpenMPT/common/Logging.cpp 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/common/Logging.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -11,7 +11,7 @@ #include "stdafx.h" #include "Logging.h" -#include "mptFstream.h" +#include "mptFileIO.h" #if defined(MODPLUG_TRACKER) #include "mptAtomic.h" #endif Added: trunk/OpenMPT/common/mptFileIO.cpp =================================================================== --- trunk/OpenMPT/common/mptFileIO.cpp (rev 0) +++ trunk/OpenMPT/common/mptFileIO.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -0,0 +1,226 @@ +/* + * mptFileIO.cpp + * ------------- + * Purpose: File I/O wrappers + * Notes : (currently none) + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + + +#include "stdafx.h" +#include "mptFileIO.h" + + +OPENMPT_NAMESPACE_BEGIN + + +#ifdef MODPLUG_TRACKER + +CMappedFile::~CMappedFile() +//------------------------- +{ + Close(); +} + + +bool CMappedFile::Open(const mpt::PathString &filename) +//----------------------------------------------------- +{ + m_hFile = CreateFileW( + filename.AsNative().c_str(), + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); + if(m_hFile == INVALID_HANDLE_VALUE) + { + m_hFile = nullptr; + return false; + } + m_FileName = filename; + return true; +} + + +void CMappedFile::Close() +//----------------------- +{ + m_FileName = mpt::PathString(); + // Unlock file + if(m_hFMap) + { + if(m_pData) + { + UnmapViewOfFile(m_pData); + m_pData = nullptr; + } + CloseHandle(m_hFMap); + m_hFMap = nullptr; + } else if(m_pData) + { + free(m_pData); + m_pData = nullptr; + } + + // Close file handle + if(m_hFile) + { + CloseHandle(m_hFile); + m_hFile = nullptr; + } +} + + +size_t CMappedFile::GetLength() +//----------------------------- +{ + LARGE_INTEGER size; + if(GetFileSizeEx(m_hFile, &size) == FALSE) + { + return 0; + } + return mpt::saturate_cast<size_t>(size.QuadPart); +} + + +const void *CMappedFile::Lock() +//----------------------------- +{ + size_t length = GetLength(); + if(!length) return nullptr; + + void *lpStream; + + HANDLE hmf = CreateFileMapping( + m_hFile, + NULL, + PAGE_READONLY, + 0, 0, + NULL); + + // Try memory-mapping first + if(hmf) + { + lpStream = MapViewOfFile( + hmf, + FILE_MAP_READ, + 0, 0, + length); + if(lpStream) + { + m_hFMap = hmf; + m_pData = lpStream; + return lpStream; + } + CloseHandle(hmf); + hmf = nullptr; + } + + // Fallback if memory-mapping fails for some weird reason + if((lpStream = malloc(length)) == nullptr) return nullptr; + memset(lpStream, 0, length); + size_t bytesToRead = length; + size_t bytesRead = 0; + while(bytesToRead > 0) + { + DWORD chunkToRead = mpt::saturate_cast<DWORD>(length); + DWORD chunkRead = 0; + if(ReadFile(m_hFile, (char*)lpStream + bytesRead, chunkToRead, &chunkRead, NULL) == FALSE) + { + // error + free(lpStream); + return nullptr; + } + bytesRead += chunkRead; + bytesToRead -= chunkRead; + } + m_pData = lpStream; + return lpStream; +} + + +#endif // MODPLUG_TRACKER + + + +#if defined(MPT_WITH_PATHSTRING) + +InputFile::InputFile() +{ + return; +} + +InputFile::InputFile(const mpt::PathString &filename) + : m_Filename(filename) +{ +#if defined(MPT_FILEREADER_STD_ISTREAM) + m_File.open(m_Filename, std::ios::binary | std::ios::in); +#else + m_File.Open(m_Filename); +#endif +} + +InputFile::~InputFile() +{ + return; +} + + +bool InputFile::Open(const mpt::PathString &filename) +{ + m_Filename = filename; +#if defined(MPT_FILEREADER_STD_ISTREAM) + m_File.open(m_Filename, std::ios::binary | std::ios::in); + return m_File.good(); +#else + return m_File.Open(m_Filename); +#endif +} + + +bool InputFile::IsValid() const +{ +#if defined(MPT_FILEREADER_STD_ISTREAM) + return m_File.good(); +#else + return m_File.IsOpen(); +#endif +} + +#if defined(MPT_FILEREADER_STD_ISTREAM) + +InputFile::ContentsRef InputFile::Get() +{ + InputFile::ContentsRef result; + result.first = &m_File; + result.second = m_File.good() ? &m_Filename : nullptr; + return result; +} + +#else + +InputFile::ContentsRef InputFile::Get() +{ + InputFile::ContentsRef result; + result.first.data = nullptr; + result.first.size = 0; + result.second = nullptr; + if(!m_File.IsOpen()) + { + return result; + } + result.first.data = reinterpret_cast<const char*>(m_File.Lock()); + result.first.size = m_File.GetLength(); + result.second = &m_Filename; + return result; +} + +#endif + +#endif // MPT_WITH_PATHSTRING + + +OPENMPT_NAMESPACE_END Property changes on: trunk/OpenMPT/common/mptFileIO.cpp ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/x-c++src \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Copied: trunk/OpenMPT/common/mptFileIO.h (from rev 4488, trunk/OpenMPT/common/mptFstream.h) =================================================================== --- trunk/OpenMPT/common/mptFileIO.h (rev 0) +++ trunk/OpenMPT/common/mptFileIO.h 2014-10-25 15:27:10 UTC (rev 4489) @@ -0,0 +1,555 @@ +/* + * mptFileIO.h + * ----------- + * Purpose: A wrapper around std::fstream, fixing VS2008 charset conversion braindamage, and enforcing usage of mpt::PathString. + * Notes : You should only ever use these wrappers instead of plain std::fstream classes. + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + +#pragma once + +#include <fstream> +#include <ios> +#include <ostream> +#include <streambuf> + +#include "../common/mptString.h" +#include "../common/mptPathString.h" +#if defined(MPT_WITH_PATHSTRING) +#include "../common/mptIO.h" +#include <utility> +#endif + + +OPENMPT_NAMESPACE_BEGIN + + +#if defined(MPT_WITH_PATHSTRING) + + +static inline FILE * mpt_fopen(const mpt::PathString &filename, const char *mode) +//------------------------------------------------------------------------------- +{ + #if MPT_OS_WINDOWS + #if defined(MPT_WITH_CHARSET_LOCALE) + return _wfopen(filename.AsNative().c_str(), mode ? mpt::ToWide(mpt::CharsetLocale, mode).c_str() : nullptr); + #else + return _wfopen(filename.AsNative().c_str(), mode ? mpt::ToWide(mpt::CharsetUTF8, mode).c_str() : nullptr); + #endif + #else // !MPT_OS_WINDOWS + return fopen(filename.AsNative().c_str(), mode); + #endif // MPT_OS_WINDOWS +} + + +namespace mpt +{ + +#if MPT_COMPILER_MSVC && MPT_MSVC_BEFORE(2010,0) + +// VS2008 converts char filenames with CRT mbcs string conversion functions to wchar_t filenames. +// This is totally wrong for Win32 GUI applications because the C locale does not necessarily match the current windows ANSI codepage (CP_ACP). +// Work around this insanity by using our own string conversions for the std::fstream filenames. + +#define MPT_FSTREAM_DO_CONVERSIONS + +#elif MPT_COMPILER_GCC + +#if MPT_OS_WINDOWS +// GCC C++ library has no wchar_t overloads +#define MPT_FSTREAM_DO_CONVERSIONS +#define MPT_FSTREAM_DO_CONVERSIONS_ANSI +#endif + +#endif + +#ifdef MPT_FSTREAM_DO_CONVERSIONS +#define MPT_FSTREAM_OPEN(filename, mode) detail::fstream_open<Tbase>(*this, (filename), (mode)) +#else +#define MPT_FSTREAM_OPEN(filename, mode) Tbase::open((filename), (mode)) +#endif + +namespace detail +{ + +template<typename Tbase> +inline void fstream_open(Tbase & base, const mpt::PathString & filename, std::ios_base::openmode mode) +{ +#if defined( MPT_FSTREAM_DO_CONVERSIONS_ANSI) + base.open(mpt::ToLocale(filename.AsNative()).c_str(), mode); +#else + base.open(filename.AsNative().c_str(), mode); +#endif +} + +#ifdef MPT_FSTREAM_DO_CONVERSIONS + +template<typename Tbase> +inline void fstream_open(Tbase & base, const std::wstring & filename, std::ios_base::openmode mode) +{ + detail::fstream_open<Tbase>(base, mpt::PathString::FromWide(filename), mode); +} + +template<typename Tbase> +inline void fstream_open(Tbase & base, const wchar_t * filename, std::ios_base::openmode mode) +{ + detail::fstream_open<Tbase>(base, mpt::PathString::FromWide(filename ? std::wstring(filename) : std::wstring()), mode); +} + +template<typename Tbase> +inline void fstream_open(Tbase & base, const std::string & filename, std::ios_base::openmode mode) +{ + detail::fstream_open<Tbase>(base, mpt::PathString::FromWide(mpt::ToWide(mpt::CharsetLocale, filename)), mode); +} + +template<typename Tbase> +inline void fstream_open(Tbase & base, const char * filename, std::ios_base::openmode mode) +{ + detail::fstream_open<Tbase>(base, mpt::PathString::FromWide(mpt::ToWide(mpt::CharsetLocale, filename ? std::string(filename) : std::string())), mode); +} + +#endif + +} // namespace detail + +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); + } + 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); + } + MPT_DEPRECATED_PATH void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) + { + MPT_FSTREAM_OPEN(filename, mode); + } + MPT_DEPRECATED_PATH void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) + { + MPT_FSTREAM_OPEN(filename.c_str(), mode); + } +#if MPT_OS_WINDOWS + MPT_DEPRECATED_PATH void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) + { + MPT_FSTREAM_OPEN(filename, mode); + } + MPT_DEPRECATED_PATH void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) + { + MPT_FSTREAM_OPEN(filename.c_str(), mode); + } +#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); + } + void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in) + { + detail::fstream_open<Tbase>(*this, filename, mode); + } + MPT_DEPRECATED_PATH void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in) + { + MPT_FSTREAM_OPEN(filename, mode); + } + MPT_DEPRECATED_PATH void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in) + { + MPT_FSTREAM_OPEN(filename.c_str(), mode); + } +#if MPT_OS_WINDOWS + MPT_DEPRECATED_PATH void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in) + { + MPT_FSTREAM_OPEN(filename, mode); + } + MPT_DEPRECATED_PATH void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in) + { + MPT_FSTREAM_OPEN(filename.c_str(), mode); + } +#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); + } + void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::out) + { + detail::fstream_open<Tbase>(*this, filename, mode); + } + MPT_DEPRECATED_PATH void open(const char * filename, std::ios_base::openmode mode = std::ios_base::out) + { + MPT_FSTREAM_OPEN(filename, mode); + } + MPT_DEPRECATED_PATH void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::out) + { + MPT_FSTREAM_OPEN(filename.c_str(), mode); + } +#if MPT_OS_WINDOWS + MPT_DEPRECATED_PATH void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::out) + { + MPT_FSTREAM_OPEN(filename, mode); + } + MPT_DEPRECATED_PATH void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::out) + { + MPT_FSTREAM_OPEN(filename.c_str(), mode); + } +#endif +}; + +#undef MPT_FSTREAM_OPEN + + + +// class FILE_ostream, FILE_output_streambuf and FILE_output_buffered_streambuf +// provide a portable way of wrapping a std::ostream around an FILE* opened for output. +// They offer similar functionality to the badly documented +// MSVC std::fstream(FILE*) constructor or GCC libstdc++ __gnu_cxx::stdio_sync_filebuf class, +// and, for other compilers, provide a race-free alternative to +// closing the FILE* and opening it again as a std::ofstream. +// +// Only output functionality is implemented because we have no need for an input wrapper. +// +// During the whole lifetime of the iostream wrappers, the FILE* object is assumend to be +// either +// - NULL +// or +// - valid +// - opened for writing in non-append mode +// - opened in binary mode +// - seekable +// Some of these preconditions cannot be verified, +// and even the others do not get verified. +// Behaviour in case of any unmet preconditions is undefined. +// +// The buffered streambuf and the ostream use a buffer of 64KiB by default. +// +// For FILE_output_streambuf, coherency with the underlying FILE* is always guaranteed. +// For FILE_ostream and FILE_output_buffered_streambuf, coherence is only +// guaranteed when flush() or pubsync() get called. +// The constructors and destructors take care to not violate coherency. +// When mixing FILE* and iostream I/O during the lifetime of the iostream objects, +// the user is responsible for providing coherency via the appropriate +// flush and sync functions. +// Behaviour in case of incoherent access is undefined. + + +class FILE_output_streambuf : public std::streambuf +{ +public: + typedef std::streambuf::char_type char_type; + typedef std::streambuf::traits_type traits_type; + typedef traits_type::int_type int_type; + typedef traits_type::pos_type pos_type; + typedef traits_type::off_type off_type; +protected: + FILE *f; +public: + FILE_output_streambuf(FILE *f) + : f(f) + { + return; + } + ~FILE_output_streambuf() + { + return; + } +protected: + virtual int_type overflow(int_type ch) + { + if(!mpt::IO::IsValid(f)) + { + return traits_type::eof(); + } + if(traits_type::eq_int_type(ch, traits_type::eof())) + { + return traits_type::eof(); + } + char_type c = traits_type::to_char_type(ch); + if(!mpt::IO::WriteRaw(f, &c, 1)) + { + return traits_type::eof(); + } + return ch; + } + virtual int sync() + { + if(!mpt::IO::IsValid(f)) + { + return -1; + } + if(!mpt::IO::Flush(f)) + { + return -1; + } + return 0; + } + virtual pos_type seekpos(pos_type pos, std::ios_base::openmode which) + { + if(!mpt::IO::IsValid(f)) + { + return pos_type(off_type(-1)); + } + return seekoff(pos, std::ios_base::beg, which); + } + virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) + { + if(!mpt::IO::IsValid(f)) + { + return pos_type(off_type(-1)); + } + if(which & std::ios_base::in) + { + return pos_type(off_type(-1)); + } + if(!(which & std::ios_base::out)) + { + return pos_type(off_type(-1)); + } + mpt::IO::Offset oldpos = mpt::IO::TellWrite(f); + if(dir == std::ios_base::beg) + { + if(!mpt::IO::SeekAbsolute(f, off)) + { + mpt::IO::SeekAbsolute(f, oldpos); + return pos_type(off_type(-1)); + } + } else if(dir == std::ios_base::cur) + { + if(!mpt::IO::SeekRelative(f, off)) + { + mpt::IO::SeekAbsolute(f, oldpos); + return pos_type(off_type(-1)); + } + } else if(dir == std::ios_base::end) + { + if(!(mpt::IO::SeekEnd(f) && mpt::IO::SeekRelative(f, off))) + { + mpt::IO::SeekAbsolute(f, oldpos); + return pos_type(off_type(-1)); + } + } else + { + return pos_type(off_type(-1)); + } + mpt::IO::Offset newpos = mpt::IO::TellWrite(f); + if(!mpt::IO::OffsetFits<off_type>(newpos)) + { + mpt::IO::SeekAbsolute(f, oldpos); + return pos_type(off_type(-1)); + } + return pos_type(static_cast<off_type>(newpos)); + } +}; // class FILE_output_streambuf + + +class FILE_output_buffered_streambuf : public FILE_output_streambuf +{ +public: + typedef std::streambuf::char_type char_type; + typedef std::streambuf::traits_type traits_type; + typedef traits_type::int_type int_type; + typedef traits_type::pos_type pos_type; + typedef traits_type::off_type off_type; +private: + typedef FILE_output_streambuf Tparent; + std::vector<char_type> buf; +public: + FILE_output_buffered_streambuf(FILE *f, std::size_t bufSize = 64*1024) + : FILE_output_streambuf(f) + , buf((bufSize > 0) ? bufSize : 1) + { + setp(&buf[0], &buf[0] + buf.size()); + } + ~FILE_output_buffered_streambuf() + { + if(!mpt::IO::IsValid(f)) + { + return; + } + WriteOut(); + } +private: + bool IsDirty() const + { + return ((pptr() - pbase()) > 0); + } + bool WriteOut() + { + std::ptrdiff_t n = pptr() - pbase(); + std::ptrdiff_t left = n; + while(left > 0) + { + int backchunk = mpt::saturate_cast<int>(-left); + pbump(backchunk); + left += backchunk; + } + return mpt::IO::WriteRaw(f, pbase(), n); + } +protected: + virtual int_type overflow(int_type ch) + { + if(!mpt::IO::IsValid(f)) + { + return traits_type::eof(); + } + if(traits_type::eq_int_type(ch, traits_type::eof())) + { + return traits_type::eof(); + } + if(!WriteOut()) + { + return traits_type::eof(); + } + char_type c = traits_type::to_char_type(ch); + *pptr() = c; + pbump(1); + return ch; + } + virtual int sync() + { + if(!mpt::IO::IsValid(f)) + { + return -1; + } + if(!WriteOut()) + { + return -1; + } + return Tparent::sync(); + } + virtual pos_type seekpos(pos_type pos, std::ios_base::openmode which) + { + if(!mpt::IO::IsValid(f)) + { + return pos_type(off_type(-1)); + } + if(!WriteOut()) + { + return pos_type(off_type(-1)); + } + return Tparent::seekpos(pos, which); + } + virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) + { + if(!mpt::IO::IsValid(f)) + { + return pos_type(off_type(-1)); + } + if(!WriteOut()) + { + return pos_type(off_type(-1)); + } + return Tparent::seekoff(off, dir, which); + } +}; // class FILE_output_buffered_streambuf + + +class FILE_ostream : public std::ostream { +private: + FILE *f; + FILE_output_buffered_streambuf buf; +public: + FILE_ostream(FILE *f, std::size_t bufSize = 64*1024) + : std::ostream(&buf) + , f(f) + , buf(f, bufSize) + { + if(mpt::IO::IsValid(f)) mpt::IO::Flush(f); + } + ~FILE_ostream() + { + flush(); + buf.pubsync(); + if(mpt::IO::IsValid(f)) mpt::IO::Flush(f); + } +}; // class FILE_ostream + + +} // namespace mpt + + + +#ifdef MODPLUG_TRACKER +//=============== +class CMappedFile +//=============== +{ +protected: + HANDLE m_hFile; + HANDLE m_hFMap; + void *m_pData; + mpt::PathString m_FileName; + +public: + CMappedFile() : m_hFile(nullptr), m_hFMap(nullptr), m_pData(nullptr) { } + ~CMappedFile(); + +public: + bool Open(const mpt::PathString &filename); + bool IsOpen() const { return m_hFile != NULL && m_hFile != INVALID_HANDLE_VALUE; } + const mpt::PathString * GetpFilename() const { return &m_FileName; } + void Close(); + size_t GetLength(); + const void *Lock(); +}; +#endif // MODPLUG_TRACKER + + +//============= +class InputFile +//============= +{ +private: + mpt::PathString m_Filename; + #ifdef MPT_FILEREADER_STD_ISTREAM + mpt::ifstream m_File; + #else + CMappedFile m_File; + #endif +public: + InputFile(); + InputFile(const mpt::PathString &filename); + ~InputFile(); + bool Open(const mpt::PathString &filename); + bool IsValid() const; +#if defined(MPT_FILEREADER_STD_ISTREAM) + typedef std::pair<std::istream*, const mpt::PathString*> ContentsRef; +#else + struct Data + { + const char *data; + std::size_t size; + }; + typedef std::pair<InputFile::Data, const mpt::PathString*> ContentsRef; +#endif + InputFile::ContentsRef Get(); +}; + + +#endif // MPT_WITH_PATHSTRING + + +OPENMPT_NAMESPACE_END + Deleted: trunk/OpenMPT/common/mptFstream.h =================================================================== --- trunk/OpenMPT/common/mptFstream.h 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/common/mptFstream.h 2014-10-25 15:27:10 UTC (rev 4489) @@ -1,555 +0,0 @@ -/* - * mptFstream.h - * ------------ - * Purpose: A wrapper around std::fstream, fixing VS2008 charset conversion braindamage, and enforcing usage of mpt::PathString. - * Notes : You should only ever use these wrappers instead of plain std::fstream classes. - * Authors: OpenMPT Devs - * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. - */ - -#pragma once - -#include <fstream> -#include <ios> -#include <ostream> -#include <streambuf> - -#include "../common/mptString.h" -#include "../common/mptPathString.h" -#if defined(MPT_WITH_PATHSTRING) -#include "../common/mptIO.h" -#include <utility> -#endif - - -OPENMPT_NAMESPACE_BEGIN - - -#if defined(MPT_WITH_PATHSTRING) - - -static inline FILE * mpt_fopen(const mpt::PathString &filename, const char *mode) -//------------------------------------------------------------------------------- -{ - #if MPT_OS_WINDOWS - #if defined(MPT_WITH_CHARSET_LOCALE) - return _wfopen(filename.AsNative().c_str(), mode ? mpt::ToWide(mpt::CharsetLocale, mode).c_str() : nullptr); - #else - return _wfopen(filename.AsNative().c_str(), mode ? mpt::ToWide(mpt::CharsetUTF8, mode).c_str() : nullptr); - #endif - #else // !MPT_OS_WINDOWS - return fopen(filename.AsNative().c_str(), mode); - #endif // MPT_OS_WINDOWS -} - - -namespace mpt -{ - -#if MPT_COMPILER_MSVC && MPT_MSVC_BEFORE(2010,0) - -// VS2008 converts char filenames with CRT mbcs string conversion functions to wchar_t filenames. -// This is totally wrong for Win32 GUI applications because the C locale does not necessarily match the current windows ANSI codepage (CP_ACP). -// Work around this insanity by using our own string conversions for the std::fstream filenames. - -#define MPT_FSTREAM_DO_CONVERSIONS - -#elif MPT_COMPILER_GCC - -#if MPT_OS_WINDOWS -// GCC C++ library has no wchar_t overloads -#define MPT_FSTREAM_DO_CONVERSIONS -#define MPT_FSTREAM_DO_CONVERSIONS_ANSI -#endif - -#endif - -#ifdef MPT_FSTREAM_DO_CONVERSIONS -#define MPT_FSTREAM_OPEN(filename, mode) detail::fstream_open<Tbase>(*this, (filename), (mode)) -#else -#define MPT_FSTREAM_OPEN(filename, mode) Tbase::open((filename), (mode)) -#endif - -namespace detail -{ - -template<typename Tbase> -inline void fstream_open(Tbase & base, const mpt::PathString & filename, std::ios_base::openmode mode) -{ -#if defined( MPT_FSTREAM_DO_CONVERSIONS_ANSI) - base.open(mpt::ToLocale(filename.AsNative()).c_str(), mode); -#else - base.open(filename.AsNative().c_str(), mode); -#endif -} - -#ifdef MPT_FSTREAM_DO_CONVERSIONS - -template<typename Tbase> -inline void fstream_open(Tbase & base, const std::wstring & filename, std::ios_base::openmode mode) -{ - detail::fstream_open<Tbase>(base, mpt::PathString::FromWide(filename), mode); -} - -template<typename Tbase> -inline void fstream_open(Tbase & base, const wchar_t * filename, std::ios_base::openmode mode) -{ - detail::fstream_open<Tbase>(base, mpt::PathString::FromWide(filename ? std::wstring(filename) : std::wstring()), mode); -} - -template<typename Tbase> -inline void fstream_open(Tbase & base, const std::string & filename, std::ios_base::openmode mode) -{ - detail::fstream_open<Tbase>(base, mpt::PathString::FromWide(mpt::ToWide(mpt::CharsetLocale, filename)), mode); -} - -template<typename Tbase> -inline void fstream_open(Tbase & base, const char * filename, std::ios_base::openmode mode) -{ - detail::fstream_open<Tbase>(base, mpt::PathString::FromWide(mpt::ToWide(mpt::CharsetLocale, filename ? std::string(filename) : std::string())), mode); -} - -#endif - -} // namespace detail - -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); - } - 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); - } - MPT_DEPRECATED_PATH void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) - { - MPT_FSTREAM_OPEN(filename, mode); - } - MPT_DEPRECATED_PATH void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) - { - MPT_FSTREAM_OPEN(filename.c_str(), mode); - } -#if MPT_OS_WINDOWS - MPT_DEPRECATED_PATH void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) - { - MPT_FSTREAM_OPEN(filename, mode); - } - MPT_DEPRECATED_PATH void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) - { - MPT_FSTREAM_OPEN(filename.c_str(), mode); - } -#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); - } - void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in) - { - detail::fstream_open<Tbase>(*this, filename, mode); - } - MPT_DEPRECATED_PATH void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in) - { - MPT_FSTREAM_OPEN(filename, mode); - } - MPT_DEPRECATED_PATH void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in) - { - MPT_FSTREAM_OPEN(filename.c_str(), mode); - } -#if MPT_OS_WINDOWS - MPT_DEPRECATED_PATH void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in) - { - MPT_FSTREAM_OPEN(filename, mode); - } - MPT_DEPRECATED_PATH void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in) - { - MPT_FSTREAM_OPEN(filename.c_str(), mode); - } -#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); - } - void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::out) - { - detail::fstream_open<Tbase>(*this, filename, mode); - } - MPT_DEPRECATED_PATH void open(const char * filename, std::ios_base::openmode mode = std::ios_base::out) - { - MPT_FSTREAM_OPEN(filename, mode); - } - MPT_DEPRECATED_PATH void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::out) - { - MPT_FSTREAM_OPEN(filename.c_str(), mode); - } -#if MPT_OS_WINDOWS - MPT_DEPRECATED_PATH void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::out) - { - MPT_FSTREAM_OPEN(filename, mode); - } - MPT_DEPRECATED_PATH void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::out) - { - MPT_FSTREAM_OPEN(filename.c_str(), mode); - } -#endif -}; - -#undef MPT_FSTREAM_OPEN - - - -// class FILE_ostream, FILE_output_streambuf and FILE_output_buffered_streambuf -// provide a portable way of wrapping a std::ostream around an FILE* opened for output. -// They offer similar functionality to the badly documented -// MSVC std::fstream(FILE*) constructor or GCC libstdc++ __gnu_cxx::stdio_sync_filebuf class, -// and, for other compilers, provide a race-free alternative to -// closing the FILE* and opening it again as a std::ofstream. -// -// Only output functionality is implemented because we have no need for an input wrapper. -// -// During the whole lifetime of the iostream wrappers, the FILE* object is assumend to be -// either -// - NULL -// or -// - valid -// - opened for writing in non-append mode -// - opened in binary mode -// - seekable -// Some of these preconditions cannot be verified, -// and even the others do not get verified. -// Behaviour in case of any unmet preconditions is undefined. -// -// The buffered streambuf and the ostream use a buffer of 64KiB by default. -// -// For FILE_output_streambuf, coherency with the underlying FILE* is always guaranteed. -// For FILE_ostream and FILE_output_buffered_streambuf, coherence is only -// guaranteed when flush() or pubsync() get called. -// The constructors and destructors take care to not violate coherency. -// When mixing FILE* and iostream I/O during the lifetime of the iostream objects, -// the user is responsible for providing coherency via the appropriate -// flush and sync functions. -// Behaviour in case of incoherent access is undefined. - - -class FILE_output_streambuf : public std::streambuf -{ -public: - typedef std::streambuf::char_type char_type; - typedef std::streambuf::traits_type traits_type; - typedef traits_type::int_type int_type; - typedef traits_type::pos_type pos_type; - typedef traits_type::off_type off_type; -protected: - FILE *f; -public: - FILE_output_streambuf(FILE *f) - : f(f) - { - return; - } - ~FILE_output_streambuf() - { - return; - } -protected: - virtual int_type overflow(int_type ch) - { - if(!mpt::IO::IsValid(f)) - { - return traits_type::eof(); - } - if(traits_type::eq_int_type(ch, traits_type::eof())) - { - return traits_type::eof(); - } - char_type c = traits_type::to_char_type(ch); - if(!mpt::IO::WriteRaw(f, &c, 1)) - { - return traits_type::eof(); - } - return ch; - } - virtual int sync() - { - if(!mpt::IO::IsValid(f)) - { - return -1; - } - if(!mpt::IO::Flush(f)) - { - return -1; - } - return 0; - } - virtual pos_type seekpos(pos_type pos, std::ios_base::openmode which) - { - if(!mpt::IO::IsValid(f)) - { - return pos_type(off_type(-1)); - } - return seekoff(pos, std::ios_base::beg, which); - } - virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) - { - if(!mpt::IO::IsValid(f)) - { - return pos_type(off_type(-1)); - } - if(which & std::ios_base::in) - { - return pos_type(off_type(-1)); - } - if(!(which & std::ios_base::out)) - { - return pos_type(off_type(-1)); - } - mpt::IO::Offset oldpos = mpt::IO::TellWrite(f); - if(dir == std::ios_base::beg) - { - if(!mpt::IO::SeekAbsolute(f, off)) - { - mpt::IO::SeekAbsolute(f, oldpos); - return pos_type(off_type(-1)); - } - } else if(dir == std::ios_base::cur) - { - if(!mpt::IO::SeekRelative(f, off)) - { - mpt::IO::SeekAbsolute(f, oldpos); - return pos_type(off_type(-1)); - } - } else if(dir == std::ios_base::end) - { - if(!(mpt::IO::SeekEnd(f) && mpt::IO::SeekRelative(f, off))) - { - mpt::IO::SeekAbsolute(f, oldpos); - return pos_type(off_type(-1)); - } - } else - { - return pos_type(off_type(-1)); - } - mpt::IO::Offset newpos = mpt::IO::TellWrite(f); - if(!mpt::IO::OffsetFits<off_type>(newpos)) - { - mpt::IO::SeekAbsolute(f, oldpos); - return pos_type(off_type(-1)); - } - return pos_type(static_cast<off_type>(newpos)); - } -}; // class FILE_output_streambuf - - -class FILE_output_buffered_streambuf : public FILE_output_streambuf -{ -public: - typedef std::streambuf::char_type char_type; - typedef std::streambuf::traits_type traits_type; - typedef traits_type::int_type int_type; - typedef traits_type::pos_type pos_type; - typedef traits_type::off_type off_type; -private: - typedef FILE_output_streambuf Tparent; - std::vector<char_type> buf; -public: - FILE_output_buffered_streambuf(FILE *f, std::size_t bufSize = 64*1024) - : FILE_output_streambuf(f) - , buf((bufSize > 0) ? bufSize : 1) - { - setp(&buf[0], &buf[0] + buf.size()); - } - ~FILE_output_buffered_streambuf() - { - if(!mpt::IO::IsValid(f)) - { - return; - } - WriteOut(); - } -private: - bool IsDirty() const - { - return ((pptr() - pbase()) > 0); - } - bool WriteOut() - { - std::ptrdiff_t n = pptr() - pbase(); - std::ptrdiff_t left = n; - while(left > 0) - { - int backchunk = mpt::saturate_cast<int>(-left); - pbump(backchunk); - left += backchunk; - } - return mpt::IO::WriteRaw(f, pbase(), n); - } -protected: - virtual int_type overflow(int_type ch) - { - if(!mpt::IO::IsValid(f)) - { - return traits_type::eof(); - } - if(traits_type::eq_int_type(ch, traits_type::eof())) - { - return traits_type::eof(); - } - if(!WriteOut()) - { - return traits_type::eof(); - } - char_type c = traits_type::to_char_type(ch); - *pptr() = c; - pbump(1); - return ch; - } - virtual int sync() - { - if(!mpt::IO::IsValid(f)) - { - return -1; - } - if(!WriteOut()) - { - return -1; - } - return Tparent::sync(); - } - virtual pos_type seekpos(pos_type pos, std::ios_base::openmode which) - { - if(!mpt::IO::IsValid(f)) - { - return pos_type(off_type(-1)); - } - if(!WriteOut()) - { - return pos_type(off_type(-1)); - } - return Tparent::seekpos(pos, which); - } - virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) - { - if(!mpt::IO::IsValid(f)) - { - return pos_type(off_type(-1)); - } - if(!WriteOut()) - { - return pos_type(off_type(-1)); - } - return Tparent::seekoff(off, dir, which); - } -}; // class FILE_output_buffered_streambuf - - -class FILE_ostream : public std::ostream { -private: - FILE *f; - FILE_output_buffered_streambuf buf; -public: - FILE_ostream(FILE *f, std::size_t bufSize = 64*1024) - : std::ostream(&buf) - , f(f) - , buf(f, bufSize) - { - if(mpt::IO::IsValid(f)) mpt::IO::Flush(f); - } - ~FILE_ostream() - { - flush(); - buf.pubsync(); - if(mpt::IO::IsValid(f)) mpt::IO::Flush(f); - } -}; // class FILE_ostream - - -} // namespace mpt - - - -#ifdef MODPLUG_TRACKER -//=============== -class CMappedFile -//=============== -{ -protected: - HANDLE m_hFile; - HANDLE m_hFMap; - void *m_pData; - mpt::PathString m_FileName; - -public: - CMappedFile() : m_hFile(nullptr), m_hFMap(nullptr), m_pData(nullptr) { } - ~CMappedFile(); - -public: - bool Open(const mpt::PathString &filename); - bool IsOpen() const { return m_hFile != NULL && m_hFile != INVALID_HANDLE_VALUE; } - const mpt::PathString * GetpFilename() const { return &m_FileName; } - void Close(); - size_t GetLength(); - const void *Lock(); -}; -#endif // MODPLUG_TRACKER - - -//============= -class InputFile -//============= -{ -private: - mpt::PathString m_Filename; - #ifdef MPT_FILEREADER_STD_ISTREAM - mpt::ifstream m_File; - #else - CMappedFile m_File; - #endif -public: - InputFile(); - InputFile(const mpt::PathString &filename); - ~InputFile(); - bool Open(const mpt::PathString &filename); - bool IsValid() const; -#if defined(MPT_FILEREADER_STD_ISTREAM) - typedef std::pair<std::istream*, const mpt::PathString*> ContentsRef; -#else - struct Data - { - const char *data; - std::size_t size; - }; - typedef std::pair<InputFile::Data, const mpt::PathString*> ContentsRef; -#endif - InputFile::ContentsRef Get(); -}; - - -#endif // MPT_WITH_PATHSTRING - - -OPENMPT_NAMESPACE_END - Modified: trunk/OpenMPT/common/mptIO.cpp =================================================================== --- trunk/OpenMPT/common/mptIO.cpp 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/common/mptIO.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -254,224 +254,3 @@ OPENMPT_NAMESPACE_END - - - -// following ode should be moved to common/mptFileIO.cpp and common/mptFstream.h should be renamed to common/mptFileIO.h. - -#include "mptFstream.h" - - -OPENMPT_NAMESPACE_BEGIN - - -#ifdef MODPLUG_TRACKER - -CMappedFile::~CMappedFile() -//------------------------- -{ - Close(); -} - - -bool CMappedFile::Open(const mpt::PathString &filename) -//----------------------------------------------------- -{ - m_hFile = CreateFileW( - filename.AsNative().c_str(), - GENERIC_READ, - FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); - if(m_hFile == INVALID_HANDLE_VALUE) - { - m_hFile = nullptr; - return false; - } - m_FileName = filename; - return true; -} - - -void CMappedFile::Close() -//----------------------- -{ - m_FileName = mpt::PathString(); - // Unlock file - if(m_hFMap) - { - if(m_pData) - { - UnmapViewOfFile(m_pData); - m_pData = nullptr; - } - CloseHandle(m_hFMap); - m_hFMap = nullptr; - } else if(m_pData) - { - free(m_pData); - m_pData = nullptr; - } - - // Close file handle - if(m_hFile) - { - CloseHandle(m_hFile); - m_hFile = nullptr; - } -} - - -size_t CMappedFile::GetLength() -//----------------------------- -{ - LARGE_INTEGER size; - if(GetFileSizeEx(m_hFile, &size) == FALSE) - { - return 0; - } - return mpt::saturate_cast<size_t>(size.QuadPart); -} - - -const void *CMappedFile::Lock() -//----------------------------- -{ - size_t length = GetLength(); - if(!length) return nullptr; - - void *lpStream; - - HANDLE hmf = CreateFileMapping( - m_hFile, - NULL, - PAGE_READONLY, - 0, 0, - NULL); - - // Try memory-mapping first - if(hmf) - { - lpStream = MapViewOfFile( - hmf, - FILE_MAP_READ, - 0, 0, - length); - if(lpStream) - { - m_hFMap = hmf; - m_pData = lpStream; - return lpStream; - } - CloseHandle(hmf); - hmf = nullptr; - } - - // Fallback if memory-mapping fails for some weird reason - if((lpStream = malloc(length)) == nullptr) return nullptr; - memset(lpStream, 0, length); - size_t bytesToRead = length; - size_t bytesRead = 0; - while(bytesToRead > 0) - { - DWORD chunkToRead = mpt::saturate_cast<DWORD>(length); - DWORD chunkRead = 0; - if(ReadFile(m_hFile, (char*)lpStream + bytesRead, chunkToRead, &chunkRead, NULL) == FALSE) - { - // error - free(lpStream); - return nullptr; - } - bytesRead += chunkRead; - bytesToRead -= chunkRead; - } - m_pData = lpStream; - return lpStream; -} - - -#endif // MODPLUG_TRACKER - - - -#if defined(MPT_WITH_PATHSTRING) - -InputFile::InputFile() -{ - return; -} - -InputFile::InputFile(const mpt::PathString &filename) - : m_Filename(filename) -{ - #if defined(MPT_FILEREADER_STD_ISTREAM) - m_File.open(m_Filename, std::ios::binary | std::ios::in); - #else - m_File.Open(m_Filename); - #endif -} - -InputFile::~InputFile() -{ - return; -} - - -bool InputFile::Open(const mpt::PathString &filename) -{ - m_Filename = filename; - #if defined(MPT_FILEREADER_STD_ISTREAM) - m_File.open(m_Filename, std::ios::binary | std::ios::in); - return m_File.good(); - #else - return m_File.Open(m_Filename); - #endif -} - - -bool InputFile::IsValid() const -{ - #if defined(MPT_FILEREADER_STD_ISTREAM) - return m_File.good(); - #else - return m_File.IsOpen(); - #endif -} - -#if defined(MPT_FILEREADER_STD_ISTREAM) - -InputFile::ContentsRef InputFile::Get() -{ - InputFile::ContentsRef result; - result.first = &m_File; - result.second = m_File.good() ? &m_Filename : nullptr; - return result; -} - -#else - -InputFile::ContentsRef InputFile::Get() -{ - InputFile::ContentsRef result; - result.first.data = nullptr; - result.first.size = 0; - result.second = nullptr; - if(!m_File.IsOpen()) - { - return result; - } - result.first.data = reinterpret_cast<const char*>(m_File.Lock()); - result.first.size = m_File.GetLength(); - result.second = &m_Filename; - return result; -} - -#endif - -#endif // MPT_WITH_PATHSTRING - - - -OPENMPT_NAMESPACE_END Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2014-10-25 15:27:10 UTC (rev 4489) @@ -185,7 +185,7 @@ <ClInclude Include="..\common\Logging.h" /> <ClInclude Include="..\common\misc_util.h" /> <ClInclude Include="..\common\mptAtomic.h" /> - <ClInclude Include="..\common\mptFstream.h" /> + <ClInclude Include="..\common\mptFileIO.h" /> <ClInclude Include="..\common\mptIO.h" /> <ClInclude Include="..\common\mptPathString.h" /> <ClInclude Include="..\common\mptString.h" /> @@ -261,6 +261,7 @@ <ClCompile Include="..\common\ComponentManager.cpp" /> <ClCompile Include="..\common\Logging.cpp" /> <ClCompile Include="..\common\misc_util.cpp" /> + <ClCompile Include="..\common\mptFileIO.cpp" /> <ClCompile Include="..\common\mptIO.cpp" /> <ClCompile Include="..\common\mptPathString.cpp" /> <ClCompile Include="..\common\mptString.cpp" /> @@ -333,6 +334,7 @@ <ClCompile Include="..\soundlib\tuning.cpp" /> <ClCompile Include="..\soundlib\tuningbase.cpp" /> <ClCompile Include="..\soundlib\tuningCollection.cpp" /> + <ClCompile Include="..\soundlib\UpgradeModule.cpp" /> <ClCompile Include="..\soundlib\WAVTools.cpp" /> <ClCompile Include="..\soundlib\WindowedFIR.cpp" /> <ClCompile Include="..\soundlib\XMTools.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2014-10-25 15:27:10 UTC (rev 4489) @@ -224,9 +224,6 @@ <ClInclude Include="libopenmpt_stream_callbacks_file.h"> <Filter>Header Files</Filter> </ClInclude> - <ClInclude Include="..\common\mptFstream.h"> - <Filter>Header Files\common</Filter> - </ClInclude> <ClInclude Include="..\common\mptPathString.h"> <Filter>Header Files\common</Filter> </ClInclude> @@ -269,6 +266,9 @@ <ClInclude Include="..\common\ComponentManager.h"> <Filter>Header Files\common</Filter> </ClInclude> + <ClInclude Include="..\common\mptFileIO.h"> + <Filter>Header Files\common</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp"> @@ -526,5 +526,11 @@ <ClCompile Include="..\common\mptIO.cpp"> <Filter>Source Files\common</Filter> </ClCompile> + <ClCompile Include="..\common\mptFileIO.cpp"> + <Filter>Source Files\common</Filter> + </ClCompile> + <ClCompile Include="..\soundlib\UpgradeModule.cpp"> + <Filter>Source Files\soundlib</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj 2014-10-25 15:27:10 UTC (rev 4489) @@ -193,7 +193,7 @@ <ClInclude Include="..\common\Logging.h" /> <ClInclude Include="..\common\misc_util.h" /> <ClInclude Include="..\common\mptAtomic.h" /> - <ClInclude Include="..\common\mptFstream.h" /> + <ClInclude Include="..\common\mptFileIO.h" /> <ClInclude Include="..\common\mptIO.h" /> <ClInclude Include="..\common\mptPathString.h" /> <ClInclude Include="..\common\mptString.h" /> @@ -271,6 +271,7 @@ <ClCompile Include="..\common\ComponentManager.cpp" /> <ClCompile Include="..\common\Logging.cpp" /> <ClCompile Include="..\common\misc_util.cpp" /> + <ClCompile Include="..\common\mptFileIO.cpp" /> <ClCompile Include="..\common\mptIO.cpp" /> <ClCompile Include="..\common\mptPathString.cpp" /> <ClCompile Include="..\common\mptString.cpp" /> @@ -343,6 +344,7 @@ <ClCompile Include="..\soundlib\tuning.cpp" /> <ClCompile Include="..\soundlib\tuningbase.cpp" /> <ClCompile Include="..\soundlib\tuningCollection.cpp" /> + <ClCompile Include="..\soundlib\UpgradeModule.cpp" /> <ClCompile Include="..\soundlib\WAVTools.cpp" /> <ClCompile Include="..\soundlib\WindowedFIR.cpp" /> <ClCompile Include="..\soundlib\XMTools.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters 2014-10-25 15:27:10 UTC (rev 4489) @@ -230,9 +230,6 @@ <ClInclude Include="libopenmpt_stream_callbacks_file.h"> <Filter>Header Files</Filter> </ClInclude> - <ClInclude Include="..\common\mptFstream.h"> - <Filter>Header Files\common</Filter> - </ClInclude> <ClInclude Include="..\common\mptPathString.h"> <Filter>Header Files\common</Filter> </ClInclude> @@ -275,6 +272,9 @@ <ClInclude Include="..\common\ComponentManager.h"> <Filter>Header Files\common</Filter> </ClInclude> + <ClInclude Include="..\common\mptFileIO.h"> + <Filter>Header Files\common</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp"> @@ -532,5 +532,11 @@ <ClCompile Include="..\common\mptIO.cpp"> <Filter>Source Files\common</Filter> </ClCompile> + <ClCompile Include="..\common\mptFileIO.cpp"> + <Filter>Source Files\common</Filter> + </ClCompile> + <ClCompile Include="..\soundlib\UpgradeModule.cpp"> + <Filter>Source Files\soundlib</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj 2014-10-25 15:27:10 UTC (rev 4489) @@ -189,7 +189,7 @@ <ClInclude Include="..\common\Logging.h" /> <ClInclude Include="..\common\misc_util.h" /> <ClInclude Include="..\common\mptAtomic.h" /> - <ClInclude Include="..\common\mptFstream.h" /> + <ClInclude Include="..\common\mptFileIO.h" /> <ClInclude Include="..\common\mptIO.h" /> <ClInclude Include="..\common\mptPathString.h" /> <ClInclude Include="..\common\mptString.h" /> @@ -265,6 +265,7 @@ <ClCompile Include="..\common\ComponentManager.cpp" /> <ClCompile Include="..\common\Logging.cpp" /> <ClCompile Include="..\common\misc_util.cpp" /> + <ClCompile Include="..\common\mptFileIO.cpp" /> <ClCompile Include="..\common\mptIO.cpp" /> <ClCompile Include="..\common\mptPathString.cpp" /> <ClCompile Include="..\common\mptString.cpp" /> @@ -337,6 +338,7 @@ <ClCompile Include="..\soundlib\tuning.cpp" /> <ClCompile Include="..\soundlib\tuningbase.cpp" /> <ClCompile Include="..\soundlib\tuningCollection.cpp" /> + <ClCompile Include="..\soundlib\UpgradeModule.cpp" /> <ClCompile Include="..\soundlib\WAVTools.cpp" /> <ClCompile Include="..\soundlib\WindowedFIR.cpp" /> <ClCompile Include="..\soundlib\XMTools.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters 2014-10-25 15:27:10 UTC (rev 4489) @@ -224,9 +224,6 @@ <ClInclude Include="libopenmpt_stream_callbacks_file.h"> <Filter>Header Files</Filter> </ClInclude> - <ClInclude Include="..\common\mptFstream.h"> - <Filter>Header Files\common</Filter> - </ClInclude> <ClInclude Include="..\common\mptPathString.h"> <Filter>Header Files\common</Filter> </ClInclude> @@ -269,6 +266,9 @@ <ClInclude Include="..\common\ComponentManager.h"> <Filter>Header Files\common</Filter> </ClInclude> + <ClInclude Include="..\common\mptFileIO.h"> + <Filter>Header Files\common</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp"> @@ -529,5 +529,11 @@ <ClCompile Include="..\common\mptIO.cpp"> <Filter>Source Files\common</Filter> </ClCompile> + <ClCompile Include="..\common\mptFileIO.cpp"> + <Filter>Source Files\common</Filter> + </ClCompile> + <ClCompile Include="..\soundlib\UpgradeModule.cpp"> + <Filter>Source Files\soundlib</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/mptrack/Ctrl_ins.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_ins.cpp 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/Ctrl_ins.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -23,7 +23,8 @@ #include "../common/misc_util.h" #include "../common/StringFixer.h" #include "SelectPluginDialog.h" -#include "MemoryMappedFile.h" +#include "../common/mptFileIO.h" +#include "../soundlib/FileReader.h" #include "FileDialog.h" @@ -1422,11 +1423,11 @@ BOOL CCtrlInstruments::OpenInstrument(const mpt::PathString &fileName) //-------------------------------------------------------------------- { - CMappedFile f; BOOL bFirst, bOk; BeginWaitCursor(); - if(!f.Open(fileName)) + InputFile f(fileName); + if(!f.IsValid()) { EndWaitCursor(); return FALSE; Modified: trunk/OpenMPT/mptrack/Ctrl_smp.cpp =================================================================== --- trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/Ctrl_smp.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -27,7 +27,7 @@ #include "modsmp_ctrl.h" #include "Autotune.h" #include "../common/StringFixer.h" -#include "MemoryMappedFile.h" +#include "../common/mptFileIO.h" #include "../soundlib/FileReader.h" #include "../soundlib/SampleFormatConverters.h" #include "FileDialog.h" @@ -778,9 +778,9 @@ bool CCtrlSamples::OpenSample(const mpt::PathString &fileName) //------------------------------------------------------------ { - CMappedFile f; BeginWaitCursor(); - if(!f.Open(fileName)) + InputFile f(fileName); + if(!f.IsValid()) { EndWaitCursor(); return false; Modified: trunk/OpenMPT/mptrack/MainFrm.cpp =================================================================== --- trunk/OpenMPT/mptrack/MainFrm.cpp 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/MainFrm.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -34,7 +34,7 @@ #include "SelectPluginDialog.h" #include "ExceptionHandler.h" #include "PatternClipboard.h" -#include "MemoryMappedFile.h" +#include "../common/mptFileIO.h" #include "../soundlib/FileReader.h" #include "../common/Profiler.h" #include "FileDialog.h" @@ -1524,9 +1524,8 @@ if(!ok && !filename.empty()) { - CMappedFile f; - - if(f.Open(filename)) + InputFile f(filename); + if(f.IsValid()) { FileReader file = GetFileReader(f); if(file.IsValid()) Deleted: trunk/OpenMPT/mptrack/MemoryMappedFile.cpp =================================================================== --- trunk/OpenMPT/mptrack/MemoryMappedFile.cpp 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/MemoryMappedFile.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -1,20 +0,0 @@ -/* - * MemoryMappedFile.cpp - * -------------------- - * Purpose: Wrapper class for memory-mapped files - * Notes : (currently none) - * Authors: OpenMPT Devs - * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. - */ - - -#include "stdafx.h" -#include "MemoryMappedFile.h" - - -OPENMPT_NAMESPACE_BEGIN - - - - -OPENMPT_NAMESPACE_END Deleted: trunk/OpenMPT/mptrack/MemoryMappedFile.h =================================================================== --- trunk/OpenMPT/mptrack/MemoryMappedFile.h 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/MemoryMappedFile.h 2014-10-25 15:27:10 UTC (rev 4489) @@ -1,18 +0,0 @@ -/* - * MemoryMappedFile.h - * ------------------ - * Purpose: Header file for memory-mapped file wrapper - * Notes : (currently none) - * Authors: OpenMPT Devs - * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. - */ - - -#pragma once - -#include "../soundlib/FileReader.h" -#include "../common/mptFstream.h" - -OPENMPT_NAMESPACE_BEGIN - -OPENMPT_NAMESPACE_END Modified: trunk/OpenMPT/mptrack/Moddoc.cpp =================================================================== --- trunk/OpenMPT/mptrack/Moddoc.cpp 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/Moddoc.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -29,11 +29,7 @@ #include "modsmp_ctrl.h" #include "CleanupSong.h" #include "../common/StringFixer.h" -#if defined(MPT_FILEREADER_STD_ISTREAM) -#include "../common/mptFstream.h" -#else -#include "MemoryMappedFile.h" -#endif +#include "../common/mptFileIO.h" #include "../soundlib/FileReader.h" #include <shlwapi.h> #include "FileDialog.h" Modified: trunk/OpenMPT/mptrack/View_tre.cpp =================================================================== --- trunk/OpenMPT/mptrack/View_tre.cpp 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/View_tre.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -18,7 +18,7 @@ #include "Dlsbank.h" #include "dlg_misc.h" #include "vstplug.h" -#include "MemoryMappedFile.h" +#include "../common/mptFileIO.h" #include "../soundlib/FileReader.h" #include "FileDialog.h" #include "Globals.h" @@ -360,9 +360,8 @@ if(!songName.empty() && mpt::PathString::CompareNoCase(m_SongFileName, songName)) { // Load module for previewing its instruments - CMappedFile f; - - if(f.Open(libPath + songName)) + InputFile f(libPath + songName); + if(f.IsValid()) { FileReader file = GetFileReader(f); if(file.IsValid()) Modified: trunk/OpenMPT/mptrack/Vstplug.cpp =================================================================== --- trunk/OpenMPT/mptrack/Vstplug.cpp 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/Vstplug.cpp 2014-10-25 15:27:10 UTC (rev 4489) @@ -23,9 +23,9 @@ #include "../soundlib/MIDIEvents.h" #include "MIDIMappingDialog.h" #include "../common/StringFixer.h" -#include "MemoryMappedFile.h" +#include "../common/mptFileIO.h" +#include "../soundlib/FileReader.h" #include "FileDialog.h" -#include "../common/mptFstream.h" OPENMPT_NAMESPACE_BEGIN @@ -1044,9 +1044,9 @@ fileName = dlg.GetFirstFile(); } - CMappedFile f; const char *errorStr = nullptr; - if(f.Open(fileName)) + InputFile f(fileName); + if(f.IsValid()) { FileReader file = GetFileReader(f); errorStr = VSTPresets::GetErrorMessage(VSTPresets::LoadFile(file, *this)); Modified: trunk/OpenMPT/mptrack/mptrack_08.vcproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_08.vcproj 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/mptrack_08.vcproj 2014-10-25 15:27:10 UTC (rev 4489) @@ -572,10 +572,6 @@ > </File> <File - RelativePath=".\MemoryMappedFile.cpp" - > - </File> - <File RelativePath="..\soundlib\Message.cpp" > </File> @@ -660,6 +656,10 @@ > </File> <File + RelativePath="..\common\mptFileIO.cpp" + > + </File> + <File RelativePath=".\MPTHacks.cpp" > </File> @@ -868,6 +868,10 @@ > </File> <File + RelativePath="..\soundlib\UpgradeModule.cpp" + > + </File> + <File RelativePath="..\common\version.cpp" > </File> @@ -1270,10 +1274,6 @@ > </File> <File - RelativePath=".\MemoryMappedFile.h" - > - </File> - <File RelativePath="..\soundlib\MIDIEvents.h" > </File> @@ -1354,7 +1354,7 @@ > </File> <File - RelativePath="..\common\mptFstream.h" + RelativePath="..\common\mptFileIO.h" > </File> <File Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2014-10-25 12:52:24 UTC (rev 4488) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2014-10-25 15:27:10 UTC (rev 4489) @@ -728,6 +728,7 @@ <ClCompile Include="..\common\ComponentManager.cpp" /> <ClCompile Include="..\common\Logging.cpp" /> <ClCompile Include="..\common\misc_util.cpp" /> + <ClCompile Include="..\common\mptFileIO.cpp" /> <ClCompile Include="..\common\mptIO.cpp" /> <ClCompile Include="..\common\mptPathString.cpp" /> <ClCompile Include="..\common\mptString.cpp" /> @@ -791,6 +792,7 @@ <ClCompile Include="..\soundlib\SoundFilePlayConfig.cpp" /> <ClCompile Include="..\soundlib\Tables.cpp" /> <ClCompile Include="..\soundlib\Tagging.cpp" /> + <ClCompile Include="..\soundlib\UpgradeModule.cpp" /> <ClCompile Include="..\soundlib\WAVTools.cpp" /> <ClCompile Include="..\soundlib\WindowedFIR.cpp" /> <ClCompile Include="..\soundlib\XMTools.cpp" /> @@ -832,7 +834,6 @@ <ClCompile Include="KeyConfigDlg.cpp" /> <ClCompile Include="Mainbar.cpp" /> <ClCompile Include="MainFrm.cpp" /> - <ClCompile Include="MemoryMappedFile.cpp" /> <ClCompile Include="MIDIMacroDialog.cpp" /> <ClCompile Include="MIDIMapping.cpp" /> <ClCompile Include="MIDIMappingDialog.cpp" /> @@ -942,7 +943,7 @@ <ClInclude Include="..\common\Logging.h" /> <ClInclude Include="..\common\misc_util.h" /> <ClInclude Include="..\common\mptAtomic.h" /> - <ClInclude Include="..\common\mptFstream.h" /> + <ClInclude Include="..\common\mptFileIO.h" /> <ClInclude Include="..\common\mptIO.h" /> <ClInclude Include="..\common\mptPathString.h" /> <ClInclude Include="..\common\mptString.h" /> @@ -1045,7 +1046,6 @@ <ClInclude Include="InputHandler.h" /> <ClInclude Include="Mainbar.h" /> <ClInclude Include="Mainfrm.h" /> ... [truncated message content] |