From: <man...@us...> - 2013-11-11 23:33:43
|
Revision: 3189 http://sourceforge.net/p/modplug/code/3189 Author: manxorist Date: 2013-11-11 23:33:33 +0000 (Mon, 11 Nov 2013) Log Message: ----------- [Ref] Use mpt::fstream wrappers around std::fstream for all compilers, thereby enforcing the usage of mpt::PathString. Modified Paths: -------------- trunk/OpenMPT/common/mptFstream.h trunk/OpenMPT/mptrack/CommandSet.cpp trunk/OpenMPT/mptrack/Mod2wave.cpp trunk/OpenMPT/mptrack/Settings.cpp trunk/OpenMPT/mptrack/TuningDialog.cpp trunk/OpenMPT/mptrack/Vstplug.cpp trunk/OpenMPT/soundlib/Load_it.cpp trunk/OpenMPT/soundlib/tuningCollection.cpp trunk/OpenMPT/test/test.cpp Modified: trunk/OpenMPT/common/mptFstream.h =================================================================== --- trunk/OpenMPT/common/mptFstream.h 2013-11-11 16:00:18 UTC (rev 3188) +++ trunk/OpenMPT/common/mptFstream.h 2013-11-11 23:33:33 UTC (rev 3189) @@ -1,7 +1,7 @@ /* * mptFstream.h * ------------ - * Purpose: A wrapper around std::fstream, fixing VS2008 charset conversion braindamage. + * 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. @@ -11,9 +11,8 @@ #include <fstream> -#if MPT_COMPILER_MSVC && MPT_MSVC_BEFORE(2010,0) #include "../common/mptString.h" -#endif +#include "../common/mptPathString.h" namespace mpt { @@ -24,168 +23,160 @@ // 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 + +#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) +{ + base.open(filename.AsNative().c_str(), mode); +} + +#ifdef MPT_FSTREAM_DO_CONVERSIONS + +template<typename Tbase> +inline void fstream_open(Tbase & base, const std::wstring & filename, std::ios_base::openmode mode) +{ + base.open(filename.c_str(), mode); +} + +template<typename Tbase> +inline void fstream_open(Tbase & base, const wchar_t * filename, std::ios_base::openmode mode) +{ + base.open(filename, 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::String::Decode(filename, mpt::CharsetLocale), mode); +} + +template<typename Tbase> +inline void fstream_open(Tbase & base, const char * filename, std::ios_base::openmode mode) +{ + detail::fstream_open<Tbase>(base, filename ? std::string(filename) : std::string(), mode); +} + +#endif + +} // namespace detail + class fstream : public std::fstream { private: - void mptopen(const std::wstring & filename, std::ios_base::openmode mode) - { - std::fstream::open(filename.c_str(), mode); - } - void mptopen(const std::string & filename, std::ios_base::openmode mode) - { - mptopen(mpt::String::Decode(filename, mpt::CharsetLocale).c_str(), mode); - } - void mptopen(const char * filename, std::ios_base::openmode mode) - { - mptopen(filename ? std::string(filename) : std::string(), mode); - } + typedef std::fstream Tbase; public: fstream() {} - fstream(const char * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) + fstream(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) { - mptopen(filename, mode); + detail::fstream_open<Tbase>(*this, filename, mode); } - fstream(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) + void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) { - mptopen(filename, mode); + detail::fstream_open<Tbase>(*this, filename, mode); } - fstream(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) + MPT_DEPRECATED_PATH void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename, mode); } - fstream(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) + MPT_DEPRECATED_PATH void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename.c_str(), mode); } - void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) +#if defined(WIN32) + MPT_DEPRECATED_PATH void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename, mode); } - void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) + MPT_DEPRECATED_PATH void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename.c_str(), mode); } - void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) - { - mptopen(filename, mode); - } - void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) - { - mptopen(filename, mode); - } +#endif }; class ifstream : public std::ifstream { private: - void mptopen(const std::wstring & filename, std::ios_base::openmode mode) - { - std::ifstream::open(filename.c_str(), mode); - } - void mptopen(const std::string & filename, std::ios_base::openmode mode) - { - mptopen(mpt::String::Decode(filename, mpt::CharsetLocale).c_str(), mode); - } - void mptopen(const char * filename, std::ios_base::openmode mode) - { - mptopen(filename ? std::string(filename) : std::string(), mode); - } + typedef std::ifstream Tbase; public: ifstream() {} - ifstream(const char * filename, std::ios_base::openmode mode = std::ios_base::in) + ifstream(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in) { - mptopen(filename, mode); + detail::fstream_open<Tbase>(*this, filename, mode); } - ifstream(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in) + void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in) { - mptopen(filename, mode); + detail::fstream_open<Tbase>(*this, filename, mode); } - ifstream(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in) + MPT_DEPRECATED_PATH void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename, mode); } - ifstream(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in) + MPT_DEPRECATED_PATH void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename.c_str(), mode); } - void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in) +#if defined(WIN32) + MPT_DEPRECATED_PATH void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename, mode); } - void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in) + MPT_DEPRECATED_PATH void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename.c_str(), mode); } - void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in) - { - mptopen(filename, mode); - } - void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in) - { - mptopen(filename, mode); - } +#endif }; class ofstream : public std::ofstream { private: - void mptopen(const std::wstring & filename, std::ios_base::openmode mode) - { - std::ofstream::open(filename.c_str(), mode); - } - void mptopen(const std::string & filename, std::ios_base::openmode mode) - { - mptopen(mpt::String::Decode(filename, mpt::CharsetLocale).c_str(), mode); - } - void mptopen(const char * filename, std::ios_base::openmode mode) - { - mptopen(filename ? std::string(filename) : std::string(), mode); - } + typedef std::ofstream Tbase; public: ofstream() {} - ofstream(const char * filename, std::ios_base::openmode mode = std::ios_base::out) + ofstream(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::out) { - mptopen(filename, mode); + detail::fstream_open<Tbase>(*this, filename, mode); } - ofstream(const std::string & filename, std::ios_base::openmode mode = std::ios_base::out) + void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::out) { - mptopen(filename, mode); + detail::fstream_open<Tbase>(*this, filename, mode); } - ofstream(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::out) + MPT_DEPRECATED_PATH void open(const char * filename, std::ios_base::openmode mode = std::ios_base::out) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename, mode); } - ofstream(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::out) + MPT_DEPRECATED_PATH void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::out) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename.c_str(), mode); } - void open(const char * filename, std::ios_base::openmode mode = std::ios_base::out) +#if defined(WIN32) + MPT_DEPRECATED_PATH void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::out) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename, mode); } - void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::out) + MPT_DEPRECATED_PATH void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::out) { - mptopen(filename, mode); + MPT_FSTREAM_OPEN(filename.c_str(), mode); } - void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::out) - { - mptopen(filename, mode); - } - void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::out) - { - mptopen(filename, mode); - } +#endif }; -#else +#undef MPT_FSTREAM_OPEN -typedef std::fstream fstream; -typedef std::ifstream ifstream; -typedef std::ofstream ofstream; - -#endif - } // namespace mpt Modified: trunk/OpenMPT/mptrack/CommandSet.cpp =================================================================== --- trunk/OpenMPT/mptrack/CommandSet.cpp 2013-11-11 16:00:18 UTC (rev 3188) +++ trunk/OpenMPT/mptrack/CommandSet.cpp 2013-11-11 23:33:33 UTC (rev 3189) @@ -1679,7 +1679,7 @@ bool CCommandSet::LoadFile(const mpt::PathString &filename) //--------------------------------------------------------- { - mpt::ifstream fin(filename.AsNative().c_str()); + mpt::ifstream fin(filename); if (fin.fail()) { Reporting::Warning(L"Can't open keybindings file " + filename.ToWide() + L" for reading. Default keybindings will be used."); Modified: trunk/OpenMPT/mptrack/Mod2wave.cpp =================================================================== --- trunk/OpenMPT/mptrack/Mod2wave.cpp 2013-11-11 16:00:18 UTC (rev 3188) +++ trunk/OpenMPT/mptrack/Mod2wave.cpp 2013-11-11 23:33:33 UTC (rev 3189) @@ -879,10 +879,10 @@ mpt::fstream normalizeFile; if(m_Settings.Normalize) { - normalizeFile.open(normalizeFileName.AsNative().c_str(), std::ios::binary | std::ios::in | std::ios::out | std::ios::trunc); + normalizeFile.open(normalizeFileName, std::ios::binary | std::ios::in | std::ios::out | std::ios::trunc); } - mpt::ofstream fileStream(m_lpszFileName.AsNative().c_str(), std::ios::binary | std::ios::trunc); + mpt::ofstream fileStream(m_lpszFileName, std::ios::binary | std::ios::trunc); if(!fileStream) { Modified: trunk/OpenMPT/mptrack/Settings.cpp =================================================================== --- trunk/OpenMPT/mptrack/Settings.cpp 2013-11-11 16:00:18 UTC (rev 3188) +++ trunk/OpenMPT/mptrack/Settings.cpp 2013-11-11 23:33:33 UTC (rev 3189) @@ -434,7 +434,7 @@ static std::vector<char> ReadFile(const mpt::PathString &filename) { - mpt::ifstream s(filename.AsNative().c_str(), std::ios::binary); + mpt::ifstream s(filename, std::ios::binary); std::vector<char> result; while(s) { @@ -449,7 +449,7 @@ static void WriteFileUTF16LE(const mpt::PathString &filename, const std::wstring &str) { STATIC_ASSERT(sizeof(wchar_t) == 2); - mpt::ofstream inifile(filename.AsNative().c_str(), std::ios::binary | std::ios::trunc); + mpt::ofstream inifile(filename, std::ios::binary | std::ios::trunc); const uint8 UTF16LE_BOM[] = { 0xff, 0xfe }; inifile.write(reinterpret_cast<const char*>(UTF16LE_BOM), 2); inifile.write(reinterpret_cast<const char*>(str.c_str()), str.length() * sizeof(std::wstring::value_type)); Modified: trunk/OpenMPT/mptrack/TuningDialog.cpp =================================================================== --- trunk/OpenMPT/mptrack/TuningDialog.cpp 2013-11-11 16:00:18 UTC (rev 3188) +++ trunk/OpenMPT/mptrack/TuningDialog.cpp 2013-11-11 23:33:33 UTC (rev 3189) @@ -643,7 +643,7 @@ bool failure = true; - mpt::ofstream fout(dlg.GetFirstFile().AsNative().c_str(), std::ios::binary); + mpt::ofstream fout(dlg.GetFirstFile(), std::ios::binary); if(filterIndex == 0) { @@ -702,7 +702,7 @@ if (bIsTun) { - mpt::ifstream fin(files[counter].AsNative().c_str(), std::ios::binary); + mpt::ifstream fin(files[counter], std::ios::binary); pT = CTuningRTI::DeserializeOLD(fin); if(pT == 0) {fin.clear(); fin.seekg(0); pT = CTuningRTI::Deserialize(fin);} @@ -1467,7 +1467,7 @@ CTuningDialog::EnSclImport CTuningDialog::ImportScl(const mpt::PathString &filename, LPCTSTR pszName) //--------------------------------------------------------------------------------------------------- { - mpt::ifstream iStrm(filename.AsNative().c_str(), std::ios::in | std::ios::binary); + mpt::ifstream iStrm(filename, std::ios::in | std::ios::binary); if(!iStrm) { return enSclImportFailUnableToOpenFile; Modified: trunk/OpenMPT/mptrack/Vstplug.cpp =================================================================== --- trunk/OpenMPT/mptrack/Vstplug.cpp 2013-11-11 16:00:18 UTC (rev 3188) +++ trunk/OpenMPT/mptrack/Vstplug.cpp 2013-11-11 23:33:33 UTC (rev 3189) @@ -1609,7 +1609,7 @@ bool bank = (dlg.GetExtension() == MPT_PATHSTRING("fxb")); - mpt::fstream f(dlg.GetFirstFile().AsNative().c_str(), std::ios::out | std::ios::trunc | std::ios::binary); + mpt::fstream f(dlg.GetFirstFile(), std::ios::out | std::ios::trunc | std::ios::binary); if(f.good() && VSTPresets::SaveFile(f, *this, bank)) { return true; Modified: trunk/OpenMPT/soundlib/Load_it.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_it.cpp 2013-11-11 16:00:18 UTC (rev 3188) +++ trunk/OpenMPT/soundlib/Load_it.cpp 2013-11-11 23:33:33 UTC (rev 3189) @@ -1629,7 +1629,7 @@ // Using std::ios::in | std::ios::out | std::ios::ate prevents truncation and sets the file pointer to the end of the so-far saved file, // but it also allows reading, which does no harm but is not what we actually really want here. // That's a very broken interface. - mpt::ofstream fout(filename.AsNative().c_str(), std::ios::binary | std::ios::ate | std::ios::in); + mpt::ofstream fout(filename, std::ios::binary | std::ios::ate | std::ios::in); #endif const uint32 MPTStartPos = (uint32)fout.tellp(); Modified: trunk/OpenMPT/soundlib/tuningCollection.cpp =================================================================== --- trunk/OpenMPT/soundlib/tuningCollection.cpp 2013-11-11 16:00:18 UTC (rev 3188) +++ trunk/OpenMPT/soundlib/tuningCollection.cpp 2013-11-11 23:33:33 UTC (rev 3189) @@ -124,7 +124,7 @@ { if(m_SavefilePath.empty()) return SERIALIZATION_FAILURE; - mpt::ofstream fout(m_SavefilePath.AsNative().c_str(), std::ios::binary); + mpt::ofstream fout(m_SavefilePath, std::ios::binary); if(!fout.good()) return SERIALIZATION_FAILURE; @@ -139,7 +139,7 @@ { if(m_SavefilePath.empty()) return SERIALIZATION_FAILURE; - mpt::ifstream fin(m_SavefilePath.AsNative().c_str(), std::ios::binary); + mpt::ifstream fin(m_SavefilePath, std::ios::binary); if(!fin.good()) return SERIALIZATION_FAILURE; Modified: trunk/OpenMPT/test/test.cpp =================================================================== --- trunk/OpenMPT/test/test.cpp 2013-11-11 16:00:18 UTC (rev 3188) +++ trunk/OpenMPT/test/test.cpp 2013-11-11 23:33:33 UTC (rev 3189) @@ -1472,7 +1472,7 @@ static TSoundFileContainer CreateSoundFileContainer(const mpt::PathString &filename) { - mpt::ifstream stream(filename.AsNative().c_str(), std::ios::binary); + mpt::ifstream stream(filename, std::ios::binary); FileReader file(&stream); std::shared_ptr<CSoundFile> pSndFile(new CSoundFile()); pSndFile->Create(file, CSoundFile::loadCompleteModule); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |