From 55fe97bcdcd2328e05500b18d777734cccda0b75 Mon Sep 17 00:00:00 2001
From: dsiders
This unit contains basic functions similar to those in the RTL, but use UTF-8 instead of the default system encoding. Please note that AnsiToUTF8 and UTF8ToAnsi need a widestring manager under Linux, BSD, and macOS. Normally these operating systems use UTF-8 as the system encoding so the WideStringManager is not needed.
+
+ CompareFileNames is an Integer function used to compare the specified file names (and lengths).
+
+ The Filename1 and Filename2 arguments are PChar types with pointers to the first character in each of the compared file names.The Len1 and Len2 arguments contain the length (or number of characters) for the pointers. If a length value is omitted, it implies the corresponding file name argument is empty.
+
+ ResolveLinks indicates whether file system or symbolic links are resolved before comparing the file name arguments. When set to True, the ComparePhysicalFilenames routine is called to resolve the file system links to actual path and file names for the comparison. For platforms where NotLiteralFilenames is defined, the file names are copied to a temporary string before resolving the file names.
+
+ Otherwise, the CompareFileNames routine (in
+ The return value is 0 (zero) when the file names contain (or resolve to) the same files on the local file system. It is set to the difference between the lengths in Len1 and Len2 when one of the arguments has been omitted. For platforms where NotLiteralFilenames is not defined, it contains the initial difference between the ordinal values in the file names. Otherwise, it is set to the value from CompareFilenames (in
+ ExpandUNCFileNameUTF8 is a String function used to expand the UNC file name in the FileName argument to an absolute UNC file name. It is the UTF-8-enabled equivalent of the ExpandUNCFileName routine from the FPC RTL. +
++ ExpandUNCFileNameUTF8 calls the ExpandUNCFileName routine in RTL to get the return value for the method. Drive letters which are mapped to shared disks are replaced with the UNC device name for the mapped drive. The return value is an empty string ('') if an error occurs in ExpandUNCFileName. +
++ FileIsInPath is a Boolean function used to determine whether the file in FileName is located in the specified Path. FileIsInPath calls the CleanAndExpandFilename and CleanAndExpandDirectory routines to resolve drive letters or relative path information included in the arguments. +
++ The return value is True when the resolved paths for the arguments contain the same values. FileIsInPath does not verify whether Path or FileName actually exist on the local file system. It compares the specified string values only. +
++ Use DirectoryExists or DirectoryExistsUTF8 to determine whether a path exists on the local file system. +
++ Use FileExists or FileExistsUTF8 to determine whether a file exists on the local file system. +
++ Example: +
+
+// uses FileUtil;
+// ...
+// var
+// sDir, sFile: String;
+// bResult: Boolean;
+// ...
+// neither the file nor the directory exist on the file system
+sDir := 'q:\bogus\path\';
+sFile := 'q:\bogus\path\..\path\filename.ext';
+bResult := FileIsInPath(sFile, sDir);
+
+ShowMessage('File: ' + sFile + LineEnding +
+ 'is in Path: ' + sDir + '?' + LineEnding +
+ 'FileIsInPath() result is: ' + bResult.ToString(TUseBoolStrs.True) + '.');
+
+ - FileIsInDirectory - checks whether a file with FileName exists within the given Directory. + FileIsInDirectory is a Boolean function used to determine whether the file in FileName is located in Directory. FileIsInDirectory calls the CleanAndExpandFilename and CleanAndExpandDirectory routines to resolve drive letters or relative path information included in the arguments.
++ The return value is True when the resolved paths for the arguments contain the same values. FileIsInDirectory does not verify whether Directory or FileName actually exist on the local file system. It compares the specified string values only. +
++ Use DirectoryExists or DirectoryExistsUTF8 to determine whether a path exists on the local file system. +
++ Use FileExists or FileExistsUTF8 to determine whether a file exists on the local file system. +
++ Example: +
+
+// uses FileUtil;
+// ...
+// var
+// sDir, sFile: String;
+// bResult: Boolean;
+// ...
+// neither the file nor the directory exist on the file system
+sDir := 'q:\bogus\path\';
+sFile := 'q:\bogus\path\..\path\filename.ext';
+bResult := FileIsInDirectory(sFile, sDir);
+
+ShowMessage('File: ' + sFile + LineEnding +
+ 'is in Directory: ' + sDir + '?' + LineEnding +
+ 'FileIsInDirectory() result is: ' + bResult.ToString(TUseBoolStrs.True) + '.');
+
@@ -397,21 +497,21 @@
- FindDiskFilename finds the file that best fits the supplied filename. Searches for the filename case on disk. The file must exist.
+ FindDiskFilename is a String function used to get the file name which most closely matches the value in the FileName argument. It does not use case-sensitivity when comparing file names - regardless of the platform. In addition, the file must exist on the local file system.
- For example: If Filename='file' and there is only a 'File', then 'File' will be returned.
+ FileName can include a fully-qualified path including a drive letter on Windows platforms. A drive letter in the value is always converted to uppercase. The path to the file is extracted and sanitized to resolve relative path references.
+ The file path is used to locate and compare all files in the directory by calling FindFirstUTF8 and FindNextUTF8. Each file entry is examined using CompareFilenamesIgnoreCase for a case-insensitive match for the specified file name. When a single match is found, its fully qualified path and file name are assigned as the return value. If more than one file is a match, the result is ambiguous and the resolved value for the original FileName argument is returned.
+
+ Example:
+
+// uses FileUtil;
+// var AFileName, AResult: String;
+// ...
+
+// Windows
+AFileName := 'c:\WINDOWS\WIN.INI';
+AResult := FindDiskFileName(AFileName);
+// AResult contains: 'C:\Windows\win.ini';
+
+// UNIX-like platforms
+AFileName := '/ETC/FONTS/FONTS.CONF';
+AResult := FindDiskFileName(AFileName);
+// AResult contains: '/etc/fonts/fonts.conf';
+