Created
July 12, 2013 06:43
-
-
Save dkstar88/5982413 to your computer and use it in GitHub Desktop.
FilenameHelper is a helper for TFilename, its purpose is to be able
to use TFilename like an object, codes readable and less writing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
FilenameHelper is a helper for TFilename, its purpose is to be able | |
to use TFilename like an object, codes readable and less writing. | |
Author: William Yang | |
Email: [email protected] | |
Features: | |
--------- | |
Where before extract a file path | |
-------------------------------- | |
Before: | |
>> path := ExtractFilePath(filename) | |
Now: | |
>> path := filename.Path | |
Change a path: | |
-------------------------------- | |
Before: | |
>> path := ChangeFilePath(filename, newpath); | |
Now: | |
>> path := filename.Path(newpath); | |
Demo Codes: | |
------------ | |
var | |
filename: TFilename; | |
begin | |
try | |
filename := ParamStr(0); | |
Writeln('Path: ', filename.Path); | |
Writeln('Filename: ', filename.Filename); | |
Writeln('Extention: ', filename.Extension); | |
Writeln('Change Path to C:\Windows'); | |
filename := filename.Path('C:\Windows'); | |
Writeln('Filename: ', filename); | |
Writeln('Change filename to php.ini'); | |
filename := filename.Filename('php.ini'); | |
Writeln('Filename: ', filename); | |
Writeln('Change extension to .old'); | |
Writeln('Filename: ', filename.Extension('.old')); | |
Readln; | |
except | |
on E: Exception do | |
Writeln(E.ClassName, ': ', E.Message); | |
end; | |
end. | |
} | |
unit FilenameHelper; | |
interface | |
uses SysUtils; | |
type | |
TFilenameHelper = record helper for TFilename | |
function Path: TFilename; overload; | |
function Extension: String; overload; | |
function Filename: String; overload; | |
function Path(APath: String): TFilename; overload; | |
function Extension(AExt: String): TFilename; overload; | |
function Filename(AFilename: String): TFilename; overload; | |
{$IFDEF MSWINDOWS} | |
function Drive: String; overload; | |
function Drive(ADrive: String): TFilename; overload; | |
{$ENDIF} | |
function Append(APart: String): TFilename; overload; | |
function IncludeTrailingPathDelimiter: TFilename; | |
function IsRelativePath: Boolean; | |
end; | |
implementation | |
{ TFilenameHelper } | |
function TFilenameHelper.Extension: String; | |
begin | |
Result := ExtractFileExt(Self); | |
end; | |
function TFilenameHelper.Append(APart: String): TFilename; | |
begin | |
Result := SysUtils.IncludeTrailingPathDelimiter(Self) + APart; | |
end; | |
{$IFDEF MSWINDOWS} | |
function TFilenameHelper.Drive: String; | |
begin | |
Result := ExtractFileDrive(Self); | |
end; | |
function TFilenameHelper.Drive(ADrive: String): TFilename; | |
begin | |
Result := ''; | |
if ADrive.Length > 0 then | |
begin | |
if (Length(Self) >= 2) and (String(Self).Chars[1] = DriveDelim) then | |
Self := ADrive + Copy(Self, 1, Length(Self)-1); | |
end; | |
end; | |
{$ENDIF} | |
function TFilenameHelper.Extension(AExt: String): TFilename; | |
begin | |
Result := ChangeFileExt(Self, AExt); | |
end; | |
function TFilenameHelper.Filename(AFilename: String): TFilename; | |
begin | |
Result := SysUtils.IncludeTrailingPathDelimiter(ExtractFilePath(Self)) + AFilename; | |
end; | |
function TFilenameHelper.IncludeTrailingPathDelimiter: TFilename; | |
begin | |
Result := SysUtils.IncludeTrailingPathDelimiter(Self); | |
end; | |
function TFilenameHelper.IsRelativePath: Boolean; | |
begin | |
Result := SysUtils.IsRelativePath(Self); | |
end; | |
function TFilenameHelper.Filename: String; | |
begin | |
Result := ExtractFilename(Self); | |
end; | |
function TFilenameHelper.Path(APath: String): TFilename; | |
begin | |
Result := ChangeFilePath(Self, APath); | |
end; | |
function TFilenameHelper.Path: TFilename; | |
begin | |
Result := ExtractFilePath(Self); | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment