CopyParam.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "Exceptions.h"
  6. #include "CopyParam.h"
  7. #include "HierarchicalStorage.h"
  8. #include "TextsCore.h"
  9. #include "Interface.h"
  10. //---------------------------------------------------------------------------
  11. const wchar_t * TransferModeNames[] = { L"binary", L"ascii", L"automatic" };
  12. const int TransferModeNamesCount = LENOF(TransferModeNames);
  13. //---------------------------------------------------------------------------
  14. __fastcall TCopyParamType::TCopyParamType()
  15. {
  16. Default();
  17. }
  18. //---------------------------------------------------------------------------
  19. __fastcall TCopyParamType::TCopyParamType(const TCopyParamType & Source)
  20. {
  21. Assign(&Source);
  22. }
  23. //---------------------------------------------------------------------------
  24. __fastcall TCopyParamType::~TCopyParamType()
  25. {
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TCopyParamType::Default()
  29. {
  30. // when changing defaults, make sure GetInfoStr() can handle it
  31. FileNameCase = ncNoChange;
  32. PreserveReadOnly = false;
  33. PreserveTime = true;
  34. PreserveTimeDirs = false;
  35. Rights.Number = TRights::rfDefault;
  36. PreserveRights = false; // Was True until #106
  37. IgnorePermErrors = false;
  38. AsciiFileMask.Masks = L"*.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; "
  39. "*.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml";
  40. TransferMode = tmBinary;
  41. AddXToDirectories = true;
  42. ResumeSupport = rsSmart;
  43. ResumeThreshold = 100 * 1024; // (100 KB)
  44. InvalidCharsReplacement = TokenReplacement;
  45. LocalInvalidChars = ::LocalInvalidChars;
  46. CalculateSize = true;
  47. FileMask = AnyMask;
  48. IncludeFileMask.Masks = L"";
  49. TransferSkipList = NULL;
  50. TransferResumeFile = L"";
  51. ClearArchive = false;
  52. RemoveCtrlZ = false;
  53. RemoveBOM = false;
  54. CPSLimit = 0;
  55. NewerOnly = false;
  56. EncryptNewFiles = true;
  57. ExcludeHiddenFiles = false;
  58. ExcludeEmptyDirectories = false;
  59. Size = -1;
  60. PartOffset = -1;
  61. PartSize = -1;
  62. OnceDoneOperation = odoIdle;
  63. OnTransferOut = NULL;
  64. OnTransferIn = NULL;
  65. }
  66. //---------------------------------------------------------------------------
  67. UnicodeString __fastcall TCopyParamType::GetInfoStr(
  68. UnicodeString Separator, int Attrs) const
  69. {
  70. UnicodeString Result;
  71. bool SomeAttrIncluded;
  72. UnicodeString ScriptArgs;
  73. UnicodeString AssemblyCode;
  74. DoGetInfoStr(
  75. Separator, Attrs, Result, SomeAttrIncluded,
  76. UnicodeString(), ScriptArgs, TAssemblyLanguage(0), AssemblyCode);
  77. return Result;
  78. }
  79. //---------------------------------------------------------------------------
  80. bool __fastcall TCopyParamType::AnyUsableCopyParam(int Attrs) const
  81. {
  82. UnicodeString Result;
  83. bool SomeAttrIncluded;
  84. UnicodeString ScriptArgs;
  85. UnicodeString AssemblyCode;
  86. DoGetInfoStr(
  87. L";", Attrs, Result, SomeAttrIncluded,
  88. UnicodeString(), ScriptArgs, TAssemblyLanguage(0), AssemblyCode);
  89. return SomeAttrIncluded;
  90. }
  91. //---------------------------------------------------------------------------
  92. UnicodeString __fastcall TCopyParamType::GenerateTransferCommandArgs(int Attrs, const UnicodeString & Link) const
  93. {
  94. UnicodeString Result;
  95. bool SomeAttrIncluded;
  96. UnicodeString ScriptArgs;
  97. UnicodeString AssemblyCode;
  98. DoGetInfoStr(
  99. L";", Attrs, Result, SomeAttrIncluded,
  100. Link, ScriptArgs, TAssemblyLanguage(0), AssemblyCode);
  101. return ScriptArgs;
  102. }
  103. //---------------------------------------------------------------------------
  104. UnicodeString __fastcall TCopyParamType::GenerateAssemblyCode(TAssemblyLanguage Language, int Attrs) const
  105. {
  106. UnicodeString Result;
  107. bool SomeAttrIncluded;
  108. UnicodeString ScriptArgs;
  109. UnicodeString AssemblyCode;
  110. DoGetInfoStr(L";", Attrs, Result, SomeAttrIncluded, UnicodeString(), ScriptArgs, Language, AssemblyCode);
  111. return AssemblyCode;
  112. }
  113. //---------------------------------------------------------------------------
  114. void __fastcall TCopyParamType::DoGetInfoStr(
  115. UnicodeString Separator, int Options,
  116. UnicodeString & Result, bool & SomeAttrIncluded,
  117. const UnicodeString & Link, UnicodeString & ScriptArgs, TAssemblyLanguage Language, UnicodeString & AssemblyCode) const
  118. {
  119. TCopyParamType Defaults;
  120. TCopyParamType ScriptNonDefaults;
  121. TCopyParamType CodeNonDefaults;
  122. bool SomeAttrExcluded = false;
  123. SomeAttrIncluded = false;
  124. #define ADD(STR, EXCEPT) \
  125. FLAGCLEAR(Options, EXCEPT) ? (AddToList(Result, (STR), Separator), SomeAttrIncluded = true, true) : (SomeAttrExcluded = true, false)
  126. bool AsciiFileMaskDiffers = (TransferMode == tmAutomatic) && !(AsciiFileMask == Defaults.AsciiFileMask);
  127. bool TransferModeDiffers = ((TransferMode != Defaults.TransferMode) || AsciiFileMaskDiffers);
  128. if (FLAGCLEAR(Options, cpaIncludeMaskOnly | cpaNoTransferMode))
  129. {
  130. // Adding Transfer type unconditionally
  131. bool FormatMask;
  132. int Ident;
  133. switch (TransferMode)
  134. {
  135. case tmBinary:
  136. FormatMask = false;
  137. Ident = 2;
  138. break;
  139. case tmAscii:
  140. FormatMask = false;
  141. Ident = 3;
  142. break;
  143. case tmAutomatic:
  144. default:
  145. FormatMask = !(AsciiFileMask == Defaults.AsciiFileMask);
  146. Ident = FormatMask ? 4 : 5;
  147. break;
  148. }
  149. UnicodeString S = FORMAT(LoadStrPart(COPY_INFO_TRANSFER_TYPE2, 1),
  150. (LoadStrPart(COPY_INFO_TRANSFER_TYPE2, Ident)));
  151. if (FormatMask)
  152. {
  153. S = FORMAT(S, (AsciiFileMask.Masks));
  154. }
  155. AddToList(Result, S, Separator);
  156. if (TransferModeDiffers)
  157. {
  158. ADD("", cpaIncludeMaskOnly | cpaNoTransferMode);
  159. ScriptArgs += RtfSwitchValue(TRANSFER_SWITCH, Link, TransferModeNames[TransferMode]);
  160. const wchar_t * TransferModeMembers[] = { L"Binary", L"Ascii", L"Automatic" };
  161. AssemblyCode += AssemblyProperty(
  162. Language, TransferOptionsClassName, L"TransferMode", L"TransferMode", TransferModeMembers[TransferMode], false);
  163. if (AsciiFileMaskDiffers)
  164. {
  165. ScriptNonDefaults.AsciiFileMask = AsciiFileMask;
  166. CodeNonDefaults.AsciiFileMask = AsciiFileMask;
  167. }
  168. }
  169. }
  170. else
  171. {
  172. if (TransferModeDiffers)
  173. {
  174. SomeAttrExcluded = true;
  175. }
  176. }
  177. if (FileNameCase != Defaults.FileNameCase)
  178. {
  179. if (ADD(FORMAT(LoadStrPart(COPY_INFO_FILENAME, 1),
  180. (LoadStrPart(COPY_INFO_FILENAME, FileNameCase + 2))),
  181. cpaIncludeMaskOnly))
  182. {
  183. ScriptNonDefaults.FileNameCase = FileNameCase;
  184. CodeNonDefaults.FileNameCase = FileNameCase;
  185. }
  186. }
  187. if (InvalidCharsReplacement != Defaults.InvalidCharsReplacement)
  188. {
  189. int Except = cpaIncludeMaskOnly;
  190. if (InvalidCharsReplacement == NoReplacement)
  191. {
  192. ADD(LoadStr(COPY_INFO_DONT_REPLACE_INV_CHARS), Except);
  193. }
  194. if (FLAGCLEAR(Options, Except))
  195. {
  196. ScriptNonDefaults.InvalidCharsReplacement = InvalidCharsReplacement;
  197. CodeNonDefaults.InvalidCharsReplacement = InvalidCharsReplacement;
  198. }
  199. }
  200. if ((PreserveRights != Defaults.PreserveRights) ||
  201. (PreserveRights &&
  202. ((Rights != Defaults.Rights) || (AddXToDirectories != Defaults.AddXToDirectories))))
  203. {
  204. const int Except = cpaIncludeMaskOnly | cpaNoRights;
  205. if (DebugAlwaysTrue(PreserveRights))
  206. {
  207. UnicodeString RightsStr = Rights.Text;
  208. if (AddXToDirectories)
  209. {
  210. RightsStr += L", " + LoadStr(COPY_INFO_ADD_X_TO_DIRS);
  211. }
  212. ADD(FORMAT(LoadStr(COPY_INFO_PERMISSIONS), (RightsStr)),
  213. Except);
  214. if (FLAGCLEAR(Options, Except))
  215. {
  216. ScriptArgs += RtfSwitchValue(PERMISSIONS_SWITCH, Link, Rights.Octal);
  217. const UnicodeString FilePermissionsClassName = L"FilePermissions";
  218. const bool Inline = true;
  219. UnicodeString FilePermissions =
  220. AssemblyNewClassInstanceStart(Language, FilePermissionsClassName, Inline) +
  221. AssemblyProperty(Language, FilePermissionsClassName, L"Octal", Rights.Octal, Inline) +
  222. AssemblyNewClassInstanceEnd(Language, Inline);
  223. AssemblyCode += AssemblyPropertyRaw(Language, TransferOptionsClassName, L"FilePermissions", FilePermissions, false);
  224. }
  225. }
  226. if ((AddXToDirectories != Defaults.AddXToDirectories) && FLAGCLEAR(Options, Except))
  227. {
  228. ScriptNonDefaults.AddXToDirectories = AddXToDirectories;
  229. CodeNonDefaults.AddXToDirectories = AddXToDirectories;
  230. }
  231. }
  232. bool APreserveTimeDirs = PreserveTime && PreserveTimeDirs;
  233. if ((PreserveTime != Defaults.PreserveTime) || (APreserveTimeDirs != Defaults.PreserveTimeDirs))
  234. {
  235. bool AddPreserveTime = false;
  236. UnicodeString Str = LoadStr(PreserveTime ? COPY_INFO_TIMESTAMP : COPY_INFO_DONT_PRESERVE_TIME);
  237. const int ExceptDirs = cpaNoPreserveTimeDirs;
  238. if (APreserveTimeDirs != Defaults.PreserveTimeDirs)
  239. {
  240. if (DebugAlwaysTrue(PreserveTimeDirs))
  241. {
  242. if (FLAGCLEAR(Options, ExceptDirs))
  243. {
  244. Str = FMTLOAD(COPY_INFO_PRESERVE_TIME_DIRS, (Str));
  245. AddPreserveTime = true;
  246. }
  247. }
  248. ADD("", ExceptDirs);
  249. }
  250. const int Except = cpaIncludeMaskOnly | cpaNoPreserveTime;
  251. if (PreserveTime != Defaults.PreserveTime)
  252. {
  253. if (FLAGCLEAR(Options, Except))
  254. {
  255. AddPreserveTime = true;
  256. }
  257. ADD(L"", Except);
  258. }
  259. if (AddPreserveTime)
  260. {
  261. AddToList(Result, Str, Separator);
  262. }
  263. if (FLAGCLEAR(Options, Except))
  264. {
  265. if (PreserveTime)
  266. {
  267. if (PreserveTimeDirs && FLAGCLEAR(Options, ExceptDirs))
  268. {
  269. ScriptArgs += RtfSwitchValue(PRESERVETIME_SWITCH, Link, PRESERVETIMEDIRS_SWITCH_VALUE);
  270. CodeNonDefaults.PreserveTimeDirs = PreserveTimeDirs;
  271. }
  272. else
  273. {
  274. ScriptArgs += RtfSwitch(PRESERVETIME_SWITCH, Link);
  275. }
  276. }
  277. else
  278. {
  279. ScriptArgs += RtfSwitch(NOPRESERVETIME_SWITCH, Link);
  280. AssemblyCode += AssemblyProperty(Language, TransferOptionsClassName, L"PreserveTimestamp", false, false);
  281. }
  282. }
  283. }
  284. if ((PreserveRights || PreserveTime) &&
  285. (IgnorePermErrors != Defaults.IgnorePermErrors))
  286. {
  287. if (DebugAlwaysTrue(IgnorePermErrors))
  288. {
  289. if (ADD(LoadStr(COPY_INFO_IGNORE_PERM_ERRORS), cpaIncludeMaskOnly | cpaNoIgnorePermErrors))
  290. {
  291. ScriptNonDefaults.IgnorePermErrors = IgnorePermErrors;
  292. CodeNonDefaults.IgnorePermErrors = IgnorePermErrors;
  293. }
  294. }
  295. }
  296. if (PreserveReadOnly != Defaults.PreserveReadOnly)
  297. {
  298. if (DebugAlwaysTrue(PreserveReadOnly))
  299. {
  300. if (ADD(LoadStr(COPY_INFO_PRESERVE_READONLY), cpaIncludeMaskOnly | cpaNoPreserveReadOnly))
  301. {
  302. ScriptNonDefaults.PreserveReadOnly = PreserveReadOnly;
  303. CodeNonDefaults.PreserveReadOnly = PreserveReadOnly;
  304. }
  305. }
  306. }
  307. if (CalculateSize != Defaults.CalculateSize)
  308. {
  309. if (DebugAlwaysTrue(!CalculateSize))
  310. {
  311. ADD(LoadStr(COPY_INFO_DONT_CALCULATE_SIZE), cpaIncludeMaskOnly | cpaNoCalculateSize);
  312. // Always false in scripting, in assembly controlled by use of FileTransferProgress
  313. }
  314. }
  315. if (ClearArchive != Defaults.ClearArchive)
  316. {
  317. if (DebugAlwaysTrue(ClearArchive))
  318. {
  319. if (ADD(LoadStr(COPY_INFO_CLEAR_ARCHIVE), cpaIncludeMaskOnly | cpaNoClearArchive))
  320. {
  321. ScriptNonDefaults.ClearArchive = ClearArchive;
  322. CodeNonDefaults.ClearArchive = ClearArchive;
  323. }
  324. }
  325. }
  326. if ((TransferMode == tmAscii) || (TransferMode == tmAutomatic))
  327. {
  328. if (RemoveBOM != Defaults.RemoveBOM)
  329. {
  330. if (DebugAlwaysTrue(RemoveBOM))
  331. {
  332. if (ADD(LoadStr(COPY_INFO_REMOVE_BOM), cpaIncludeMaskOnly | cpaNoRemoveBOM | cpaNoTransferMode))
  333. {
  334. ScriptNonDefaults.RemoveBOM = RemoveBOM;
  335. CodeNonDefaults.RemoveBOM = RemoveBOM;
  336. }
  337. }
  338. }
  339. if (RemoveCtrlZ != Defaults.RemoveCtrlZ)
  340. {
  341. if (DebugAlwaysTrue(RemoveCtrlZ))
  342. {
  343. if (ADD(LoadStr(COPY_INFO_REMOVE_CTRLZ), cpaIncludeMaskOnly | cpaNoRemoveCtrlZ | cpaNoTransferMode))
  344. {
  345. ScriptNonDefaults.RemoveCtrlZ = RemoveCtrlZ;
  346. CodeNonDefaults.RemoveCtrlZ = RemoveCtrlZ;
  347. }
  348. }
  349. }
  350. }
  351. if (!(IncludeFileMask == Defaults.IncludeFileMask))
  352. {
  353. if (ADD(FORMAT(LoadStr(COPY_INFO_FILE_MASK), (IncludeFileMask.Masks)), cpaNoIncludeMask))
  354. {
  355. ScriptArgs += RtfSwitch(FILEMASK_SWITCH, Link, IncludeFileMask.Masks);
  356. AssemblyCode += AssemblyProperty(Language, TransferOptionsClassName, L"FileMask", IncludeFileMask.Masks, false);
  357. }
  358. }
  359. DebugAssert(FTransferSkipList.get() == NULL);
  360. DebugAssert(FTransferResumeFile.IsEmpty());
  361. if (CPSLimit > 0)
  362. {
  363. int LimitKB = int(CPSLimit / 1024);
  364. if (ADD(FMTLOAD(COPY_INFO_CPS_LIMIT2, (LimitKB)), cpaIncludeMaskOnly))
  365. {
  366. ScriptArgs += RtfSwitch(SPEED_SWITCH, Link, LimitKB);
  367. AssemblyCode += AssemblyProperty(Language, TransferOptionsClassName, L"SpeedLimit", LimitKB, false);
  368. }
  369. }
  370. if (NewerOnly != Defaults.NewerOnly)
  371. {
  372. if (DebugAlwaysTrue(NewerOnly))
  373. {
  374. if (ADD(StripHotkey(LoadStr(COPY_PARAM_NEWER_ONLY)), cpaIncludeMaskOnly | cpaNoNewerOnly))
  375. {
  376. ScriptArgs += RtfSwitch(NEWERONLY_SWICH, Link);
  377. CodeNonDefaults.NewerOnly = NewerOnly;
  378. }
  379. }
  380. }
  381. if (EncryptNewFiles != Defaults.EncryptNewFiles)
  382. {
  383. if (!DebugAlwaysFalse(EncryptNewFiles))
  384. {
  385. if (ADD(StripHotkey(LoadStr(COPY_INFO_DONT_ENCRYPT_NEW_FILES)), cpaIncludeMaskOnly | cpaNoEncryptNewFiles))
  386. {
  387. ScriptNonDefaults.EncryptNewFiles = EncryptNewFiles;
  388. CodeNonDefaults.EncryptNewFiles = EncryptNewFiles;
  389. }
  390. }
  391. }
  392. if (ExcludeHiddenFiles != Defaults.ExcludeHiddenFiles)
  393. {
  394. if (DebugAlwaysTrue(ExcludeHiddenFiles))
  395. {
  396. if (ADD(StripHotkey(LoadStr(COPY_INFO_EXCLUDE_HIDDEN_FILES)), cpaNoIncludeMask))
  397. {
  398. ScriptNonDefaults.ExcludeHiddenFiles = ExcludeHiddenFiles;
  399. CodeNonDefaults.ExcludeHiddenFiles = ExcludeHiddenFiles;
  400. }
  401. }
  402. }
  403. if (ExcludeEmptyDirectories != Defaults.ExcludeEmptyDirectories)
  404. {
  405. if (DebugAlwaysTrue(ExcludeEmptyDirectories))
  406. {
  407. if (ADD(StripHotkey(LoadStr(COPY_INFO_EXCLUDE_EMPTY_DIRS)), 0))
  408. {
  409. ScriptNonDefaults.ExcludeEmptyDirectories = ExcludeEmptyDirectories;
  410. CodeNonDefaults.ExcludeEmptyDirectories = ExcludeEmptyDirectories;
  411. }
  412. }
  413. }
  414. bool ResumeThresholdDiffers = ((ResumeSupport == rsSmart) && (ResumeThreshold != Defaults.ResumeThreshold));
  415. if (((ResumeSupport != Defaults.ResumeSupport) || ResumeThresholdDiffers) &&
  416. (TransferMode != tmAscii) && FLAGCLEAR(Options, cpaNoResumeSupport))
  417. {
  418. UnicodeString Value;
  419. UnicodeString CodeState;
  420. int ResumeThresholdKB = (ResumeThreshold / 1024);
  421. switch (ResumeSupport)
  422. {
  423. case rsOff:
  424. Value = ToggleNames[ToggleOff];
  425. CodeState = L"Off";
  426. break;
  427. case rsOn:
  428. Value = ToggleNames[ToggleOn];
  429. CodeState = L"On";
  430. break;
  431. case rsSmart:
  432. Value = IntToStr(ResumeThresholdKB);
  433. break;
  434. }
  435. ScriptArgs += RtfSwitchValue(RESUMESUPPORT_SWITCH, Link, Value);
  436. const UnicodeString ResumeSupportClassName = L"TransferResumeSupport";
  437. const bool Inline = true;
  438. UnicodeString ResumeSupportCode =
  439. AssemblyNewClassInstanceStart(Language, ResumeSupportClassName, Inline);
  440. if (ResumeSupport == rsSmart)
  441. {
  442. ResumeSupportCode += AssemblyProperty(Language, ResumeSupportClassName, L"Threshold", ResumeThresholdKB, Inline);
  443. }
  444. else
  445. {
  446. ResumeSupportCode += AssemblyProperty(Language, ResumeSupportClassName, L"State", L"TransferResumeSupportState", CodeState, Inline);
  447. }
  448. ResumeSupportCode += AssemblyNewClassInstanceEnd(Language, Inline);
  449. AssemblyCode += AssemblyPropertyRaw(Language, TransferOptionsClassName, L"ResumeSupport", ResumeSupportCode, false);
  450. }
  451. std::unique_ptr<TStringList> RawOptions;
  452. std::unique_ptr<TOptionsStorage> OptionsStorage;
  453. RawOptions.reset(new TStringList());
  454. OptionsStorage.reset(new TOptionsStorage(RawOptions.get(), true));
  455. ScriptNonDefaults.Save(OptionsStorage.get(), &Defaults);
  456. if (RawOptions->Count > 0)
  457. {
  458. ScriptArgs +=
  459. RtfSwitch(RAWTRANSFERSETTINGS_SWITCH, Link) +
  460. FORMAT(L"[%d]", (RawOptions->Count)) +
  461. StringsToParams(RawOptions.get());
  462. }
  463. RawOptions.reset(new TStringList());
  464. OptionsStorage.reset(new TOptionsStorage(RawOptions.get(), true));
  465. CodeNonDefaults.Save(OptionsStorage.get(), &Defaults);
  466. if (!AssemblyCode.IsEmpty())
  467. {
  468. AssemblyCode =
  469. AssemblyNewClassInstanceStart(Language, TransferOptionsClassName, false) +
  470. AssemblyCode +
  471. AssemblyNewClassInstanceEnd(Language, false);
  472. }
  473. if (RawOptions->Count > 0)
  474. {
  475. if (AssemblyCode.IsEmpty())
  476. {
  477. AssemblyCode = AssemblyVariableDeclaration(Language) + AssemblyNewClassInstance(Language, TransferOptionsClassName, false);
  478. if (Language == alCSharp)
  479. {
  480. AssemblyCode += RtfText(L"()");
  481. }
  482. AssemblyCode += AssemblyStatementSeparator(Language) + RtfPara;
  483. }
  484. else
  485. {
  486. AssemblyCode += RtfPara;
  487. }
  488. AssemblyCode +=
  489. AssemblyAddRawSettings(Language, RawOptions.get(), TransferOptionsClassName, L"AddRawSettings");
  490. }
  491. if (SomeAttrExcluded)
  492. {
  493. Result += (Result.IsEmpty() ? UnicodeString() : Separator) +
  494. FORMAT(LoadStrPart(COPY_INFO_NOT_USABLE, 1),
  495. (LoadStrPart(COPY_INFO_NOT_USABLE, (SomeAttrIncluded ? 2 : 3))));
  496. }
  497. else if (Result.IsEmpty())
  498. {
  499. Result = LoadStr(COPY_INFO_DEFAULT);
  500. }
  501. #undef ADD
  502. }
  503. //---------------------------------------------------------------------------
  504. void __fastcall TCopyParamType::Assign(const TCopyParamType * Source)
  505. {
  506. DebugAssert(Source != NULL);
  507. #define COPY(Prop) Prop = Source->Prop
  508. COPY(FileNameCase);
  509. COPY(PreserveReadOnly);
  510. COPY(PreserveTime);
  511. COPY(PreserveTimeDirs);
  512. COPY(Rights);
  513. COPY(AsciiFileMask);
  514. COPY(TransferMode);
  515. COPY(AddXToDirectories);
  516. COPY(PreserveRights);
  517. COPY(IgnorePermErrors);
  518. COPY(ResumeSupport);
  519. COPY(ResumeThreshold);
  520. COPY(InvalidCharsReplacement);
  521. COPY(LocalInvalidChars);
  522. COPY(CalculateSize);
  523. COPY(FileMask);
  524. COPY(IncludeFileMask);
  525. COPY(TransferSkipList);
  526. COPY(TransferResumeFile);
  527. COPY(ClearArchive);
  528. COPY(RemoveCtrlZ);
  529. COPY(RemoveBOM);
  530. COPY(CPSLimit);
  531. COPY(NewerOnly);
  532. COPY(EncryptNewFiles);
  533. COPY(ExcludeHiddenFiles);
  534. COPY(ExcludeEmptyDirectories);
  535. COPY(Size);
  536. COPY(PartOffset);
  537. COPY(PartSize);
  538. COPY(OnceDoneOperation);
  539. COPY(OnTransferOut);
  540. COPY(OnTransferIn);
  541. #undef COPY
  542. }
  543. //---------------------------------------------------------------------------
  544. TCopyParamType & __fastcall TCopyParamType::operator =(const TCopyParamType & rhp)
  545. {
  546. Assign(&rhp);
  547. return *this;
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TCopyParamType::SetLocalInvalidChars(UnicodeString value)
  551. {
  552. if (value != LocalInvalidChars)
  553. {
  554. FLocalInvalidChars = value;
  555. FTokenizibleChars = FLocalInvalidChars + TokenPrefix;
  556. }
  557. }
  558. //---------------------------------------------------------------------------
  559. bool __fastcall TCopyParamType::GetReplaceInvalidChars() const
  560. {
  561. return (InvalidCharsReplacement != NoReplacement);
  562. }
  563. //---------------------------------------------------------------------------
  564. void __fastcall TCopyParamType::SetReplaceInvalidChars(bool value)
  565. {
  566. if (ReplaceInvalidChars != value)
  567. {
  568. InvalidCharsReplacement = (value ? TokenReplacement : NoReplacement);
  569. }
  570. }
  571. //---------------------------------------------------------------------------
  572. UnicodeString __fastcall TCopyParamType::ValidLocalFileName(UnicodeString FileName) const
  573. {
  574. return ::ValidLocalFileName(FileName, InvalidCharsReplacement, FTokenizibleChars, LocalInvalidChars);
  575. }
  576. //---------------------------------------------------------------------------
  577. UnicodeString __fastcall TCopyParamType::RestoreChars(UnicodeString FileName) const
  578. {
  579. if (InvalidCharsReplacement == TokenReplacement)
  580. {
  581. wchar_t * InvalidChar = FileName.c_str();
  582. while ((InvalidChar = wcschr(InvalidChar, TokenPrefix)) != NULL)
  583. {
  584. int Index = InvalidChar - FileName.c_str() + 1;
  585. if (FileName.Length() >= Index + 2)
  586. {
  587. UnicodeString Hex = FileName.SubString(Index + 1, 2);
  588. wchar_t Char = static_cast<wchar_t>(HexToByte(Hex));
  589. if ((Char != L'\0') &&
  590. ((FTokenizibleChars.Pos(Char) > 0) ||
  591. (((Char == L' ') || (Char == L'.')) && (Index == FileName.Length() - 2))))
  592. {
  593. FileName[Index] = Char;
  594. FileName.Delete(Index + 1, 2);
  595. InvalidChar = FileName.c_str() + Index;
  596. }
  597. else if ((Hex == L"00") &&
  598. ((Index == FileName.Length() - 2) || (FileName[Index + 3] == L'.')) &&
  599. IsReservedName(FileName.SubString(1, Index - 1) + FileName.SubString(Index + 3, FileName.Length() - Index - 3 + 1)))
  600. {
  601. FileName.Delete(Index, 3);
  602. InvalidChar = FileName.c_str() + Index - 1;
  603. }
  604. else
  605. {
  606. InvalidChar++;
  607. }
  608. }
  609. else
  610. {
  611. InvalidChar++;
  612. }
  613. }
  614. }
  615. return FileName;
  616. }
  617. //---------------------------------------------------------------------------
  618. UnicodeString __fastcall TCopyParamType::ValidLocalPath(UnicodeString Path) const
  619. {
  620. UnicodeString Result;
  621. while (!Path.IsEmpty())
  622. {
  623. if (!Result.IsEmpty())
  624. {
  625. Result += L"\\";
  626. }
  627. Result += ValidLocalFileName(CutToChar(Path, L'\\', false));
  628. }
  629. return Result;
  630. }
  631. //---------------------------------------------------------------------------
  632. UnicodeString __fastcall TCopyParamType::ChangeFileName(UnicodeString FileName,
  633. TOperationSide Side, bool FirstLevel) const
  634. {
  635. if (FirstLevel)
  636. {
  637. FileName = MaskFileName(FileName, FileMask);
  638. }
  639. switch (FileNameCase) {
  640. case ncUpperCase: FileName = FileName.UpperCase(); break;
  641. case ncLowerCase: FileName = FileName.LowerCase(); break;
  642. case ncFirstUpperCase: FileName = FileName.SubString(1, 1).UpperCase() +
  643. FileName.SubString(2, FileName.Length()-1).LowerCase(); break;
  644. case ncLowerCaseShort:
  645. if ((FileName.Length() <= 12) && (FileName.Pos(L".") <= 9) &&
  646. (FileName == FileName.UpperCase()))
  647. {
  648. FileName = FileName.LowerCase();
  649. }
  650. break;
  651. case ncNoChange:
  652. default:
  653. /*nothing*/
  654. break;
  655. }
  656. if (Side == osRemote)
  657. {
  658. FileName = ValidLocalFileName(FileName);
  659. }
  660. else
  661. {
  662. FileName = RestoreChars(FileName);
  663. }
  664. return FileName;
  665. }
  666. //---------------------------------------------------------------------------
  667. bool __fastcall TCopyParamType::UseAsciiTransfer(UnicodeString FileName,
  668. TOperationSide Side, const TFileMasks::TParams & Params) const
  669. {
  670. switch (TransferMode)
  671. {
  672. case tmBinary: return false;
  673. case tmAscii: return true;
  674. case tmAutomatic: return AsciiFileMask.Matches(FileName, (Side == osLocal),
  675. false, &Params);
  676. default: DebugFail(); return false;
  677. }
  678. }
  679. //---------------------------------------------------------------------------
  680. TRights __fastcall TCopyParamType::RemoteFileRights(Integer Attrs) const
  681. {
  682. TRights R = Rights;
  683. if ((Attrs & faDirectory) && AddXToDirectories)
  684. R.AddExecute();
  685. return R;
  686. }
  687. //---------------------------------------------------------------------------
  688. UnicodeString __fastcall TCopyParamType::GetLogStr() const
  689. {
  690. wchar_t CaseC[] = L"NULFS";
  691. wchar_t ModeC[] = L"BAM";
  692. wchar_t ResumeC[] = L"YSN";
  693. // OpenArray (ARRAYOFCONST) supports only up to 19 arguments, so we had to split it
  694. return
  695. FORMAT(
  696. L" PrTime: %s%s; PrRO: %s; Rght: %s; PrR: %s (%s); FnCs: %s; RIC: %s; "
  697. "Resume: %s (%d); CalcS: %s; Mask: %s\n",
  698. (BooleanToEngStr(PreserveTime),
  699. UnicodeString(PreserveTime && PreserveTimeDirs ? L"+Dirs" : L""),
  700. BooleanToEngStr(PreserveReadOnly),
  701. Rights.Text,
  702. BooleanToEngStr(PreserveRights),
  703. BooleanToEngStr(IgnorePermErrors),
  704. CaseC[FileNameCase],
  705. CharToHex(InvalidCharsReplacement),
  706. ResumeC[ResumeSupport],
  707. (int)ResumeThreshold,
  708. BooleanToEngStr(CalculateSize),
  709. FileMask)) +
  710. FORMAT(
  711. L" TM: %s; ClAr: %s; RemEOF: %s; RemBOM: %s; CPS: %u; NewerOnly: %s; EncryptNewFiles: %s; ExcludeHiddenFiles: %s; ExcludeEmptyDirectories: %s; InclM: %s; ResumeL: %d\n"
  712. " AscM: %s\n",
  713. (ModeC[TransferMode],
  714. BooleanToEngStr(ClearArchive),
  715. BooleanToEngStr(RemoveCtrlZ),
  716. BooleanToEngStr(RemoveBOM),
  717. int(CPSLimit),
  718. BooleanToEngStr(NewerOnly),
  719. BooleanToEngStr(EncryptNewFiles),
  720. BooleanToEngStr(ExcludeHiddenFiles),
  721. BooleanToEngStr(ExcludeEmptyDirectories),
  722. IncludeFileMask.Masks,
  723. ((FTransferSkipList.get() != NULL) ? FTransferSkipList->Count : 0) + (!FTransferResumeFile.IsEmpty() ? 1 : 0),
  724. AsciiFileMask.Masks));
  725. }
  726. //---------------------------------------------------------------------------
  727. int __fastcall TCopyParamType::LocalFileAttrs(const TRights & Rights) const
  728. {
  729. int Result = 0;
  730. if (PreserveReadOnly && !Rights.Right[TRights::rrUserWrite])
  731. {
  732. Result |= faReadOnly;
  733. }
  734. return Result;
  735. }
  736. //---------------------------------------------------------------------------
  737. bool __fastcall TCopyParamType::AllowResume(__int64 Size, const UnicodeString & FileName) const
  738. {
  739. bool Result;
  740. if (FileName.Length() + PartialExt.Length() > 255) // it's a different limit than MAX_PATH
  741. {
  742. Result = false;
  743. }
  744. else
  745. {
  746. switch (ResumeSupport)
  747. {
  748. case rsOn: Result = true; break;
  749. case rsOff: Result = false; break;
  750. case rsSmart: Result = (Size >= ResumeThreshold); break;
  751. default: DebugFail(); Result = false; break;
  752. }
  753. }
  754. return Result;
  755. }
  756. //---------------------------------------------------------------------------
  757. bool __fastcall TCopyParamType::AllowAnyTransfer() const
  758. {
  759. return
  760. IncludeFileMask.Masks.IsEmpty() &&
  761. !ExcludeHiddenFiles &&
  762. !ExcludeEmptyDirectories &&
  763. ((FTransferSkipList.get() == NULL) || (FTransferSkipList->Count == 0)) &&
  764. FTransferResumeFile.IsEmpty();
  765. }
  766. //---------------------------------------------------------------------------
  767. bool __fastcall TCopyParamType::AllowTransfer(UnicodeString FileName,
  768. TOperationSide Side, bool Directory, const TFileMasks::TParams & Params, bool Hidden) const
  769. {
  770. bool Result = true;
  771. if (Hidden && ExcludeHiddenFiles)
  772. {
  773. Result = false;
  774. }
  775. else if (!IncludeFileMask.Masks.IsEmpty())
  776. {
  777. Result = IncludeFileMask.Matches(FileName, (Side == osLocal),
  778. Directory, &Params);
  779. }
  780. return Result;
  781. }
  782. //---------------------------------------------------------------------------
  783. bool __fastcall TCopyParamType::SkipTransfer(
  784. UnicodeString FileName, bool Directory) const
  785. {
  786. bool Result = false;
  787. // we deliberately do not filter directories, as path is added to resume list
  788. // when a transfer of file or directory is started,
  789. // so for directories we need to recurse and check every single file
  790. if (!Directory && (FTransferSkipList.get() != NULL))
  791. {
  792. Result = (FTransferSkipList->IndexOf(FileName) >= 0);
  793. }
  794. return Result;
  795. }
  796. //---------------------------------------------------------------------------
  797. bool __fastcall TCopyParamType::ResumeTransfer(UnicodeString FileName) const
  798. {
  799. // Returning true has the same effect as cpResume
  800. return
  801. (FileName == FTransferResumeFile) &&
  802. DebugAlwaysTrue(!FTransferResumeFile.IsEmpty());
  803. }
  804. //---------------------------------------------------------------------------
  805. TStrings * __fastcall TCopyParamType::GetTransferSkipList() const
  806. {
  807. return FTransferSkipList.get();
  808. }
  809. //---------------------------------------------------------------------------
  810. void __fastcall TCopyParamType::SetTransferSkipList(TStrings * value)
  811. {
  812. if ((value == NULL) || (value->Count == 0))
  813. {
  814. FTransferSkipList.reset(NULL);
  815. }
  816. else
  817. {
  818. FTransferSkipList.reset(new TStringList());
  819. FTransferSkipList->AddStrings(value);
  820. FTransferSkipList->Sorted = true;
  821. }
  822. }
  823. //---------------------------------------------------------------------------
  824. void __fastcall TCopyParamType::Load(THierarchicalStorage * Storage)
  825. {
  826. AddXToDirectories = Storage->ReadBool(L"AddXToDirectories", AddXToDirectories);
  827. AsciiFileMask.Masks = Storage->ReadString(L"Masks", AsciiFileMask.Masks);
  828. FileNameCase = (TFileNameCase)Storage->ReadInteger(L"FileNameCase", FileNameCase);
  829. PreserveReadOnly = Storage->ReadBool(L"PreserveReadOnly", PreserveReadOnly);
  830. PreserveTime = Storage->ReadBool(L"PreserveTime", PreserveTime);
  831. PreserveTimeDirs = Storage->ReadBool(L"PreserveTimeDirs", PreserveTimeDirs);
  832. PreserveRights = Storage->ReadBool(L"PreserveRights", PreserveRights);
  833. IgnorePermErrors = Storage->ReadBool(L"IgnorePermErrors", IgnorePermErrors);
  834. Rights.Text = Storage->ReadString(L"Text", Rights.Text);
  835. TransferMode = (TTransferMode)Storage->ReadInteger(L"TransferMode", TransferMode);
  836. ResumeSupport = (TResumeSupport)Storage->ReadInteger(L"ResumeSupport", ResumeSupport);
  837. ResumeThreshold = Storage->ReadInt64(L"ResumeThreshold", ResumeThreshold);
  838. InvalidCharsReplacement = (wchar_t)Storage->ReadInteger(L"ReplaceInvalidChars",
  839. (unsigned int)InvalidCharsReplacement);
  840. LocalInvalidChars = Storage->ReadString(L"LocalInvalidChars", LocalInvalidChars);
  841. CalculateSize = Storage->ReadBool(L"CalculateSize", CalculateSize);
  842. if (Storage->ValueExists(L"IncludeFileMask"))
  843. {
  844. IncludeFileMask.Masks = Storage->ReadString(L"IncludeFileMask", IncludeFileMask.Masks);
  845. }
  846. else if (Storage->ValueExists(L"ExcludeFileMask"))
  847. {
  848. UnicodeString ExcludeFileMask = Storage->ReadString(L"ExcludeFileMask", L"");
  849. if (!ExcludeFileMask.IsEmpty())
  850. {
  851. bool NegativeExclude = Storage->ReadBool(L"NegativeExclude", false);
  852. if (NegativeExclude)
  853. {
  854. IncludeFileMask.Masks = ExcludeFileMask;
  855. }
  856. // convert at least simple cases to new format
  857. else if (ExcludeFileMask.Pos(IncludeExcludeFileMasksDelimiter) == 0)
  858. {
  859. IncludeFileMask.Masks = UnicodeString(IncludeExcludeFileMasksDelimiter) + ExcludeFileMask;
  860. }
  861. }
  862. }
  863. TransferSkipList = NULL;
  864. TransferResumeFile = L"";
  865. ClearArchive = Storage->ReadBool(L"ClearArchive", ClearArchive);
  866. RemoveCtrlZ = Storage->ReadBool(L"RemoveCtrlZ", RemoveCtrlZ);
  867. RemoveBOM = Storage->ReadBool(L"RemoveBOM", RemoveBOM);
  868. CPSLimit = Storage->ReadInteger(L"CPSLimit", CPSLimit);
  869. NewerOnly = Storage->ReadBool(L"NewerOnly", NewerOnly);
  870. EncryptNewFiles = Storage->ReadBool(L"EncryptNewFiles", EncryptNewFiles);
  871. ExcludeHiddenFiles = Storage->ReadBool(L"ExcludeHiddenFiles", ExcludeHiddenFiles);
  872. ExcludeEmptyDirectories = Storage->ReadBool(L"ExcludeEmptyDirectories", ExcludeEmptyDirectories);
  873. Size = -1;
  874. PartOffset = -1;
  875. PartSize = -1;
  876. OnceDoneOperation = odoIdle;
  877. OnTransferOut = NULL;
  878. OnTransferIn = NULL;
  879. }
  880. //---------------------------------------------------------------------------
  881. void __fastcall TCopyParamType::Save(THierarchicalStorage * Storage, const TCopyParamType * Defaults) const
  882. {
  883. // Same as in TSessionData::DoSave
  884. #define WRITE_DATA_EX(TYPE, NAME, PROPERTY, CONV) \
  885. if ((Defaults != NULL) && (CONV(Defaults->PROPERTY) == CONV(PROPERTY))) \
  886. { \
  887. Storage->DeleteValue(NAME); \
  888. } \
  889. else \
  890. { \
  891. Storage->Write ## TYPE(NAME, CONV(PROPERTY)); \
  892. }
  893. #define WRITE_DATA_CONV(TYPE, NAME, PROPERTY) WRITE_DATA_EX(TYPE, NAME, PROPERTY, WRITE_DATA_CONV_FUNC)
  894. #define WRITE_DATA(TYPE, PROPERTY) WRITE_DATA_EX(TYPE, TEXT(#PROPERTY), PROPERTY, )
  895. WRITE_DATA(Bool, AddXToDirectories);
  896. WRITE_DATA_EX(String, L"Masks", AsciiFileMask.Masks, );
  897. WRITE_DATA(Integer, FileNameCase);
  898. WRITE_DATA(Bool, PreserveReadOnly);
  899. WRITE_DATA(Bool, PreserveTime);
  900. WRITE_DATA(Bool, PreserveTimeDirs);
  901. WRITE_DATA(Bool, PreserveRights);
  902. WRITE_DATA(Bool, IgnorePermErrors);
  903. WRITE_DATA_EX(String, L"Text", Rights.Text, );
  904. WRITE_DATA(Integer, TransferMode);
  905. WRITE_DATA(Integer, ResumeSupport);
  906. WRITE_DATA(Int64, ResumeThreshold);
  907. #define WRITE_DATA_CONV_FUNC(X) (unsigned int)(X)
  908. WRITE_DATA_CONV(Integer, L"ReplaceInvalidChars", InvalidCharsReplacement);
  909. WRITE_DATA(String, LocalInvalidChars);
  910. WRITE_DATA(Bool, CalculateSize);
  911. WRITE_DATA_EX(String, L"IncludeFileMask", IncludeFileMask.Masks, );
  912. Storage->DeleteValue(L"ExcludeFileMask"); // obsolete
  913. Storage->DeleteValue(L"NegativeExclude"); // obsolete
  914. DebugAssert(FTransferSkipList.get() == NULL);
  915. DebugAssert(FTransferResumeFile.IsEmpty());
  916. WRITE_DATA(Bool, ClearArchive);
  917. WRITE_DATA(Bool, RemoveCtrlZ);
  918. WRITE_DATA(Bool, RemoveBOM);
  919. WRITE_DATA(Integer, CPSLimit);
  920. WRITE_DATA(Bool, NewerOnly);
  921. WRITE_DATA(Bool, EncryptNewFiles);
  922. WRITE_DATA(Bool, ExcludeHiddenFiles);
  923. WRITE_DATA(Bool, ExcludeEmptyDirectories);
  924. DebugAssert(Size < 0);
  925. DebugAssert(PartOffset < 0);
  926. DebugAssert(PartSize < 0);
  927. DebugAssert(OnceDoneOperation == odoIdle);
  928. DebugAssert(OnTransferOut == NULL);
  929. DebugAssert(OnTransferIn == NULL);
  930. }
  931. //---------------------------------------------------------------------------
  932. #define C(Property) (Property == rhp.Property)
  933. bool __fastcall TCopyParamType::operator==(const TCopyParamType & rhp) const
  934. {
  935. DebugAssert(FTransferSkipList.get() == NULL);
  936. DebugAssert(FTransferResumeFile.IsEmpty());
  937. DebugAssert(OnTransferOut == NULL);
  938. DebugAssert(OnTransferIn == NULL);
  939. DebugAssert(rhp.FTransferSkipList.get() == NULL);
  940. DebugAssert(rhp.FTransferResumeFile.IsEmpty());
  941. DebugAssert(rhp.OnTransferOut == NULL);
  942. DebugAssert(rhp.OnTransferIn == NULL);
  943. return
  944. C(AddXToDirectories) &&
  945. C(AsciiFileMask) &&
  946. C(FileNameCase) &&
  947. C(PreserveReadOnly) &&
  948. C(PreserveTime) &&
  949. C(PreserveTimeDirs) &&
  950. C(PreserveRights) &&
  951. C(IgnorePermErrors) &&
  952. C(Rights) &&
  953. C(TransferMode) &&
  954. C(ResumeSupport) &&
  955. C(ResumeThreshold) &&
  956. C(InvalidCharsReplacement) &&
  957. C(LocalInvalidChars) &&
  958. C(CalculateSize) &&
  959. C(IncludeFileMask) &&
  960. C(ClearArchive) &&
  961. C(RemoveCtrlZ) &&
  962. C(RemoveBOM) &&
  963. C(CPSLimit) &&
  964. C(NewerOnly) &&
  965. C(EncryptNewFiles) &&
  966. C(ExcludeHiddenFiles) &&
  967. C(ExcludeEmptyDirectories) &&
  968. C(Size) &&
  969. C(PartOffset) &&
  970. C(PartSize) &&
  971. C(OnceDoneOperation) &&
  972. true;
  973. }
  974. #undef C
  975. //---------------------------------------------------------------------------
  976. const unsigned long MinSpeed = 8 * 1024;
  977. const unsigned long MaxSpeed = 8 * 1024 * 1024;
  978. //---------------------------------------------------------------------------
  979. static bool __fastcall TryGetSpeedLimit(const UnicodeString & Text, unsigned long & Speed)
  980. {
  981. bool Result;
  982. if (AnsiSameText(Text, LoadStr(SPEED_UNLIMITED)))
  983. {
  984. Speed = 0;
  985. Result = true;
  986. }
  987. else
  988. {
  989. int SSpeed;
  990. Result = TryStrToInt(Text, SSpeed) && (SSpeed >= 0);
  991. if (Result)
  992. {
  993. Speed = SSpeed * 1024;
  994. }
  995. }
  996. return Result;
  997. }
  998. //---------------------------------------------------------------------------
  999. unsigned long __fastcall GetSpeedLimit(const UnicodeString & Text)
  1000. {
  1001. unsigned long Speed;
  1002. if (!TryGetSpeedLimit(Text, Speed))
  1003. {
  1004. throw Exception(FMTLOAD(SPEED_INVALID, (Text)));
  1005. }
  1006. return Speed;
  1007. }
  1008. //---------------------------------------------------------------------------
  1009. UnicodeString __fastcall SetSpeedLimit(unsigned long Limit)
  1010. {
  1011. UnicodeString Text;
  1012. if (Limit == 0)
  1013. {
  1014. Text = LoadStr(SPEED_UNLIMITED);
  1015. }
  1016. else
  1017. {
  1018. Text = IntToStr(int(Limit / 1024));
  1019. }
  1020. return Text;
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. void __fastcall CopySpeedLimits(TStrings * Source, TStrings * Dest)
  1024. {
  1025. std::unique_ptr<TStringList> Temp(new TStringList());
  1026. bool Unlimited = false;
  1027. for (int Index = 0; Index < Source->Count; Index++)
  1028. {
  1029. UnicodeString Text = Source->Strings[Index];
  1030. unsigned long Speed;
  1031. bool Valid = TryGetSpeedLimit(Text, Speed);
  1032. if ((!Valid || (Speed == 0)) && !Unlimited)
  1033. {
  1034. Temp->Add(LoadStr(SPEED_UNLIMITED));
  1035. Unlimited = true;
  1036. }
  1037. else if (Valid && (Speed > 0))
  1038. {
  1039. Temp->Add(Text);
  1040. }
  1041. }
  1042. if (!Unlimited)
  1043. {
  1044. Temp->Insert(0, LoadStr(SPEED_UNLIMITED));
  1045. }
  1046. Dest->Assign(Temp.get());
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. TOperationSide ReverseOperationSide(TOperationSide Side)
  1050. {
  1051. TOperationSide Result;
  1052. switch (Side)
  1053. {
  1054. case osLocal:
  1055. Result = osRemote;
  1056. break;
  1057. case osRemote:
  1058. Result = osLocal;
  1059. break;
  1060. default:
  1061. case osCurrent:
  1062. DebugFail();
  1063. Result = Side;
  1064. break;
  1065. }
  1066. return Result;
  1067. }