fileutil/zh CN
From Free Pascal wiki
Jump to navigationJump to search
│
English (en) │
français (fr) │
русский (ru) │
中文(中国大陆) (zh_CN) │
Lazarus的FileUtil单元包含用以维持Delphi的FileUtil单元的兼容性的函数和过程。专门处理UTF8文件的文件例行程序应该转到LazFileUtils单元。
函数列表
//文件属性或状态
function CompareFilenames(const Filename1, Filename2: string): integer; inline;
function CompareFilenamesIgnoreCase(const Filename1, Filename2: string): integer; inline;
function CompareFilenames(const Filename1, Filename2: string; ResolveLinks: boolean): integer;
function CompareFilenames(Filename1: PChar; Len1: integer; Filename2: PChar; Len2: integer; ResolveLinks: boolean): integer;
function FilenameIsAbsolute(const TheFilename: string):boolean; inline;
function FilenameIsWinAbsolute(const TheFilename: string):boolean; inline;
function FilenameIsUnixAbsolute(const TheFilename: string):boolean; inline;
procedure CheckIfFileIsExecutable(const AFilename: string); inline;
procedure CheckIfFileIsSymlink(const AFilename: string); inline;
function FileIsReadable(const AFilename: string): boolean; inline;
function FileIsWritable(const AFilename: string): boolean; inline;
function FileIsText(const AFilename: string): boolean; inline;
function FileIsText(const AFilename: string; out FileReadable: boolean): boolean; inline;
function FileIsExecutable(const AFilename: string): boolean; inline;
function FileIsSymlink(const AFilename: string): boolean; inline;
function FileIsHardLink(const AFilename: string): boolean; inline;
function FileSize(const Filename: string): int64; overload; inline;
function GetFileDescription(const AFilename: string): string; inline;
function ReadAllLinks(const Filename: string; ExceptionOnError: boolean): string; //如果一个链接是破损的,返回''
function TryReadAllLinks(const Filename: string): string; //如果一个链接是破损的,返回文件名称
//目录
function DirPathExists(const FileName: String): Boolean; inline;
function ForceDirectory(DirectoryName: string): boolean; inline;
function DeleteDirectory(const DirectoryName: string; OnlyChildren: boolean): boolean;
function ProgramDirectory: string;
function DirectoryIsWritable(const DirectoryName: string): boolean; inline;
//文件名称部分
function ExtractFileNameOnly(const AFilename: string): string; inline;
function ExtractFileNameWithoutExt(const AFilename: string): string;
function CompareFileExt(const Filename, Ext: string; CaseSensitive: boolean): integer; overload; inline;
function CompareFileExt(const Filename, Ext: string): integer; overload; inline;
function FilenameIsPascalUnit(const Filename: string): boolean;
function AppendPathDelim(const Path: string): string; inline;
function ChompPathDelim(const Path: string): string; inline;
function TrimFilename(const AFilename: string): string; inline;
function CleanAndExpandFilename(const Filename: string): string; inline;
function CleanAndExpandDirectory(const Filename: string): string; inline;
function CreateAbsoluteSearchPath(const SearchPath, BaseDirectory: string): string;
function CreateRelativePath(const Filename, BaseDirectory: string; UsePointDirectory: boolean = false; AlwaysRequireSharedBaseFolder: Boolean = True): string; inline;
function CreateAbsolutePath(const Filename, BaseDirectory: string): string;
function FileIsInPath(const Filename, Path: string): boolean;
function FileIsInDirectory(const Filename, Directory: string): boolean;
//文件搜索
type
TSearchFileInPathFlag = (
sffDontSearchInBasePath,
sffSearchLoUpCase
);
TSearchFileInPathFlags = set of TSearchFileInPathFlag;
const
AllDirectoryEntriesMask = '*';
function GetAllFilesMask: string; inline;
function GetExeExt: string; inline;
function SearchFileInPath(const Filename, BasePath, SearchPath, Delimiter: string; Flags: TSearchFileInPathFlags): string;
function SearchAllFilesInPath(const Filename, BasePath, SearchPath, Delimiter: string; Flags: TSearchFileInPathFlags): TStrings;
function FindDiskFilename(const Filename: string): string;
function FindDiskFileCaseInsensitive(const Filename: string): string;
function FindDefaultExecutablePath(const Executable: string; const BaseDir: string = ''): string;
type
TFileIterator = class
private
FPath: String;
FLevel: Integer;
FFileInfo: TSearchRec;
FSearching: Boolean;
function GetFileName: String;
public
procedure Stop;
function IsDirectory: Boolean;
public
property FileName: String read GetFileName;
property FileInfo: TSearchRec read FFileInfo;
property Level: Integer read FLevel;
property Path: String read FPath;
property Searching: Boolean read FSearching;
end;
TFileFoundEvent = procedure (FileIterator: TFileIterator) of object;
TDirectoryFoundEvent = procedure (FileIterator: TFileIterator) of object;
TDirectoryEnterEvent = procedure (FileIterator: TFileIterator) of object;
TFileSearcher = class(TFileIterator)
private
FMaskSeparator: char;
FFollowSymLink: Boolean;
FOnFileFound: TFileFoundEvent;
FOnDirectoryFound: TDirectoryFoundEvent;
FOnDirectoryEnter: TDirectoryEnterEvent;
FFileAttribute: Word;
FDirectoryAttribute: Word;
procedure RaiseSearchingError;
protected
procedure DoDirectoryEnter; virtual;
procedure DoDirectoryFound; virtual;
procedure DoFileFound; virtual;
public
constructor Create;
procedure Search(const ASearchPath: String; ASearchMask: String = '';
ASearchSubDirs: Boolean = True; CaseSensitive: Boolean = False);
public
property MaskSeparator: char read FMaskSeparator write FMaskSeparator;
property FollowSymLink: Boolean read FFollowSymLink write FFollowSymLink;
property FileAttribute: Word read FFileAttribute write FFileAttribute default faAnyfile;
property DirectoryAttribute: Word read FDirectoryAttribute write FDirectoryAttribute default faDirectory;
property OnDirectoryFound: TDirectoryFoundEvent read FOnDirectoryFound write FOnDirectoryFound;
property OnFileFound: TFileFoundEvent read FOnFileFound write FOnFileFound;
property OnDirectoryEnter: TDirectoryEnterEvent read FOnDirectoryEnter write FOnDirectoryEnter;
end;
function [[FindAllFiles]]( const SearchPath: String; SearchMask: String = ''; SearchSubDirs: Boolean = True): TStringList;
function FindAllDirectories(const SearchPath: string; SearchSubDirs: Boolean = True): TStringList;
//复制一个文件或一整个目录树
function [[CopyFile]](const SrcFilename, DestFilename: string; Flags: TCopyFileFlags=[cffOverwriteFile]): boolean;
function [[CopyFile]](const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean;
function CopyDirTree(const SourceDir, TargetDir: string; Flags: TCopyFileFlags=[]): Boolean;
//文件动作
function ReadFileToString(const Filename: string): string;
function GetTempFilename(const Directory, Prefix: string): string; inline;
//基本的函数,类似于RTL,但是使用UTF-8,而不是使用系统编码
//AnsiToUTF8和UTF8ToAnsi在Linux、BSD、MacOSX下需要一个widestring管理器
//但是通常情况下,这些操作系统使用UTF-8作为系统编码,因此不需要widestring管理器。
function NeedRTLAnsi: boolean; inline;// true,如果系统编码不是UTF-8
function NeedRTLAnsi: boolean; inline;// true,如果系统编码不是UTF-8
procedure SetNeedRTLAnsi(NewValue: boolean); inline;
function UTF8ToSys(const s: string): string; inline;//像UTF8ToAnsi一样,但是更多的独立于widestring管理器
function SysToUTF8(const s: string): string; inline;//像AnsiToUTF8一样,但是更多的独立于widestring管理器
function ConsoleToUTF8(const s: string): string; inline;//转换OEM编码字符串到UTF8 (与一些Windows的特殊函数一起使用)
function UTF8ToConsole(const s: string): string; inline;//转换UTF8字符串到控制台编码(由Write、WriteLn使用)
// 文件操作
function FileExistsUTF8(const Filename: string): boolean; inline;
function FileAgeUTF8(const FileName: string): Longint; inline;
function DirectoryExistsUTF8(const Directory: string): Boolean; inline;
function ExpandFileNameUTF8(const FileName: string): string; inline;
function ExpandUNCFileNameUTF8(const FileName: string): string;
function ExtractShortPathNameUTF8(Const FileName : String) : String;
function FindFirstUTF8(const Path: string; Attr: Longint; out Rslt: TSearchRec): Longint; inline;
function FindNextUTF8(var Rslt: TSearchRec): Longint; inline;
procedure FindCloseUTF8(var F: TSearchrec); inline;
function FileSetDateUTF8(const FileName: String; Age: Longint): Longint; inline;
function FileGetAttrUTF8(const FileName: String): Longint; inline;
function FileSetAttrUTF8(const Filename: String; Attr: longint): Longint; inline;
function DeleteFileUTF8(const FileName: String): Boolean; inline;
function RenameFileUTF8(const OldName, NewName: String): Boolean; inline;
function FileSearchUTF8(const Name, DirList : String; ImplicitCurrentDir : Boolean = True): String; inline;
function FileIsReadOnlyUTF8(const FileName: String): Boolean; inline;
function GetCurrentDirUTF8: String; inline;
function SetCurrentDirUTF8(const NewDir: String): Boolean; inline;
function CreateDirUTF8(const NewDir: String): Boolean; inline;
function RemoveDirUTF8(const Dir: String): Boolean; inline;
function ForceDirectoriesUTF8(const Dir: string): Boolean; inline;
function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle; inline;
function FileCreateUTF8(Const FileName : string) : THandle; overload; inline;
function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle; overload; inline;
//环境
function ParamStrUTF8(Param: Integer): string; inline;
function GetEnvironmentStringUTF8(Index: Integer): string; inline;
function GetEnvironmentVariableUTF8(const EnvVar: string): String; inline;
function GetAppConfigDirUTF8(Global: Boolean; Create: boolean = false): string; inline;
function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false; CreateDir: boolean = false): string; inline;
//其它
function SysErrorMessageUTF8(ErrorCode: Integer): String; inline;
如何使用单元
为在一个工程中启动FileUtil,只需要将LazUtils添加到所需要的软件包中。按照这个步骤:
1- 转到Lazarus IDE 菜单"工程 > 工程查看器"。
2- 在"工程查看器"对话框窗口中,单击"添加 > 新建需要"。
3- 在"新建需要"对话框窗口中,查找LazUtils单元,然后单击OK。
4- 现在,你可以将FileUtil添加到一个Uses分句之中。
贡献者和更改
- 简体中文版本由 robsean 于 2022-05-07 创建