REFACTOR: Loop can be converted into LINQ-expression.

This commit is contained in:
2017-12-21 07:08:26 +00:00
parent 4d886dae25
commit 5592f147ac
71 changed files with 668 additions and 1131 deletions

View File

@@ -33,6 +33,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DiscImageChef.Checksums;
namespace DiscImageChef.Core
@@ -514,14 +515,7 @@ namespace DiscImageChef.Core
}
EndProgress();
double entropy = 0;
foreach(ulong l in entTable)
{
#pragma warning disable IDE0004 // Without this specific cast, it gives incorrect values
double frequency = (double)l / (double)bufferSize;
#pragma warning restore IDE0004 // Without this specific cast, it gives incorrect values
entropy += -(frequency * Math.Log(frequency, 2));
}
double entropy = entTable.Select(l => (double)l / (double)bufferSize).Select(frequency => -(frequency * Math.Log(frequency, 2))).Sum();
end = DateTime.Now;
mem = GC.GetTotalMemory(false);

View File

@@ -34,6 +34,7 @@ using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiscImageChef.CommonTypes;
using DiscImageChef.DiscImages;
@@ -376,16 +377,12 @@ namespace DiscImageChef.Core.Devices.Dumping
internal void AddSessions(Session[] cdSessions)
{
foreach(Session cdSession in cdSessions)
foreach(AlcoholSession session in cdSessions.Select(cdSession => new AlcoholSession
{
AlcoholSession session = new AlcoholSession
{
firstTrack = (ushort)cdSession.StartTrack,
lastTrack = (ushort)cdSession.EndTrack,
sessionSequence = cdSession.SessionSequence
};
sessions.Add(session);
}
firstTrack = (ushort)cdSession.StartTrack,
lastTrack = (ushort)cdSession.EndTrack,
sessionSequence = cdSession.SessionSequence
})) { sessions.Add(session); }
}
internal void SetTrackTypes(byte point, TrackType mode, TrackSubchannelType subMode)

View File

@@ -244,14 +244,12 @@ namespace DiscImageChef.Core.Devices.Dumping
sessionsForAlcohol[i].SessionSequence = (ushort)(i + 1);
sessionsForAlcohol[i].StartTrack = ushort.MaxValue;
}
foreach(FullTOC.TrackDataDescriptor trk in toc.Value.TrackDescriptors)
if(trk.POINT > 0 && trk.POINT < 0xA0 && trk.SessionNumber <= sessionsForAlcohol.Length)
{
if(trk.POINT < sessionsForAlcohol[trk.SessionNumber - 1].StartTrack)
sessionsForAlcohol[trk.SessionNumber - 1].StartTrack = trk.POINT;
if(trk.POINT > sessionsForAlcohol[trk.SessionNumber - 1].EndTrack)
sessionsForAlcohol[trk.SessionNumber - 1].EndTrack = trk.POINT;
}
foreach(FullTOC.TrackDataDescriptor trk in toc.Value.TrackDescriptors.Where(trk => trk.POINT > 0 && trk.POINT < 0xA0 && trk.SessionNumber <= sessionsForAlcohol.Length)) {
if(trk.POINT < sessionsForAlcohol[trk.SessionNumber - 1].StartTrack)
sessionsForAlcohol[trk.SessionNumber - 1].StartTrack = trk.POINT;
if(trk.POINT > sessionsForAlcohol[trk.SessionNumber - 1].EndTrack)
sessionsForAlcohol[trk.SessionNumber - 1].EndTrack = trk.POINT;
}
alcohol.AddSessions(sessionsForAlcohol);
@@ -264,67 +262,65 @@ namespace DiscImageChef.Core.Devices.Dumping
List<TrackType> trackList = new List<TrackType>();
long lastSector = 0;
string lastMsf = null;
foreach(FullTOC.TrackDataDescriptor trk in sortedTracks)
if(trk.ADR == 1 || trk.ADR == 4)
if(trk.POINT >= 0x01 && trk.POINT <= 0x63)
foreach(FullTOC.TrackDataDescriptor trk in sortedTracks.Where(trk => trk.ADR == 1 || trk.ADR == 4)) if(trk.POINT >= 0x01 && trk.POINT <= 0x63)
{
TrackType track = new TrackType
{
TrackType track = new TrackType
{
Sequence = new TrackSequenceType {Session = trk.SessionNumber, TrackNumber = trk.POINT}
};
if((TOC_CONTROL)(trk.CONTROL & 0x0D) == TOC_CONTROL.DataTrack ||
(TOC_CONTROL)(trk.CONTROL & 0x0D) == TOC_CONTROL.DataTrackIncremental)
track.TrackType1 = TrackTypeTrackType.mode1;
else track.TrackType1 = TrackTypeTrackType.audio;
if(trk.PHOUR > 0)
track.StartMSF = string.Format("{3:D2}:{0:D2}:{1:D2}:{2:D2}", trk.PMIN, trk.PSEC,
trk.PFRAME, trk.PHOUR);
else track.StartMSF = string.Format("{0:D2}:{1:D2}:{2:D2}", trk.PMIN, trk.PSEC, trk.PFRAME);
track.StartSector = trk.PHOUR * 3600 * 75 + trk.PMIN * 60 * 75 + trk.PSEC * 75 + trk.PFRAME -
150;
trackList.Add(track);
}
else if(trk.POINT == 0xA2)
Sequence = new TrackSequenceType {Session = trk.SessionNumber, TrackNumber = trk.POINT}
};
if((TOC_CONTROL)(trk.CONTROL & 0x0D) == TOC_CONTROL.DataTrack ||
(TOC_CONTROL)(trk.CONTROL & 0x0D) == TOC_CONTROL.DataTrackIncremental)
track.TrackType1 = TrackTypeTrackType.mode1;
else track.TrackType1 = TrackTypeTrackType.audio;
if(trk.PHOUR > 0)
track.StartMSF = string.Format("{3:D2}:{0:D2}:{1:D2}:{2:D2}", trk.PMIN, trk.PSEC,
trk.PFRAME, trk.PHOUR);
else track.StartMSF = string.Format("{0:D2}:{1:D2}:{2:D2}", trk.PMIN, trk.PSEC, trk.PFRAME);
track.StartSector = trk.PHOUR * 3600 * 75 + trk.PMIN * 60 * 75 + trk.PSEC * 75 + trk.PFRAME -
150;
trackList.Add(track);
}
else if(trk.POINT == 0xA2)
{
int phour, pmin, psec, pframe;
if(trk.PFRAME == 0)
{
int phour, pmin, psec, pframe;
if(trk.PFRAME == 0)
{
pframe = 74;
pframe = 74;
if(trk.PSEC == 0)
if(trk.PSEC == 0)
{
psec = 59;
if(trk.PMIN == 0)
{
psec = 59;
if(trk.PMIN == 0)
{
pmin = 59;
phour = trk.PHOUR - 1;
}
else
{
pmin = trk.PMIN - 1;
phour = trk.PHOUR;
}
pmin = 59;
phour = trk.PHOUR - 1;
}
else
{
psec = trk.PSEC - 1;
pmin = trk.PMIN;
pmin = trk.PMIN - 1;
phour = trk.PHOUR;
}
}
else
{
pframe = trk.PFRAME - 1;
psec = trk.PSEC;
psec = trk.PSEC - 1;
pmin = trk.PMIN;
phour = trk.PHOUR;
}
if(phour > 0) lastMsf = string.Format("{3:D2}:{0:D2}:{1:D2}:{2:D2}", pmin, psec, pframe, phour);
else lastMsf = string.Format("{0:D2}:{1:D2}:{2:D2}", pmin, psec, pframe);
lastSector = phour * 3600 * 75 + pmin * 60 * 75 + psec * 75 + pframe - 150;
}
else
{
pframe = trk.PFRAME - 1;
psec = trk.PSEC;
pmin = trk.PMIN;
phour = trk.PHOUR;
}
if(phour > 0) lastMsf = string.Format("{3:D2}:{0:D2}:{1:D2}:{2:D2}", pmin, psec, pframe, phour);
else lastMsf = string.Format("{0:D2}:{1:D2}:{2:D2}", pmin, psec, pframe);
lastSector = phour * 3600 * 75 + pmin * 60 * 75 + psec * 75 + pframe - 150;
}
TrackType[] tracks = trackList.ToArray();
for(int t = 1; t < tracks.Length; t++)

View File

@@ -33,6 +33,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DiscImageChef.CommonTypes;
using DiscImageChef.Console;
@@ -278,8 +279,7 @@ namespace DiscImageChef.Core.Devices.Dumping
decMode.Value.Header.BlockDescriptors.Length >= 1)
scsiDensityCode = (byte)decMode.Value.Header.BlockDescriptors[0].Density;
foreach(Decoders.SCSI.Modes.ModePage modePage in decMode.Value.Pages)
containsFloppyPage |= modePage.Page == 0x05;
containsFloppyPage = decMode.Value.Pages.Aggregate(containsFloppyPage, (current, modePage) => current | (modePage.Page == 0x05));
}
}
}

View File

@@ -33,6 +33,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DiscImageChef.Console;
using DiscImageChef.Devices;
using DiscImageChef.Metadata;
@@ -133,7 +134,7 @@ namespace DiscImageChef.Core.Devices.Report.SCSI
}
if(inq.VersionDescriptors != null)
{
foreach(ushort descriptor in inq.VersionDescriptors) if(descriptor != 0) versionDescriptors.Add(descriptor);
versionDescriptors.AddRange(inq.VersionDescriptors.Where(descriptor => descriptor != 0));
if(versionDescriptors.Count > 0)
report.SCSI.Inquiry.VersionDescriptors = versionDescriptors.ToArray();
@@ -184,18 +185,16 @@ namespace DiscImageChef.Core.Devices.Report.SCSI
if(evpdPages != null && evpdPages.Length > 0)
{
List<pageType> evpds = new List<pageType>();
foreach(byte page in evpdPages)
if(page != 0x80)
{
DicConsole.WriteLine("Querying SCSI EVPD {0:X2}h...", page);
sense = dev.ScsiInquiry(out buffer, out senseBuffer, page);
if(sense) continue;
foreach(byte page in evpdPages.Where(page => page != 0x80)) {
DicConsole.WriteLine("Querying SCSI EVPD {0:X2}h...", page);
sense = dev.ScsiInquiry(out buffer, out senseBuffer, page);
if(sense) continue;
pageType evpd = new pageType();
evpd.page = page;
evpd.value = buffer;
evpds.Add(evpd);
}
pageType evpd = new pageType();
evpd.page = page;
evpd.value = buffer;
evpds.Add(evpd);
}
if(evpds.Count > 0) report.SCSI.EVPDPages = evpds.ToArray();
}

View File

@@ -31,6 +31,7 @@
// ****************************************************************************/
using System.Collections.Generic;
using System.Linq;
using DiscImageChef.CommonTypes;
using DiscImageChef.Filesystems;
using DiscImageChef.DiscImages;
@@ -41,11 +42,10 @@ namespace DiscImageChef.Core
{
public static void Identify(ImagePlugin imagePlugin, out List<string> idPlugins, Partition partition)
{
idPlugins = new List<string>();
PluginBase plugins = new PluginBase();
plugins.RegisterAllPlugins();
foreach(Filesystem plugin in plugins.PluginsList.Values) if(plugin.Identify(imagePlugin, partition)) idPlugins.Add(plugin.Name.ToLower());
idPlugins = (from plugin in plugins.PluginsList.Values where plugin.Identify(imagePlugin, partition) select plugin.Name.ToLower()).ToList();
}
}
}

View File

@@ -31,6 +31,7 @@
// ****************************************************************************/
using System;
using System.Linq;
using DiscImageChef.Console;
using DiscImageChef.Filters;
using DiscImageChef.DiscImages;
@@ -50,10 +51,20 @@ namespace DiscImageChef.Core
imageFormat = null;
// Check all but RAW plugin
foreach(ImagePlugin imageplugin in plugins.ImagePluginsList.Values)
if(imageplugin.PluginUuid != new Guid("12345678-AAAA-BBBB-CCCC-123456789000"))
foreach(ImagePlugin imageplugin in plugins.ImagePluginsList.Values.Where(imageplugin => imageplugin.PluginUuid != new Guid("12345678-AAAA-BBBB-CCCC-123456789000"))) try
{
try
DicConsole.DebugWriteLine("Format detection", "Trying plugin {0}", imageplugin.Name);
if(!imageplugin.IdentifyImage(imageFilter)) continue;
imageFormat = imageplugin;
break;
}
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
catch { }
// Check only RAW plugin
if(imageFormat == null)
foreach(ImagePlugin imageplugin in plugins.ImagePluginsList.Values.Where(imageplugin => imageplugin.PluginUuid == new Guid("12345678-AAAA-BBBB-CCCC-123456789000"))) try
{
DicConsole.DebugWriteLine("Format detection", "Trying plugin {0}", imageplugin.Name);
if(!imageplugin.IdentifyImage(imageFilter)) continue;
@@ -63,26 +74,6 @@ namespace DiscImageChef.Core
}
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
catch { }
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
}
// Check only RAW plugin
if(imageFormat == null)
foreach(ImagePlugin imageplugin in plugins.ImagePluginsList.Values)
if(imageplugin.PluginUuid == new Guid("12345678-AAAA-BBBB-CCCC-123456789000"))
{
try
{
DicConsole.DebugWriteLine("Format detection", "Trying plugin {0}", imageplugin.Name);
if(!imageplugin.IdentifyImage(imageFilter)) continue;
imageFormat = imageplugin;
break;
}
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
catch { }
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
}
// Still not recognized
if(imageFormat == null) return null;

View File

@@ -123,11 +123,9 @@ namespace DiscImageChef.Core
// Be sure that device partitions are not excluded if not mapped by any scheme...
if(image.ImageInfo.ImageHasPartitions)
{
List<ulong> startLocations = new List<ulong>();
List<ulong> startLocations = childPartitions.Select(detectedPartition => detectedPartition.Start).ToList();
foreach(Partition detectedPartition in childPartitions) startLocations.Add(detectedPartition.Start);
foreach(Partition imagePartition in image.GetPartitions()) if(!startLocations.Contains(imagePartition.Start)) childPartitions.Add(imagePartition);
childPartitions.AddRange(image.GetPartitions().Where(imagePartition => !startLocations.Contains(imagePartition.Start)));
}
Partition[] childArray = childPartitions
@@ -144,7 +142,7 @@ namespace DiscImageChef.Core
List<string> schemes = new List<string>();
foreach(Partition part in partitions) if(!schemes.Contains(part.Scheme)) schemes.Add(part.Scheme);
foreach(Partition part in partitions.Where(part => !schemes.Contains(part.Scheme))) schemes.Add(part.Scheme);
foreach(string scheme in schemes) Statistics.AddPartition(scheme);
}

View File

@@ -33,6 +33,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Xml.Serialization;
@@ -105,14 +106,12 @@ namespace DiscImageChef.Core
long count = 0;
OsStats old = null;
foreach(OsStats nvs in AllStats.OperatingSystems)
if(nvs.name == Interop.DetectOS.GetRealPlatformID().ToString() &&
nvs.version == Interop.DetectOS.GetVersion())
{
count = nvs.Value + 1;
old = nvs;
break;
}
foreach(OsStats nvs in AllStats.OperatingSystems.Where(nvs => nvs.name == Interop.DetectOS.GetRealPlatformID().ToString() &&
nvs.version == Interop.DetectOS.GetVersion())) {
count = nvs.Value + 1;
old = nvs;
break;
}
if(old != null) AllStats.OperatingSystems.Remove(old);
@@ -131,13 +130,11 @@ namespace DiscImageChef.Core
long count = 0;
NameValueStats old = null;
foreach(NameValueStats nvs in AllStats.Versions)
if(nvs.name == Version.GetVersion())
{
count = nvs.Value + 1;
old = nvs;
break;
}
foreach(NameValueStats nvs in AllStats.Versions.Where(nvs => nvs.name == Version.GetVersion())) {
count = nvs.Value + 1;
old = nvs;
break;
}
if(old != null) AllStats.Versions.Remove(old);
@@ -333,13 +330,7 @@ namespace DiscImageChef.Core
if(AllStats.Filesystems == null) AllStats.Filesystems = new List<NameValueStats>();
if(CurrentStats.Filesystems == null) CurrentStats.Filesystems = new List<NameValueStats>();
NameValueStats old = null;
foreach(NameValueStats nvs in AllStats.Filesystems)
if(nvs.name == filesystem)
{
old = nvs;
break;
}
NameValueStats old = AllStats.Filesystems.FirstOrDefault(nvs => nvs.name == filesystem);
NameValueStats nw = new NameValueStats();
if(old != null)
@@ -355,13 +346,7 @@ namespace DiscImageChef.Core
}
AllStats.Filesystems.Add(nw);
old = null;
foreach(NameValueStats nvs in CurrentStats.Filesystems)
if(nvs.name == filesystem)
{
old = nvs;
break;
}
old = CurrentStats.Filesystems.FirstOrDefault(nvs => nvs.name == filesystem);
nw = new NameValueStats();
if(old != null)
@@ -385,13 +370,7 @@ namespace DiscImageChef.Core
if(AllStats.Partitions == null) AllStats.Partitions = new List<NameValueStats>();
if(CurrentStats.Partitions == null) CurrentStats.Partitions = new List<NameValueStats>();
NameValueStats old = null;
foreach(NameValueStats nvs in AllStats.Partitions)
if(nvs.name == partition)
{
old = nvs;
break;
}
NameValueStats old = AllStats.Partitions.FirstOrDefault(nvs => nvs.name == partition);
NameValueStats nw = new NameValueStats();
if(old != null)
@@ -407,13 +386,7 @@ namespace DiscImageChef.Core
}
AllStats.Partitions.Add(nw);
old = null;
foreach(NameValueStats nvs in CurrentStats.Partitions)
if(nvs.name == partition)
{
old = nvs;
break;
}
old = CurrentStats.Partitions.FirstOrDefault(nvs => nvs.name == partition);
nw = new NameValueStats();
if(old != null)
@@ -437,13 +410,7 @@ namespace DiscImageChef.Core
if(AllStats.Filters == null) AllStats.Filters = new List<NameValueStats>();
if(CurrentStats.Filters == null) CurrentStats.Filters = new List<NameValueStats>();
NameValueStats old = null;
foreach(NameValueStats nvs in AllStats.Filters)
if(nvs.name == format)
{
old = nvs;
break;
}
NameValueStats old = AllStats.Filters.FirstOrDefault(nvs => nvs.name == format);
NameValueStats nw = new NameValueStats();
if(old != null)
@@ -459,13 +426,7 @@ namespace DiscImageChef.Core
}
AllStats.Filters.Add(nw);
old = null;
foreach(NameValueStats nvs in CurrentStats.Filters)
if(nvs.name == format)
{
old = nvs;
break;
}
old = CurrentStats.Filters.FirstOrDefault(nvs => nvs.name == format);
nw = new NameValueStats();
if(old != null)
@@ -489,13 +450,7 @@ namespace DiscImageChef.Core
if(AllStats.MediaImages == null) AllStats.MediaImages = new List<NameValueStats>();
if(CurrentStats.MediaImages == null) CurrentStats.MediaImages = new List<NameValueStats>();
NameValueStats old = null;
foreach(NameValueStats nvs in AllStats.MediaImages)
if(nvs.name == format)
{
old = nvs;
break;
}
NameValueStats old = AllStats.MediaImages.FirstOrDefault(nvs => nvs.name == format);
NameValueStats nw = new NameValueStats();
if(old != null)
@@ -511,13 +466,7 @@ namespace DiscImageChef.Core
}
AllStats.MediaImages.Add(nw);
old = null;
foreach(NameValueStats nvs in CurrentStats.MediaImages)
if(nvs.name == format)
{
old = nvs;
break;
}
old = CurrentStats.MediaImages.FirstOrDefault(nvs => nvs.name == format);
nw = new NameValueStats();
if(old != null)
@@ -546,14 +495,7 @@ namespace DiscImageChef.Core
else if(dev.IsFireWire) deviceBus = "FireWire";
else deviceBus = dev.Type.ToString();
DeviceStats old = null;
foreach(DeviceStats ds in AllStats.Devices)
if(ds.Manufacturer == dev.Manufacturer && ds.Model == dev.Model && ds.Revision == dev.Revision &&
ds.Bus == deviceBus)
{
old = ds;
break;
}
DeviceStats old = AllStats.Devices.FirstOrDefault(ds => ds.Manufacturer == dev.Manufacturer && ds.Model == dev.Model && ds.Revision == dev.Revision && ds.Bus == deviceBus);
if(old != null) AllStats.Devices.Remove(old);
@@ -565,14 +507,7 @@ namespace DiscImageChef.Core
nw.ManufacturerSpecified = true;
AllStats.Devices.Add(nw);
old = null;
foreach(DeviceStats ds in CurrentStats.Devices)
if(ds.Manufacturer == dev.Manufacturer && ds.Model == dev.Model && ds.Revision == dev.Revision &&
ds.Bus == deviceBus)
{
old = ds;
break;
}
old = CurrentStats.Devices.FirstOrDefault(ds => ds.Manufacturer == dev.Manufacturer && ds.Model == dev.Model && ds.Revision == dev.Revision && ds.Bus == deviceBus);
if(old != null) CurrentStats.Devices.Remove(old);
@@ -592,13 +527,7 @@ namespace DiscImageChef.Core
if(AllStats.Medias == null) AllStats.Medias = new List<MediaStats>();
if(CurrentStats.Medias == null) CurrentStats.Medias = new List<MediaStats>();
MediaStats old = null;
foreach(MediaStats ms in AllStats.Medias)
if(ms.real == real && ms.type == type.ToString())
{
old = ms;
break;
}
MediaStats old = AllStats.Medias.FirstOrDefault(ms => ms.real == real && ms.type == type.ToString());
MediaStats nw = new MediaStats();
if(old != null)
@@ -616,13 +545,7 @@ namespace DiscImageChef.Core
}
AllStats.Medias.Add(nw);
old = null;
foreach(MediaStats ms in CurrentStats.Medias)
if(ms.real == real && ms.type == type.ToString())
{
old = ms;
break;
}
old = CurrentStats.Medias.FirstOrDefault(ms => ms.real == real && ms.type == type.ToString());
nw = new MediaStats();
if(old != null)