DefaultFileImpl -> FileStream

This commit is contained in:
Matt Nadareski
2022-05-19 22:32:58 -07:00
parent ddb2be278a
commit 3ea294f4d9
19 changed files with 107 additions and 119 deletions

View File

@@ -8,6 +8,7 @@
*/
using System;
using System.IO;
namespace LibMSPackSharp.CAB
{
@@ -66,12 +67,12 @@ namespace LibMSPackSharp.CAB
/// <summary>
/// Input file handle
/// </summary>
public DefaultFileImpl InputFileHandle { get; set; }
public FileStream InputFileHandle { get; set; }
/// <summary>
/// Output file handle
/// </summary>
public DefaultFileImpl OutputFileHandle { get; set; }
public FileStream OutputFileHandle { get; set; }
/// <summary>
/// Input data consumed

View File

@@ -15,6 +15,7 @@
*/
using System;
using System.IO;
using System.Text;
using LibMSPackSharp.Compression;
using static LibMSPackSharp.CAB.Constants;
@@ -72,7 +73,7 @@ namespace LibMSPackSharp.CAB
/// <see cref="Error"/>
public Cabinet Open(string filename)
{
DefaultFileImpl fileHandle = System.Open(filename, OpenMode.MSPACK_SYS_OPEN_READ);
FileStream fileHandle = System.Open(filename, OpenMode.MSPACK_SYS_OPEN_READ);
if (fileHandle == null)
{
Error = Error.MSPACK_ERR_OPEN;
@@ -211,7 +212,7 @@ namespace LibMSPackSharp.CAB
}
// Open file and get its full file length
DefaultFileImpl fh; Cabinet cab = null;
FileStream fh; Cabinet cab = null;
if ((fh = System.Open(filename, OpenMode.MSPACK_SYS_OPEN_READ)) != null)
{
long firstlen = 0;
@@ -402,7 +403,7 @@ namespace LibMSPackSharp.CAB
}
// Open file for output
DefaultFileImpl fh = System.Open(filename, OpenMode.MSPACK_SYS_OPEN_WRITE);
FileStream fh = System.Open(filename, OpenMode.MSPACK_SYS_OPEN_WRITE);
if (fh == null)
return Error = Error.MSPACK_ERR_OPEN;
@@ -634,9 +635,9 @@ namespace LibMSPackSharp.CAB
return bytes - todo;
}
else if (file is DefaultFileImpl impl)
else if (file is FileStream impl)
{
return SystemImpl.DefaultSystem.Read(file, buffer, pointer, bytes);
return SystemImpl.DefaultSystem.Read(impl, buffer, pointer, bytes);
}
return -1;
@@ -658,9 +659,9 @@ namespace LibMSPackSharp.CAB
return bytes;
}
else if (file is DefaultFileImpl impl)
else if (file is FileStream impl)
{
return SystemImpl.DefaultSystem.Write(file, buffer, pointer, bytes);
return SystemImpl.DefaultSystem.Write(impl, buffer, pointer, bytes);
}
// Unknown file to write to
@@ -870,7 +871,7 @@ namespace LibMSPackSharp.CAB
/// The inner loop of <see cref="Search(Decompressor, string)"/>, to make it easier to
/// break out of the loop and be sure that all resources are freed
/// </summary>
private Error Find(byte[] buf, DefaultFileImpl fh, string filename, long flen, ref long firstlen, out Cabinet firstcab)
private Error Find(byte[] buf, FileStream fh, string filename, long flen, ref long firstlen, out Cabinet firstcab)
{
firstcab = null;
Cabinet cab, link = null;
@@ -1213,7 +1214,7 @@ namespace LibMSPackSharp.CAB
/// Fills out a pre-existing Cabinet structure, allocates memory
/// for folders and files as necessary
/// </summary>
private Error ReadHeaders(DefaultFileImpl fh, Cabinet cab, long offset, bool salvage, bool quiet)
private Error ReadHeaders(FileStream fh, Cabinet cab, long offset, bool salvage, bool quiet)
{
Error err = Error.MSPACK_ERR_OK;
Folder fol, linkfol = null;
@@ -1436,7 +1437,7 @@ namespace LibMSPackSharp.CAB
return Error.MSPACK_ERR_OK;
}
private string ReadString(DefaultFileImpl fh, bool permitEmpty, ref Error error)
private string ReadString(FileStream fh, bool permitEmpty, ref Error error)
{
long position = System.Tell(fh);
byte[] buf = new byte[256];

View File

@@ -7,6 +7,7 @@
* For further details, see the file COPYING.LIB distributed with libmspack
*/
using System.IO;
using LibMSPackSharp.Compression;
namespace LibMSPackSharp.CHM
@@ -41,11 +42,11 @@ namespace LibMSPackSharp.CHM
/// <summary>
/// Input file handle
/// </summary>
public DefaultFileImpl InputFileHandle { get; set; }
public FileStream InputFileHandle { get; set; }
/// <summary>
/// Output file handle
/// </summary>
public DefaultFileImpl OutputFileHandle { get; set; }
public FileStream OutputFileHandle { get; set; }
}
}

View File

@@ -8,6 +8,7 @@
*/
using System;
using System.IO;
using System.Linq;
using System.Text;
using LibMSPackSharp.Compression;
@@ -152,7 +153,7 @@ namespace LibMSPackSharp.CHM
SystemImpl sys = self.System;
DefaultFileImpl fh;
FileStream fh;
if ((fh = sys.Open(filename, OpenMode.MSPACK_SYS_OPEN_READ)) != null)
{
chm = new Header();
@@ -249,7 +250,7 @@ namespace LibMSPackSharp.CHM
/// non-zero, all file entries will also be read. fills out a pre-existing
/// mschmd_header structure, allocates memory for files as necessary
/// </summary>
public static Error ReadHeaders(SystemImpl sys, DefaultFileImpl fh, Header chm, bool entire)
public static Error ReadHeaders(SystemImpl sys, FileStream fh, Header chm, bool entire)
{
uint section, nameLen, x, errors, numChunks;
byte[] buf = new byte[0x54];
@@ -569,7 +570,7 @@ namespace LibMSPackSharp.CHM
{
DecompressorImpl self = d as DecompressorImpl;
SystemImpl sys;
DefaultFileImpl fh;
FileStream fh;
// p and end are initialised to prevent MSVC warning about "potentially"
// uninitialised usage. This is provably untrue, but MS won't fix:
@@ -699,7 +700,7 @@ namespace LibMSPackSharp.CHM
/// Reads the given chunk into memory, storing it in a chunk cache
/// so it doesn't need to be read from disk more than once
/// </summary>
public static byte[] ReadChunk(DecompressorImpl self, Header chm, DefaultFileImpl fh, uint chunkNum)
public static byte[] ReadChunk(DecompressorImpl self, Header chm, FileStream fh, uint chunkNum)
{
SystemImpl sys = self.System;
@@ -1003,7 +1004,7 @@ namespace LibMSPackSharp.CHM
}
// Open file for output
DefaultFileImpl fh;
FileStream fh;
if ((fh = sys.Open(filename, OpenMode.MSPACK_SYS_OPEN_WRITE)) == null)
return self.Error = Error.MSPACK_ERR_OPEN;
@@ -1120,9 +1121,9 @@ namespace LibMSPackSharp.CHM
return bytes;
}
else if (file is DefaultFileImpl impl)
else if (file is FileStream impl)
{
return SystemImpl.DefaultSystem.Write(file, buffer, offset, bytes);
return SystemImpl.DefaultSystem.Write(impl, buffer, offset, bytes);
}
// Unknown file to write to

View File

@@ -11,6 +11,7 @@
*/
using System;
using System.IO;
namespace LibMSPackSharp.Compression
{
@@ -28,12 +29,12 @@ namespace LibMSPackSharp.Compression
/// <summary>
/// Input file handle
/// </summary>
public object Input { get; set; }
public FileStream Input { get; set; }
/// <summary>
/// Output file handle
/// </summary>
public object Output { get; set; }
public FileStream Output { get; set; }
public Error Error { get; set; }

View File

@@ -11,6 +11,8 @@
* For further details, see the file COPYING.LIB distributed with libmspack
*/
using System.IO;
namespace LibMSPackSharp.Compression
{
public class LZSS
@@ -54,7 +56,7 @@ namespace LibMSPackSharp.Compression
/// </param>
/// <param name="mode">one of LZSSMode values</param>
/// <returns>an error code, or MSPACK_ERR_OK if successful</returns>
public static Error Decompress(SystemImpl system, object input, object output, int inputBufferSize, LZSSMode mode)
public static Error Decompress(SystemImpl system, FileStream input, FileStream output, int inputBufferSize, LZSSMode mode)
{
uint i, c, mpos, len;
int read;

View File

@@ -75,6 +75,7 @@
*/
using System;
using System.IO;
namespace LibMSPackSharp.Compression
{
@@ -267,7 +268,7 @@ namespace LibMSPackSharp.Compression
/// a pointer to an initialised LZXDStream structure, or null if
/// there was not enough memory or parameters to the function were wrong.
/// </returns>
public static LZXDStream Init(SystemImpl system, DefaultFileImpl input, DefaultFileImpl output, int windowBits, int resetInterval, int inputBufferSize, long outputLength, bool isDelta)
public static LZXDStream Init(SystemImpl system, FileStream input, FileStream output, int windowBits, int resetInterval, int inputBufferSize, long outputLength, bool isDelta)
{
uint windowSize = (uint)(1 << windowBits);
@@ -354,7 +355,7 @@ namespace LibMSPackSharp.Compression
/// than the LZX window size.
/// </param>
/// <returns>an error code, or MSPACK_ERR_OK if successful</returns>
public static Error SetReferenceData(LZXDStream lzx, SystemImpl system, object input, uint length)
public static Error SetReferenceData(LZXDStream lzx, SystemImpl system, FileStream input, uint length)
{
if (lzx == null)
return Error.MSPACK_ERR_ARGS;

View File

@@ -11,6 +11,7 @@
*/
using System;
using System.IO;
namespace LibMSPackSharp.Compression
{
@@ -93,7 +94,7 @@ namespace LibMSPackSharp.Compression
/// and 'holes' left will be filled with zero bytes. This allows at least
/// a partial recovery of erroneous data.
/// </summary>
public static MSZIPDStream Init(SystemImpl system, DefaultFileImpl input, DefaultFileImpl output, int input_buffer_size, bool repair_mode)
public static MSZIPDStream Init(SystemImpl system, FileStream input, FileStream output, int input_buffer_size, bool repair_mode)
{
if (system == null)
return null;
@@ -237,7 +238,7 @@ namespace LibMSPackSharp.Compression
// Write a frame
i = (out_bytes < zip.BytesOutput) ? (int)out_bytes : zip.BytesOutput;
if (zip.Output is DefaultFileImpl)
if (zip.Output is FileStream)
{
if (SystemImpl.DefaultSystem.Write(zip.Output, zip.Window, zip.OutputPointer, i) != i)
return zip.Error = Error.MSPACK_ERR_WRITE;

View File

@@ -7,28 +7,22 @@
* For further details, see the file COPYING.LIB distributed with libmspack
*/
using System.IO;
namespace LibMSPackSharp.Compression
{
public class None
{
public static NoneState Init(SystemImpl sys, DefaultFileImpl input, DefaultFileImpl output, int bufsize)
public static NoneState Init(SystemImpl sys, FileStream input, FileStream output, int bufsize)
{
NoneState state = new NoneState();
byte[] buf = new byte[bufsize];
if (state != null && buf != null)
return new NoneState()
{
state.Sys = sys;
state.Input = input;
state.Output = output;
state.Buffer = buf;
state.BufferSize = bufsize;
}
else
{
state = null;
}
return state;
Sys = sys,
Input = input,
Output = output,
Buffer = new byte[bufsize],
BufferSize = bufsize,
};
}
public static Error Decompress(object s, long bytes)

View File

@@ -7,6 +7,8 @@
* For further details, see the file COPYING.LIB distributed with libmspack
*/
using System.IO;
namespace LibMSPackSharp.Compression
{
/// <summary>
@@ -16,9 +18,9 @@ namespace LibMSPackSharp.Compression
{
public SystemImpl Sys { get; set; }
public object Input { get; set; }
public FileStream Input { get; set; }
public object Output { get; set; }
public FileStream Output { get; set; }
public byte[] Buffer { get; set; }

View File

@@ -21,6 +21,7 @@
*/
using System;
using System.IO;
namespace LibMSPackSharp.Compression
{
@@ -91,7 +92,7 @@ namespace LibMSPackSharp.Compression
/// - window_bits is the size of the Quantum window, from 1Kb(10) to 2Mb(21).
/// - input_buffer_size is the number of bytes to use to store bitstream data.
/// </summary>
public static QTMDStream Init(SystemImpl system, DefaultFileImpl input, DefaultFileImpl output, int window_bits, int input_buffer_size)
public static QTMDStream Init(SystemImpl system, FileStream input, FileStream output, int window_bits, int input_buffer_size)
{
uint window_size = (uint)(1 << window_bits);

View File

@@ -1,20 +0,0 @@
/* This file is part of libmspack.
* (C) 2003-2004 Stuart Caie.
*
* libmspack is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License (LGPL) version 2.1
*
* For further details, see the file COPYING.LIB distributed with libmspack
*/
using System.IO;
namespace LibMSPackSharp
{
public class DefaultFileImpl
{
public Stream FileHandle { get; set; }
public string Name { get; set; }
};
}

View File

@@ -7,10 +7,12 @@
* For further details, see the file COPYING.LIB distributed with libmspack
*/
using System.IO;
namespace LibMSPackSharp.KWAJ
{
public class HeaderImpl : Header
{
public DefaultFileImpl FileHandle { get; set; }
public FileStream FileHandle { get; set; }
}
}

View File

@@ -8,6 +8,7 @@
*/
using System;
using System.IO;
using System.Text;
using LibMSPackSharp.Compression;
@@ -76,7 +77,7 @@ namespace LibMSPackSharp.KWAJ
SystemImpl sys = self.System;
DefaultFileImpl fh = sys.Open(filename, OpenMode.MSPACK_SYS_OPEN_READ);
FileStream fh = sys.Open(filename, OpenMode.MSPACK_SYS_OPEN_READ);
HeaderImpl hdr = new HeaderImpl();
if (fh != null && hdr != null)
{
@@ -130,7 +131,7 @@ namespace LibMSPackSharp.KWAJ
/// <summary>
/// Reads the headers of a KWAJ format file
/// </summary>
public static Error ReadHeaders(SystemImpl sys, DefaultFileImpl fh, Header hdr)
public static Error ReadHeaders(SystemImpl sys, FileStream fh, Header hdr)
{
int i;
@@ -283,7 +284,7 @@ namespace LibMSPackSharp.KWAJ
return self.Error = Error.MSPACK_ERR_ARGS;
SystemImpl sys = self.System;
DefaultFileImpl fh = (hdr as HeaderImpl)?.FileHandle;
FileStream fh = (hdr as HeaderImpl)?.FileHandle;
if (fh == null)
return Error.MSPACK_ERR_ARGS;
@@ -292,7 +293,7 @@ namespace LibMSPackSharp.KWAJ
return self.Error = Error.MSPACK_ERR_SEEK;
// Open file for output
DefaultFileImpl outfh;
FileStream outfh;
if ((outfh = sys.Open(filename, OpenMode.MSPACK_SYS_OPEN_WRITE)) == null)
return self.Error = Error.MSPACK_ERR_OPEN;
@@ -401,7 +402,7 @@ namespace LibMSPackSharp.KWAJ
* 2 fake bytes in then stops), so we implement our own.
*/
private static InternalStream LZHInit(SystemImpl sys, object input, object output)
private static InternalStream LZHInit(SystemImpl sys, FileStream input, FileStream output)
{
if (sys == null || input == null || output == null)
return null;

View File

@@ -21,6 +21,7 @@
*/
using System;
using System.IO;
using LibMSPackSharp.Compression;
namespace LibMSPackSharp.OAB
@@ -93,7 +94,7 @@ namespace LibMSPackSharp.OAB
return bytes_written;
}
else if (baseFile is DefaultFileImpl impl)
else if (baseFile is FileStream impl)
{
return SystemImpl.DefaultSystem.Write(impl, buf, pointer, size);
}
@@ -118,7 +119,7 @@ namespace LibMSPackSharp.OAB
SystemImpl sys = self.System;
DefaultFileImpl infh = sys.Open(input, OpenMode.MSPACK_SYS_OPEN_READ);
FileStream infh = sys.Open(input, OpenMode.MSPACK_SYS_OPEN_READ);
if (infh == null)
{
ret = Error.MSPACK_ERR_OPEN;
@@ -150,7 +151,7 @@ namespace LibMSPackSharp.OAB
uint block_max = BitConverter.ToUInt32(hdrbuf, oabhead_BlockMax);
uint target_size = BitConverter.ToUInt32(hdrbuf, oabhead_TargetSize);
DefaultFileImpl outfh = sys.Open(output, OpenMode.MSPACK_SYS_OPEN_WRITE);
FileStream outfh = sys.Open(output, OpenMode.MSPACK_SYS_OPEN_WRITE);
if (outfh == null)
{
ret = Error.MSPACK_ERR_OPEN;
@@ -243,7 +244,7 @@ namespace LibMSPackSharp.OAB
in_ofh.Available = (int)blk_csize;
out_ofh.CRC = 0xffffffff;
lzx = LZX.Init(oabd_sys, in_ofh, out_ofh, window_bits, 0, self.BufferSize, blk_dsize, true);
lzx = LZX.Init(oabd_sys, in_ofh.OrigFile, out_ofh.OrigFile, window_bits, 0, self.BufferSize, blk_dsize, true);
if (lzx == null)
{
ret = Error.MSPACK_ERR_NOMEMORY;
@@ -321,7 +322,7 @@ namespace LibMSPackSharp.OAB
SystemImpl sys = self.System;
DefaultFileImpl infh = sys.Open(input, OpenMode.MSPACK_SYS_OPEN_READ);
FileStream infh = sys.Open(input, OpenMode.MSPACK_SYS_OPEN_READ);
if (infh == null)
{
ret = Error.MSPACK_ERR_OPEN;
@@ -357,7 +358,7 @@ namespace LibMSPackSharp.OAB
if (block_max < patchblk_SIZEOF)
block_max = patchblk_SIZEOF;
DefaultFileImpl basefh = sys.Open(basePath, OpenMode.MSPACK_SYS_OPEN_READ);
FileStream basefh = sys.Open(basePath, OpenMode.MSPACK_SYS_OPEN_READ);
if (basefh == null)
{
ret = Error.MSPACK_ERR_OPEN;
@@ -369,7 +370,7 @@ namespace LibMSPackSharp.OAB
return ret;
}
DefaultFileImpl outfh = sys.Open(output, OpenMode.MSPACK_SYS_OPEN_WRITE);
FileStream outfh = sys.Open(output, OpenMode.MSPACK_SYS_OPEN_WRITE);
if (outfh == null)
{
ret = Error.MSPACK_ERR_OPEN;
@@ -440,7 +441,7 @@ namespace LibMSPackSharp.OAB
in_ofh.Available = (int)blk_csize;
out_ofh.CRC = 0xffffffff;
lzx = LZX.Init(oabd_sys, in_ofh, out_ofh, window_bits, 0, 4096, blk_dsize, true);
lzx = LZX.Init(oabd_sys, in_ofh.OrigFile, out_ofh.OrigFile, window_bits, 0, 4096, blk_dsize, true);
if (lzx == null)
{
ret = Error.MSPACK_ERR_NOMEMORY;
@@ -522,7 +523,7 @@ namespace LibMSPackSharp.OAB
return ret;
}
private static Error CopyFileHandle(SystemImpl sys, object infh, object outfh, int bytes_to_copy, byte[] buf, int buf_size)
private static Error CopyFileHandle(SystemImpl sys, FileStream infh, FileStream outfh, int bytes_to_copy, byte[] buf, int buf_size)
{
while (bytes_to_copy != 0)
{

View File

@@ -14,13 +14,15 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
using System.IO;
namespace LibMSPackSharp.OAB
{
public class InternalFile : DefaultFileImpl
public class InternalFile
{
public SystemImpl OrigSys { get; set; }
public object OrigFile { get; set; }
public FileStream OrigFile { get; set; }
public uint CRC { get; set; }

View File

@@ -7,10 +7,12 @@
* For further details, see the file COPYING.LIB distributed with libmspack
*/
using System.IO;
namespace LibMSPackSharp.SZDD
{
public class HeaderImpl : Header
{
public DefaultFileImpl FileHandle { get; set; }
public FileStream FileHandle { get; set; }
}
}

View File

@@ -8,6 +8,7 @@
*/
using System;
using System.IO;
using System.Linq;
using LibMSPackSharp.Compression;
@@ -33,7 +34,7 @@ namespace LibMSPackSharp.SZDD
SystemImpl sys = self.System;
DefaultFileImpl fh = sys.Open(filename, OpenMode.MSPACK_SYS_OPEN_READ);
FileStream fh = sys.Open(filename, OpenMode.MSPACK_SYS_OPEN_READ);
HeaderImpl hdr = new HeaderImpl();
if (fh != null && hdr != null)
{
@@ -97,7 +98,7 @@ namespace LibMSPackSharp.SZDD
/// <summary>
/// Reads the headers of an SZDD format file
/// </summary>
public static Error ReadHeaders(SystemImpl sys, object fh, Header hdr)
public static Error ReadHeaders(SystemImpl sys, FileStream fh, Header hdr)
{
// Read and check signature
byte[] buf = new byte[8];
@@ -154,7 +155,7 @@ namespace LibMSPackSharp.SZDD
SystemImpl sys = self.System;
DefaultFileImpl fh = (hdr as HeaderImpl)?.FileHandle;
FileStream fh = (hdr as HeaderImpl)?.FileHandle;
if (fh == null)
return Error.MSPACK_ERR_ARGS;
@@ -164,7 +165,7 @@ namespace LibMSPackSharp.SZDD
return self.Error = Error.MSPACK_ERR_SEEK;
// Open file for output
DefaultFileImpl outfh;
FileStream outfh;
if ((outfh = sys.Open(filename, OpenMode.MSPACK_SYS_OPEN_WRITE)) == null)
return self.Error = Error.MSPACK_ERR_OPEN;

View File

@@ -50,40 +50,33 @@ namespace LibMSPackSharp
/// </param>
/// <param name="mode">One of the <see cref="OpenMode"/> values</param>
/// <returns>
/// A pointer to a DefaultFileImpl structure. This structure officially
/// A pointer to a FileStream structure. This structure officially
/// contains no members, its true contents are up to the
/// SystemImpl implementor. It should contain whatever is needed
/// for other SystemImpl methods to operate. Returning the null
/// pointer indicates an error condition.
/// </returns>
public DefaultFileImpl Open(string filename, OpenMode mode)
public FileStream Open(string filename, OpenMode mode)
{
try
{
DefaultFileImpl fileHandle = new DefaultFileImpl();
switch (mode)
{
case OpenMode.MSPACK_SYS_OPEN_READ:
fileHandle.FileHandle = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
break;
return File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
case OpenMode.MSPACK_SYS_OPEN_WRITE:
fileHandle.FileHandle = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
break;
return File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
case OpenMode.MSPACK_SYS_OPEN_UPDATE:
fileHandle.FileHandle = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
break;
return File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
case OpenMode.MSPACK_SYS_OPEN_APPEND:
fileHandle.FileHandle = File.Open(filename, FileMode.Append, FileAccess.ReadWrite, FileShare.ReadWrite);
break;
return File.Open(filename, FileMode.Append, FileAccess.ReadWrite, FileShare.ReadWrite);
default:
return null;
}
return fileHandle;
}
catch (Exception ex)
{
@@ -98,7 +91,7 @@ namespace LibMSPackSharp
/// </summary>
/// <param name="file">the file to close</param>
/// <see cref="Open(string, OpenMode)"/>
public void Close(DefaultFileImpl file) => file?.FileHandle?.Close();
public void Close(FileStream file) => file?.Close();
/// <summary>
/// Reads a given number of bytes from an open file.
@@ -115,7 +108,7 @@ namespace LibMSPackSharp
/// </returns>
/// <see cref="Open(string, OpenMode)"/>
/// <see cref="Write"/>
public Func<object, byte[], int, int, int> Read;
public Func<FileStream, byte[], int, int, int> Read;
/// <summary>
/// Writes a given number of bytes to an open file.
@@ -132,7 +125,7 @@ namespace LibMSPackSharp
/// </returns>
/// <see cref="Open(string, OpenMode)"/>
/// <see cref="Read"/>
public Func<object, byte[], int, int, int> Write;
public Func<FileStream, byte[], int, int, int> Write;
/// <summary>
/// Seeks to a specific file offset within an open file.
@@ -152,8 +145,8 @@ namespace LibMSPackSharp
/// <param name="mode">One of the <see cref="SeekMode"/> values</param>
/// <returns>zero for success, non-zero for an error</returns>
/// <see cref="Open(string, OpenMode)"/>
/// <see cref="Tell(DefaultFileImpl)"/>
public bool Seek(DefaultFileImpl self, long offset, SeekMode mode)
/// <see cref="Tell(FileStream)"/>
public bool Seek(FileStream self, long offset, SeekMode mode)
{
if (self == null)
return false;
@@ -161,15 +154,15 @@ namespace LibMSPackSharp
switch (mode)
{
case SeekMode.MSPACK_SYS_SEEK_START:
try { self.FileHandle.Seek(offset, SeekOrigin.Begin); return true; }
try { self.Seek(offset, SeekOrigin.Begin); return true; }
catch { return false; }
case SeekMode.MSPACK_SYS_SEEK_CUR:
try { self.FileHandle.Seek(offset, SeekOrigin.Current); return true; }
try { self.Seek(offset, SeekOrigin.Current); return true; }
catch { return false; }
case SeekMode.MSPACK_SYS_SEEK_END:
try { self.FileHandle.Seek(offset, SeekOrigin.End); return true; }
try { self.Seek(offset, SeekOrigin.End); return true; }
catch { return false; }
default:
@@ -183,8 +176,8 @@ namespace LibMSPackSharp
/// <param name="file">the file whose file position is wanted</param>
/// <returns>the current file position of the file</returns>
/// <see cref="Open(string, OpenMode)"/>
/// <see cref="Seek(DefaultFileImpl, long, SeekMode)"/>
public long Tell(DefaultFileImpl self) => (self != null ? self.FileHandle.Position : 0);
/// <see cref="Seek(FileStream, long, SeekMode)"/>
public long Tell(FileStream self) => self?.Position ?? 0;
/// <summary>
/// Used to send messages from the library to the user.
@@ -200,7 +193,7 @@ namespace LibMSPackSharp
/// </param>
/// <param name="format">a printf() style format string. It does NOT include a trailing newline.</param>
/// <see cref="Open(string, OpenMode)"/>
public void Message(DefaultFileImpl file, string format)
public void Message(FileStream file, string format)
{
if (file != null)
Console.Error.Write($"{file.Name}: ");
@@ -213,11 +206,11 @@ namespace LibMSPackSharp
/// <summary>
/// Returns the length of a file opened for reading
/// </summary>
public Error GetFileLength(DefaultFileImpl file, out long length)
public Error GetFileLength(FileStream file, out long length)
{
try
{
length = file?.FileHandle?.Length ?? 0;
length = file?.Length ?? 0;
return Error.MSPACK_ERR_OK;
}
catch
@@ -247,10 +240,10 @@ namespace LibMSPackSharp
private static int DefaultRead(object file, byte[] buffer, int pointer, int bytes)
{
DefaultFileImpl self = file as DefaultFileImpl;
FileStream self = file as FileStream;
if (self != null && buffer != null && bytes >= 0)
{
try { return self.FileHandle.Read(buffer, pointer, bytes); }
try { return self.Read(buffer, pointer, bytes); }
catch { }
}
@@ -259,10 +252,10 @@ namespace LibMSPackSharp
private static int DefaultWrite(object file, byte[] buffer, int pointer, int bytes)
{
DefaultFileImpl self = file as DefaultFileImpl;
FileStream self = file as FileStream;
if (self != null && buffer != null && bytes >= 0)
{
try { self.FileHandle.Write(buffer, pointer, bytes); }
try { self.Write(buffer, pointer, bytes); }
catch { return -1; }
return bytes;
}