CEF4Delphi/source/uCEFZipReader.pas

175 lines
5.3 KiB
ObjectPascal
Raw Normal View History

2017-01-27 16:37:51 +01:00
unit uCEFZipReader;
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
2017-02-05 20:56:46 +01:00
{$I cef.inc}
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
{$MINENUMSIZE 4}
2017-01-27 16:37:51 +01:00
interface
uses
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes;
2017-01-27 16:37:51 +01:00
type
2024-09-03 17:26:03 +02:00
/// <summary>
/// Class that supports the reading of zip archives via the zlib unzip API.
/// The functions of this interface should only be called on the thread that
/// creates the object.
/// </summary>
/// <remarks>
/// <para><see cref="uCEFTypes|TCefZipReader">Implements TCefZipReader</see></para>
/// <para><see href="https://bitbucket.org/chromiumembedded/cef/src/master/include/capi/cef_zip_reader_capi.h">CEF source file: /include/capi/cef_zip_reader_capi.h (cef_zip_reader_t)</see></para>
/// </remarks>
TCefZipReaderRef = class(TCefBaseRefCountedRef, ICefZipReader)
2017-01-27 16:37:51 +01:00
protected
2024-09-03 17:26:03 +02:00
/// <summary>
/// Moves the cursor to the first file in the archive. Returns true (1) if the
/// cursor position was set successfully.
/// </summary>
2017-01-27 16:37:51 +01:00
function MoveToFirstFile: Boolean;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Moves the cursor to the next file in the archive. Returns true (1) if the
/// cursor position was set successfully.
/// </summary>
2017-01-27 16:37:51 +01:00
function MoveToNextFile: Boolean;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Moves the cursor to the specified file in the archive. If |caseSensitive|
/// is true (1) then the search will be case sensitive. Returns true (1) if
/// the cursor position was set successfully.
/// </summary>
2017-01-27 16:37:51 +01:00
function MoveToFile(const fileName: ustring; caseSensitive: Boolean): Boolean;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Closes the archive. This should be called directly to ensure that cleanup
/// occurs on the correct thread.
/// </summary>
2017-01-27 16:37:51 +01:00
function Close: Boolean;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Returns the name of the file.
/// </summary>
2017-01-27 16:37:51 +01:00
function GetFileName: ustring;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Returns the uncompressed size of the file.
/// </summary>
2017-01-27 16:37:51 +01:00
function GetFileSize: Int64;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Returns the last modified timestamp for the file.
/// </summary>
2022-09-04 19:18:07 +02:00
function GetFileLastModified: TCefBaseTime;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Opens the file for reading of uncompressed data. A read password may
/// optionally be specified.
/// </summary>
2017-01-27 16:37:51 +01:00
function OpenFile(const password: ustring): Boolean;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Closes the file.
/// </summary>
2017-01-27 16:37:51 +01:00
function CloseFile: Boolean;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Read uncompressed file contents into the specified buffer. Returns < 0 if
/// an error occurred, 0 if at the end of file, or the number of bytes read.
/// </summary>
2017-01-27 16:37:51 +01:00
function ReadFile(buffer: Pointer; bufferSize: NativeUInt): Integer;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Returns the current offset in the uncompressed file contents.
/// </summary>
2017-01-27 16:37:51 +01:00
function Tell: Int64;
2024-09-03 17:26:03 +02:00
/// <summary>
/// Returns true (1) if at end of the file contents.
/// </summary>
2017-01-27 16:37:51 +01:00
function Eof: Boolean;
2024-09-03 17:26:03 +02:00
2017-01-27 16:37:51 +01:00
public
class function UnWrap(data: Pointer): ICefZipReader;
class function New(const stream: ICefStreamReader): ICefZipReader;
end;
implementation
uses
uCEFMiscFunctions, uCEFLibFunctions;
function TCefZipReaderRef.Close: Boolean;
begin
Result := PCefZipReader(FData)^.close(PCefZipReader(FData)) <> 0;
2017-01-27 16:37:51 +01:00
end;
function TCefZipReaderRef.CloseFile: Boolean;
begin
Result := PCefZipReader(FData)^.close_file(PCefZipReader(FData)) <> 0;
2017-01-27 16:37:51 +01:00
end;
class function TCefZipReaderRef.New(const stream: ICefStreamReader): ICefZipReader;
begin
Result := UnWrap(cef_zip_reader_create(CefGetData(stream)));
end;
function TCefZipReaderRef.Eof: Boolean;
begin
Result := PCefZipReader(FData)^.eof(PCefZipReader(FData)) <> 0;
2017-01-27 16:37:51 +01:00
end;
2022-09-04 19:18:07 +02:00
function TCefZipReaderRef.GetFileLastModified: TCefBaseTime;
2017-01-27 16:37:51 +01:00
begin
Result := PCefZipReader(FData)^.get_file_last_modified(PCefZipReader(FData));
2017-01-27 16:37:51 +01:00
end;
function TCefZipReaderRef.GetFileName: ustring;
begin
Result := CefStringFreeAndGet(PCefZipReader(FData)^.get_file_name(PCefZipReader(FData)));
2017-01-27 16:37:51 +01:00
end;
function TCefZipReaderRef.GetFileSize: Int64;
begin
Result := PCefZipReader(FData)^.get_file_size(PCefZipReader(FData));
2017-01-27 16:37:51 +01:00
end;
function TCefZipReaderRef.MoveToFile(const fileName: ustring; caseSensitive: Boolean): Boolean;
2017-01-27 16:37:51 +01:00
var
TempFilename : TCefString;
2017-01-27 16:37:51 +01:00
begin
TempFilename := CefString(fileName);
Result := PCefZipReader(FData)^.move_to_file(PCefZipReader(FData), @TempFilename, Ord(caseSensitive)) <> 0;
2017-01-27 16:37:51 +01:00
end;
function TCefZipReaderRef.MoveToFirstFile: Boolean;
begin
Result := PCefZipReader(FData)^.move_to_first_file(PCefZipReader(FData)) <> 0;
2017-01-27 16:37:51 +01:00
end;
function TCefZipReaderRef.MoveToNextFile: Boolean;
begin
Result := PCefZipReader(FData)^.move_to_next_file(PCefZipReader(FData)) <> 0;
2017-01-27 16:37:51 +01:00
end;
function TCefZipReaderRef.OpenFile(const password: ustring): Boolean;
var
TempPassword : TCefString;
2017-01-27 16:37:51 +01:00
begin
TempPassword := CefString(password);
Result := PCefZipReader(FData)^.open_file(PCefZipReader(FData), @TempPassword) <> 0;
2017-01-27 16:37:51 +01:00
end;
function TCefZipReaderRef.ReadFile(buffer: Pointer; bufferSize: NativeUInt): Integer;
2017-01-27 16:37:51 +01:00
begin
Result := PCefZipReader(FData)^.read_file(PCefZipReader(FData), buffer, buffersize);
2017-01-27 16:37:51 +01:00
end;
function TCefZipReaderRef.Tell: Int64;
begin
Result := PCefZipReader(FData)^.tell(PCefZipReader(FData));
2017-01-27 16:37:51 +01:00
end;
class function TCefZipReaderRef.UnWrap(data: Pointer): ICefZipReader;
begin
if (data <> nil) then
Result := Create(data) as ICefZipReader
else
2017-01-27 16:37:51 +01:00
Result := nil;
end;
end.