Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
crutchy-
committed
Jan 3, 2015
1 parent
76ff738
commit c20b15a
Showing
16 changed files
with
4,801 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
unit JMC_FileUtils; | ||
|
||
interface | ||
|
||
uses | ||
Classes, | ||
ShellAPI, | ||
SysUtils, | ||
Windows, | ||
FileCtrl; | ||
|
||
function ExpandPath(const MasterFileName, SlaveFileName: string): string; | ||
function ExpandPathFromExe(const SlaveFileName: string): string; | ||
function RelatePathFromExe(const SlaveFileName: string): string; | ||
function SettingsFileName: string; | ||
function FileToStr(const FileName: string; var S: string): Boolean; | ||
function StrToFile(const FileName: string; var S: string): Boolean; | ||
function CreatePath(const Path: string): Boolean; | ||
function OpenFile(const FileName: string; const OverrideValidation: Boolean = False): Boolean; | ||
|
||
implementation | ||
|
||
{ ExpandPath doesn't work when: | ||
MasterFileName = 'C:\My Documents\Delphi Projects\ParserFAR23\Source\Data\' | ||
SlaveFileName = '\Data\2007-11-22 FAR 23.htm' } | ||
function ExpandPath(const MasterFileName, SlaveFileName: string): string; // If SlaveFileName is a path only, encompass a call to ExpandPath with a call to IncludeTrailingPathDelimiter. | ||
var | ||
S: string; | ||
begin | ||
S := SysUtils.GetCurrentDir; | ||
if not SysUtils.SetCurrentDir(ExtractFilePath(MasterFileName)) then | ||
begin | ||
Result := ''; | ||
Exit; | ||
end; | ||
Result := SysUtils.ExpandFileName(SlaveFileName); | ||
SysUtils.SetCurrentDir(S); | ||
end; | ||
|
||
function ExpandPathFromExe(const SlaveFileName: string): string; | ||
begin | ||
Result := ExpandPath(SysUtils.ExtractFilePath(ParamStr(0)), SlaveFileName); | ||
end; | ||
|
||
function RelatePathFromExe(const SlaveFileName: string): string; | ||
begin | ||
Result := SysUtils.ExtractRelativePath(SysUtils.ExtractFilePath(ParamStr(0)), SlaveFileName); | ||
end; | ||
|
||
function SettingsFileName: string; | ||
begin | ||
Result := SysUtils.ChangeFileExt(ParamStr(0), '.ini'); | ||
end; | ||
|
||
function FileToStr(const FileName: string; var S: string): Boolean; // FileName must be fully qualified | ||
var | ||
F: TFileStream; | ||
Buffer: array[1..1024] of Char; | ||
Temp: string; | ||
i: Integer; | ||
begin | ||
Result := False; | ||
S := ''; | ||
if SysUtils.FileExists(FileName) then | ||
try | ||
F := Classes.TFileStream.Create(FileName, fmOpenRead + fmShareDenyNone); | ||
F.Seek(0, soFromBeginning); | ||
repeat | ||
i := F.Read(Buffer, SizeOf(Buffer)); | ||
Temp := Copy(Buffer, 1, i); | ||
S := S + Temp; | ||
until Length(Temp) = 0; | ||
F.Free; | ||
Result := True; | ||
except | ||
Result := False; | ||
end; | ||
end; | ||
|
||
function StrToFile(const FileName: string; var S: string): Boolean; // FileName must be fully qualified | ||
var | ||
F: TextFile; | ||
Path: string; | ||
begin | ||
Path := ExtractFilePath(FileName); | ||
if not FileCtrl.DirectoryExists(Path) then | ||
FileCtrl.ForceDirectories(Path); | ||
AssignFile(F, FileName); | ||
try | ||
Rewrite(F); | ||
except | ||
Result := False; | ||
Exit; | ||
end; | ||
Write(F, S); | ||
CloseFile(F); | ||
Result := True; | ||
end; | ||
|
||
function CreatePath(const Path: string): Boolean; | ||
begin | ||
try | ||
Result := FileCtrl.ForceDirectories(Path); | ||
except | ||
Result := False; | ||
end; | ||
end; | ||
|
||
function OpenFile(const FileName: string; const OverrideValidation: Boolean = False): Boolean; | ||
begin | ||
Result := SysUtils.FileExists(FileName) or OverrideValidation; | ||
if Result then | ||
Result := ShellAPI.ShellExecute(0, 'open', PChar(FileName), '', nil, SW_SHOWNORMAL) <= 32; | ||
end; | ||
|
||
end. |
Oops, something went wrong.