Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sicos2002 committed Mar 18, 2021
1 parent b27103e commit 4f9e749
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 44 deletions.
Binary file not shown.
17 changes: 5 additions & 12 deletions IFilterTextReader/FilterReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public override int Read(char[] buffer, int index, int count)
{
if (_charsLeftFromLastRead != null)
{
var charsToCopy = (_charsLeftFromLastRead.Length < count - charsRead)
var charsToCopy = _charsLeftFromLastRead.Length < count - charsRead
? _charsLeftFromLastRead.Length
: count - charsRead;

Expand Down Expand Up @@ -476,11 +476,8 @@ public override int Read(char[] buffer, int index, int count)
var result = _filter.GetChunk(_chunkBuffer);
_chunkValid = result == NativeMethods.IFilterReturnCode.S_OK;


if (_chunkValid)
{
_chunk = Marshal.PtrToStructure<NativeMethods.STAT_CHUNK>(_chunkBuffer);
}

switch (result)
{
Expand Down Expand Up @@ -522,7 +519,7 @@ public override int Read(char[] buffer, int index, int count)
var pvValue = new NativeMethods.PROPVARIANT();

// To convert from our C# PropVariant to the interop IntPtr:
IntPtr valuePtr = Marshal.AllocHGlobal(Marshal.SizeOf(pvValue));
var valuePtr = Marshal.AllocHGlobal(Marshal.SizeOf(pvValue));
Marshal.StructureToPtr(pvValue, valuePtr, false);

try
Expand Down Expand Up @@ -706,21 +703,17 @@ private void CheckResult(NativeMethods.IFilterReturnCode result)
switch (result)
{
case NativeMethods.IFilterReturnCode.FILTER_E_PASSWORD:
throw new IFFileIsPasswordProtected("The file '" + _fileName +
"' or a file inside this file (e.g. in the case of a ZIP) is password protected");
throw new IFFileIsPasswordProtected($"The file '{_fileName}' or a file inside this file (e.g. in the case of a ZIP) is password protected");

case NativeMethods.IFilterReturnCode.E_ACCESSDENIED:
case NativeMethods.IFilterReturnCode.FILTER_E_ACCESS:
throw new IFAccessFailure("Unable to acces the IFilter or file");

case NativeMethods.IFilterReturnCode.E_OUTOFMEMORY:
throw new OutOfMemoryException("Not enough memory to proceed reading the file '" +
_fileName +
"'");
throw new OutOfMemoryException($"Not enough memory to proceed reading the file '{_fileName}'");

case NativeMethods.IFilterReturnCode.FILTER_E_UNKNOWNFORMAT:
throw new IFUnknownFormat("The file '" + _fileName +
"' is not in the format the IFilter would expect it to be");
throw new IFUnknownFormat($"The file '{_fileName}' is not in the format the IFilter would expect it to be");
}
}
#endregion
Expand Down
17 changes: 6 additions & 11 deletions IFilterTextReader/IStreamWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal class IStreamWrapper : IStream
/// <param name="stream"></param>
public IStreamWrapper(Stream stream)
{
// ReSharper disable once LocalizableElement
_stream = stream ?? throw new ArgumentNullException(nameof(stream), "Can't wrap null stream.");
}
#endregion
Expand All @@ -61,12 +62,10 @@ public IStreamWrapper(Stream stream)
/// <param name="pcbRead"></param>
public void Read(byte[] pv, int cb, IntPtr pcbRead)
{
int bytesRead = _stream.Read(pv, 0, cb);
var bytesRead = _stream.Read(pv, 0, cb);

if (pcbRead != IntPtr.Zero)
{
Marshal.WriteInt32(pcbRead, bytesRead);
}

}
#endregion
Expand All @@ -83,9 +82,7 @@ public void Write(byte[] pv, int cb, IntPtr pcbWritten)
_stream.Write(pv, 0, cb);

if (pcbWritten != IntPtr.Zero)
{
Marshal.WriteInt32(pcbWritten, cb);
}
}
#endregion

Expand All @@ -98,12 +95,10 @@ public void Write(byte[] pv, int cb, IntPtr pcbWritten)
/// <param name="plibNewPosition"></param>
public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
{
long newPosition = _stream.Seek(dlibMove, (SeekOrigin)dwOrigin);
var newPosition = _stream.Seek(dlibMove, (SeekOrigin)dwOrigin);

if (plibNewPosition != IntPtr.Zero)
{
Marshal.WriteInt64(plibNewPosition, newPosition);
}
}
#endregion

Expand Down Expand Up @@ -208,10 +203,10 @@ public void UnlockRegion(long libOffset, long cb, int dwLockType)
/// </summary>
/// <param name="pstatstg"></param>
/// <param name="grfStatFlag"></param>
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag)
public void Stat(out STATSTG pstatstg, int grfStatFlag)
{
//IStreamWrapper wants the length
var tempStatstg = new System.Runtime.InteropServices.ComTypes.STATSTG {cbSize = _stream.Length};
// IStreamWrapper wants the length
var tempStatstg = new STATSTG {cbSize = _stream.Length};
pstatstg = tempStatstg;
}
#endregion
Expand Down
18 changes: 4 additions & 14 deletions IFilterTextReader/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,15 @@ public Job()
{
_jobHandle = NativeMethods.CreateJobObject(IntPtr.Zero, null);

var info = new NativeMethods.JOBOBJECT_BASIC_LIMIT_INFORMATION
{
LimitFlags = 0x2000
};

var extendedInfo = new NativeMethods.JOBOBJECT_EXTENDED_LIMIT_INFORMATION
{
BasicLimitInformation = info
};
var info = new NativeMethods.JOBOBJECT_BASIC_LIMIT_INFORMATION {LimitFlags = 0x2000};
var extendedInfo = new NativeMethods.JOBOBJECT_EXTENDED_LIMIT_INFORMATION {BasicLimitInformation = info};

var length = Marshal.SizeOf(typeof(NativeMethods.JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
var extendedInfoPtr = Marshal.AllocHGlobal(length);
Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);

if (!NativeMethods.SetInformationJobObject(_jobHandle, NativeMethods.JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length))
throw new Exception(string.Format("Unable to set information. Error: {0}", Marshal.GetLastWin32Error()));
throw new Exception($"Unable to set information. Error: {Marshal.GetLastWin32Error()}");
}
#endregion

Expand All @@ -81,15 +74,12 @@ public void Dispose()
/// Disposes this object
/// </summary>
/// <param name="disposing"></param>
// ReSharper disable once UnusedParameter.Local
private void Dispose(bool disposing)
{
if (_disposed)
return;

if (disposing)
{
}

NativeMethods.CloseHandle(_jobHandle);
_jobHandle = IntPtr.Zero;
_disposed = true;
Expand Down
9 changes: 2 additions & 7 deletions IFilterTextReader/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,7 @@ internal interface IPersistStream : IPersist
[DllImport("ole32.dll")]
internal static extern int CreateStreamOnHGlobal(IntPtr hGlobal, bool fDeleteOnRelease, out IStream ppstm);

[
System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", MessageId = "1"),
DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)
]
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
internal static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
Expand All @@ -894,9 +891,7 @@ internal interface IPersistStream : IPersist
internal static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("propsys.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern uint PSGetNameFromPropertyKey(
ref PROPERTYKEY propkey,
[Out, MarshalAs(UnmanagedType.LPWStr)] out string ppszCanonicalName);
internal static extern uint PSGetNameFromPropertyKey(ref PROPERTYKEY propkey, [Out, MarshalAs(UnmanagedType.LPWStr)] out string ppszCanonicalName);

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
internal static extern IntPtr CreateJobObject(IntPtr a, string lpName);
Expand Down
2 changes: 2 additions & 0 deletions IFilterTextReader/UnmappedPropertyEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
//

using System;
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global

namespace IFilterTextReader
{
Expand Down

0 comments on commit 4f9e749

Please sign in to comment.