Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Random code standard cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
squid-box committed Mar 31, 2023
1 parent cdcae1e commit 6a22efd
Show file tree
Hide file tree
Showing 39 changed files with 540 additions and 547 deletions.
24 changes: 10 additions & 14 deletions SevenZip.Tests/SevenZipExtractorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public void ExtractionWithCancellationTest()
{
using var tmp = new SevenZipExtractor(@"TestData\multiple_files.7z");

tmp.FileExtractionStarted += (s, e) =>
tmp.FileExtractionStarted += (_, args) =>
{
if (e.FileInfo.Index == 2)
if (args.FileInfo.Index == 2)
{
e.Cancel = true;
args.Cancel = true;
}
};

Expand All @@ -95,11 +95,11 @@ public void ExtractionWithSkipTest()
{
using var tmp = new SevenZipExtractor(@"TestData\multiple_files.7z");

tmp.FileExtractionStarted += (s, e) =>
tmp.FileExtractionStarted += (_, args) =>
{
if (e.FileInfo.Index == 1)
if (args.FileInfo.Index == 1)
{
e.Skip = true;
args.Skip = true;
}
};

Expand Down Expand Up @@ -178,17 +178,13 @@ public void ThreadedExtractionTest()

var t1 = new Thread(() =>
{
using (var tmp = new SevenZipExtractor(@"TestData\multiple_files.7z"))
{
tmp.ExtractArchive(destination1);
}
using var tmp = new SevenZipExtractor(@"TestData\multiple_files.7z");
tmp.ExtractArchive(destination1);
});
var t2 = new Thread(() =>
{
using (var tmp = new SevenZipExtractor(@"TestData\multiple_files.7z"))
{
tmp.ExtractArchive(destination2);
}
using var tmp = new SevenZipExtractor(@"TestData\multiple_files.7z");
tmp.ExtractArchive(destination2);
});

t1.Start();
Expand Down
4 changes: 2 additions & 2 deletions SevenZip/ArchiveOpenCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal sealed class ArchiveOpenCallback : CallbackBase, IArchiveOpenCallback,
/// <param name="fileName">Volume file name.</param>
private void Init(string fileName)
{
if (!String.IsNullOrEmpty(fileName))
if (!string.IsNullOrEmpty(fileName))
{
_fileInfo = new FileInfo(fileName);
_volumeFileNames.Add(fileName);
Expand Down Expand Up @@ -98,7 +98,7 @@ public int GetProperty(ItemPropId propId, ref PropVariant value)
break;
case ItemPropId.Size:
value.VarType = VarEnum.VT_UI8;
value.UInt64Value = (UInt64) _fileInfo.Length;
value.UInt64Value = (ulong) _fileInfo.Length;
break;
case ItemPropId.Attributes:
value.VarType = VarEnum.VT_UI4;
Expand Down
71 changes: 34 additions & 37 deletions SevenZip/ArchiveUpdateCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ private bool EventsForGetStream(uint index)
var fiea = new FileNameEventArgs(_files != null? _files[index].Name : _entries[index],
PercentDoneEventArgs.ProducePercentDone(_doneRate));
OnFileCompression(fiea);

if (fiea.Cancel)
{
Canceled = true;
Expand Down Expand Up @@ -314,26 +315,17 @@ private bool EventsForGetStream(uint index)

private void OnFileCompression(FileNameEventArgs e)
{
if (FileCompressionStarted != null)
{
FileCompressionStarted(this, e);
}
FileCompressionStarted?.Invoke(this, e);
}

private void OnCompressing(ProgressEventArgs e)
{
if (Compressing != null)
{
Compressing(this, e);
}
Compressing?.Invoke(this, e);
}

private void OnFileCompressionFinished(EventArgs e)
{
if (FileCompressionFinished != null)
{
FileCompressionFinished(this, e);
}
FileCompressionFinished?.Invoke(this, e);
}

#endregion
Expand All @@ -351,7 +343,7 @@ public int GetUpdateItemInfo(uint index, ref int newData, ref int newProperties,
case InternalCompressionMode.Create:
newData = 1;
newProperties = 1;
indexInArchive = UInt32.MaxValue;
indexInArchive = uint.MaxValue;
break;
case InternalCompressionMode.Append:
if (index < _indexInArchive)
Expand All @@ -364,7 +356,7 @@ public int GetUpdateItemInfo(uint index, ref int newData, ref int newProperties,
{
newData = 1;
newProperties = 1;
indexInArchive = UInt32.MaxValue;
indexInArchive = uint.MaxValue;
}
break;
case InternalCompressionMode.Modify:
Expand All @@ -374,24 +366,28 @@ public int GetUpdateItemInfo(uint index, ref int newData, ref int newProperties,
if (_updateData.FileNamesToModify.ContainsKey((int)index)
&& _updateData.FileNamesToModify[(int)index] == null)
{
indexInArchive = (UInt32)_updateData.ArchiveFileData.Count;
foreach (KeyValuePair<Int32, string> pairModification in _updateData.FileNamesToModify)
if ((pairModification.Key <= index) && (pairModification.Value == null))
indexInArchive = (uint)_updateData.ArchiveFileData.Count;

foreach (var pairModification in _updateData.FileNamesToModify)
{
if (pairModification.Key <= index && (pairModification.Value == null))
{
do
{
indexInArchive--;
}
while ((indexInArchive > 0) && _updateData.FileNamesToModify.ContainsKey((Int32)indexInArchive)
&& (_updateData.FileNamesToModify[(Int32)indexInArchive] == null));
} while (indexInArchive > 0
&& _updateData.FileNamesToModify.ContainsKey((int)indexInArchive)
&& _updateData.FileNamesToModify[(int)indexInArchive] == null);
}
}
}
else
{
indexInArchive = index;
}
break;
}

return 0;
}

Expand Down Expand Up @@ -476,7 +472,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
#region Size

value.VarType = VarEnum.VT_UI8;
UInt64 size;
ulong size;
if (_updateData.Mode != InternalCompressionMode.Modify)
{
if (_files == null)
Expand Down Expand Up @@ -739,8 +735,9 @@ public void SetOperationResult(OperationResult operationResult)

public int CryptoGetTextPassword2(ref int passwordIsDefined, out string password)
{
passwordIsDefined = String.IsNullOrEmpty(Password) ? 0 : 1;
passwordIsDefined = string.IsNullOrEmpty(Password) ? 0 : 1;
password = Password;

return 0;
}

Expand All @@ -761,19 +758,19 @@ public void Dispose()
catch (ObjectDisposedException) {}
}

if (_wrappersToDispose != null)
if (_wrappersToDispose == null)
{
return;
}

foreach (var wrapper in _wrappersToDispose)
{
foreach (var wrapper in _wrappersToDispose)
try
{
try
{
wrapper.Dispose();
}
catch (ObjectDisposedException) {}
wrapper.Dispose();
}
catch (ObjectDisposedException) {}
}

GC.SuppressFinalize(this);
}

#endregion
Expand All @@ -784,23 +781,23 @@ private void IntEventArgsHandler(object sender, IntEventArgs e)

lock (lockObject)
{
var pold = (byte) (_bytesWrittenOld*100/_bytesCount);
var pOld = (byte) (_bytesWrittenOld*100/_bytesCount);
_bytesWritten += e.Value;
byte pnow;
byte pNow;

if (_bytesCount < _bytesWritten) //Holy shit, this check for ZIP is golden
{
pnow = 100;
pNow = 100;
}
else
{
pnow = (byte)((_bytesWritten * 100) / _bytesCount);
pNow = (byte)((_bytesWritten * 100) / _bytesCount);
}

if (pnow > pold)
if (pNow > pOld)
{
_bytesWrittenOld = _bytesWritten;
OnCompressing(new ProgressEventArgs(pnow, (byte) (pnow - pold)));
OnCompressing(new ProgressEventArgs(pNow, (byte) (pNow - pOld)));
}
}
}
Expand Down
28 changes: 13 additions & 15 deletions SevenZip/CallbackBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@ namespace SevenZip

internal class CallbackBase : MarshalByRefObject
{
private readonly string _password;
private readonly bool _reportErrors;

/// <summary>
/// User exceptions thrown during the requested operations, for example, in events.
/// </summary>
private readonly List<Exception> _exceptions = new List<Exception>();

#region Constructors

/// <summary>
/// Initializes a new instance of the CallbackBase class.
/// </summary>
protected CallbackBase()
{
_password = "";
_reportErrors = true;
Password = "";
ReportErrors = true;
}

/// <summary>
Expand All @@ -32,19 +28,19 @@ protected CallbackBase()
/// <param name="password">The archive password.</param>
protected CallbackBase(string password)
{
if (String.IsNullOrEmpty(password))
if (string.IsNullOrEmpty(password))
{
throw new SevenZipException("Empty password was specified.");
}
_password = password;
_reportErrors = true;

Password = password;
ReportErrors = true;
}
#endregion

/// <summary>
/// Gets or sets the archive password
/// </summary>
public string Password => _password;
public string Password { get; }

/// <summary>
/// Gets or sets the value indicating whether the current procedure was cancelled.
Expand All @@ -54,7 +50,7 @@ protected CallbackBase(string password)
/// <summary>
/// Gets or sets throw exceptions on archive errors flag
/// </summary>
public bool ReportErrors => _reportErrors;
public bool ReportErrors { get; }

/// <summary>
/// Gets the user exceptions thrown during the requested operations, for example, in events.
Expand All @@ -80,10 +76,11 @@ public void ClearExceptions()
/// <param name="handler">The handler responsible for the exception.</param>
public bool ThrowException(CallbackBase handler, params Exception[] e)
{
if (_reportErrors && (handler == null || !handler.Canceled))
if (ReportErrors && (handler == null || !handler.Canceled))
{
throw e[0];
}

return false;
}

Expand All @@ -93,10 +90,11 @@ public bool ThrowException(CallbackBase handler, params Exception[] e)
/// <returns>True means no exceptions.</returns>
public bool ThrowException()
{
if (HasExceptions && _reportErrors)
if (HasExceptions && ReportErrors)
{
throw _exceptions[0];
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion SevenZip/EventArguments/ExtractFileCallbackReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum ExtractFileCallbackReason
Done,

/// <summary>
/// An exception occured during extraction of the file.
/// An exception occurred during extraction of the file.
/// </summary>
Failure
}
Expand Down
6 changes: 2 additions & 4 deletions SevenZip/EventArguments/FileInfoEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
/// </summary>
public sealed class FileInfoEventArgs : PercentDoneEventArgs, ICancellable
{
private readonly ArchiveFileInfo _fileInfo;

/// <summary>
/// Initializes a new instance of the FileInfoEventArgs class.
/// </summary>
Expand All @@ -15,7 +13,7 @@ public sealed class FileInfoEventArgs : PercentDoneEventArgs, ICancellable
public FileInfoEventArgs(ArchiveFileInfo fileInfo, byte percentDone)
: base(percentDone)
{
_fileInfo = fileInfo;
FileInfo = fileInfo;
}

/// <summary>
Expand All @@ -31,6 +29,6 @@ public FileInfoEventArgs(ArchiveFileInfo fileInfo, byte percentDone)
/// <summary>
/// Gets the corresponding FileInfo to the event.
/// </summary>
public ArchiveFileInfo FileInfo => _fileInfo;
public ArchiveFileInfo FileInfo { get; }
}
}
6 changes: 2 additions & 4 deletions SevenZip/EventArguments/FileNameEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace SevenZip
/// </summary>
public sealed class FileNameEventArgs : PercentDoneEventArgs, ICancellable
{
private readonly string _fileName;

/// <summary>
/// Initializes a new instance of the FileNameEventArgs class.
/// </summary>
Expand All @@ -19,7 +17,7 @@ public sealed class FileNameEventArgs : PercentDoneEventArgs, ICancellable
public FileNameEventArgs(string fileName, byte percentDone) :
base(percentDone)
{
_fileName = fileName;
FileName = fileName;
}

/// <summary>
Expand All @@ -39,7 +37,7 @@ public bool Skip
/// <summary>
/// Gets the file name.
/// </summary>
public string FileName => _fileName;
public string FileName { get; }
}
}

Expand Down
Loading

0 comments on commit 6a22efd

Please sign in to comment.