CopyParam.cpp 37 KB

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