From 26b6a9af612b65aa31e249ae17cc68c13e43b4ba Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 12 Mar 2011 06:50:20 +0000 Subject: [PATCH] The data from strdup must be deallocated with free, not delete. I got rid of that ugly strdup and replaced the A though Z cdrom drive letter searching with Windows API stuffs. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7335 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Src/CDUtils.cpp | 58 ++++++++++++------------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/Source/Core/Common/Src/CDUtils.cpp b/Source/Core/Common/Src/CDUtils.cpp index 30dc8a27a3..4496302e46 100644 --- a/Source/Core/Common/Src/CDUtils.cpp +++ b/Source/Core/Common/Src/CDUtils.cpp @@ -23,47 +23,33 @@ #endif #ifdef _WIN32 -// Returns a string that can be used in a CreateFile call if -// c_drive letter is a character. If not NULL is returned. -const char *is_cdrom(const char c_drive_letter) +// takes a root drive path, returns true if it is a cdrom drive +bool is_cdrom(const char drive[]) { - UINT uDriveType; - char sz_win32_drive[4]; - - sz_win32_drive[0]= c_drive_letter; - sz_win32_drive[1]=':'; - sz_win32_drive[2]='\\'; - sz_win32_drive[3]='\0'; - - uDriveType = GetDriveType(sz_win32_drive); - - switch(uDriveType) - { - case DRIVE_CDROM: - { - char sz_win32_drive_full[] = "\\\\.\\X:"; - sz_win32_drive_full[4] = c_drive_letter; - return __strdup(&sz_win32_drive_full[4]); - } - default: - { - //cdio_debug("Drive %c is not a CD-ROM", c_drive_letter); - return NULL; - } - } + return (DRIVE_CDROM == GetDriveType(drive)); } -// Returns a pointer to an array of strings with the device names -std::vector cdio_get_devices() { +// Returns a vector with the device names +std::vector cdio_get_devices() +{ std::vector drives; - char drive_letter; - // Scan the system for CD-ROM drives. - for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) { - const char *drive_str=is_cdrom(drive_letter); - if (drive_str != NULL) { - drives.push_back(drive_str); - delete drive_str; + const DWORD buffsize = GetLogicalDriveStrings(0, NULL); + std::unique_ptr buff(new char[buffsize]); + if (GetLogicalDriveStrings(buffsize, buff.get()) == buffsize - 1) + { + const char* drive = buff.get(); + while (*drive) + { + if (is_cdrom(drive)) + { + std::string str(drive); + str.pop_back(); // we don't want the final backslash + drives.push_back(std::move(str)); + } + + // advance to next drive + while (*drive++) {} } } return drives;