Difference between revisions of "LazFileUtils"

From Free Pascal wiki
Jump to navigationJump to search
Line 5: Line 5:
 
function CompareFilenamesIgnoreCase(const Filename1, Filename2: string): integer;
 
function CompareFilenamesIgnoreCase(const Filename1, Filename2: string): integer;
  
function CompareFileExt(const Filename, Ext: string;
+
function CompareFileExt(const Filename, Ext: string; CaseSensitive: boolean): integer; overload;
                        CaseSensitive: boolean): integer; overload;
 
  
 
function CompareFileExt(const Filename, Ext: string): integer; overload;
 
function CompareFileExt(const Filename, Ext: string): integer; overload;
Line 12: Line 11:
 
function CompareFilenameStarts(const Filename1, Filename2: string): integer;
 
function CompareFilenameStarts(const Filename1, Filename2: string): integer;
  
function CompareFilenames(Filename1: PChar; Len1: integer;
+
function CompareFilenames(Filename1: PChar; Len1: integer; Filename2: PChar; Len2: integer): integer; overload;
  Filename2: PChar; Len2: integer): integer; overload;
 
  
function CompareFilenamesP(Filename1, Filename2: PChar;
+
function CompareFilenamesP(Filename1, Filename2: PChar; IgnoreCase: boolean = false // false = use default): integer;
  IgnoreCase: boolean = false // false = use default
 
  ): integer;
 
  
 
// directories
 
// directories
Line 73: Line 69:
 
function TrimAndExpandDirectory(const Filename: string; const BaseDir: string = ''): string; // empty string returns empty string
 
function TrimAndExpandDirectory(const Filename: string; const BaseDir: string = ''): string; // empty string returns empty string
  
function TryCreateRelativePath(const Dest, Source: String; UsePointDirectory: boolean;
+
function TryCreateRelativePath(const Dest, Source: String; UsePointDirectory: boolean; AlwaysRequireSharedBaseFolder: Boolean; out RelPath: String): Boolean;
  AlwaysRequireSharedBaseFolder: Boolean; out RelPath: String): Boolean;
 
  
function CreateRelativePath(const Filename, BaseDirectory: string;
+
function CreateRelativePath(const Filename, BaseDirectory: string;UsePointDirectory: boolean = false; AlwaysRequireSharedBaseFolder: Boolean = True): string;
  UsePointDirectory: boolean = false; AlwaysRequireSharedBaseFolder: Boolean = True): string;
 
  
 
function FileIsInPath(const Filename, Path: string): boolean;
 
function FileIsInPath(const Filename, Path: string): boolean;
Line 92: Line 86:
 
function MinimizeSearchPath(const SearchPath: string): string;
 
function MinimizeSearchPath(const SearchPath: string): string;
  
function FindPathInSearchPath(APath: PChar; APathLen: integer;
+
function FindPathInSearchPath(APath: PChar; APathLen: integer; SearchPath: PChar; SearchPathLen: integer): PChar; overload;
  SearchPath: PChar; SearchPathLen: integer): PChar; overload;
 
  
 
function FindPathInSearchPath(const APath, SearchPath: string): integer; overload;
 
function FindPathInSearchPath(const APath, SearchPath: string): integer; overload;
Line 150: Line 143:
 
function GetFileDescription(const AFilename: string): string;
 
function GetFileDescription(const AFilename: string): string;
  
function ReadAllLinks(const Filename: string;
+
function ReadAllLinks(const Filename: string; {%H-}ExceptionOnError: boolean): string; // if a link is broken returns ''
  {%H-}ExceptionOnError: boolean): string; // if a link is broken returns ''
 
  
 
function TryReadAllLinks(const Filename: string): string; // if a link is broken returns Filename
 
function TryReadAllLinks(const Filename: string): string; // if a link is broken returns Filename
Line 161: Line 153:
  
 
// Getting FileNameOnError
 
// Getting FileNameOnError
  TPhysicalFilenameOnError = (pfeException,pfeEmpty,pfeOriginal);
+
TPhysicalFilenameOnError = (pfeException,pfeEmpty,pfeOriginal);
  
 
// Getting file name except for Unix
 
// Getting file name except for Unix
function GetPhysicalFilename(const Filename: string;
+
function GetPhysicalFilename(const Filename: string; OnError: TPhysicalFilenameOnError): string;
  OnError: TPhysicalFilenameOnError): string;
 
  
 
// for Unix
 
// for Unix
function GetUnixPhysicalFilename(const Filename: string;
+
function GetUnixPhysicalFilename(const Filename: string; ExceptionOnError: boolean): string; // if a link is broken returns ''
  ExceptionOnError: boolean): string; // if a link is broken returns ''
 
  
 
// Getting paths
 
// Getting paths
 
function GetAppConfigDirUTF8(Global: Boolean; Create: boolean = false): string;
 
function GetAppConfigDirUTF8(Global: Boolean; Create: boolean = false): string;
  
function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false;
+
function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false; CreateDir: boolean = false): string;
  CreateDir: boolean = false): string;
 
  
 
function GetTempFileNameUTF8(const Dir, Prefix: String): String;
 
function GetTempFileNameUTF8(const Dir, Prefix: String): String;
Line 195: Line 184:
  
 
// Commandline Parameters
 
// Commandline Parameters
procedure SplitCmdLineParams(const Params: string; ParamList: TStrings;
+
procedure SplitCmdLineParams(const Params: string; ParamList: TStrings; ReadBackslash: boolean = false);
  ReadBackslash: boolean = false);
 
  
 
function StrToCmdLineParam(const Param: string): string;
 
function StrToCmdLineParam(const Param: string): string;
Line 202: Line 190:
 
function MergeCmdLineParams(ParamList: TStrings): string;
 
function MergeCmdLineParams(ParamList: TStrings): string;
  
// Cache
+
// Cache  
  TInvalidateFileStateCacheEvent = procedure(const Filename: string);
+
TInvalidateFileStateCacheEvent = procedure(const Filename: string); var  OnInvalidateFileStateCache: TInvalidateFileStateCacheEvent = nil;
var  OnInvalidateFileStateCache: TInvalidateFileStateCacheEvent = nil;
 
  
 
procedure InvalidateFileStateCache(const Filename: string = ''); inline;
 
procedure InvalidateFileStateCache(const Filename: string = ''); inline;

Revision as of 17:46, 11 July 2019

// comparing filenames function CompareFilenames(const Filename1, Filename2: string): integer; overload;

function CompareFilenamesIgnoreCase(const Filename1, Filename2: string): integer;

function CompareFileExt(const Filename, Ext: string; CaseSensitive: boolean): integer; overload;

function CompareFileExt(const Filename, Ext: string): integer; overload;

function CompareFilenameStarts(const Filename1, Filename2: string): integer;

function CompareFilenames(Filename1: PChar; Len1: integer; Filename2: PChar; Len2: integer): integer; overload;

function CompareFilenamesP(Filename1, Filename2: PChar; IgnoreCase: boolean = false // false = use default): integer;

// directories function DirPathExists(DirectoryName: string): boolean;

function DirectoryIsWritable(const DirectoryName: string): boolean;

// file names function ExtractFileNameOnly(const AFilename: string): string;

function FilenameIsAbsolute(const TheFilename: string):boolean;

function FilenameIsWinAbsolute(const TheFilename: string):boolean;

function FilenameIsUnixAbsolute(const TheFilename: string):boolean;

function ForceDirectory(DirectoryName: string): boolean;

procedure CheckIfFileIsExecutable(const AFilename: string);

procedure CheckIfFileIsSymlink(const AFilename: string);

function FileIsExecutable(const AFilename: string): boolean;

function FileIsSymlink(const AFilename: string): boolean;

function FileIsHardLink(const AFilename: string): boolean;

function FileIsReadable(const AFilename: string): boolean;

function FileIsWritable(const AFilename: string): boolean;

function FileIsText(const AFilename: string): boolean;

function FileIsText(const AFilename: string; out FileReadable: boolean): boolean;

function FilenameIsTrimmed(const TheFilename: string): boolean;

function FilenameIsTrimmed(StartPos: PChar; NameLen: integer): boolean;

function TrimFilename(const AFilename: string): string;

function ResolveDots(const AFilename: string): string;

Procedure ForcePathDelims(Var FileName: string);

Function GetForcedPathDelims(Const FileName: string): String;

function CleanAndExpandFilename(const Filename: string): string; // empty string returns current directory

function CleanAndExpandDirectory(const Filename: string): string; // empty string returns current directory

function TrimAndExpandFilename(const Filename: string; const BaseDir: string = ): string; // empty string returns empty string

function TrimAndExpandDirectory(const Filename: string; const BaseDir: string = ): string; // empty string returns empty string

function TryCreateRelativePath(const Dest, Source: String; UsePointDirectory: boolean; AlwaysRequireSharedBaseFolder: Boolean; out RelPath: String): Boolean;

function CreateRelativePath(const Filename, BaseDirectory: string;UsePointDirectory: boolean = false; AlwaysRequireSharedBaseFolder: Boolean = True): string;

function FileIsInPath(const Filename, Path: string): boolean;

function AppendPathDelim(const Path: string): string;

function ChompPathDelim(const Path: string): string;

// search paths function CreateAbsoluteSearchPath(const SearchPath, BaseDirectory: string): string;

function CreateRelativeSearchPath(const SearchPath, BaseDirectory: string): string;

function MinimizeSearchPath(const SearchPath: string): string;

function FindPathInSearchPath(APath: PChar; APathLen: integer; SearchPath: PChar; SearchPathLen: integer): PChar; overload;

function FindPathInSearchPath(const APath, SearchPath: string): integer; overload;

// file operations function FileExistsUTF8(const Filename: string): boolean;

function FileAgeUTF8(const FileName: string): Longint;

function DirectoryExistsUTF8(const Directory: string): Boolean;

function ExpandFileNameUTF8(const FileName: string; {const} BaseDir: string = ): string;

function FindFirstUTF8(const Path: string; Attr: Longint; out Rslt: TSearchRec): Longint;

function FindNextUTF8(var Rslt: TSearchRec): Longint;

procedure FindCloseUTF8(var F: TSearchrec); inline;

function FileSetDateUTF8(const FileName: String; Age: Longint): Longint;

function FileGetAttrUTF8(const FileName: String): Longint;

function FileSetAttrUTF8(const Filename: String; Attr: longint): Longint;

function DeleteFileUTF8(const FileName: String): Boolean;

function RenameFileUTF8(const OldName, NewName: String): Boolean;

function FileSearchUTF8(const Name, DirList : String; ImplicitCurrentDir : Boolean = True): String;

function FileIsReadOnlyUTF8(const FileName: String): Boolean;

function GetCurrentDirUTF8: String;

function SetCurrentDirUTF8(const NewDir: String): Boolean;

function CreateDirUTF8(const NewDir: String): Boolean;

function RemoveDirUTF8(const Dir: String): Boolean;

function ForceDirectoriesUTF8(const Dir: string): Boolean;

// file open or created function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle;

function FileCreateUTF8(Const FileName : string) : THandle; overload;

function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle; overload;

Function FileCreateUtf8(Const FileName : String; ShareMode : Integer; Rights : Cardinal) : THandle; overload;

// file attributes and size function FileSizeUtf8(const Filename: string): int64;

function GetFileDescription(const AFilename: string): string;

function ReadAllLinks(const Filename: string; {%H-}ExceptionOnError: boolean): string; // if a link is broken returns

function TryReadAllLinks(const Filename: string): string; // if a link is broken returns Filename

function GetShellLinkTarget(const FileName: string): string;

// for debugging function DbgSFileAttr(Attr: LongInt): String;

// Getting FileNameOnError TPhysicalFilenameOnError = (pfeException,pfeEmpty,pfeOriginal);

// Getting file name except for Unix function GetPhysicalFilename(const Filename: string; OnError: TPhysicalFilenameOnError): string;

// for Unix function GetUnixPhysicalFilename(const Filename: string; ExceptionOnError: boolean): string; // if a link is broken returns

// Getting paths function GetAppConfigDirUTF8(Global: Boolean; Create: boolean = false): string;

function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false; CreateDir: boolean = false): string;

function GetTempFileNameUTF8(const Dir, Prefix: String): String;

// UNC paths function IsUNCPath(const {%H-}Path: String): Boolean;

function ExtractUNCVolume(const {%H-}Path: String): String;

function ExtractFileRoot(FileName: String): String;

// darwin paths function GetDarwinSystemFilename(Filename: string): string;

function GetDarwinNormalizedFilename(Filename: string; nForm:Integer=2): string;

// windows paths function SHGetFolderPathUTF8(ID : Integer) : String;

// Commandline Parameters procedure SplitCmdLineParams(const Params: string; ParamList: TStrings; ReadBackslash: boolean = false);

function StrToCmdLineParam(const Param: string): string;

function MergeCmdLineParams(ParamList: TStrings): string;

// Cache TInvalidateFileStateCacheEvent = procedure(const Filename: string); var OnInvalidateFileStateCache: TInvalidateFileStateCacheEvent = nil;

procedure InvalidateFileStateCache(const Filename: string = ); inline;