|
@@ -5,9 +5,9 @@
|
|
|
Unit : Quick.Files
|
|
|
Description : Files functions
|
|
|
Author : Kike Pérez
|
|
|
- Version : 1.0
|
|
|
+ Version : 1.1
|
|
|
Created : 09/03/2018
|
|
|
- Modified : 15/03/2018
|
|
|
+ Modified : 07/04/2018
|
|
|
|
|
|
This file is part of QuickLib: https://github.com/exilon/QuickLib
|
|
|
|
|
@@ -29,18 +29,46 @@
|
|
|
|
|
|
unit Quick.Files;
|
|
|
|
|
|
+{$i QuickLib.inc}
|
|
|
+
|
|
|
interface
|
|
|
|
|
|
uses
|
|
|
Classes,
|
|
|
- System.SysUtils,
|
|
|
- Winapi.Windows;
|
|
|
+ SysUtils,
|
|
|
+ {$IFDEF FPC}
|
|
|
+ strutils,
|
|
|
+ DateUtils,
|
|
|
+ {$ENDIF}
|
|
|
+ Windows;
|
|
|
+
|
|
|
+{$IFDEF FPC}
|
|
|
+resourcestring
|
|
|
+
|
|
|
+ SPathTooLong = 'The specified path is too long';
|
|
|
+ SPathNotFound = 'The specified path was not found';
|
|
|
+ SPathFormatNotSupported = 'The path format is not supported';
|
|
|
+ SDirectoryNotEmpty = 'The specified directory is not empty';
|
|
|
+ SDirectoryAlreadyExists = 'The specified directory already exists';
|
|
|
+ SDirectoryInvalid = 'The specified directory name is invalid';
|
|
|
+ SSourceDirIsDestDir = 'The source directory is the same as the destination directory';
|
|
|
+ SSourceFileIsDestFile = 'The source file is the same as the destination file';
|
|
|
+ SPathToFileNeeded = 'The path must specify a file';
|
|
|
+ SSameRootDrive = 'The source and destination paths must contain the same root drive';
|
|
|
+ SDriveNotFound = 'The drive cannot be found';
|
|
|
+ SFileNotFound = 'The specified file was not found';
|
|
|
+ SFileAlreadyExists = 'The specified file already exists';
|
|
|
+ SInvalidCharsInPath = 'Invalid characters in path';
|
|
|
+ SInvalidCharsInFileName = 'Invalid characters in file name';
|
|
|
+{$ENDIF}
|
|
|
|
|
|
type
|
|
|
|
|
|
+
|
|
|
+ {$IFNDEF FPC}
|
|
|
TTextFileOperation = (tfOpenRead,tfOpenOverwrite,tfOpenAppend);
|
|
|
|
|
|
- TTextStreamFile = class
|
|
|
+ TTextStreamFile = class
|
|
|
private
|
|
|
fReadStream : TStreamReader;
|
|
|
fWriteStream : TStreamWriter;
|
|
@@ -54,6 +82,125 @@ type
|
|
|
procedure Close;
|
|
|
property EOF: Boolean read GetEOF;
|
|
|
end;
|
|
|
+ {$ENDIF}
|
|
|
+
|
|
|
+ {$IFDEF FPC}
|
|
|
+ EStreamError = class(Exception);
|
|
|
+ EFileStreamError = class(EStreamError)
|
|
|
+ constructor Create(ResStringRec: PResStringRec; const FileName: string);
|
|
|
+ end;
|
|
|
+
|
|
|
+ TPathPrefixType = (pptNoPrefix, pptExtended, pptExtendedUNC);
|
|
|
+
|
|
|
+ TPath = class
|
|
|
+ private
|
|
|
+ const
|
|
|
+ FCCurrentDir: string = '.';
|
|
|
+ FCParentDir: string = '..';
|
|
|
+ FCExtendedPrefix: string = '\\?\';
|
|
|
+ FCExtendedUNCPrefix: string = '\\?\UNC\';
|
|
|
+ class procedure CheckPathLength(const Path: string; const MaxLength: Integer);
|
|
|
+ public
|
|
|
+ class function GetFileNameWithoutExtension(const FileName : string) : string;
|
|
|
+ class function GetDirectoryName(const FileName : string) : string;
|
|
|
+ class function GetExtension(const Path : string) : string;
|
|
|
+ class function ChangeExtension(const Path, NewExtension : string) : string;
|
|
|
+ end;
|
|
|
+
|
|
|
+ TDirectory = class
|
|
|
+ public
|
|
|
+ class function Exists(const Path: string; FollowLink: Boolean = True): Boolean;
|
|
|
+ end;
|
|
|
+
|
|
|
+ TFile = class
|
|
|
+ public
|
|
|
+ class function Exists(const Path : string) : Boolean;
|
|
|
+ class function IsInUse(const Path : string) : Boolean;
|
|
|
+ class function GetSize(const Path : string) : Int64;
|
|
|
+ class function Create(const Path: string; const BufferSize: Integer): TFileStream; overload;
|
|
|
+ class function Create(const Path: string): TFileStream; overload;
|
|
|
+ class function GetExtension(const Path : string) : string;
|
|
|
+ class function GetCreationTime(const Path : string): TDateTime;
|
|
|
+ class function GetLastAccessTime(const Path : string): TDateTime;
|
|
|
+ class function GetLastWriteTime(const Path : string): TDateTime;
|
|
|
+ class procedure SetCreationTime(const Path: string; const CreationTime: TDateTime);
|
|
|
+ class procedure SetLastAccessTime(const Path: string; const LastAccessTime: TDateTime);
|
|
|
+ class procedure SetLastWriteTime(const Path: string; const LastWriteTime: TDateTime);
|
|
|
+ class function IsReadOnly(const Path : string) : Boolean;
|
|
|
+ class function Delete(const Path : string) : Boolean;
|
|
|
+ class function Move(const SourceFileName, DestFileName: string) : Boolean;
|
|
|
+ end;
|
|
|
+
|
|
|
+ TTextWriter = class
|
|
|
+ public
|
|
|
+ procedure Close; virtual; abstract;
|
|
|
+ procedure Flush; virtual; abstract;
|
|
|
+ procedure Write(Value: Boolean); overload; virtual; abstract;
|
|
|
+ procedure Write(Value: Char); overload; virtual; abstract;
|
|
|
+ procedure Write(Value: Double); overload; virtual; abstract;
|
|
|
+ procedure Write(Value: Integer); overload; virtual; abstract;
|
|
|
+ procedure Write(Value: Int64); overload; virtual; abstract;
|
|
|
+ procedure Write(Value: TObject); overload; virtual; abstract;
|
|
|
+ procedure Write(Value: Single); overload; virtual; abstract;
|
|
|
+ procedure Write(const Value: string); overload; virtual; abstract;
|
|
|
+ procedure Write(const aFormat: string; Args: array of const); overload; virtual; abstract;
|
|
|
+ procedure WriteLine; overload; virtual; abstract;
|
|
|
+ procedure WriteLine(Value: Boolean); overload; virtual; abstract;
|
|
|
+ procedure WriteLine(Value: Char); overload; virtual; abstract;
|
|
|
+ procedure WriteLine(Value: Double); overload; virtual; abstract;
|
|
|
+ procedure WriteLine(Value: Integer); overload; virtual; abstract;
|
|
|
+ procedure WriteLine(Value: Int64); overload; virtual; abstract;
|
|
|
+ procedure WriteLine(Value: TObject); overload; virtual; abstract;
|
|
|
+ procedure WriteLine(Value: Single); overload; virtual; abstract;
|
|
|
+ procedure WriteLine(const Value: string); overload; virtual; abstract;
|
|
|
+ procedure WriteLine(const aFormat: string; Args: array of const); overload; virtual; abstract;
|
|
|
+ procedure WriteLine(const Value: TCharArray; Index, Count: Integer); overload; virtual; abstract;
|
|
|
+ end;
|
|
|
+
|
|
|
+ TStreamWriter = class(TTextWriter)
|
|
|
+ private
|
|
|
+ FStream: TStream;
|
|
|
+ FEncoding: TEncoding;
|
|
|
+ FNewLine: string;
|
|
|
+ FAutoFlush: Boolean;
|
|
|
+ FOwnsStream: Boolean;
|
|
|
+ FBufferIndex: Integer;
|
|
|
+ FBuffer: TBytes;
|
|
|
+ procedure WriteBytes(Bytes: TBytes);
|
|
|
+ public
|
|
|
+ constructor Create(Stream: TStream); overload;
|
|
|
+ constructor Create(Stream: TStream; Encoding: TEncoding; BufferSize: Integer = 4096); overload;
|
|
|
+ constructor Create(const Filename: string; Append: Boolean = False); overload;
|
|
|
+ constructor Create(const Filename: string; Append: Boolean; Encoding: TEncoding; BufferSize: Integer = 4096); overload;
|
|
|
+ destructor Destroy; override;
|
|
|
+ procedure Close; override;
|
|
|
+ procedure Flush; override;
|
|
|
+ procedure OwnStream; inline;
|
|
|
+ procedure Write(Value: Boolean); override;
|
|
|
+ procedure Write(Value: Char); override;
|
|
|
+ procedure Write(Value: Double); override;
|
|
|
+ procedure Write(Value: Integer); override;
|
|
|
+ procedure Write(Value: Int64); override;
|
|
|
+ procedure Write(Value: TObject); override;
|
|
|
+ procedure Write(Value: Single); override;
|
|
|
+ procedure Write(const Value: string); override;
|
|
|
+ procedure Write(const aFormat: string; Args: array of const); override;
|
|
|
+ procedure WriteLine; override;
|
|
|
+ procedure WriteLine(Value: Boolean); override;
|
|
|
+ procedure WriteLine(Value: Char); override;
|
|
|
+ procedure WriteLine(Value: Double); override;
|
|
|
+ procedure WriteLine(Value: Integer); override;
|
|
|
+ procedure WriteLine(Value: Int64); override;
|
|
|
+ procedure WriteLine(Value: TObject); override;
|
|
|
+ procedure WriteLine(Value: Single); override;
|
|
|
+ procedure WriteLine(const Value: string); override;
|
|
|
+ procedure WriteLine(const aFormat: string; Args: array of const); override;
|
|
|
+ property AutoFlush: Boolean read FAutoFlush write FAutoFlush;
|
|
|
+ property NewLine: string read FNewLine write FNewLine;
|
|
|
+ property Encoding: TEncoding read FEncoding;
|
|
|
+ property BaseStream: TStream read FStream;
|
|
|
+ end;
|
|
|
+ {$ENDIF FPC}
|
|
|
|
|
|
function CreateDummyFile(const aFilename : string; const aSize : Int64) : Boolean;
|
|
|
procedure SplitFile(const aFileName : string; aSplitByteSize : Int64);
|
|
@@ -62,14 +209,20 @@ type
|
|
|
function IsFileInUse(const aFileName : string) : Boolean;
|
|
|
procedure FileReplaceText(const aFileName, aSearchText, AReplaceText : string);
|
|
|
function FileSearchText(const aFileName, SearchText: string; caseSensitive : Boolean): Longint;
|
|
|
- function GetLastAccessTime(const aFileName: string): TDateTime;
|
|
|
function GetCreationTime(const aFilename : string): TDateTime;
|
|
|
- function GetLastModificationTime(const aFileName : string): TDateTime;
|
|
|
+ function GetLastAccessTime(const aFileName: string): TDateTime;
|
|
|
+ function GetLastWriteTime(const aFileName : string): TDateTime;
|
|
|
+ {$IFDEF FPC}
|
|
|
+ function FindDelimiter(const Delimiters, S: string; StartIdx: Integer = 1): Integer;
|
|
|
+ {$ENDIF}
|
|
|
+ function ConvertDateTimeToFileTime(const DateTime: TDateTime; const UseLocalTimeZone: Boolean): TFileTime;
|
|
|
+ procedure SetDateTimeInfo(const Path: string; const CreationTime, LastAccessTime, LastWriteTime: PDateTime; const UseLocalTimeZone: Boolean);
|
|
|
|
|
|
implementation
|
|
|
|
|
|
{ TTextStreamFile }
|
|
|
|
|
|
+{$IFNDEF FPC}
|
|
|
constructor TTextStreamFile.Create(const aFileName : string; aOpenMode : TTextFileOperation);
|
|
|
var
|
|
|
Append : Boolean;
|
|
@@ -117,6 +270,367 @@ begin
|
|
|
if Assigned(fWriteStream) then fWriteStream.Close;
|
|
|
end;
|
|
|
|
|
|
+{$ENDIF NFPC}
|
|
|
+
|
|
|
+{$IFDEF FPC}
|
|
|
+
|
|
|
+{ EFileStreamError }
|
|
|
+
|
|
|
+constructor EFileStreamError.Create(ResStringRec: PResStringRec;
|
|
|
+ const FileName: string);
|
|
|
+begin
|
|
|
+ inherited CreateResFmt(ResStringRec, [ExpandFileName(FileName), SysErrorMessage(GetLastError)]);
|
|
|
+end;
|
|
|
+
|
|
|
+{ TPath }
|
|
|
+
|
|
|
+class function TPath.GetFileNameWithoutExtension(const FileName: String): String;
|
|
|
+var
|
|
|
+ fname : string;
|
|
|
+begin
|
|
|
+ fname := ExtractFileName(FileName);
|
|
|
+ Result := Copy(fname, 1, Length(fname) - Length(ExtractFileExt(fname)));
|
|
|
+end;
|
|
|
+
|
|
|
+class function TPath.ChangeExtension(const Path, NewExtension : string) : string;
|
|
|
+var
|
|
|
+ dot : string;
|
|
|
+begin
|
|
|
+ if NewExtension.Contains('.') then dot := ''
|
|
|
+ else dot := '.';
|
|
|
+ Result := TPath.GetFileNameWithoutExtension(Path) + dot + NewExtension;
|
|
|
+end;
|
|
|
+
|
|
|
+class function TPath.GetDirectoryName(const FileName : string) : string;
|
|
|
+begin
|
|
|
+ Result := ExtractFileDir(Filename);
|
|
|
+end;
|
|
|
+
|
|
|
+class procedure TPath.CheckPathLength(const Path: string; const MaxLength: Integer);
|
|
|
+begin
|
|
|
+{$IFDEF MSWINDOWS}
|
|
|
+ if (Length(Path) >= MaxLength) then
|
|
|
+{$ENDIF MSWINDOWS}
|
|
|
+{$IFDEF POSIX}
|
|
|
+ if (Length(UTF8Encode(Path)) >= MaxLength) then
|
|
|
+{$ENDIF POSIX}
|
|
|
+ raise EPathTooLongException.CreateRes(@SPathTooLong);
|
|
|
+end;
|
|
|
+
|
|
|
+class function TPath.GetExtension(const Path : string) : string;
|
|
|
+begin
|
|
|
+ Result := ExtractFileExt(Path);
|
|
|
+end;
|
|
|
+
|
|
|
+{ TDirectory }
|
|
|
+
|
|
|
+class function TDirectory.Exists(const Path: string; FollowLink: Boolean = True): Boolean;
|
|
|
+begin
|
|
|
+ Result := DirectoryExists(Path);
|
|
|
+end;
|
|
|
+
|
|
|
+{ TFile }
|
|
|
+
|
|
|
+class function TFile.Exists(const Path : string) : Boolean;
|
|
|
+begin
|
|
|
+ Result := FileExists(Path);
|
|
|
+end;
|
|
|
+
|
|
|
+class procedure TFile.SetCreationTime(const Path: string; const CreationTime: TDateTime);
|
|
|
+begin
|
|
|
+ SetDateTimeInfo(Path,@CreationTime,nil,nil,True);
|
|
|
+end;
|
|
|
+
|
|
|
+class procedure TFile.SetLastAccessTime(const Path: string; const LastAccessTime: TDateTime);
|
|
|
+begin
|
|
|
+ SetDateTimeInfo(Path,nil,@LastAccessTime,nil,True);
|
|
|
+end;
|
|
|
+
|
|
|
+class procedure TFile.SetLastWriteTime(const Path: string; const LastWriteTime: TDateTime);
|
|
|
+begin
|
|
|
+ SetDateTimeInfo(Path,nil,nil,@LastWriteTime,True);
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.IsReadOnly(const Path : string) : Boolean;
|
|
|
+begin
|
|
|
+ Result := FileIsReadOnly(Path);
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.Delete(const Path : string) : Boolean;
|
|
|
+begin
|
|
|
+ Result := DeleteFile(PChar(Path));
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.Move(const SourceFileName, DestFileName: string) : Boolean;
|
|
|
+begin
|
|
|
+ Result := MoveFile(PChar(SourceFileName),PChar(DestFileName));
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.IsInUse(const Path : string) : Boolean;
|
|
|
+begin
|
|
|
+ Result := IsFileInUse(Path);
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.GetSize(const Path : string) : Int64;
|
|
|
+var
|
|
|
+ f : File of Byte;
|
|
|
+begin
|
|
|
+ Assign(f,Path);
|
|
|
+ try
|
|
|
+ Reset (f);
|
|
|
+ Result := FileSize(f);
|
|
|
+ finally
|
|
|
+ CloseFile(f);
|
|
|
+ end;
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.GetExtension(const Path : string) : string;
|
|
|
+begin
|
|
|
+ Result := ExtractFileExt(Path);
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.GetCreationTime(const Path : string) : TDateTime;
|
|
|
+begin
|
|
|
+ Result := Quick.Files.GetCreationTime(Path);
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.GetLastAccessTime(const Path : string) : TDateTime;
|
|
|
+begin
|
|
|
+ Result := Quick.Files.GetLastAccessTime(Path);
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.GetLastWriteTime(const Path : string) : TDateTime;
|
|
|
+begin
|
|
|
+ Result := Quick.Files.GetLastWriteTime(Path);
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.Create(const Path: string; const BufferSize: Integer): TFileStream;
|
|
|
+begin
|
|
|
+ try
|
|
|
+ Result := TFileStream.Create(Path,fmCreate);
|
|
|
+ except
|
|
|
+ on E: EFileStreamError do
|
|
|
+ raise EInOutError.Create(E.Message);
|
|
|
+ end;
|
|
|
+end;
|
|
|
+
|
|
|
+class function TFile.Create(const Path: string): TFileStream;
|
|
|
+begin
|
|
|
+ Result := Create(Path, 0);
|
|
|
+end;
|
|
|
+
|
|
|
+{ TStreamWriter }
|
|
|
+
|
|
|
+procedure TStreamWriter.Close;
|
|
|
+begin
|
|
|
+ Flush;
|
|
|
+ if FOwnsStream then
|
|
|
+ FreeAndNil(FStream);
|
|
|
+end;
|
|
|
+
|
|
|
+constructor TStreamWriter.Create(Stream: TStream);
|
|
|
+begin
|
|
|
+ inherited Create;
|
|
|
+ FOwnsStream := False;
|
|
|
+ FStream := Stream;
|
|
|
+ FEncoding := TEncoding.UTF8;
|
|
|
+ SetLength(FBuffer, 1024);
|
|
|
+ FBufferIndex := 0;
|
|
|
+ FNewLine := sLineBreak;
|
|
|
+ FAutoFlush := True;
|
|
|
+end;
|
|
|
+
|
|
|
+constructor TStreamWriter.Create(Stream: TStream; Encoding: TEncoding; BufferSize: Integer);
|
|
|
+begin
|
|
|
+ inherited Create;
|
|
|
+ FOwnsStream := False;
|
|
|
+ FStream := Stream;
|
|
|
+ FEncoding := Encoding;
|
|
|
+ if BufferSize >= 128 then
|
|
|
+ SetLength(FBuffer, BufferSize)
|
|
|
+ else
|
|
|
+ SetLength(FBuffer, 128);
|
|
|
+ FBufferIndex := 0;
|
|
|
+ FNewLine := sLineBreak;
|
|
|
+ FAutoFlush := True;
|
|
|
+ if Stream.Position = 0 then
|
|
|
+ WriteBytes(FEncoding.GetPreamble);
|
|
|
+end;
|
|
|
+
|
|
|
+constructor TStreamWriter.Create(const Filename: string; Append: Boolean);
|
|
|
+begin
|
|
|
+ if (not FileExists(Filename)) or (not Append) then
|
|
|
+ FStream := TFileStream.Create(Filename, fmCreate)
|
|
|
+ else
|
|
|
+ begin
|
|
|
+ FStream := TFileStream.Create(Filename, fmOpenWrite);
|
|
|
+ FStream.Seek(0, soEnd);
|
|
|
+ end;
|
|
|
+ Create(FStream);
|
|
|
+ FOwnsStream := True;
|
|
|
+end;
|
|
|
+
|
|
|
+constructor TStreamWriter.Create(const Filename: string; Append: Boolean;
|
|
|
+ Encoding: TEncoding; BufferSize: Integer);
|
|
|
+begin
|
|
|
+ if (not FileExists(Filename)) or (not Append) then
|
|
|
+ FStream := TFileStream.Create(Filename, fmCreate)
|
|
|
+ else
|
|
|
+ begin
|
|
|
+ FStream := TFileStream.Create(Filename, fmOpenWrite);
|
|
|
+ FStream.Seek(0, soEnd);
|
|
|
+ end;
|
|
|
+ Create(FStream, Encoding, BufferSize);
|
|
|
+ FOwnsStream := True;
|
|
|
+end;
|
|
|
+
|
|
|
+destructor TStreamWriter.Destroy;
|
|
|
+begin
|
|
|
+ Close;
|
|
|
+ SetLength(FBuffer, 0);
|
|
|
+ inherited;
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Flush;
|
|
|
+begin
|
|
|
+ if FBufferIndex = 0 then
|
|
|
+ Exit;
|
|
|
+ if FStream = nil then
|
|
|
+ Exit;
|
|
|
+
|
|
|
+ FStream.Write(FBuffer[0], FBufferIndex);
|
|
|
+ FBufferIndex := 0;
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.OwnStream;
|
|
|
+begin
|
|
|
+ FOwnsStream := True;
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Write(const Value: string);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(Value));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteBytes(Bytes: TBytes);
|
|
|
+var
|
|
|
+ ByteIndex: Integer;
|
|
|
+ WriteLen: Integer;
|
|
|
+begin
|
|
|
+ ByteIndex := 0;
|
|
|
+
|
|
|
+ while ByteIndex < Length(Bytes) do
|
|
|
+ begin
|
|
|
+ WriteLen := Length(Bytes) - ByteIndex;
|
|
|
+ if WriteLen > Length(FBuffer) - FBufferIndex then
|
|
|
+ WriteLen := Length(FBuffer) - FBufferIndex;
|
|
|
+
|
|
|
+ Move(Bytes[ByteIndex], FBuffer[FBufferIndex], WriteLen);
|
|
|
+
|
|
|
+ Inc(FBufferIndex, WriteLen);
|
|
|
+ Inc(ByteIndex, WriteLen);
|
|
|
+
|
|
|
+ if FBufferIndex >= Length(FBuffer) then
|
|
|
+ Flush;
|
|
|
+ end;
|
|
|
+
|
|
|
+ if FAutoFlush then
|
|
|
+ Flush;
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Write(const aFormat: string; Args: array of const);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(Format(aFormat, Args)));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Write(Value: Single);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(FloatToStr(Value)));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Write(Value: Double);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(FloatToStr(Value)));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Write(Value: Integer);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(IntToStr(Value)));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Write(Value: Char);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(Value));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Write(Value: TObject);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(Value.ToString));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Write(Value: Int64);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(IntToStr(Value)));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.Write(Value: Boolean);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(BoolToStr(Value, True)));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine(Value: Double);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(FloatToStr(Value) + FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine(Value: Integer);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(IntToStr(Value) + FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine;
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine(Value: Boolean);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(BoolToStr(Value, True) + FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine(Value: Char);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(Value));
|
|
|
+ WriteBytes(FEncoding.GetBytes(FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine(Value: Int64);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(IntToStr(Value) + FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine(const aFormat: string; Args: array of const);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(Format(aFormat, Args) + FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine(Value: TObject);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(Value.ToString + FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine(Value: Single);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(FloatToStr(Value) + FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+procedure TStreamWriter.WriteLine(const Value: string);
|
|
|
+begin
|
|
|
+ WriteBytes(FEncoding.GetBytes(Value + FNewLine));
|
|
|
+end;
|
|
|
+
|
|
|
+{$ENDIF FPC}
|
|
|
+
|
|
|
{other functions}
|
|
|
|
|
|
function CreateDummyFile(const aFilename : string; const aSize : Int64 ) : Boolean;
|
|
@@ -261,20 +775,12 @@ function FileSearchText(const aFileName, SearchText: string; caseSensitive : Boo
|
|
|
const
|
|
|
BufferSize = $8001;
|
|
|
var
|
|
|
- {$IF CompilerVersion < 20}
|
|
|
- pBuf, pEnd, pScan, pPos: PChar;
|
|
|
- {$ELSE}
|
|
|
- pBuf, pEnd, pScan, pPos: PAnsiChar;
|
|
|
- {$ENDIF}
|
|
|
+ pBuf, pEnd, pScan, pPos: PAnsiChar;
|
|
|
filesize: LongInt;
|
|
|
bytesRemaining: LongInt;
|
|
|
bytesToRead: Integer;
|
|
|
F: file;
|
|
|
- {$IF CompilerVersion < 20}
|
|
|
- SearchFor: PChar;
|
|
|
- {$ELSE}
|
|
|
- SearchFor: PAnsiChar;
|
|
|
- {$ENDIF}
|
|
|
+ SearchFor: PAnsiChar;
|
|
|
oldMode: Word;
|
|
|
begin
|
|
|
Result := -1;
|
|
@@ -287,13 +793,21 @@ begin
|
|
|
Reset(F, 1);
|
|
|
FileMode := oldMode;
|
|
|
try
|
|
|
- {$IF CompilerVersion < 20}
|
|
|
- SearchFor := StrAlloc(Length(SearchText) + 1);
|
|
|
+ {$IFDEF FPC}
|
|
|
+ SearchFor := PChar(StrAlloc(Length(SearchText) + 1));
|
|
|
+ {$ELSE}
|
|
|
+ {$IFDEF DELPHI2010_UP}
|
|
|
+ SearchFor := PAnsiChar(StrAlloc(Length(SearchText) + 1));
|
|
|
{$ELSE}
|
|
|
- SearchFor := PAnsiChar(StrAlloc(Length(SearchText) + 1));
|
|
|
+ SearchFor := StrAlloc(Length(SearchText) + 1);
|
|
|
{$ENDIF}
|
|
|
+ {$ENDIF FPC}
|
|
|
StrPCopy(SearchFor, SearchText);
|
|
|
+ {$IFDEF FPC}
|
|
|
+ if not caseSensitive then UpperCase(SearchFor);
|
|
|
+ {$ELSE}
|
|
|
if not caseSensitive then AnsiUpper(SearchFor);
|
|
|
+ {$ENDIF}
|
|
|
GetMem(pBuf, BufferSize);
|
|
|
filesize := System.Filesize(F);
|
|
|
bytesRemaining := filesize;
|
|
@@ -308,7 +822,11 @@ begin
|
|
|
pScan := pBuf;
|
|
|
while pScan < pEnd do
|
|
|
begin
|
|
|
+ {$IFDEF FPC}
|
|
|
+ if not caseSensitive then UpperCase(pScan);
|
|
|
+ {$ELSE}
|
|
|
if not caseSensitive then AnsiUpper(pScan);
|
|
|
+ {$ENDIF}
|
|
|
pPos := StrPos(pScan, SearchFor);
|
|
|
if pPos <> nil then
|
|
|
begin
|
|
@@ -341,7 +859,11 @@ var
|
|
|
lft: TFileTime;
|
|
|
h: THandle;
|
|
|
begin
|
|
|
+ {$IFDEF FPC}
|
|
|
+ h := FindFirstFile(PAnsiChar(aFileName), ffd);
|
|
|
+ {$ELSE}
|
|
|
h := FindFirstFile(PChar(aFileName), ffd);
|
|
|
+ {$ENDIF}
|
|
|
if (INVALID_HANDLE_VALUE <> h) then
|
|
|
begin
|
|
|
FindClose(h);
|
|
@@ -358,7 +880,11 @@ var
|
|
|
lft: TFileTime;
|
|
|
h: THandle;
|
|
|
begin
|
|
|
- h := FindFirstFile(PChar(aFilename), ffd);
|
|
|
+ {$IFDEF FPC}
|
|
|
+ h := FindFirstFile(PAnsiChar(aFileName), ffd);
|
|
|
+ {$ELSE}
|
|
|
+ h := FindFirstFile(PChar(aFileName), ffd);
|
|
|
+ {$ENDIF}
|
|
|
if (INVALID_HANDLE_VALUE <> h) then
|
|
|
begin
|
|
|
FindClose(h);
|
|
@@ -368,14 +894,18 @@ begin
|
|
|
end;
|
|
|
end;
|
|
|
|
|
|
-function GetLastModificationTime(const aFileName : string): TDateTime;
|
|
|
+function GetLastWriteTime(const aFileName : string): TDateTime;
|
|
|
var
|
|
|
ffd: TWin32FindData;
|
|
|
dft: DWORD;
|
|
|
lft: TFileTime;
|
|
|
h: THandle;
|
|
|
begin
|
|
|
- h := FindFirstFile(PChar(aFilename), ffd);
|
|
|
+ {$IFDEF FPC}
|
|
|
+ h := FindFirstFile(PAnsiChar(aFileName), ffd);
|
|
|
+ {$ELSE}
|
|
|
+ h := FindFirstFile(PChar(aFileName), ffd);
|
|
|
+ {$ENDIF}
|
|
|
if (INVALID_HANDLE_VALUE <> h) then
|
|
|
begin
|
|
|
FindClose(h);
|
|
@@ -385,4 +915,162 @@ begin
|
|
|
end;
|
|
|
end;
|
|
|
|
|
|
+{$IFDEF FPC}
|
|
|
+function FindDelimiter(const Delimiters, S: string; StartIdx: Integer = 1): Integer;
|
|
|
+var
|
|
|
+ Stop: Boolean;
|
|
|
+ Len: Integer;
|
|
|
+begin
|
|
|
+ Result := 0;
|
|
|
+
|
|
|
+ Len := S.Length;
|
|
|
+ Stop := False;
|
|
|
+ while (not Stop) and (StartIdx <= Len) do
|
|
|
+ if IsDelimiter(Delimiters, S, StartIdx) then
|
|
|
+ begin
|
|
|
+ Result := StartIdx;
|
|
|
+ Stop := True;
|
|
|
+ end
|
|
|
+ else
|
|
|
+ Inc(StartIdx);
|
|
|
+end;
|
|
|
+{$ENDIF}
|
|
|
+
|
|
|
+{$IFDEF MSWINDOWS}
|
|
|
+function ConvertDateTimeToFileTime(const DateTime: TDateTime; const UseLocalTimeZone: Boolean): TFileTime;
|
|
|
+var
|
|
|
+ LFileTime: TFileTime;
|
|
|
+ SysTime: TSystemTime;
|
|
|
+begin
|
|
|
+ Result.dwLowDateTime := 0;
|
|
|
+ Result.dwLowDateTime := 0;
|
|
|
+ DecodeDateTime(DateTime, SysTime.wYear, SysTime.wMonth, SysTime.wDay,
|
|
|
+ SysTime.wHour, SysTime.wMinute, SysTime.wSecond, SysTime.wMilliseconds);
|
|
|
+
|
|
|
+ if SystemTimeToFileTime(SysTime, LFileTime) then
|
|
|
+ if UseLocalTimeZone then
|
|
|
+ LocalFileTimeToFileTime(LFileTime, Result)
|
|
|
+ else
|
|
|
+ Result := LFileTime;
|
|
|
+end;
|
|
|
+{$ENDIF}
|
|
|
+{$IFDEF POSIX}
|
|
|
+class function TDirectory.ConvertDateTimeToFileTime(const DateTime: TDateTime;
|
|
|
+ const UseLocalTimeZone: Boolean): time_t;
|
|
|
+begin
|
|
|
+ { Use the time zone if necessary }
|
|
|
+ if not UseLocalTimeZone then
|
|
|
+ Result := DateTimeToFileDate(TTimeZone.Local.ToLocalTime(DateTime))
|
|
|
+ else
|
|
|
+ Result := DateTimeToFileDate(DateTime);
|
|
|
+end;
|
|
|
+{$ENDIF}
|
|
|
+
|
|
|
+procedure SetDateTimeInfo(const Path: string; const CreationTime, LastAccessTime, LastWriteTime: PDateTime; const UseLocalTimeZone: Boolean);
|
|
|
+{$IFDEF MSWINDOWS}
|
|
|
+var
|
|
|
+ LFileHnd: THandle;
|
|
|
+ LFileAttr: Cardinal;
|
|
|
+ LFileCreationTime: PFileTime;
|
|
|
+ LFileLastAccessTime: PFileTime;
|
|
|
+ LFileLastWriteTime: PFileTime;
|
|
|
+begin
|
|
|
+ // establish what date-times must be set to the directory
|
|
|
+ LFileHnd := 0;
|
|
|
+ LFileCreationTime := nil;
|
|
|
+ LFileLastAccessTime := nil;
|
|
|
+ LFileLastWriteTime := nil;
|
|
|
+
|
|
|
+ try
|
|
|
+ try
|
|
|
+ if Assigned(CreationTime) then
|
|
|
+ begin
|
|
|
+ New(LFileCreationTime);
|
|
|
+ LFileCreationTime^ := ConvertDateTimeToFileTime(CreationTime^, UseLocalTimeZone);
|
|
|
+ end;
|
|
|
+ if Assigned(LastAccessTime) then
|
|
|
+ begin
|
|
|
+ New(LFileLastAccessTime);
|
|
|
+ LFileLastAccessTime^ := ConvertDateTimeToFileTime(LastAccessTime^, UseLocalTimeZone);
|
|
|
+ end;
|
|
|
+ if Assigned(LastWriteTime) then
|
|
|
+ begin
|
|
|
+ New(LFileLastWriteTime);
|
|
|
+ LFileLastWriteTime^ := ConvertDateTimeToFileTime(LastWriteTime^, UseLocalTimeZone);
|
|
|
+ end;
|
|
|
+
|
|
|
+ // determine if Path points to a directory or a file
|
|
|
+ SetLastError(ERROR_SUCCESS);
|
|
|
+ LFileAttr := FileGetAttr(Path);
|
|
|
+ if LFileAttr and faDirectory <> 0 then
|
|
|
+ LFileAttr := FILE_FLAG_BACKUP_SEMANTICS
|
|
|
+ else
|
|
|
+ LFileAttr := FILE_ATTRIBUTE_NORMAL;
|
|
|
+
|
|
|
+ // set the new date-times to the directory or file
|
|
|
+ LFileHnd := CreateFile(PChar(Path), GENERIC_WRITE, FILE_SHARE_WRITE, nil,
|
|
|
+ OPEN_EXISTING, LFileAttr, 0);
|
|
|
+
|
|
|
+ if LFileHnd <> INVALID_HANDLE_VALUE then
|
|
|
+ SetFileTime(LFileHnd, LFileCreationTime, LFileLastAccessTime, LFileLastWriteTime);
|
|
|
+ except
|
|
|
+ on E: EConvertError do
|
|
|
+ raise EArgumentOutOfRangeException.Create(E.Message);
|
|
|
+ end;
|
|
|
+ finally
|
|
|
+ CloseHandle(LFileHnd);
|
|
|
+ SetLastError(ERROR_SUCCESS);
|
|
|
+
|
|
|
+ Dispose(LFileCreationTime);
|
|
|
+ Dispose(LFileLastAccessTime);
|
|
|
+ Dispose(LFileLastWriteTime);
|
|
|
+ end;
|
|
|
+end;
|
|
|
+{$ENDIF}
|
|
|
+{$IFDEF POSIX}
|
|
|
+var
|
|
|
+ LFileName: Pointer;
|
|
|
+ LStatBuf: _stat;
|
|
|
+ LBuf: utimbuf;
|
|
|
+ ErrCode: Integer;
|
|
|
+ M: TMarshaller;
|
|
|
+begin
|
|
|
+ { Do nothing if no date/time passed. Ignore CreationTime. Unixes do not support creation times for files. }
|
|
|
+ if (LastAccessTime = nil) and (LastWriteTime = nil) then
|
|
|
+ Exit;
|
|
|
+
|
|
|
+ LFileName := M.AsAnsi(Path, CP_UTF8).ToPointer;
|
|
|
+
|
|
|
+ { Obtain the file times. lstat may fail }
|
|
|
+ if ((LastAccessTime = nil) or (LastWriteTime = nil)) then
|
|
|
+ begin
|
|
|
+ ErrCode := stat(LFileName, LStatBuf);
|
|
|
+
|
|
|
+ { Fail if we can't access the file properly }
|
|
|
+ if ErrCode <> 0 then
|
|
|
+ Exit; // Fail here prematurely. Do not chnage file times if we failed to fetch the old ones.
|
|
|
+ end;
|
|
|
+
|
|
|
+ try
|
|
|
+ { Preserve of set the new value }
|
|
|
+ if LastAccessTime <> nil then
|
|
|
+ LBuf.actime := ConvertDateTimeToFileTime(LastAccessTime^, UseLocalTimeZone)
|
|
|
+ else
|
|
|
+ LBuf.actime := LStatBuf.st_atime;
|
|
|
+
|
|
|
+ { Preserve of set the new value }
|
|
|
+ if LastWriteTime <> nil then
|
|
|
+ LBuf.modtime := ConvertDateTimeToFileTime(LastWriteTime^, UseLocalTimeZone)
|
|
|
+ else
|
|
|
+ LBuf.modtime := LStatBuf.st_mtime;
|
|
|
+
|
|
|
+ { Call utime to set the file times }
|
|
|
+ utime(LFileName, LBuf);
|
|
|
+ except
|
|
|
+ on E: EConvertError do // May rise in ConvertDateTimeToFileTime
|
|
|
+ raise EArgumentOutOfRangeException.Create(E.Message);
|
|
|
+ end;
|
|
|
+end;
|
|
|
+{$ENDIF}
|
|
|
+
|
|
|
end.
|