From: <man...@us...> - 2013-11-07 19:19:22
|
Revision: 3113 http://sourceforge.net/p/modplug/code/3113 Author: manxorist Date: 2013-11-07 19:19:11 +0000 (Thu, 07 Nov 2013) Log Message: ----------- [Ref] Make PathString an explicit type instead of a typedef. This allows for stricter type checking. Modified Paths: -------------- trunk/OpenMPT/common/mptString.h trunk/OpenMPT/mptrack/Moptions.cpp trunk/OpenMPT/mptrack/Mptrack.cpp trunk/OpenMPT/mptrack/Settings.cpp Modified: trunk/OpenMPT/common/mptString.h =================================================================== --- trunk/OpenMPT/common/mptString.h 2013-11-07 16:00:36 UTC (rev 3112) +++ trunk/OpenMPT/common/mptString.h 2013-11-07 19:19:11 UTC (rev 3113) @@ -160,24 +160,123 @@ } // namespace String + #if defined(WIN32) -typedef std::wstring PathString; -#define MPT_PATHSTRING(lit) L##lit -static inline std::string PathToLocale(const PathString &path) { return mpt::String::Encode(path, mpt::CharsetLocale); } -static inline std::string PathToUTF8(const PathString &path) { return mpt::String::Encode(path, mpt::CharsetUTF8); } -static inline std::wstring PathToWide(const PathString &path) { return path; } -static inline PathString LocaleToPath(const std::string &path) { return mpt::String::Decode(path, mpt::CharsetLocale); } -static inline PathString UTF8ToPath(const std::string &path) { return mpt::String::Decode(path, mpt::CharsetUTF8); } -static inline PathString WideToPath(const std::wstring &path) { return path; } + +typedef std::wstring RawPathString; + +class PathString +{ +private: + RawPathString path; +private: + PathString(const RawPathString & path) + : path(path) + { + return; + } +public: + PathString() + { + return; + } + PathString(const PathString & other) + : path(other.path) + { + return; + } + PathString & assign(const PathString & other) + { + path = other.path; + return *this; + } + PathString & operator = (const PathString & other) + { + return assign(other); + } + PathString & append(const PathString & other) + { + path.append(other.path); + return *this; + } + PathString & operator += (const PathString & other) + { + return append(other); + } + friend PathString operator + (const PathString & a, const PathString & b) + { + return PathString(a).append(b); + } +public: + // conversions + std::string ToLocale() const { return mpt::String::Encode(path, mpt::CharsetLocale); } + std::string ToUTF8() const { return mpt::String::Encode(path, mpt::CharsetUTF8); } + std::wstring ToWide() const { return path; } + static PathString FromLocale(const std::string &path) { return PathString(mpt::String::Decode(path, mpt::CharsetLocale)); } + static PathString FromUTF8(const std::string &path) { return PathString(mpt::String::Decode(path, mpt::CharsetUTF8)); } + static PathString FromWide(const std::wstring &path) { return PathString(path); } + RawPathString AsNative() const { return path; } + static PathString FromNative(const RawPathString &path) { return PathString(path); } +}; + #else // !WIN32 -typedef std::string PathString; -#define MPT_PATHSTRING(lit) lit -static inline std::string PathToLocale(const PathString &path) { return path; } -static inline std::string PathToUTF8(const PathString &path) { return mpt::String::Convert(path, mpt::CharsetLocale, mpt::CharsetUTF8); } -static inline std::wstring PathToWide(const PathString &path) { return mpt::String::Decode(path, mpt::CharsetLocale); } -static inline PathString LocaleToPath(const std::string &path) { return path; } -static inline PathString UTF8ToPath(const std::string &path) { return mpt::String::Convert(path, mpt::CharsetUTF8, mpt::CharsetLocale); } -static inline PathString WideToPath(const std::wstring &path) { return mpt::String::Encode(path, mpt::CharsetLocale); } + +typedef std::string RawPathString; + +class PathString +{ +private: + RawPathString path; +private: + PathString(const RawPathString & path) + : path(path) + { + return; + } +public: + PathString() + { + return; + } + PathString(const PathString & other) + : path(other.path) + { + return; + } + PathString & assign(const PathString & other) + { + path = other.path; + return *this; + } + PathString & operator = (const PathString & other) + { + return assign(other); + } + PathString & append(const PathString & other) + { + path.append(other.path); + return *this; + } + PathString & operator += (const PathString & other) + { + return append(other); + } + friend PathString operator + (const PathString & a, const PathString & b) + { + return PathString(a).append(b); + } +public: + // conversions + std::string ToLocale() const { return path; } + std::string ToUTF8() const { return mpt::String::Convert(path, mpt::CharsetLocale, mpt::CharsetUTF8); } + std::wstring ToWide() const { return mpt::String::Decode(path, mpt::CharsetLocale); } + static PathString FromLocale(const std::string &path) { return PathString(path); } + static PathString FromUTF8(const std::string &path) { return PathString(mpt::String::Convert(path, mpt::CharsetUTF8, mpt::CharsetLocale)); } + static PathString FromWide(const std::wstring &path) { return PathString(mpt::String::Encode(path, mpt::CharsetLocale)); } + RawPathString AsNative() const { return path; } + static PathString FromNative(const RawPathString &path) { return PathString(path); } +}; + #endif // WIN32 Modified: trunk/OpenMPT/mptrack/Moptions.cpp =================================================================== --- trunk/OpenMPT/mptrack/Moptions.cpp 2013-11-07 16:00:36 UTC (rev 3112) +++ trunk/OpenMPT/mptrack/Moptions.cpp 2013-11-07 19:19:11 UTC (rev 3113) @@ -495,7 +495,7 @@ // Ensure that all colours are reset (for outdated colour schemes) OnPresetMPT(); { - IniFileSettingsContainer file(mpt::LocaleToPath(files.first_file)); + IniFileSettingsContainer file(mpt::PathString::FromLocale(files.first_file)); for(int i = 0; i < MAX_MODCOLORS; i++) { TCHAR sKeyName[16]; @@ -515,7 +515,7 @@ if(files.abort) return; { - IniFileSettingsContainer file(mpt::LocaleToPath(files.first_file)); + IniFileSettingsContainer file(mpt::PathString::FromLocale(files.first_file)); for(int i = 0; i < MAX_MODCOLORS; i++) { TCHAR sKeyName[16]; Modified: trunk/OpenMPT/mptrack/Mptrack.cpp =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.cpp 2013-11-07 16:00:36 UTC (rev 3112) +++ trunk/OpenMPT/mptrack/Mptrack.cpp 2013-11-07 19:19:11 UTC (rev 3113) @@ -350,7 +350,7 @@ return TRUE; } - IniFileSettingsContainer file(mpt::LocaleToPath(lpszConfigFile)); + IniFileSettingsContainer file(mpt::PathString::FromLocale(lpszConfigFile)); return ImportMidiConfig(file); } @@ -417,7 +417,7 @@ //----------------------------------------------------- { if ((!glpMidiLibrary) || (!lpszConfigFile) || (!lpszConfigFile[0])) return FALSE; - IniFileSettingsContainer file(mpt::LocaleToPath(lpszConfigFile)); + IniFileSettingsContainer file(mpt::PathString::FromLocale(lpszConfigFile)); return ExportMidiConfig(file); } @@ -839,13 +839,13 @@ // Construct auto saver instance, class TrackerSettings expects it being available. CMainFrame::m_pAutoSaver = new CAutoSaver(); - m_pSettingsIniFile = new IniFileSettingsBackend(mpt::LocaleToPath(m_szConfigFileName)); + m_pSettingsIniFile = new IniFileSettingsBackend(mpt::PathString::FromLocale(m_szConfigFileName)); m_pSettings = new SettingsContainer(m_pSettingsIniFile); m_pTrackerSettings = new TrackerSettings(*m_pSettings); - m_pPluginCache = new IniFileSettingsContainer(mpt::LocaleToPath(m_szPluginCacheFileName)); + m_pPluginCache = new IniFileSettingsContainer(mpt::PathString::FromLocale(m_szPluginCacheFileName)); int mruListLength = GetSettings().Read<int32>("Misc", "MRUListLength", 10); Limit(mruListLength, 0, 15); Modified: trunk/OpenMPT/mptrack/Settings.cpp =================================================================== --- trunk/OpenMPT/mptrack/Settings.cpp 2013-11-07 16:00:36 UTC (rev 3112) +++ trunk/OpenMPT/mptrack/Settings.cpp 2013-11-07 19:19:11 UTC (rev 3113) @@ -331,14 +331,14 @@ std::vector<char> IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, const std::vector<char> &def) const { std::vector<char> result = def; - ::GetPrivateProfileStructW(GetSection(path).c_str(), GetKey(path).c_str(), &result[0], result.size(), filename.c_str()); + ::GetPrivateProfileStructW(GetSection(path).c_str(), GetKey(path).c_str(), &result[0], result.size(), filename.AsNative().c_str()); return result; } std::wstring IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, const std::wstring &def) const { std::vector<WCHAR> buf(128); - while(::GetPrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), def.c_str(), &buf[0], buf.size(), filename.c_str()) == buf.size() - 1) + while(::GetPrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), def.c_str(), &buf[0], buf.size(), filename.AsNative().c_str()) == buf.size() - 1) { buf.resize(buf.size() * 2); } @@ -348,7 +348,7 @@ double IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, double def) const { std::vector<WCHAR> buf(128); - while(::GetPrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), StringifyW(def).c_str(), &buf[0], buf.size(), filename.c_str()) == buf.size() - 1) + while(::GetPrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), StringifyW(def).c_str(), &buf[0], buf.size(), filename.AsNative().c_str()) == buf.size() - 1) { buf.resize(buf.size() * 2); } @@ -357,23 +357,23 @@ int32 IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, int32 def) const { - return (int32)::GetPrivateProfileIntW(GetSection(path).c_str(), GetKey(path).c_str(), (UINT)def, filename.c_str()); + return (int32)::GetPrivateProfileIntW(GetSection(path).c_str(), GetKey(path).c_str(), (UINT)def, filename.AsNative().c_str()); } bool IniFileSettingsBackend::ReadSettingRaw(const SettingPath &path, bool def) const { - return ::GetPrivateProfileIntW(GetSection(path).c_str(), GetKey(path).c_str(), def?1:0, filename.c_str()) ? true : false; + return ::GetPrivateProfileIntW(GetSection(path).c_str(), GetKey(path).c_str(), def?1:0, filename.AsNative().c_str()) ? true : false; } void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, const std::vector<char> &val) { - ::WritePrivateProfileStructW(GetSection(path).c_str(), GetKey(path).c_str(), (LPVOID)&val[0], val.size(), filename.c_str()); + ::WritePrivateProfileStructW(GetSection(path).c_str(), GetKey(path).c_str(), (LPVOID)&val[0], val.size(), filename.AsNative().c_str()); } void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, const std::wstring &val) { - ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), val.c_str(), filename.c_str()); + ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), val.c_str(), filename.AsNative().c_str()); if(mpt::String::Decode(mpt::String::Encode(val, mpt::CharsetLocale), mpt::CharsetLocale) != val) { @@ -384,29 +384,29 @@ // The ini file is probably ANSI encoded. ConvertToUnicode(); // Re-write non-ansi-representable value. - ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), val.c_str(), filename.c_str()); + ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), val.c_str(), filename.AsNative().c_str()); } } } void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, double val) { - ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), StringifyW(val).c_str(), filename.c_str()); + ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), StringifyW(val).c_str(), filename.AsNative().c_str()); } void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, int32 val) { - ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), StringifyW(val).c_str(), filename.c_str()); + ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), StringifyW(val).c_str(), filename.AsNative().c_str()); } void IniFileSettingsBackend::WriteSettingRaw(const SettingPath &path, bool val) { - ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), StringifyW(val?1:0).c_str(), filename.c_str()); + ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), StringifyW(val?1:0).c_str(), filename.AsNative().c_str()); } void IniFileSettingsBackend::RemoveSettingRaw(const SettingPath &path) { - ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), NULL, filename.c_str()); + ::WritePrivateProfileStringW(GetSection(path).c_str(), GetKey(path).c_str(), NULL, filename.AsNative().c_str()); } @@ -434,7 +434,7 @@ static std::vector<char> ReadFile(const mpt::PathString &filename) { - mpt::ifstream s(filename.c_str(), std::ios::binary); + mpt::ifstream s(filename.AsNative().c_str(), 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.c_str(), std::ios::binary | std::ios::trunc); + mpt::ofstream inifile(filename.AsNative().c_str(), 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)); @@ -469,11 +469,8 @@ { return; } - const std::wstring backupFilename = backupTag.empty() - ? filename + L".ansi.bak" - : filename + L".ansi." + backupTag + L".bak" - ; - CopyFileW(filename.c_str(), backupFilename.c_str(), FALSE); + const mpt::PathString backupFilename = filename + mpt::PathString::FromWide(backupTag.empty() ? L".ansi.bak" : L".ansi." + backupTag + L".bak"); + CopyFileW(filename.AsNative().c_str(), backupFilename.AsNative().c_str(), FALSE); WriteFileUTF16LE(filename, mpt::String::Decode(std::string(&data[0], &data[0] + data.size()), mpt::CharsetLocale)); } @@ -528,7 +525,7 @@ DefaultSettingsContainer::DefaultSettingsContainer() - : IniFileSettingsContainer(mpt::String::Decode(theApp.GetConfigFileName(), mpt::CharsetLocale)) + : IniFileSettingsContainer(mpt::PathString::FromLocale(theApp.GetConfigFileName())) { return; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |