CopyParam.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 = static_cast<int>(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') && (Char != L'/') &&
  590. ((FTokenizibleChars.Pos(Char) > 0) ||
  591. // not decoding lone dot
  592. (((Char == L' ') || ((Char == L'.') && (Index > 1))) && (Index == FileName.Length() - 2))))
  593. {
  594. FileName[Index] = Char;
  595. FileName.Delete(Index + 1, 2);
  596. InvalidChar = FileName.c_str() + Index;
  597. }
  598. else if ((Hex == L"00") &&
  599. ((Index == FileName.Length() - 2) || (FileName[Index + 3] == L'.')) &&
  600. IsReservedName(FileName.SubString(1, Index - 1) + FileName.SubString(Index + 3, FileName.Length() - Index - 3 + 1)))
  601. {
  602. FileName.Delete(Index, 3);
  603. InvalidChar = FileName.c_str() + Index - 1;
  604. }
  605. else
  606. {
  607. InvalidChar++;
  608. }
  609. }
  610. else
  611. {
  612. InvalidChar++;
  613. }
  614. }
  615. }
  616. return FileName;
  617. }
  618. //---------------------------------------------------------------------------
  619. UnicodeString __fastcall TCopyParamType::ValidLocalPath(UnicodeString Path) const
  620. {
  621. UnicodeString Result;
  622. while (!Path.IsEmpty())
  623. {
  624. if (!Result.IsEmpty())
  625. {
  626. Result += L"\\";
  627. }
  628. Result += ValidLocalFileName(CutToChar(Path, L'\\', false));
  629. }
  630. return Result;
  631. }
  632. //---------------------------------------------------------------------------
  633. UnicodeString __fastcall TCopyParamType::ChangeFileName(UnicodeString FileName,
  634. TOperationSide Side, bool FirstLevel) const
  635. {
  636. if (FirstLevel)
  637. {
  638. FileName = MaskFileName(FileName, FileMask);
  639. }
  640. switch (FileNameCase) {
  641. case ncUpperCase: FileName = FileName.UpperCase(); break;
  642. case ncLowerCase: FileName = FileName.LowerCase(); break;
  643. case ncFirstUpperCase: FileName = FileName.SubString(1, 1).UpperCase() +
  644. FileName.SubString(2, FileName.Length()-1).LowerCase(); break;
  645. case ncLowerCaseShort:
  646. if ((FileName.Length() <= 12) && (FileName.Pos(L".") <= 9) &&
  647. (FileName == FileName.UpperCase()))
  648. {
  649. FileName = FileName.LowerCase();
  650. }
  651. break;
  652. case ncNoChange:
  653. default:
  654. /*nothing*/
  655. break;
  656. }
  657. if (Side == osRemote)
  658. {
  659. FileName = ValidLocalFileName(FileName);
  660. }
  661. else
  662. {
  663. FileName = RestoreChars(FileName);
  664. }
  665. return FileName;
  666. }
  667. //---------------------------------------------------------------------------
  668. bool __fastcall TCopyParamType::UseAsciiTransfer(UnicodeString FileName,
  669. TOperationSide Side, const TFileMasks::TParams & Params) const
  670. {
  671. switch (TransferMode)
  672. {
  673. case tmBinary: return false;
  674. case tmAscii: return true;
  675. case tmAutomatic: return AsciiFileMask.Matches(FileName, (Side == osLocal),
  676. false, &Params);
  677. default: DebugFail(); return false;
  678. }
  679. }
  680. //---------------------------------------------------------------------------
  681. TRights __fastcall TCopyParamType::RemoteFileRights(Integer Attrs) const
  682. {
  683. TRights R = Rights;
  684. if ((Attrs & faDirectory) && AddXToDirectories)
  685. R.AddExecute();
  686. return R;
  687. }
  688. //---------------------------------------------------------------------------
  689. UnicodeString __fastcall TCopyParamType::GetLogStr() const
  690. {
  691. wchar_t CaseC[] = L"NULFS";
  692. wchar_t ModeC[] = L"BAM";
  693. wchar_t ResumeC[] = L"YSN";
  694. // OpenArray (ARRAYOFCONST) supports only up to 19 arguments, so we had to split it
  695. return
  696. FORMAT(
  697. L" PrTime: %s%s; PrRO: %s; Rght: %s; PrR: %s (%s); FnCs: %s; RIC: %s; "
  698. "Resume: %s (%d); CalcS: %s; Mask: %s\n",
  699. (BooleanToEngStr(PreserveTime),
  700. UnicodeString(PreserveTime && PreserveTimeDirs ? L"+Dirs" : L""),
  701. BooleanToEngStr(PreserveReadOnly),
  702. Rights.Text,
  703. BooleanToEngStr(PreserveRights),
  704. BooleanToEngStr(IgnorePermErrors),
  705. CaseC[FileNameCase],
  706. CharToHex(InvalidCharsReplacement),
  707. ResumeC[ResumeSupport],
  708. (int)ResumeThreshold,
  709. BooleanToEngStr(CalculateSize),
  710. FileMask)) +
  711. FORMAT(
  712. L" TM: %s; ClAr: %s; RemEOF: %s; RemBOM: %s; CPS: %u; NewerOnly: %s; EncryptNewFiles: %s; ExcludeHiddenFiles: %s; ExcludeEmptyDirectories: %s; InclM: %s; ResumeL: %d\n"
  713. " AscM: %s\n",
  714. (ModeC[TransferMode],
  715. BooleanToEngStr(ClearArchive),
  716. BooleanToEngStr(RemoveCtrlZ),
  717. BooleanToEngStr(RemoveBOM),
  718. int(CPSLimit),
  719. BooleanToEngStr(NewerOnly),
  720. BooleanToEngStr(EncryptNewFiles),
  721. BooleanToEngStr(ExcludeHiddenFiles),
  722. BooleanToEngStr(ExcludeEmptyDirectories),
  723. IncludeFileMask.Masks,
  724. ((FTransferSkipList.get() != NULL) ? FTransferSkipList->Count : 0) + (!FTransferResumeFile.IsEmpty() ? 1 : 0),
  725. AsciiFileMask.Masks));
  726. }
  727. //---------------------------------------------------------------------------
  728. int __fastcall TCopyParamType::LocalFileAttrs(const TRights & Rights) const
  729. {
  730. int Result = 0;
  731. if (PreserveReadOnly && !Rights.Right[TRights::rrUserWrite])
  732. {
  733. Result |= faReadOnly;
  734. }
  735. return Result;
  736. }
  737. //---------------------------------------------------------------------------
  738. bool __fastcall TCopyParamType::AllowResume(__int64 Size, const UnicodeString & FileName) const
  739. {
  740. bool Result;
  741. if (FileName.Length() + PartialExt.Length() > 255) // it's a different limit than MAX_PATH
  742. {
  743. Result = false;
  744. }
  745. else
  746. {
  747. switch (ResumeSupport)
  748. {
  749. case rsOn: Result = true; break;
  750. case rsOff: Result = false; break;
  751. case rsSmart: Result = (Size >= ResumeThreshold); break;
  752. default: DebugFail(); Result = false; break;
  753. }
  754. }
  755. return Result;
  756. }
  757. //---------------------------------------------------------------------------
  758. bool __fastcall TCopyParamType::AllowAnyTransfer() const
  759. {
  760. return
  761. IncludeFileMask.Masks.IsEmpty() &&
  762. !ExcludeHiddenFiles &&
  763. !ExcludeEmptyDirectories &&
  764. ((FTransferSkipList.get() == NULL) || (FTransferSkipList->Count == 0)) &&
  765. FTransferResumeFile.IsEmpty();
  766. }
  767. //---------------------------------------------------------------------------
  768. bool __fastcall TCopyParamType::AllowTransfer(UnicodeString FileName,
  769. TOperationSide Side, bool Directory, const TFileMasks::TParams & Params, bool Hidden) const
  770. {
  771. bool Result = true;
  772. if (Hidden && ExcludeHiddenFiles)
  773. {
  774. Result = false;
  775. }
  776. else if (!IncludeFileMask.Masks.IsEmpty())
  777. {
  778. Result = IncludeFileMask.Matches(FileName, (Side == osLocal),
  779. Directory, &Params);
  780. }
  781. return Result;
  782. }
  783. //---------------------------------------------------------------------------
  784. bool __fastcall TCopyParamType::SkipTransfer(
  785. UnicodeString FileName, bool Directory) const
  786. {
  787. bool Result = false;
  788. // we deliberately do not filter directories, as path is added to resume list
  789. // when a transfer of file or directory is started,
  790. // so for directories we need to recurse and check every single file
  791. if (!Directory && (FTransferSkipList.get() != NULL))
  792. {
  793. Result = (FTransferSkipList->IndexOf(FileName) >= 0);
  794. }
  795. return Result;
  796. }
  797. //---------------------------------------------------------------------------
  798. bool __fastcall TCopyParamType::ResumeTransfer(UnicodeString FileName) const
  799. {
  800. // Returning true has the same effect as cpResume
  801. return
  802. (FileName == FTransferResumeFile) &&
  803. DebugAlwaysTrue(!FTransferResumeFile.IsEmpty());
  804. }
  805. //---------------------------------------------------------------------------
  806. TStrings * __fastcall TCopyParamType::GetTransferSkipList() const
  807. {
  808. return FTransferSkipList.get();
  809. }
  810. //---------------------------------------------------------------------------
  811. void __fastcall TCopyParamType::SetTransferSkipList(TStrings * value)
  812. {
  813. if ((value == NULL) || (value->Count == 0))
  814. {
  815. FTransferSkipList.reset(NULL);
  816. }
  817. else
  818. {
  819. FTransferSkipList.reset(new TStringList());
  820. FTransferSkipList->AddStrings(value);
  821. FTransferSkipList->Sorted = true;
  822. }
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCopyParamType::Load(THierarchicalStorage * Storage)
  826. {
  827. AddXToDirectories = Storage->ReadBool(L"AddXToDirectories", AddXToDirectories);
  828. AsciiFileMask.Masks = Storage->ReadString(L"Masks", AsciiFileMask.Masks);
  829. FileNameCase = (TFileNameCase)Storage->ReadInteger(L"FileNameCase", FileNameCase);
  830. PreserveReadOnly = Storage->ReadBool(L"PreserveReadOnly", PreserveReadOnly);
  831. PreserveTime = Storage->ReadBool(L"PreserveTime", PreserveTime);
  832. PreserveTimeDirs = Storage->ReadBool(L"PreserveTimeDirs", PreserveTimeDirs);
  833. PreserveRights = Storage->ReadBool(L"PreserveRights", PreserveRights);
  834. IgnorePermErrors = Storage->ReadBool(L"IgnorePermErrors", IgnorePermErrors);
  835. Rights.Text = Storage->ReadString(L"Text", Rights.Text);
  836. TransferMode = (TTransferMode)Storage->ReadInteger(L"TransferMode", TransferMode);
  837. ResumeSupport = (TResumeSupport)Storage->ReadInteger(L"ResumeSupport", ResumeSupport);
  838. ResumeThreshold = Storage->ReadInt64(L"ResumeThreshold", ResumeThreshold);
  839. InvalidCharsReplacement = (wchar_t)Storage->ReadInteger(L"ReplaceInvalidChars",
  840. (unsigned int)InvalidCharsReplacement);
  841. LocalInvalidChars = Storage->ReadString(L"LocalInvalidChars", LocalInvalidChars);
  842. CalculateSize = Storage->ReadBool(L"CalculateSize", CalculateSize);
  843. if (Storage->ValueExists(L"IncludeFileMask"))
  844. {
  845. IncludeFileMask.Masks = Storage->ReadString(L"IncludeFileMask", IncludeFileMask.Masks);
  846. }
  847. else if (Storage->ValueExists(L"ExcludeFileMask"))
  848. {
  849. UnicodeString ExcludeFileMask = Storage->ReadString(L"ExcludeFileMask", L"");
  850. if (!ExcludeFileMask.IsEmpty())
  851. {
  852. bool NegativeExclude = Storage->ReadBool(L"NegativeExclude", false);
  853. if (NegativeExclude)
  854. {
  855. IncludeFileMask.Masks = ExcludeFileMask;
  856. }
  857. // convert at least simple cases to new format
  858. else if (ExcludeFileMask.Pos(IncludeExcludeFileMasksDelimiter) == 0)
  859. {
  860. IncludeFileMask.Masks = UnicodeString(IncludeExcludeFileMasksDelimiter) + ExcludeFileMask;
  861. }
  862. }
  863. }
  864. TransferSkipList = NULL;
  865. TransferResumeFile = L"";
  866. ClearArchive = Storage->ReadBool(L"ClearArchive", ClearArchive);
  867. RemoveCtrlZ = Storage->ReadBool(L"RemoveCtrlZ", RemoveCtrlZ);
  868. RemoveBOM = Storage->ReadBool(L"RemoveBOM", RemoveBOM);
  869. CPSLimit = Storage->ReadInteger(L"CPSLimit", CPSLimit);
  870. NewerOnly = Storage->ReadBool(L"NewerOnly", NewerOnly);
  871. EncryptNewFiles = Storage->ReadBool(L"EncryptNewFiles", EncryptNewFiles);
  872. ExcludeHiddenFiles = Storage->ReadBool(L"ExcludeHiddenFiles", ExcludeHiddenFiles);
  873. ExcludeEmptyDirectories = Storage->ReadBool(L"ExcludeEmptyDirectories", ExcludeEmptyDirectories);
  874. Size = -1;
  875. PartOffset = -1;
  876. PartSize = -1;
  877. OnceDoneOperation = odoIdle;
  878. OnTransferOut = NULL;
  879. OnTransferIn = NULL;
  880. }
  881. //---------------------------------------------------------------------------
  882. void __fastcall TCopyParamType::Save(THierarchicalStorage * Storage, const TCopyParamType * Defaults) const
  883. {
  884. // Same as in TSessionData::DoSave
  885. #define WRITE_DATA_EX(TYPE, NAME, PROPERTY, CONV) \
  886. if ((Defaults != NULL) && (CONV(Defaults->PROPERTY) == CONV(PROPERTY))) \
  887. { \
  888. Storage->DeleteValue(NAME); \
  889. } \
  890. else \
  891. { \
  892. Storage->Write ## TYPE(NAME, CONV(PROPERTY)); \
  893. }
  894. #define WRITE_DATA_CONV(TYPE, NAME, PROPERTY) WRITE_DATA_EX(TYPE, NAME, PROPERTY, WRITE_DATA_CONV_FUNC)
  895. #define WRITE_DATA(TYPE, PROPERTY) WRITE_DATA_EX(TYPE, TEXT(#PROPERTY), PROPERTY, )
  896. WRITE_DATA(Bool, AddXToDirectories);
  897. WRITE_DATA_EX(String, L"Masks", AsciiFileMask.Masks, );
  898. WRITE_DATA(Integer, FileNameCase);
  899. WRITE_DATA(Bool, PreserveReadOnly);
  900. WRITE_DATA(Bool, PreserveTime);
  901. WRITE_DATA(Bool, PreserveTimeDirs);
  902. WRITE_DATA(Bool, PreserveRights);
  903. WRITE_DATA(Bool, IgnorePermErrors);
  904. WRITE_DATA_EX(String, L"Text", Rights.Text, );
  905. WRITE_DATA(Integer, TransferMode);
  906. WRITE_DATA(Integer, ResumeSupport);
  907. WRITE_DATA(Int64, ResumeThreshold);
  908. #define WRITE_DATA_CONV_FUNC(X) (unsigned int)(X)
  909. WRITE_DATA_CONV(Integer, L"ReplaceInvalidChars", InvalidCharsReplacement);
  910. WRITE_DATA(String, LocalInvalidChars);
  911. WRITE_DATA(Bool, CalculateSize);
  912. WRITE_DATA_EX(String, L"IncludeFileMask", IncludeFileMask.Masks, );
  913. Storage->DeleteValue(L"ExcludeFileMask"); // obsolete
  914. Storage->DeleteValue(L"NegativeExclude"); // obsolete
  915. DebugAssert(FTransferSkipList.get() == NULL);
  916. DebugAssert(FTransferResumeFile.IsEmpty());
  917. WRITE_DATA(Bool, ClearArchive);
  918. WRITE_DATA(Bool, RemoveCtrlZ);
  919. WRITE_DATA(Bool, RemoveBOM);
  920. WRITE_DATA(Integer, CPSLimit);
  921. WRITE_DATA(Bool, NewerOnly);
  922. WRITE_DATA(Bool, EncryptNewFiles);
  923. WRITE_DATA(Bool, ExcludeHiddenFiles);
  924. WRITE_DATA(Bool, ExcludeEmptyDirectories);
  925. DebugAssert(Size < 0);
  926. DebugAssert(PartOffset < 0);
  927. DebugAssert(PartSize < 0);
  928. DebugAssert(OnceDoneOperation == odoIdle);
  929. DebugAssert(OnTransferOut == NULL);
  930. DebugAssert(OnTransferIn == NULL);
  931. }
  932. //---------------------------------------------------------------------------
  933. #define C(Property) (Property == rhp.Property)
  934. bool __fastcall TCopyParamType::operator==(const TCopyParamType & rhp) const
  935. {
  936. DebugAssert(FTransferSkipList.get() == NULL);
  937. DebugAssert(FTransferResumeFile.IsEmpty());
  938. DebugAssert(OnTransferOut == NULL);
  939. DebugAssert(OnTransferIn == NULL);
  940. DebugAssert(rhp.FTransferSkipList.get() == NULL);
  941. DebugAssert(rhp.FTransferResumeFile.IsEmpty());
  942. DebugAssert(rhp.OnTransferOut == NULL);
  943. DebugAssert(rhp.OnTransferIn == NULL);
  944. return
  945. C(AddXToDirectories) &&
  946. C(AsciiFileMask) &&
  947. C(FileNameCase) &&
  948. C(PreserveReadOnly) &&
  949. C(PreserveTime) &&
  950. C(PreserveTimeDirs) &&
  951. C(PreserveRights) &&
  952. C(IgnorePermErrors) &&
  953. C(Rights) &&
  954. C(TransferMode) &&
  955. C(ResumeSupport) &&
  956. C(ResumeThreshold) &&
  957. C(InvalidCharsReplacement) &&
  958. C(LocalInvalidChars) &&
  959. C(CalculateSize) &&
  960. C(IncludeFileMask) &&
  961. C(ClearArchive) &&
  962. C(RemoveCtrlZ) &&
  963. C(RemoveBOM) &&
  964. C(CPSLimit) &&
  965. C(NewerOnly) &&
  966. C(EncryptNewFiles) &&
  967. C(ExcludeHiddenFiles) &&
  968. C(ExcludeEmptyDirectories) &&
  969. C(Size) &&
  970. C(PartOffset) &&
  971. C(PartSize) &&
  972. C(OnceDoneOperation) &&
  973. true;
  974. }
  975. #undef C
  976. //---------------------------------------------------------------------------
  977. const unsigned long MinSpeed = 8 * 1024;
  978. const unsigned long MaxSpeed = 8 * 1024 * 1024;
  979. //---------------------------------------------------------------------------
  980. static bool __fastcall TryGetSpeedLimit(const UnicodeString & Text, unsigned long & Speed)
  981. {
  982. bool Result;
  983. if (AnsiSameText(Text, LoadStr(SPEED_UNLIMITED)))
  984. {
  985. Speed = 0;
  986. Result = true;
  987. }
  988. else
  989. {
  990. int SSpeed;
  991. Result = TryStrToInt(Text, SSpeed) && (SSpeed >= 0);
  992. if (Result)
  993. {
  994. Speed = SSpeed * 1024;
  995. }
  996. }
  997. return Result;
  998. }
  999. //---------------------------------------------------------------------------
  1000. unsigned long __fastcall GetSpeedLimit(const UnicodeString & Text)
  1001. {
  1002. unsigned long Speed;
  1003. if (!TryGetSpeedLimit(Text, Speed))
  1004. {
  1005. throw Exception(FMTLOAD(SPEED_INVALID, (Text)));
  1006. }
  1007. return Speed;
  1008. }
  1009. //---------------------------------------------------------------------------
  1010. UnicodeString __fastcall SetSpeedLimit(unsigned long Limit)
  1011. {
  1012. UnicodeString Text;
  1013. if (Limit == 0)
  1014. {
  1015. Text = LoadStr(SPEED_UNLIMITED);
  1016. }
  1017. else
  1018. {
  1019. Text = IntToStr(int(Limit / 1024));
  1020. }
  1021. return Text;
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall CopySpeedLimits(TStrings * Source, TStrings * Dest)
  1025. {
  1026. std::unique_ptr<TStringList> Temp(new TStringList());
  1027. bool Unlimited = false;
  1028. for (int Index = 0; Index < Source->Count; Index++)
  1029. {
  1030. UnicodeString Text = Source->Strings[Index];
  1031. unsigned long Speed;
  1032. bool Valid = TryGetSpeedLimit(Text, Speed);
  1033. if ((!Valid || (Speed == 0)) && !Unlimited)
  1034. {
  1035. Temp->Add(LoadStr(SPEED_UNLIMITED));
  1036. Unlimited = true;
  1037. }
  1038. else if (Valid && (Speed > 0))
  1039. {
  1040. Temp->Add(Text);
  1041. }
  1042. }
  1043. if (!Unlimited)
  1044. {
  1045. Temp->Insert(0, LoadStr(SPEED_UNLIMITED));
  1046. }
  1047. Dest->Assign(Temp.get());
  1048. }
  1049. //---------------------------------------------------------------------------
  1050. TOperationSide ReverseOperationSide(TOperationSide Side)
  1051. {
  1052. TOperationSide Result;
  1053. switch (Side)
  1054. {
  1055. case osLocal:
  1056. Result = osRemote;
  1057. break;
  1058. case osRemote:
  1059. Result = osLocal;
  1060. break;
  1061. default:
  1062. case osCurrent:
  1063. DebugFail();
  1064. Result = Side;
  1065. break;
  1066. }
  1067. return Result;
  1068. }