added File is readable check when opening a file

git-svn-id: trunk@8397 -
This commit is contained in:
mattias 2005-12-31 12:40:49 +00:00
parent 891e102965
commit 89545f1d61
4 changed files with 32 additions and 5 deletions

View File

@ -131,10 +131,12 @@ function LoadCodeBuffer(var ACodeBuffer: TCodeBuffer; const AFilename: string;
Flags: TLoadBufferFlags): TModalResult;
var
ACaption, AText: string;
FileReadable: boolean;
begin
repeat
FileReadable:=true;
if (lbfCheckIfText in Flags)
and FileExists(AFilename) and (not FileIsText(AFilename))
and (not FileIsText(AFilename,FileReadable)) and FileReadable
then begin
if lbfQuiet in Flags then begin
Result:=mrCancel;
@ -147,8 +149,12 @@ begin
end;
if Result<>mrOk then break;
end;
ACodeBuffer:=CodeToolBoss.LoadFile(AFilename,lbfUpdateFromDisk in Flags,
lbfRevert in Flags);
if FileReadable then
ACodeBuffer:=CodeToolBoss.LoadFile(AFilename,lbfUpdateFromDisk in Flags,
lbfRevert in Flags)
else
ACodeBuffer:=nil;
if ACodeBuffer<>nil then begin
Result:=mrOk;
end else begin

View File

@ -6544,6 +6544,7 @@ var Ext,AText,ACaption: string;
NewBuf: TCodeBuffer;
LastDesigner: TDesigner;
AnUnitInfo: TUnitInfo;
FileReadable: Boolean;
begin
// close the old project
if SomethingOfProjectIsModified then begin
@ -6576,7 +6577,7 @@ begin
Result:=MessageDlg(ACaption, AText, mtError, [mbAbort], 0);
exit;
end;
// if there is a project info file, load that instead
if (Ext<>'.lpi') and (FileExists(ChangeFileExt(AFileName,'.lpi'))) then begin
// load instead of program file the project info file
@ -6584,13 +6585,19 @@ begin
Ext:='.lpi';
end;
if (not FileIsText(AFilename)) then begin
if (not FileIsText(AFilename,FileReadable)) and FileReadable then begin
ACaption:=lisFileNotText;
AText:=Format(lisFileDoesNotLookLikeATextFileOpenItAnyway, ['"', AFilename,
'"', #13, #13]);
Result:=MessageDlg(ACaption, AText, mtConfirmation, [mbYes, mbAbort], 0);
if Result=mrAbort then exit;
end;
if not FileReadable then begin
Result:=QuestionDlg('Unable to read file',
'Unable to read file "'+AFilename+'".',
mtError,[mrCancel,'Skip file',mrAbort,'Abort all loading'],0);
exit;
end;
if ofAddToRecent in Flags then
AddRecentProjectFileToEnvironment(AFileName);

View File

@ -43,6 +43,7 @@ procedure CheckIfFileIsSymlink(const AFilename: string);
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 FileIsExecutable(const AFilename: string): boolean;
function FileIsSymlink(const AFilename: string): boolean;
function GetFileDescription(const AFilename: string): string;

View File

@ -397,12 +397,22 @@ end;
function FileIsText(const AFilename: string): boolean;
------------------------------------------------------------------------------}
function FileIsText(const AFilename: string): boolean;
var
FileReadable: Boolean;
begin
Result:=FileIsText(AFilename,FileReadable);
if FileReadable then ;
end;
function FileIsText(const AFilename: string; out FileReadable: boolean
): boolean;
var fs: TFileStream;
Buf: string;
Len, i: integer;
NewLine: boolean;
begin
Result:=false;
FileReadable:=true;
try
fs:=TFileStream.Create(AFilename,fmOpenRead);
try
@ -427,6 +437,9 @@ begin
fs.Free;
end;
except
on E: Exception do begin
FileReadable:=false;
end;
end;
end;