EditorManager.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <CoreMain.h>
  6. #include <TextsWin.h>
  7. #include <SessionData.h>
  8. #include "WinConfiguration.h"
  9. #include "EditorManager.h"
  10. #include <algorithm>
  11. #include <DateUtils.hpp>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. TEditedFileData::TEditedFileData()
  16. {
  17. ForceText = false;
  18. Terminal = NULL;
  19. SessionData = NULL;
  20. }
  21. //---------------------------------------------------------------------------
  22. TEditedFileData::~TEditedFileData()
  23. {
  24. delete SessionData;
  25. }
  26. //---------------------------------------------------------------------------
  27. __fastcall TEditorManager::TEditorManager()
  28. {
  29. FSection = new TCriticalSection();
  30. FOnFileChange = NULL;
  31. FOnFileReload = NULL;
  32. FOnFileEarlyClosed = NULL;
  33. FOnFileUploadComplete = NULL;
  34. }
  35. //---------------------------------------------------------------------------
  36. __fastcall TEditorManager::~TEditorManager()
  37. {
  38. for (unsigned int i = FFiles.size(); i > 0; i--)
  39. {
  40. int Index = i - 1;
  41. TFileData * FileData = &FFiles[Index];
  42. // pending should be only external editors and files being uploaded
  43. DebugAssert(FileData->Closed || FileData->External);
  44. if (!FileData->Closed)
  45. {
  46. if (!CloseFile(Index, true, true))
  47. {
  48. ReleaseFile(Index);
  49. }
  50. }
  51. }
  52. delete FSection;
  53. }
  54. //---------------------------------------------------------------------------
  55. bool __fastcall TEditorManager::Empty(bool IgnoreClosed)
  56. {
  57. TGuard Guard(FSection);
  58. bool Result;
  59. if (!IgnoreClosed)
  60. {
  61. Result = (FFiles.size() == 0);
  62. }
  63. else
  64. {
  65. Result = true;
  66. for (unsigned int i = 0; i < FFiles.size(); i++)
  67. {
  68. if (!FFiles[i].Closed)
  69. {
  70. Result = false;
  71. break;
  72. }
  73. }
  74. }
  75. return Result;
  76. }
  77. //---------------------------------------------------------------------------
  78. bool __fastcall TEditorManager::CanAddFile(const UnicodeString RemoteDirectory,
  79. const UnicodeString OriginalFileName, const UnicodeString SessionName,
  80. TObject *& Token, UnicodeString & ExistingLocalRootDirectory,
  81. UnicodeString & ExistingLocalDirectory)
  82. {
  83. TGuard Guard(FSection);
  84. bool Result = true;
  85. Token = NULL;
  86. for (unsigned int i = 0; i < FFiles.size(); i++)
  87. {
  88. TFileData * FileData = &FFiles[i];
  89. // include even "closed" (=being uploaded) files as it is nonsense
  90. // to download file being uploaded
  91. if ((FileData->Data->RemoteDirectory == RemoteDirectory) &&
  92. (FileData->Data->OriginalFileName == OriginalFileName) &&
  93. (FileData->Data->SessionName == SessionName))
  94. {
  95. if (!FileData->External)
  96. {
  97. Result = false;
  98. if (!FileData->Closed && (FileData->Token != NULL))
  99. {
  100. Token = FileData->Token;
  101. }
  102. }
  103. else
  104. {
  105. // MDI editor?
  106. if (FileData->Process == INVALID_HANDLE_VALUE)
  107. {
  108. // file is just being uploaded, do not allow new editor instance
  109. if (FileData->Closed)
  110. {
  111. Result = false;
  112. }
  113. else
  114. {
  115. UnicodeString AExistingLocalDirectory = ExtractFilePath(FileData->FileName);
  116. // If the temporary directory was deleted, behave like the file was never opened
  117. if (DirectoryExists(AExistingLocalDirectory))
  118. {
  119. // get directory where the file already is so we download it there again
  120. ExistingLocalRootDirectory = FileData->Data->LocalRootDirectory;
  121. ExistingLocalDirectory = AExistingLocalDirectory;
  122. }
  123. CloseFile(i, false, false); // do not delete file
  124. Result = true;
  125. }
  126. }
  127. else
  128. {
  129. Result = false;
  130. }
  131. }
  132. break;
  133. }
  134. }
  135. if (Result)
  136. {
  137. if (FFiles.size() >= WinConfiguration->Editor.MaxEditors)
  138. {
  139. throw Exception(LoadStr(TOO_MANY_EDITORS));
  140. }
  141. }
  142. return Result;
  143. }
  144. //---------------------------------------------------------------------------
  145. TEditedFileData * TEditorManager::FindByUploadCompleteEvent(HANDLE UploadCompleteEvent)
  146. {
  147. TGuard Guard(FSection);
  148. for (unsigned int i = 0; i < FFiles.size(); i++)
  149. {
  150. TFileData * FileData = &FFiles[i];
  151. if (FileData->UploadCompleteEvent == UploadCompleteEvent)
  152. {
  153. return FileData->Data;
  154. }
  155. }
  156. return NULL;
  157. }
  158. //---------------------------------------------------------------------------
  159. void __fastcall TEditorManager::ProcessFiles(TEditedFileProcessEvent Callback, void * Arg)
  160. {
  161. TGuard Guard(FSection);
  162. for (unsigned int i = 0; i < FFiles.size(); i++)
  163. {
  164. TFileData * FileData = &FFiles[i];
  165. Callback(FileData->FileName, FileData->Data,
  166. (FileData->Closed ? NULL : FileData->Token), Arg);
  167. }
  168. }
  169. //---------------------------------------------------------------------------
  170. bool __fastcall TEditorManager::CloseInternalEditors(TNotifyEvent CloseCallback)
  171. {
  172. TGuard Guard(FSection);
  173. // Traverse from end, as closing internal editor causes deletion of
  174. // respective file vector element.
  175. TObject * PrevToken = NULL;
  176. for (unsigned int i = FFiles.size(); i > 0; i--)
  177. {
  178. // Note that element may be deleted by external cause (like if external editor
  179. // is closed while "save confirmation" message is displayed).
  180. if (i <= FFiles.size())
  181. {
  182. TFileData * FileData = &FFiles[i - 1];
  183. // PrevToken is simple check not to ask same editor twice, however
  184. // it does not solve all posibilities
  185. if (!FileData->Closed && (FileData->Token != NULL) &&
  186. (FileData->Token != PrevToken))
  187. {
  188. CloseCallback(FileData->Token);
  189. }
  190. }
  191. }
  192. bool Result = true;
  193. for (unsigned int i = 0; i < FFiles.size(); i++)
  194. {
  195. TFileData * FileData = &FFiles[i];
  196. if (!FileData->Closed && (FileData->Token != NULL))
  197. {
  198. Result = false;
  199. break;
  200. }
  201. }
  202. return Result;
  203. }
  204. //---------------------------------------------------------------------------
  205. bool __fastcall TEditorManager::CloseExternalFilesWithoutProcess()
  206. {
  207. TGuard Guard(FSection);
  208. for (unsigned int i = FFiles.size(); i > 0; i--)
  209. {
  210. TFileData * FileData = &FFiles[i - 1];
  211. if (!FileData->Closed && FileData->External &&
  212. (FileData->Process == INVALID_HANDLE_VALUE))
  213. {
  214. CloseFile(i - 1, true, true);
  215. }
  216. }
  217. return true;
  218. }
  219. //---------------------------------------------------------------------------
  220. void __fastcall TEditorManager::AddFileInternal(const UnicodeString FileName,
  221. TEditedFileData * AData, TObject * Token)
  222. {
  223. TGuard Guard(FSection);
  224. std::unique_ptr<TEditedFileData> Data(AData);
  225. TFileData FileData;
  226. FileData.FileName = FileName;
  227. FileData.External = false;
  228. FileData.Process = INVALID_HANDLE_VALUE;
  229. FileData.Token = Token;
  230. AddFile(FileData, Data.release());
  231. }
  232. //---------------------------------------------------------------------------
  233. void __fastcall TEditorManager::AddFileExternal(const UnicodeString FileName,
  234. TEditedFileData * AData, HANDLE Process)
  235. {
  236. TGuard Guard(FSection);
  237. std::unique_ptr<TEditedFileData> Data(AData);
  238. TFileData FileData;
  239. FileData.FileName = FileName;
  240. FileData.External = true;
  241. FileData.Process = Process;
  242. FileData.Token = NULL;
  243. UnicodeString FilePath = ExtractFilePath(FileData.FileName);
  244. if (Process != INVALID_HANDLE_VALUE)
  245. {
  246. FProcesses.push_back(Process);
  247. }
  248. AddFile(FileData, Data.release());
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall TEditorManager::Check()
  252. {
  253. TGuard Guard(FSection);
  254. int Index;
  255. for (Index = 0; Index < static_cast<int>(FFiles.size()); Index++)
  256. {
  257. TDateTime NewTimestamp;
  258. if (HasFileChanged(Index, NewTimestamp))
  259. {
  260. TDateTime N = NormalizeTimestamp(Now());
  261. // Let the editor finish writing to the file
  262. // (first to avoid uploading partially saved file, second
  263. // because the timestamp may change more than once during saving).
  264. // WORKAROUND WithinPastMilliSeconds seems buggy that it return true even if NewTimestamp is within future
  265. if ((NewTimestamp <= N) &&
  266. !WithinPastMilliSeconds(N, NewTimestamp, GUIConfiguration->KeepUpToDateChangeDelay))
  267. {
  268. CheckFileChange(Index, false);
  269. }
  270. }
  271. }
  272. if (FProcesses.size() > 0)
  273. {
  274. do
  275. {
  276. Index = WaitFor(FProcesses.size(), &(FProcesses[0]), PROCESS);
  277. if (Index >= 0)
  278. {
  279. try
  280. {
  281. CheckFileChange(Index, false);
  282. }
  283. __finally
  284. {
  285. if (!EarlyClose(Index))
  286. {
  287. // CheckFileChange may fail,
  288. // but we want to close handles anyway
  289. CloseFile(Index, false, true);
  290. }
  291. }
  292. }
  293. }
  294. while ((Index >= 0) && (FProcesses.size() > 0));
  295. }
  296. if (FUploadCompleteEvents.size() > 0)
  297. {
  298. do
  299. {
  300. Index = WaitFor(FUploadCompleteEvents.size(), &(FUploadCompleteEvents[0]),
  301. EVENT);
  302. if (Index >= 0)
  303. {
  304. UploadComplete(Index);
  305. }
  306. }
  307. while ((Index >= 0) && (FUploadCompleteEvents.size() > 0));
  308. }
  309. }
  310. //---------------------------------------------------------------------------
  311. bool __fastcall TEditorManager::EarlyClose(int Index)
  312. {
  313. TFileData * FileData = &FFiles[Index];
  314. bool Result =
  315. (FileData->Process != INVALID_HANDLE_VALUE) &&
  316. (Now() - FileData->Opened <=
  317. TDateTime(0, 0, static_cast<unsigned short>(WinConfiguration->Editor.EarlyClose), 0)) &&
  318. (FOnFileEarlyClosed != NULL);
  319. if (Result)
  320. {
  321. Result = false;
  322. FOnFileEarlyClosed(FileData->Data, Result);
  323. if (Result)
  324. {
  325. // forget the associated process
  326. CloseProcess(Index);
  327. }
  328. }
  329. return Result;
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TEditorManager::FileChanged(TObject * Token)
  333. {
  334. TGuard Guard(FSection);
  335. int Index = FindFile(Token);
  336. DebugAssert(Index >= 0);
  337. DebugAssert(!FFiles[Index].External);
  338. CheckFileChange(Index, true);
  339. }
  340. //---------------------------------------------------------------------------
  341. void __fastcall TEditorManager::FileReload(TObject * Token)
  342. {
  343. TGuard Guard(FSection);
  344. int Index = FindFile(Token);
  345. DebugAssert(Index >= 0);
  346. TFileData * FileData = &FFiles[Index];
  347. DebugAssert(!FileData->External);
  348. TAutoFlag ReloadingFlag(FileData->Reloading);
  349. OnFileReload(FileData->FileName, FileData->Data);
  350. GetFileTimestamp(FileData->FileName, FileData->Timestamp);
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TEditorManager::FileClosed(TObject * Token, bool Forced)
  354. {
  355. TGuard Guard(FSection);
  356. int Index = FindFile(Token);
  357. DebugAssert(Index >= 0);
  358. DebugAssert(!FFiles[Index].External);
  359. CheckFileChange(Index, false);
  360. CloseFile(Index, false, !Forced);
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall TEditorManager::AddFile(TFileData & FileData, TEditedFileData * AData)
  364. {
  365. std::unique_ptr<TEditedFileData> Data(AData);
  366. GetFileTimestamp(FileData.FileName, FileData.Timestamp);
  367. FileData.Closed = false;
  368. FileData.UploadCompleteEvent = INVALID_HANDLE_VALUE;
  369. FileData.Opened = Now();
  370. FileData.Reupload = false;
  371. FileData.Reloading = false;
  372. FileData.Saves = 0;
  373. FileData.Data = Data.get();
  374. FFiles.push_back(FileData);
  375. Data.release(); // ownership passed
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall TEditorManager::UploadComplete(int Index)
  379. {
  380. TFileData * FileData = &FFiles[Index];
  381. DebugAssert(FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE);
  382. CloseHandle(FileData->UploadCompleteEvent);
  383. FUploadCompleteEvents.erase(std::find(FUploadCompleteEvents.begin(),
  384. FUploadCompleteEvents.end(), FileData->UploadCompleteEvent));
  385. FileData->UploadCompleteEvent = INVALID_HANDLE_VALUE;
  386. if (FileData->Closed)
  387. {
  388. CloseFile(Index, false, true);
  389. }
  390. else
  391. {
  392. if (FileData->Reupload)
  393. {
  394. FileData->Reupload = false;
  395. CheckFileChange(Index, true);
  396. }
  397. else if ((FileData->Token != NULL) && (FOnFileUploadComplete != NULL))
  398. {
  399. FOnFileUploadComplete(FileData->Token);
  400. }
  401. }
  402. }
  403. //---------------------------------------------------------------------------
  404. void __fastcall TEditorManager::CloseProcess(int Index)
  405. {
  406. TFileData * FileData = &FFiles[Index];
  407. FProcesses.erase(std::find(FProcesses.begin(), FProcesses.end(), FileData->Process));
  408. DebugCheck(CloseHandle(FileData->Process));
  409. FileData->Process = INVALID_HANDLE_VALUE;
  410. }
  411. //---------------------------------------------------------------------------
  412. void __fastcall TEditorManager::ReleaseFile(int Index)
  413. {
  414. TFileData * FileData = &FFiles[Index];
  415. delete FileData->Data;
  416. FileData->Data = NULL;
  417. }
  418. //---------------------------------------------------------------------------
  419. bool __fastcall TEditorManager::CloseFile(int Index, bool IgnoreErrors, bool Delete)
  420. {
  421. bool Result = false;
  422. TFileData * FileData = &FFiles[Index];
  423. if (FileData->Process != INVALID_HANDLE_VALUE)
  424. {
  425. CloseProcess(Index);
  426. }
  427. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  428. {
  429. FileData->Closed = true;
  430. AppLogFmt(L"Opened/edited file \"%s\" has been closed, but the file is still being uploaded.", (FileData->FileName));
  431. }
  432. else
  433. {
  434. UnicodeString LocalRootDirectory = FileData->Data->LocalRootDirectory;
  435. UnicodeString FileName = FileData->FileName; // Before it's released
  436. ReleaseFile(Index);
  437. FFiles.erase(FFiles.begin() + Index);
  438. Result = true;
  439. if (Delete && !LocalRootDirectory.IsEmpty())
  440. {
  441. if (!RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory)) &&
  442. !IgnoreErrors)
  443. {
  444. throw Exception(FMTLOAD(DELETE_TEMP_EXECUTE_FILE_ERROR, (LocalRootDirectory)));
  445. }
  446. AppLogFmt(L"Deleted opened/edited file [%s] folder \"%s\".", (FileName, LocalRootDirectory));
  447. }
  448. else
  449. {
  450. AppLogFmt(L"Opened/edited file \"%s\" has been closed.", (FileName));
  451. }
  452. }
  453. return Result;
  454. }
  455. //---------------------------------------------------------------------------
  456. TDateTime TEditorManager::NormalizeTimestamp(const TDateTime & Timestamp)
  457. {
  458. return TTimeZone::Local->ToUniversalTime(Timestamp);
  459. }
  460. //---------------------------------------------------------------------------
  461. bool TEditorManager::GetFileTimestamp(const UnicodeString & FileName, TDateTime & Timestamp)
  462. {
  463. TSearchRecSmart ASearchRec;
  464. bool Result = FileSearchRec(FileName, ASearchRec);
  465. if (Result)
  466. {
  467. Timestamp = NormalizeTimestamp(ASearchRec.GetLastWriteTime());
  468. }
  469. return Result;
  470. }
  471. //---------------------------------------------------------------------------
  472. bool __fastcall TEditorManager::HasFileChanged(int Index, TDateTime & NewTimestamp)
  473. {
  474. TFileData * FileData = &FFiles[Index];
  475. bool Result;
  476. if (FileData->Reloading)
  477. {
  478. Result = false;
  479. }
  480. else
  481. {
  482. Result =
  483. GetFileTimestamp(FileData->FileName, NewTimestamp) &&
  484. (FileData->Timestamp != NewTimestamp);
  485. }
  486. return Result;
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TEditorManager::CheckFileChange(int Index, bool Force)
  490. {
  491. TDateTime NewTimestamp;
  492. bool Changed = HasFileChanged(Index, NewTimestamp);
  493. if (Force || Changed)
  494. {
  495. TFileData * FileData = &FFiles[Index];
  496. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  497. {
  498. AppLogFmt(L"Opened/edited file \"%s\" was changed, but it still being uploaded.", (FileData->FileName));
  499. FileData->Reupload = true;
  500. }
  501. else
  502. {
  503. if (Changed)
  504. {
  505. AppLogFmt(L"Opened/edited file \"%s\" was changed.", (FileData->FileName));
  506. }
  507. else
  508. {
  509. AppLogFmt(L"Forcing upload of opened/edited file \"%s\".", (FileData->FileName));
  510. }
  511. bool Upload = true;
  512. if (!Force)
  513. {
  514. HANDLE Handle = CreateFile(ApiPath(FileData->FileName).c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
  515. if (Handle == INVALID_HANDLE_VALUE)
  516. {
  517. int Error = GetLastError();
  518. AppLogFmt(L"Opened/edited file \"%s\" is locked, delaying upload.", (FileData->FileName));
  519. if (Error == ERROR_ACCESS_DENIED)
  520. {
  521. Upload = false;
  522. }
  523. }
  524. else
  525. {
  526. CloseHandle(Handle);
  527. }
  528. }
  529. if (Upload)
  530. {
  531. FileData->UploadCompleteEvent = CreateEvent(NULL, false, false, NULL);
  532. FUploadCompleteEvents.push_back(FileData->UploadCompleteEvent);
  533. TDateTime PrevTimestamp = FileData->Timestamp;
  534. FileData->Timestamp = NewTimestamp;
  535. FileData->Saves++;
  536. if (FileData->Saves == 1)
  537. {
  538. Configuration->Usage->Inc(L"RemoteFilesSaved");
  539. }
  540. Configuration->Usage->Inc(L"RemoteFileSaves");
  541. try
  542. {
  543. DebugAssert(OnFileChange != NULL);
  544. bool Retry = false;
  545. AppLogFmt(L"Uploading opened/edited file \"%s\".", (FileData->FileName));
  546. OnFileChange(FileData->FileName, FileData->Data, FileData->UploadCompleteEvent, Retry);
  547. if (Retry)
  548. {
  549. AppLogFmt(L"Will retry uploading opened/edited file \"%s\".", (FileData->FileName));
  550. UploadComplete(Index);
  551. FileData->Timestamp = PrevTimestamp;
  552. }
  553. }
  554. catch(...)
  555. {
  556. AppLogFmt(L"Failed to upload opened/edited file \"%s\".", (FileData->FileName));
  557. // upload failed (was not even started)
  558. if (FileData->UploadCompleteEvent != INVALID_HANDLE_VALUE)
  559. {
  560. UploadComplete(Index);
  561. }
  562. throw;
  563. }
  564. }
  565. }
  566. }
  567. }
  568. //---------------------------------------------------------------------------
  569. int __fastcall TEditorManager::FindFile(const TObject * Token)
  570. {
  571. int Index = -1;
  572. for (unsigned int i = 0; i < FFiles.size(); i++)
  573. {
  574. if (!FFiles[i].Closed && (FFiles[i].Token == Token))
  575. {
  576. Index = i;
  577. break;
  578. }
  579. }
  580. return Index;
  581. }
  582. //---------------------------------------------------------------------------
  583. int __fastcall TEditorManager::WaitFor(unsigned int Count, const HANDLE * Handles,
  584. TWaitHandle WaitFor)
  585. {
  586. static const unsigned int Offset = MAXIMUM_WAIT_OBJECTS;
  587. int Result = -1;
  588. unsigned int Start = 0;
  589. while ((Result < 0) && (Start < Count))
  590. {
  591. unsigned int C = (Count - Start > Offset ? Offset : Count - Start);
  592. unsigned int WaitResult = WaitForMultipleObjects(C, Handles + Start, false, 0);
  593. if (WaitResult == WAIT_FAILED)
  594. {
  595. throw Exception(LoadStr(WATCH_ERROR_GENERAL));
  596. }
  597. else if (WaitResult != WAIT_TIMEOUT)
  598. {
  599. // WAIT_OBJECT_0 is zero
  600. DebugAssert(WaitResult < WAIT_OBJECT_0 + Count);
  601. HANDLE Handle = Handles[WaitResult - WAIT_OBJECT_0];
  602. for (unsigned int i = 0; i < FFiles.size(); i++)
  603. {
  604. TFileData * Data = &FFiles[i];
  605. HANDLE FHandle;
  606. switch (WaitFor)
  607. {
  608. case PROCESS:
  609. FHandle = Data->Process;
  610. break;
  611. case EVENT:
  612. FHandle = Data->UploadCompleteEvent;
  613. break;
  614. };
  615. if (FHandle == Handle)
  616. {
  617. Result = Start + i;
  618. break;
  619. }
  620. }
  621. DebugAssert(Result >= 0);
  622. }
  623. Start += C;
  624. }
  625. return Result;
  626. }
  627. //---------------------------------------------------------------------------