Skip to content

Commit

Permalink
[Refactor] Convert to switch expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
claunia committed May 1, 2024
1 parent 87613c0 commit b75fcf0
Show file tree
Hide file tree
Showing 48 changed files with 1,323 additions and 2,621 deletions.
17 changes: 5 additions & 12 deletions Aaru.Core/Devices/Dumping/CompactDisc/Dump.cs
Expand Up @@ -398,18 +398,11 @@ void CompactDisc()
break;
}

switch(desiredSubchannel)
{
case MmcSubchannel.None:
subType = TrackSubchannelType.None;

break;
case MmcSubchannel.Raw:
case MmcSubchannel.Q16:
subType = TrackSubchannelType.Raw;

break;
}
subType = desiredSubchannel switch
{
MmcSubchannel.None => TrackSubchannelType.None,
MmcSubchannel.Raw or MmcSubchannel.Q16 => TrackSubchannelType.Raw
};

blockSize = sectorSize + subSize;

Expand Down
14 changes: 5 additions & 9 deletions Aaru.Core/Devices/Dumping/Sbc/Dump.cs
Expand Up @@ -91,15 +91,11 @@ partial class Dump

if(opticalDisc)
{
switch(dskType)
{
case MediaType.REV35:
case MediaType.REV70:
case MediaType.REV120:
opticalDisc = false;

break;
}
opticalDisc = dskType switch
{
MediaType.REV35 or MediaType.REV70 or MediaType.REV120 => false,
_ => opticalDisc
};
}

_dumpLog.WriteLine(Localization.Core.Initializing_reader);
Expand Down
23 changes: 8 additions & 15 deletions Aaru.Core/Devices/Scanning/MediaScan.cs
Expand Up @@ -81,21 +81,14 @@ public sealed partial class MediaScan
/// <exception cref="NotSupportedException">Unknown device type</exception>
public ScanResults Scan()
{
switch(_dev.Type)
{
case DeviceType.ATA:
return Ata();
case DeviceType.MMC:
case DeviceType.SecureDigital:
return SecureDigital();
case DeviceType.NVMe:
return Nvme();
case DeviceType.ATAPI:
case DeviceType.SCSI:
return Scsi();
default:
throw new NotSupportedException(Localization.Core.Unknown_device_type);
}
return _dev.Type switch
{
DeviceType.ATA => Ata(),
DeviceType.MMC or DeviceType.SecureDigital => SecureDigital(),
DeviceType.NVMe => Nvme(),
DeviceType.ATAPI or DeviceType.SCSI => Scsi(),
_ => throw new NotSupportedException(Localization.Core.Unknown_device_type)
};
}

/// <summary>Aborts the running media scan</summary>
Expand Down
155 changes: 72 additions & 83 deletions Aaru.Core/Error.cs
Expand Up @@ -42,96 +42,85 @@ public static class Error
/// <returns>Error description.</returns>
public static string Print(int errno)
{
switch(DetectOS.GetRealPlatformID())
{
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.Win32NT:
case PlatformID.WinCE:
case PlatformID.WindowsPhone:
case PlatformID.Xbox:
return PrintWin32Error(errno);
case PlatformID.Unix:
case PlatformID.MacOSX:
case PlatformID.iOS:
case PlatformID.Linux:
case PlatformID.Solaris:
case PlatformID.NetBSD:
case PlatformID.OpenBSD:
case PlatformID.FreeBSD:
case PlatformID.DragonFly:
case PlatformID.Android:
case PlatformID.Tizen:
case PlatformID.Hurd:
case PlatformID.Haiku:
case PlatformID.HPUX:
case PlatformID.AIX:
case PlatformID.OS400:
case PlatformID.IRIX:
case PlatformID.Minix:
case PlatformID.QNX:
case PlatformID.SINIX:
case PlatformID.Tru64:
case PlatformID.Ultrix:
case PlatformID.OpenServer:
case PlatformID.UnixWare:
case PlatformID.zOS:
return PrintUnixError(errno);
case PlatformID.Wii:
return string.Format(Localization.Core.error_code_0, errno);
case PlatformID.WiiU:
return string.Format(Localization.Core.error_code_0, errno);
case PlatformID.PlayStation3:
return string.Format(Localization.Core.error_code_0, errno);
case PlatformID.PlayStation4:
return string.Format(Localization.Core.error_code_0, errno);
case PlatformID.NonStop:
return string.Format(Localization.Core.error_code_0, errno);
case PlatformID.Unknown:
return string.Format(Localization.Core.error_code_0, errno);
default:
return string.Format(Localization.Core.error_code_0, errno);
}
return DetectOS.GetRealPlatformID() switch
{
PlatformID.Win32S
or PlatformID.Win32Windows
or PlatformID.Win32NT
or PlatformID.WinCE
or PlatformID.WindowsPhone
or PlatformID.Xbox => PrintWin32Error(errno),
PlatformID.Unix
or PlatformID.MacOSX
or PlatformID.iOS
or PlatformID.Linux
or PlatformID.Solaris
or PlatformID.NetBSD
or PlatformID.OpenBSD
or PlatformID.FreeBSD
or PlatformID.DragonFly
or PlatformID.Android
or PlatformID.Tizen
or PlatformID.Hurd
or PlatformID.Haiku
or PlatformID.HPUX
or PlatformID.AIX
or PlatformID.OS400
or PlatformID.IRIX
or PlatformID.Minix
or PlatformID.QNX
or PlatformID.SINIX
or PlatformID.Tru64
or PlatformID.Ultrix
or PlatformID.OpenServer
or PlatformID.UnixWare
or PlatformID.zOS => PrintUnixError(errno),
PlatformID.Wii => string.Format(Localization.Core.error_code_0, errno),
PlatformID.WiiU => string.Format(Localization.Core.error_code_0, errno),
PlatformID.PlayStation3 => string.Format(Localization.Core.error_code_0, errno),
PlatformID.PlayStation4 => string.Format(Localization.Core.error_code_0, errno),
PlatformID.NonStop => string.Format(Localization.Core.error_code_0, errno),
PlatformID.Unknown => string.Format(Localization.Core.error_code_0, errno),
_ => string.Format(Localization.Core.error_code_0, errno)
};
}

static string PrintUnixError(int errno)
{
switch(errno)
{
case 2: // ENOENT
case 19: // ENODEV
return Localization.Core.The_specified_device_cannot_be_found;
case 13: // EACCESS
return Localization.Core.Not_enough_permissions_to_open_the_device;
case 16: // EBUSY
return Localization.Core.The_specified_device_is_in_use_by_another_process;
case 30: // EROFS
return Localization.Core.Cannot_open_the_device_in_writable_mode_as_needed_by_some_commands;
default:
return string.Format(Localization.Core.error_code_0, errno);
}
return errno switch
{
2 or 19 => // ENODEV
// ENOENT
Localization.Core.The_specified_device_cannot_be_found,
13 => // EACCESS
Localization.Core.Not_enough_permissions_to_open_the_device,
16 => // EBUSY
Localization.Core.The_specified_device_is_in_use_by_another_process,
30 => // EROFS
Localization.Core.Cannot_open_the_device_in_writable_mode_as_needed_by_some_commands,
_ => string.Format(Localization.Core.error_code_0, errno)
};
}

static string PrintWin32Error(int errno)
{
switch(errno)
{
case 2: // ERROR_FILE_NOT_FOUND
case 3: // ERROR_PATH_NOT_FOUND
return Localization.Core.The_specified_device_cannot_be_found;
case 5: // ERROR_ACCESS_DENIED
return Localization.Core.Not_enough_permissions_to_open_the_device;
case 19: // ERROR_WRITE_PROTECT
return Localization.Core.Cannot_open_the_device_in_writable_mode_as_needed_by_some_commands;
case 32: // ERROR_SHARING_VIOLATION
case 33: // ERROR_LOCK_VIOLATION
case 108: // ERROR_DRIVE_LOCKED
case 170: // ERROR_BUSY
return Localization.Core.The_specified_device_is_in_use_by_another_process;
case 130: // ERROR_DIRECT_ACCESS_HANDLE
return Localization.Core.Tried_to_open_a_file_instead_of_a_device;
default:
return string.Format(Localization.Core.error_code_0, errno);
}
return errno switch
{
2 or 3 => // ERROR_PATH_NOT_FOUND
// ERROR_FILE_NOT_FOUND
Localization.Core.The_specified_device_cannot_be_found,
5 => // ERROR_ACCESS_DENIED
Localization.Core.Not_enough_permissions_to_open_the_device,
19 => // ERROR_WRITE_PROTECT
Localization.Core.Cannot_open_the_device_in_writable_mode_as_needed_by_some_commands,
32 or 33 or 108 or 170 => // ERROR_BUSY
// ERROR_DRIVE_LOCKED
// ERROR_LOCK_VIOLATION
// ERROR_SHARING_VIOLATION
Localization.Core.The_specified_device_is_in_use_by_another_process,
130 => // ERROR_DIRECT_ACCESS_HANDLE
Localization.Core.Tried_to_open_a_file_instead_of_a_device,
_ => string.Format(Localization.Core.error_code_0, errno)
};
}
}
39 changes: 9 additions & 30 deletions Aaru.Core/Logging/MHDDLog.cs
Expand Up @@ -68,36 +68,15 @@ sealed class MhddLog
_mhddFs = new MemoryStream();
_logFile = outputFile;

string mode;

switch(dev.Type)
{
case DeviceType.ATA:
case DeviceType.ATAPI:
mode = "MODE: IDE";

break;
case DeviceType.SCSI:
mode = "MODE: SCSI";

break;
case DeviceType.MMC:
mode = "MODE: MMC";

break;
case DeviceType.NVMe:
mode = "MODE: NVMe";

break;
case DeviceType.SecureDigital:
mode = "MODE: SD";

break;
default:
mode = "MODE: IDE";

break;
}
string mode = dev.Type switch
{
DeviceType.ATA or DeviceType.ATAPI => "MODE: IDE",
DeviceType.SCSI => "MODE: SCSI",
DeviceType.MMC => "MODE: MMC",
DeviceType.NVMe => "MODE: NVMe",
DeviceType.SecureDigital => "MODE: SD",
_ => "MODE: IDE"
};

var device = $"DEVICE: {dev.Manufacturer} {dev.Model}";
var fw = $"F/W: {dev.FirmwareRevision}";
Expand Down

0 comments on commit b75fcf0

Please sign in to comment.