EditorManager.cpp 20 KB

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