From: <man...@us...> - 2014-03-24 12:13:59
|
Revision: 3953 http://sourceforge.net/p/modplug/code/3953 Author: manxorist Date: 2014-03-24 12:13:47 +0000 (Mon, 24 Mar 2014) Log Message: ----------- [Ref] Move all UUID/GUID/IID/CLSID functions to common/misc_util.h, wrap the full set of Win32 UUID<->string conversion functions in C++ style and document them in misc_util.h . [Ref] Add Util::GetTempDirectory(). [Ref] Add Util::CreateTempFileName() which generates a UUID-based temporary filename and allows specifying a full prefix (not supported by GetTempFileName) and file extension (not supported by GetTempFileName or _wtempnam). [Ref] Test: Add basic sanity checks for UUID functions. Modified Paths: -------------- trunk/OpenMPT/common/misc_util.cpp trunk/OpenMPT/common/misc_util.h trunk/OpenMPT/mptrack/ExceptionHandler.cpp trunk/OpenMPT/mptrack/Mod2wave.cpp trunk/OpenMPT/mptrack/TrackerSettings.cpp trunk/OpenMPT/mptrack/TrackerSettings.h trunk/OpenMPT/mptrack/UpdateCheck.cpp trunk/OpenMPT/sounddev/SoundDeviceASIO.cpp trunk/OpenMPT/sounddev/SoundDeviceDirectSound.cpp trunk/OpenMPT/soundlib/plugins/DmoToVst.cpp trunk/OpenMPT/soundlib/plugins/PluginManager.cpp trunk/OpenMPT/test/test.cpp Modified: trunk/OpenMPT/common/misc_util.cpp =================================================================== --- trunk/OpenMPT/common/misc_util.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/common/misc_util.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -339,37 +339,207 @@ namespace Util { - std::wstring CLSIDToString(CLSID clsid) - //------------------------------------- + + +std::wstring CLSIDToString(CLSID clsid) +//------------------------------------- +{ + std::wstring str; + LPOLESTR tmp = nullptr; + ::StringFromCLSID(clsid, &tmp); + if(tmp) { - std::wstring str; - LPOLESTR tmp = nullptr; - StringFromCLSID(clsid, &tmp); - if(tmp) - { - str = tmp; - CoTaskMemFree(tmp); - tmp = nullptr; - } - return str; + str = tmp; + ::CoTaskMemFree(tmp); + tmp = nullptr; } + return str; +} - bool StringToCLSID(const std::wstring &str, CLSID &clsid) - //------------------------------------------------------- +CLSID StringToCLSID(const std::wstring &str) +//------------------------------------------ +{ + CLSID clsid = CLSID(); + std::vector<OLECHAR> tmp(str.c_str(), str.c_str() + str.length() + 1); + if(::CLSIDFromString(&tmp[0], &clsid) != S_OK) { - std::vector<OLECHAR> tmp(str.c_str(), str.c_str() + str.length() + 1); - return CLSIDFromString(&tmp[0], &clsid) == S_OK; + return CLSID(); } + return clsid; +} - bool IsCLSID(const std::wstring &str) - //----------------------------------- +bool VerifyStringToCLSID(const std::wstring &str, CLSID &clsid) +//------------------------------------------------------------- +{ + std::vector<OLECHAR> tmp(str.c_str(), str.c_str() + str.length() + 1); + return (::CLSIDFromString(&tmp[0], &clsid) == S_OK); +} + + +bool IsCLSID(const std::wstring &str) +//----------------------------------- +{ + CLSID clsid = CLSID(); + std::vector<OLECHAR> tmp(str.c_str(), str.c_str() + str.length() + 1); + return (::CLSIDFromString(&tmp[0], &clsid) == S_OK); +} + + +std::wstring IIDToString(IID iid) +//------------------------------- +{ + std::wstring str; + LPOLESTR tmp = nullptr; + ::StringFromIID(iid, &tmp); + if(tmp) { - CLSID clsid = CLSID(); - std::vector<OLECHAR> tmp(str.c_str(), str.c_str() + str.length() + 1); - return CLSIDFromString(&tmp[0], &clsid) == S_OK; + str = tmp; + ::CoTaskMemFree(tmp); + tmp = nullptr; } + return str; +} + + +IID StringToIID(const std::wstring &str) +//-------------------------------------- +{ + IID iid = IID(); + std::vector<OLECHAR> tmp(str.c_str(), str.c_str() + str.length() + 1); + ::IIDFromString(&tmp[0], &iid); + return iid; +} + + +std::wstring GUIDToString(GUID guid) +//---------------------------------- +{ + std::vector<OLECHAR> tmp(256); + ::StringFromGUID2(guid, &tmp[0], tmp.size()); + return &tmp[0]; +} + + +GUID StringToGUID(const std::wstring &str) +//---------------------------------------- +{ + return StringToIID(str); +} + + +UUID StringToUUID(const std::wstring &str) +//---------------------------------------- +{ + UUID uuid = UUID(); + std::vector<wchar_t> tmp(str.c_str(), str.c_str() + str.length() + 1); + if(::UuidFromStringW((RPC_WSTR)(&(tmp[0])), &uuid) != RPC_S_OK) + { + return UUID(); + } + return uuid; +} + + +std::wstring UUIDToString(UUID uuid) +//---------------------------------- +{ + std::wstring str; + RPC_WSTR tmp = nullptr; + if(::UuidToStringW(&uuid, &tmp) != RPC_S_OK) + { + return std::wstring(); + } + str = (wchar_t*)tmp; + ::RpcStringFreeW(&tmp); + return str; +} + + +bool IsValid(UUID uuid) +//--------------------- +{ + return false + || uuid.Data1 != 0 + || uuid.Data2 != 0 + || uuid.Data3 != 0 + || uuid.Data4[0] != 0 + || uuid.Data4[1] != 0 + || uuid.Data4[2] != 0 + || uuid.Data4[3] != 0 + || uuid.Data4[4] != 0 + || uuid.Data4[5] != 0 + || uuid.Data4[6] != 0 + || uuid.Data4[7] != 0 + ; +} + + +GUID CreateGUID() +//--------------- +{ + GUID guid = GUID(); + if(::CoCreateGuid(&guid) != S_OK) + { + return GUID(); + } + return guid; +} + + +UUID CreateUUID() +//--------------- +{ + UUID uuid = UUID(); + RPC_STATUS status = ::UuidCreate(&uuid); + if(status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) + { + return UUID(); + } + return uuid; +} + + +UUID CreateLocalUUID() +//-------------------- +{ + UUID uuid = UUID(); + RPC_STATUS status = ::UuidCreateSequential(&uuid); + if(status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) + { + return UUID(); + } + return uuid; +} + + +mpt::PathString GetTempDirectory() +//-------------------------------- +{ + WCHAR tempPath[MAX_PATH+2]; + MemsetZero(tempPath); + DWORD result = GetTempPathW(MAX_PATH+1, tempPath); + if(result == 0 || result > MAX_PATH+1) + { // error + // use app directory as fallback + return mpt::GetAppPath(); + } + return mpt::PathString::FromNative(tempPath); +} + + +mpt::PathString CreateTempFileName(const mpt::PathString &fileNamePrefix, const mpt::PathString &fileNameExtension) +//----------------------------------------------------------------------------------------------------------------- +{ + mpt::PathString filename = Util::GetTempDirectory(); + filename += (!fileNamePrefix.empty() ? fileNamePrefix + MPT_PATHSTRING("_") : mpt::PathString()); + filename += mpt::PathString::FromWide(Util::UUIDToString(Util::CreateLocalUUID())); + filename += (!fileNameExtension.empty() ? MPT_PATHSTRING(".") + fileNameExtension : mpt::PathString()); + return filename; +} + + } // namespace Util #endif // MODPLUG_TRACKER Modified: trunk/OpenMPT/common/misc_util.h =================================================================== --- trunk/OpenMPT/common/misc_util.h 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/common/misc_util.h 2014-03-24 12:13:47 UTC (rev 3953) @@ -626,9 +626,48 @@ namespace Util { - std::wstring CLSIDToString(CLSID clsid); - bool StringToCLSID(const std::wstring &str, CLSID &clsid); - bool IsCLSID(const std::wstring &str); + +// COM CLSID<->string conversion +// A CLSID string is not necessarily a standard UUID string, +// it might also be a symbolic name for the interface. +// (see CLSIDFromString ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms680589%28v=vs.85%29.aspx )) +std::wstring CLSIDToString(CLSID clsid); +CLSID StringToCLSID(const std::wstring &str); +bool VerifyStringToCLSID(const std::wstring &str, CLSID &clsid); +bool IsCLSID(const std::wstring &str); + +// COM IID<->string conversion +IID StringToIID(const std::wstring &str); +std::wstring IIDToString(IID iid); + +// General GUID<->string conversion. +// The string must/will be in standard GUID format: {4F9A455D-E7EF-4367-B2F0-0C83A38A5C72} +GUID StringToGUID(const std::wstring &str); +std::wstring GUIDToString(GUID guid); + +// General UUID<->string conversion. +// The string must/will be in standard UUID format: 4f9a455d-e7ef-4367-b2f0-0c83a38a5c72 +UUID StringToUUID(const std::wstring &str); +std::wstring UUIDToString(UUID uuid); + +// Checks the UUID against the NULL UUID. Returns false if it is NULL, true otherwise. +bool IsValid(UUID uuid); + +// Create a COM GUID +GUID CreateGUID(); + +// Create a UUID +UUID CreateUUID(); + +// Create a UUID that contains local, traceable information. Safe for local use. +UUID CreateLocalUUID(); + +// Returns temporary directory (with trailing backslash added) (e.g. "C:\TEMP\") +mpt::PathString GetTempDirectory(); + +// Returns a new unique absolute path. +mpt::PathString CreateTempFileName(const mpt::PathString &fileNamePrefix = mpt::PathString(), const mpt::PathString &fileNameExtension = MPT_PATHSTRING("tmp")); + } // namespace Util #endif // MODPLUG_TRACKER Modified: trunk/OpenMPT/mptrack/ExceptionHandler.cpp =================================================================== --- trunk/OpenMPT/mptrack/ExceptionHandler.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/mptrack/ExceptionHandler.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -43,9 +43,7 @@ mpt::PathString baseRescuePath; { // Create a crash directory - WCHAR tempPath[MAX_PATH]; - GetTempPathW(CountOf(tempPath), tempPath); - baseRescuePath = mpt::PathString::FromNative(tempPath) + MPT_PATHSTRING("OpenMPT Crash Files\\"); + baseRescuePath = Util::GetTempDirectory() + MPT_PATHSTRING("OpenMPT Crash Files\\"); if(!PathIsDirectoryW(baseRescuePath.AsNative().c_str())) { CreateDirectoryW(baseRescuePath.AsNative().c_str(), nullptr); Modified: trunk/OpenMPT/mptrack/Mod2wave.cpp =================================================================== --- trunk/OpenMPT/mptrack/Mod2wave.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/mptrack/Mod2wave.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -942,9 +942,7 @@ } float normalizePeak = 0.0f; - wchar_t *tempPath = _wtempnam(L"", L"OpenMPT_mod2wave"); - const mpt::PathString normalizeFileName = mpt::PathString::FromNative(tempPath); - free(tempPath); + const mpt::PathString normalizeFileName = Util::CreateTempFileName(MPT_PATHSTRING("OpenMPT")); mpt::fstream normalizeFile; if(m_Settings.Normalize) { Modified: trunk/OpenMPT/mptrack/TrackerSettings.cpp =================================================================== --- trunk/OpenMPT/mptrack/TrackerSettings.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/mptrack/TrackerSettings.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -133,7 +133,7 @@ // Version , IniVersion(conf, "Version", "Version", "") , gcsPreviousVersion(GetStoredVersion(IniVersion)) - , gcsInstallGUID(conf, "Version", "InstallGUID", "") + , gcsInstallGUID(conf, "Version", "InstallGUID", std::wstring()) // Display , m_ShowSplashScreen(conf, "Display", "ShowSplashScreen", true) , gbMdiMaximize(conf, "Display", "MDIMaximize", true) @@ -366,15 +366,10 @@ const MptVersion::VersionNum storedVersion = gcsPreviousVersion; // Version - if(gcsInstallGUID == "") + if(gcsInstallGUID.Get().empty()) { - // No GUID found - generate one. - GUID guid; - CoCreateGuid(&guid); - BYTE* Str; - UuidToString((UUID*)&guid, &Str); - gcsInstallGUID = Str; - RpcStringFree(&Str); + // No UUID found - generate one. + gcsInstallGUID = Util::UUIDToString(Util::CreateUUID()); } // Sound Settings Modified: trunk/OpenMPT/mptrack/TrackerSettings.h =================================================================== --- trunk/OpenMPT/mptrack/TrackerSettings.h 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/mptrack/TrackerSettings.h 2014-03-24 12:13:47 UTC (rev 3953) @@ -327,7 +327,7 @@ Setting<std::string> IniVersion; const MptVersion::VersionNum gcsPreviousVersion; - Setting<CString> gcsInstallGUID; + Setting<std::wstring> gcsInstallGUID; // Display Modified: trunk/OpenMPT/mptrack/UpdateCheck.cpp =================================================================== --- trunk/OpenMPT/mptrack/UpdateCheck.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/mptrack/UpdateCheck.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -86,7 +86,7 @@ #error "Platform-specific identifier missing" #endif updateURL.Replace("$VERSION", versionStr); - updateURL.Replace("$GUID", GetSendGUID() ? TrackerSettings::Instance().gcsInstallGUID.Get() : "anonymous"); + updateURL.Replace("$GUID", GetSendGUID() ? mpt::ToCString(TrackerSettings::Instance().gcsInstallGUID.Get()) : "anonymous"); // Establish a connection. internetHandle = InternetOpen(userAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); Modified: trunk/OpenMPT/sounddev/SoundDeviceASIO.cpp =================================================================== --- trunk/OpenMPT/sounddev/SoundDeviceASIO.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/sounddev/SoundDeviceASIO.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -644,8 +644,7 @@ { return; } - CLSID clsid; - Util::StringToCLSID(GetDeviceInternalID(), clsid); + CLSID clsid = Util::StringToCLSID(GetDeviceInternalID()); try { if(CoCreateInstance(clsid,0,CLSCTX_INPROC_SERVER, clsid, (void **)&m_pAsioDrv) != S_OK) Modified: trunk/OpenMPT/sounddev/SoundDeviceDirectSound.cpp =================================================================== --- trunk/OpenMPT/sounddev/SoundDeviceDirectSound.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/sounddev/SoundDeviceDirectSound.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -33,32 +33,6 @@ #ifndef NO_DSOUND -static std::wstring GuidToString(GUID guid) -//----------------------------------------- -{ - std::wstring str; - LPOLESTR tmp = nullptr; - StringFromIID(guid, &tmp); - if(tmp) - { - str = tmp; - CoTaskMemFree(tmp); - tmp = nullptr; - } - return str; -} - - -static GUID StringToGuid(const std::wstring &str) -//----------------------------------------------- -{ - GUID guid = GUID(); - std::vector<OLECHAR> tmp(str.c_str(), str.c_str() + str.length() + 1); - IIDFromString(&tmp[0], &guid); - return guid; -} - - static BOOL WINAPI DSEnumCallbackW(GUID * lpGuid, LPCWSTR lpstrDescription, LPCWSTR, LPVOID lpContext) //---------------------------------------------------------------------------------------------------- { @@ -77,7 +51,7 @@ info.apiName = SoundDeviceTypeToString(SNDDEV_DSOUND); if(lpGuid) { - info.internalID = GuidToString(*lpGuid); + info.internalID = Util::GUIDToString(*lpGuid); } devices.push_back(info); return TRUE; @@ -143,7 +117,7 @@ } else { const std::wstring internalID = GetDeviceInternalID(); - GUID guid = internalID.empty() ? GUID() : StringToGuid(internalID); + GUID guid = internalID.empty() ? GUID() : Util::StringToGUID(internalID); if(DirectSoundCreate(internalID.empty() ? NULL : &guid, &dummy, NULL) != DS_OK) { return caps; @@ -197,7 +171,7 @@ if(m_piDS) return true; const std::wstring internalID = GetDeviceInternalID(); - GUID guid = internalID.empty() ? GUID() : StringToGuid(internalID); + GUID guid = internalID.empty() ? GUID() : Util::StringToGUID(internalID); if(DirectSoundCreate(internalID.empty() ? NULL : &guid, &m_piDS, NULL) != DS_OK) return false; if(!m_piDS) return false; m_piDS->SetCooperativeLevel(m_Settings.hWnd, m_Settings.ExclusiveMode ? DSSCL_WRITEPRIMARY : DSSCL_PRIORITY); Modified: trunk/OpenMPT/soundlib/plugins/DmoToVst.cpp =================================================================== --- trunk/OpenMPT/soundlib/plugins/DmoToVst.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/soundlib/plugins/DmoToVst.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -519,7 +519,7 @@ //---------------------------------- { CLSID clsid; - if (Util::StringToCLSID(lib.dllPath.ToWide(), clsid)) + if (Util::VerifyStringToCLSID(lib.dllPath.ToWide(), clsid)) { IMediaObject *pMO = nullptr; IMediaObjectInPlace *pMOIP = nullptr; Modified: trunk/OpenMPT/soundlib/plugins/PluginManager.cpp =================================================================== --- trunk/OpenMPT/soundlib/plugins/PluginManager.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/soundlib/plugins/PluginManager.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -154,7 +154,7 @@ { CLSID clsid; std::wstring formattedKey = std::wstring(L"{") + std::wstring(keyname) + std::wstring(L"}"); - if(Util::StringToCLSID(formattedKey, clsid)) + if(Util::VerifyStringToCLSID(formattedKey, clsid)) { HKEY hksub; formattedKey = std::wstring(L"software\\classes\\DirectShow\\MediaObjects\\") + std::wstring(keyname); @@ -167,9 +167,8 @@ if(ERROR_SUCCESS == RegQueryValueExW(hksub, nullptr, 0, &datatype, (LPBYTE)name, &datasize)) { mpt::String::SetNullTerminator(name); - StringFromGUID2(clsid, keyname, 100); - VSTPluginLib *plug = new (std::nothrow) VSTPluginLib(mpt::PathString::FromNative(keyname), mpt::PathString::FromNative(name)); + VSTPluginLib *plug = new (std::nothrow) VSTPluginLib(mpt::PathString::FromNative(Util::GUIDToString(clsid)), mpt::PathString::FromNative(name)); if(plug != nullptr) { pluginList.push_back(plug); Modified: trunk/OpenMPT/test/test.cpp =================================================================== --- trunk/OpenMPT/test/test.cpp 2014-03-24 11:16:58 UTC (rev 3952) +++ trunk/OpenMPT/test/test.cpp 2014-03-24 12:13:47 UTC (rev 3953) @@ -266,6 +266,12 @@ } +static bool IsEqualUUID(const UUID &lhs, const UUID &rhs) +{ + return std::memcmp(&lhs, &rhs, sizeof(UUID)) == 0; +} + + static noinline void TestMisc() //----------------------------- { @@ -542,6 +548,18 @@ VERIFY_EQUAL(MPT_PATHSTRING("\\\\server\\path\\file").RelativePathToAbsolute(exePath), MPT_PATHSTRING("\\\\server\\path\\file")); #endif + // UUID +#ifdef MODPLUG_TRACKER + VERIFY_EQUAL(Util::IsValid(Util::CreateGUID()), true); + VERIFY_EQUAL(Util::IsValid(Util::CreateUUID()), true); + VERIFY_EQUAL(Util::IsValid(Util::CreateLocalUUID()), true); + UUID uuid = Util::CreateUUID(); + VERIFY_EQUAL(IsEqualUUID(uuid, Util::StringToUUID(Util::UUIDToString(uuid))), true); + VERIFY_EQUAL(IsEqualUUID(uuid, Util::StringToGUID(Util::GUIDToString(uuid))), true); + VERIFY_EQUAL(IsEqualUUID(uuid, Util::StringToIID(Util::IIDToString(uuid))), true); + VERIFY_EQUAL(IsEqualUUID(uuid, Util::StringToCLSID(Util::CLSIDToString(uuid))), true); +#endif + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |