- try //try..except for procedure
- // open the text file
- hFile:= Windows.CreateFile(PChar(FileName),GENERIC_READ,0,nil,
- OPEN_EXISTING,
- FILE_FLAG_SEQUENTIAL_SCAN,0);
- if hFile= INVALID_HANDLE_VALUE then
- begin
- MessageDlg('The file "' + FileName + '" could not be found!',
- mtError,[mbOK],0);
- exit;
- end;
-
- try //try..finally for hFile
- FileSize:= FileSeek(hFile,0,2); //get file size
- if FileSize = -1 then
- RaiseLastOSError;
-
- //move file content to InString
- FileSeek(hFile,0,0);
- SetLength(InString,FileSize);
- PInString:= PChar(InString);
- FileRead(hFile,PInString^,FileSize);
-
- //search the string backwards for the first appearance of ":"
- Index:= FileSize;
- Repeat
- dec(Index);
- if InString[Index] = ':' then
- Break;
- Until (InString[Index] = #10) or (Index = 1);
-
- //if the last line of lyx.bat doesn't contain a ":" (a path)
- if (InString[Index] = #10) or (Index = 1) then
- begin
- MessageDlg('The file lyx.bat is corrupted!',mtError,[mbOK],0);
- exit;
- end;
-
- //jump before the ":" to the drive letter
- dec(Index);
- //search for the LaunchName = end of the path
- Last:= Pos(LaunchName,InString);
- //the InString contains between Index and Last the wanted path
- PathR:= Copy(InString,Index,Last - Index);
- //attach LyX's executable to the path
- PathR:= Path + ExecName;
-
- finally //close the text file
- Windows.CloseHandle(hFile);
- end; //end finally
-
- except //when an error occurred somewhere in the procedure
- MessageDlg('The file "' + FileName + '" is corrupted!',mtError,[mbOK],0);
- end; //end except