FileMasks.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "FileMasks.h"
  5. #include "Common.h"
  6. #include "TextsCore.h"
  7. #include "RemoteFiles.h"
  8. #include "PuttyTools.h"
  9. #include "Terminal.h"
  10. #include <StrUtils.hpp>
  11. //---------------------------------------------------------------------------
  12. extern const wchar_t IncludeExcludeFileMasksDelimiter = L'|';
  13. UnicodeString FileMasksDelimiters = L";,";
  14. static UnicodeString AllFileMasksDelimiters = FileMasksDelimiters + IncludeExcludeFileMasksDelimiter;
  15. static UnicodeString DirectoryMaskDelimiters = L"/\\";
  16. static UnicodeString FileMasksDelimiterStr = UnicodeString(FileMasksDelimiters[1]) + L' ';
  17. UnicodeString AnyMask = L"*.*";
  18. static UnicodeString MaskSymbols = L"?*[";
  19. //---------------------------------------------------------------------------
  20. __fastcall EFileMasksException::EFileMasksException(
  21. UnicodeString Message, int AErrorStart, int AErrorLen) :
  22. Exception(Message)
  23. {
  24. ErrorStart = AErrorStart;
  25. ErrorLen = AErrorLen;
  26. }
  27. //---------------------------------------------------------------------------
  28. UnicodeString __fastcall MaskFilePart(const UnicodeString Part, const UnicodeString Mask, bool& Masked)
  29. {
  30. UnicodeString Result;
  31. int RestStart = 1;
  32. bool Delim = false;
  33. for (int Index = 1; Index <= Mask.Length(); Index++)
  34. {
  35. switch (Mask[Index])
  36. {
  37. case L'\\':
  38. if (!Delim)
  39. {
  40. Delim = true;
  41. Masked = false;
  42. break;
  43. }
  44. case L'*':
  45. if (!Delim)
  46. {
  47. Result += Part.SubString(RestStart, Part.Length() - RestStart + 1);
  48. RestStart = Part.Length() + 1;
  49. Masked = true;
  50. break;
  51. }
  52. case L'?':
  53. if (!Delim)
  54. {
  55. if (RestStart <= Part.Length())
  56. {
  57. Result += Part[RestStart];
  58. RestStart++;
  59. }
  60. Masked = true;
  61. break;
  62. }
  63. default:
  64. Result += Mask[Index];
  65. RestStart++;
  66. Delim = false;
  67. break;
  68. }
  69. }
  70. return Result;
  71. }
  72. //---------------------------------------------------------------------------
  73. UnicodeString __fastcall MaskFileName(UnicodeString FileName, const UnicodeString Mask)
  74. {
  75. if (IsEffectiveFileNameMask(Mask))
  76. {
  77. bool Masked;
  78. int P = Mask.LastDelimiter(L".");
  79. if (P > 0)
  80. {
  81. int P2 = FileName.LastDelimiter(".");
  82. // only dot at beginning of file name is not considered as
  83. // name/ext separator
  84. UnicodeString FileExt = P2 > 1 ?
  85. FileName.SubString(P2 + 1, FileName.Length() - P2) : UnicodeString();
  86. FileExt = MaskFilePart(FileExt, Mask.SubString(P + 1, Mask.Length() - P), Masked);
  87. if (P2 > 1)
  88. {
  89. FileName.SetLength(P2 - 1);
  90. }
  91. FileName = MaskFilePart(FileName, Mask.SubString(1, P - 1), Masked);
  92. if (!FileExt.IsEmpty())
  93. {
  94. FileName += L"." + FileExt;
  95. }
  96. }
  97. else
  98. {
  99. FileName = MaskFilePart(FileName, Mask, Masked);
  100. }
  101. }
  102. return FileName;
  103. }
  104. //---------------------------------------------------------------------------
  105. bool __fastcall IsFileNameMask(const UnicodeString & Mask)
  106. {
  107. bool Result = Mask.IsEmpty(); // empty mask is the same as *
  108. if (!Result)
  109. {
  110. MaskFilePart(UnicodeString(), Mask, Result);
  111. }
  112. return Result;
  113. }
  114. //---------------------------------------------------------------------------
  115. bool __fastcall IsEffectiveFileNameMask(const UnicodeString & Mask)
  116. {
  117. return !Mask.IsEmpty() && (Mask != L"*") && (Mask != AnyMask);
  118. }
  119. //---------------------------------------------------------------------------
  120. UnicodeString __fastcall DelimitFileNameMask(UnicodeString Mask)
  121. {
  122. for (int i = 1; i <= Mask.Length(); i++)
  123. {
  124. if (wcschr(L"\\*?", Mask[i]) != NULL)
  125. {
  126. Mask.Insert(L"\\", i);
  127. i++;
  128. }
  129. }
  130. return Mask;
  131. }
  132. //---------------------------------------------------------------------------
  133. //---------------------------------------------------------------------------
  134. TFileMasks::TParams::TParams() :
  135. Size(0)
  136. {
  137. }
  138. //---------------------------------------------------------------------------
  139. UnicodeString TFileMasks::TParams::ToString() const
  140. {
  141. return UnicodeString(L"[") + IntToStr(Size) + L"/" + DateTimeToStr(Modification) + L"]";
  142. }
  143. //---------------------------------------------------------------------------
  144. //---------------------------------------------------------------------------
  145. bool __fastcall TFileMasks::IsMask(const UnicodeString Mask)
  146. {
  147. return (Mask.LastDelimiter(MaskSymbols) > 0);
  148. }
  149. //---------------------------------------------------------------------------
  150. UnicodeString TFileMasks::EscapeMask(const UnicodeString & S)
  151. {
  152. UnicodeString Result = S;
  153. if (Result.LastDelimiter(MaskSymbols) > 0) // optimization
  154. {
  155. for (int Index = 1; Index < Result.Length(); Index++)
  156. {
  157. if (MaskSymbols.Pos(Result[Index]) > 0)
  158. {
  159. Result.Insert(L"[", Index);
  160. Index += 2;
  161. Result.Insert(L"]", Index);
  162. }
  163. }
  164. }
  165. return Result;
  166. }
  167. //---------------------------------------------------------------------------
  168. UnicodeString __fastcall TFileMasks::NormalizeMask(const UnicodeString & Mask, const UnicodeString & AnyMask)
  169. {
  170. if (!IsEffectiveFileNameMask(Mask))
  171. {
  172. return AnyMask;
  173. }
  174. else
  175. {
  176. return Mask;
  177. }
  178. }
  179. //---------------------------------------------------------------------------
  180. UnicodeString __fastcall TFileMasks::ComposeMaskStr(
  181. TStrings * MasksStr, bool Directory)
  182. {
  183. UnicodeString Result;
  184. UnicodeString ResultNoDirMask;
  185. for (int I = 0; I < MasksStr->Count; I++)
  186. {
  187. UnicodeString Str = MasksStr->Strings[I].Trim();
  188. if (!Str.IsEmpty())
  189. {
  190. for (int P = 1; P <= Str.Length(); P++)
  191. {
  192. if (Str.IsDelimiter(AllFileMasksDelimiters, P))
  193. {
  194. Str.Insert(Str[P], P);
  195. P++;
  196. }
  197. }
  198. UnicodeString StrNoDirMask;
  199. if (Directory)
  200. {
  201. StrNoDirMask = Str;
  202. Str = MakeDirectoryMask(Str);
  203. }
  204. else
  205. {
  206. while (Str.IsDelimiter(DirectoryMaskDelimiters, Str.Length()))
  207. {
  208. Str.SetLength(Str.Length() - 1);
  209. }
  210. StrNoDirMask = Str;
  211. }
  212. AddToList(Result, Str, FileMasksDelimiterStr);
  213. AddToList(ResultNoDirMask, StrNoDirMask, FileMasksDelimiterStr);
  214. }
  215. }
  216. // For directories, the above will add slash ay the end of masks,
  217. // breaking size and time masks and thus circumverting their validation.
  218. // This performes as hoc validation to cover the scenario.
  219. // For files this makes no difference, but no harm either
  220. TFileMasks Temp(Directory ? 1 : 0);
  221. Temp = ResultNoDirMask;
  222. return Result;
  223. }
  224. //---------------------------------------------------------------------------
  225. UnicodeString __fastcall TFileMasks::ComposeMaskStr(
  226. TStrings * IncludeFileMasksStr, TStrings * ExcludeFileMasksStr,
  227. TStrings * IncludeDirectoryMasksStr, TStrings * ExcludeDirectoryMasksStr)
  228. {
  229. UnicodeString IncludeMasks = ComposeMaskStr(IncludeFileMasksStr, false);
  230. AddToList(IncludeMasks, ComposeMaskStr(IncludeDirectoryMasksStr, true), FileMasksDelimiterStr);
  231. UnicodeString ExcludeMasks = ComposeMaskStr(ExcludeFileMasksStr, false);
  232. AddToList(ExcludeMasks, ComposeMaskStr(ExcludeDirectoryMasksStr, true), FileMasksDelimiterStr);
  233. UnicodeString Result = IncludeMasks;
  234. if (!ExcludeMasks.IsEmpty())
  235. {
  236. if (!Result.IsEmpty())
  237. {
  238. Result += L' ';
  239. }
  240. Result += UnicodeString(IncludeExcludeFileMasksDelimiter) + L' ' + ExcludeMasks;
  241. }
  242. return Result;
  243. }
  244. //---------------------------------------------------------------------------
  245. __fastcall TFileMasks::TFileMasks()
  246. {
  247. Init();
  248. }
  249. //---------------------------------------------------------------------------
  250. __fastcall TFileMasks::TFileMasks(int ForceDirectoryMasks)
  251. {
  252. Init();
  253. FForceDirectoryMasks = ForceDirectoryMasks;
  254. }
  255. //---------------------------------------------------------------------------
  256. __fastcall TFileMasks::TFileMasks(const TFileMasks & Source)
  257. {
  258. Init();
  259. DoCopy(Source);
  260. }
  261. //---------------------------------------------------------------------------
  262. __fastcall TFileMasks::TFileMasks(const UnicodeString & AMasks)
  263. {
  264. Init();
  265. SetStr(AMasks, false);
  266. }
  267. //---------------------------------------------------------------------------
  268. __fastcall TFileMasks::~TFileMasks()
  269. {
  270. Clear();
  271. }
  272. //---------------------------------------------------------------------------
  273. void TFileMasks::DoCopy(const TFileMasks & Source)
  274. {
  275. FForceDirectoryMasks = Source.FForceDirectoryMasks;
  276. FNoImplicitMatchWithDirExcludeMask = Source.FNoImplicitMatchWithDirExcludeMask;
  277. FAllDirsAreImplicitlyIncluded = Source.FAllDirsAreImplicitlyIncluded;
  278. FLocalRoot = Source.FLocalRoot;
  279. FRemoteRoot = Source.FRemoteRoot;
  280. SetStr(Source.Masks, false);
  281. }
  282. //---------------------------------------------------------------------------
  283. void __fastcall TFileMasks::Init()
  284. {
  285. FForceDirectoryMasks = -1;
  286. FNoImplicitMatchWithDirExcludeMask = false;
  287. FAllDirsAreImplicitlyIncluded = false;
  288. FAnyRelative = false;
  289. DoInit(false);
  290. }
  291. //---------------------------------------------------------------------------
  292. void __fastcall TFileMasks::DoInit(bool Delete)
  293. {
  294. for (size_t Index = 0; Index < LENOF(FMasksStr); Index++)
  295. {
  296. if (Delete)
  297. {
  298. delete FMasksStr[Index];
  299. }
  300. FMasksStr[Index] = NULL;
  301. }
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TFileMasks::Clear()
  305. {
  306. DoInit(true);
  307. for (size_t Index = 0; Index < LENOF(FMasks); Index++)
  308. {
  309. Clear(FMasks[Index]);
  310. }
  311. }
  312. //---------------------------------------------------------------------------
  313. void __fastcall TFileMasks::Clear(TMasks & Masks)
  314. {
  315. TMasks::iterator I = Masks.begin();
  316. while (I != Masks.end())
  317. {
  318. delete (*I).FileNameMask;
  319. delete (*I).RemoteDirectoryMask;
  320. delete (*I).LocalDirectoryMask;
  321. I++;
  322. }
  323. Masks.clear();
  324. }
  325. //---------------------------------------------------------------------------
  326. bool __fastcall TFileMasks::MatchesMasks(
  327. const UnicodeString & FileName, bool Local, bool Directory,
  328. const UnicodeString & Path, const TParams * Params, const TMasks & Masks, bool Recurse)
  329. {
  330. bool Result = false;
  331. TMasks::const_iterator I = Masks.begin();
  332. while (!Result && (I != Masks.end()))
  333. {
  334. const TMask & Mask = *I;
  335. Masks::TMask * DirectoryMask = Local ? Mask.LocalDirectoryMask : Mask.RemoteDirectoryMask;
  336. Result =
  337. MatchesMaskMask(Mask.DirectoryMaskKind, DirectoryMask, Path) &&
  338. MatchesMaskMask(Mask.FileNameMaskKind, Mask.FileNameMask, FileName);
  339. if (Result)
  340. {
  341. bool HasSize = (Params != NULL);
  342. switch (Mask.HighSizeMask)
  343. {
  344. case TMask::None:
  345. Result = true;
  346. break;
  347. case TMask::Open:
  348. Result = HasSize && (Params->Size < Mask.HighSize);
  349. break;
  350. case TMask::Close:
  351. Result = HasSize && (Params->Size <= Mask.HighSize);
  352. break;
  353. }
  354. if (Result)
  355. {
  356. switch (Mask.LowSizeMask)
  357. {
  358. case TMask::None:
  359. Result = true;
  360. break;
  361. case TMask::Open:
  362. Result = HasSize && (Params->Size > Mask.LowSize);
  363. break;
  364. case TMask::Close:
  365. Result = HasSize && (Params->Size >= Mask.LowSize);
  366. break;
  367. }
  368. }
  369. bool HasModification = (Params != NULL);
  370. if (Result)
  371. {
  372. switch (Mask.HighModificationMask)
  373. {
  374. case TMask::None:
  375. Result = true;
  376. break;
  377. case TMask::Open:
  378. Result = HasModification && (Params->Modification < Mask.HighModification);
  379. break;
  380. case TMask::Close:
  381. Result = HasModification && (Params->Modification <= Mask.HighModification);
  382. break;
  383. }
  384. }
  385. if (Result)
  386. {
  387. switch (Mask.LowModificationMask)
  388. {
  389. case TMask::None:
  390. Result = true;
  391. break;
  392. case TMask::Open:
  393. Result = HasModification && (Params->Modification > Mask.LowModification);
  394. break;
  395. case TMask::Close:
  396. Result = HasModification && (Params->Modification >= Mask.LowModification);
  397. break;
  398. }
  399. }
  400. }
  401. I++;
  402. }
  403. if (!Result && Directory && !IsUnixRootPath(Path) && Recurse)
  404. {
  405. UnicodeString ParentFileName = UnixExtractFileName(Path);
  406. UnicodeString ParentPath = SimpleUnixExcludeTrailingBackslash(UnixExtractFilePath(Path));
  407. // Pass Params down or not?
  408. // Currently it includes Size/Time only, what is not used for directories.
  409. // So it depends on future use. Possibly we should make a copy
  410. // and pass on only relevant fields.
  411. Result = MatchesMasks(ParentFileName, Local, true, ParentPath, Params, Masks, Recurse);
  412. }
  413. return Result;
  414. }
  415. //---------------------------------------------------------------------------
  416. bool TFileMasks::DoMatches(
  417. const UnicodeString & FileName, bool Local, bool Directory, const UnicodeString & Path, const TParams * Params,
  418. bool RecurseInclude, bool & ImplicitMatch) const
  419. {
  420. if (FileName == L"aaa2.txt")
  421. {
  422. UnicodeString X;
  423. }
  424. bool ImplicitIncludeMatch = (FAllDirsAreImplicitlyIncluded && Directory) || FMasks[MASK_INDEX(Directory, true)].empty();
  425. bool ExplicitIncludeMatch = MatchesMasks(FileName, Local, Directory, Path, Params, FMasks[MASK_INDEX(Directory, true)], RecurseInclude);
  426. bool Result =
  427. (ImplicitIncludeMatch || ExplicitIncludeMatch) &&
  428. !MatchesMasks(FileName, Local, Directory, Path, Params, FMasks[MASK_INDEX(Directory, false)], false);
  429. ImplicitMatch =
  430. Result && ImplicitIncludeMatch && !ExplicitIncludeMatch &&
  431. ((Directory && FNoImplicitMatchWithDirExcludeMask) || FMasks[MASK_INDEX(Directory, false)].empty());
  432. return Result;
  433. }
  434. //---------------------------------------------------------------------------
  435. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Local,
  436. bool Directory, const TParams * Params) const
  437. {
  438. bool ImplicitMatch;
  439. return Matches(FileName, Local, Directory, Params, true, ImplicitMatch);
  440. }
  441. //---------------------------------------------------------------------------
  442. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Local,
  443. bool Directory, const TParams * Params, bool RecurseInclude, bool & ImplicitMatch) const
  444. {
  445. bool Result;
  446. if (Local)
  447. {
  448. UnicodeString Path = ExtractFilePath(FileName);
  449. if (!Path.IsEmpty())
  450. {
  451. Path = ToUnixPath(ExcludeTrailingBackslash(Path));
  452. }
  453. Result = DoMatches(ExtractFileName(FileName), Local, Directory, Path, Params,
  454. RecurseInclude, ImplicitMatch);
  455. }
  456. else
  457. {
  458. Result = DoMatches(UnixExtractFileName(FileName), Local, Directory,
  459. SimpleUnixExcludeTrailingBackslash(UnixExtractFilePath(FileName)), Params,
  460. RecurseInclude, ImplicitMatch);
  461. }
  462. return Result;
  463. }
  464. //---------------------------------------------------------------------------
  465. bool TFileMasks::MatchesFileName(const UnicodeString & FileName, bool Directory, const TParams * Params) const
  466. {
  467. bool ImplicitMatch;
  468. return DoMatches(FileName, false, Directory, EmptyStr, Params, true, ImplicitMatch);
  469. }
  470. //---------------------------------------------------------------------------
  471. bool __fastcall TFileMasks::operator ==(const TFileMasks & rhm) const
  472. {
  473. return (Masks == rhm.Masks);
  474. }
  475. //---------------------------------------------------------------------------
  476. TFileMasks & __fastcall TFileMasks::operator =(const UnicodeString & rhs)
  477. {
  478. Masks = rhs;
  479. return *this;
  480. }
  481. //---------------------------------------------------------------------------
  482. TFileMasks & __fastcall TFileMasks::operator =(const TFileMasks & rhm)
  483. {
  484. DoInit(true);
  485. DoCopy(rhm);
  486. return *this;
  487. }
  488. //---------------------------------------------------------------------------
  489. bool __fastcall TFileMasks::operator ==(const UnicodeString & rhs) const
  490. {
  491. return (Masks == rhs);
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall TFileMasks::ThrowError(int Start, int End)
  495. {
  496. throw EFileMasksException(
  497. FMTLOAD(MASK_ERROR, (Masks.SubString(Start, End - Start + 1))),
  498. Start, End - Start + 1);
  499. }
  500. //---------------------------------------------------------------------------
  501. Masks::TMask * TFileMasks::DoCreateMaskMask(const UnicodeString & Str)
  502. {
  503. return new Masks::TMask(Str);
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TFileMasks::CreateMaskMask(
  507. const UnicodeString & Mask, int Start, int End, bool Ex, TMask::TKind & MaskKind, Masks::TMask *& MaskMask)
  508. {
  509. try
  510. {
  511. DebugAssert(MaskMask == NULL);
  512. if (Ex && !IsEffectiveFileNameMask(Mask))
  513. {
  514. MaskKind = TMask::TKind::Any;
  515. MaskMask = NULL;
  516. }
  517. else
  518. {
  519. MaskKind = (Ex && (Mask == L"*.")) ? TMask::TKind::NoExt : TMask::TKind::Regular;
  520. MaskMask = DoCreateMaskMask(Mask);
  521. }
  522. }
  523. catch(...)
  524. {
  525. ThrowError(Start, End);
  526. }
  527. }
  528. //---------------------------------------------------------------------------
  529. UnicodeString __fastcall TFileMasks::MakeDirectoryMask(UnicodeString Str)
  530. {
  531. DebugAssert(!Str.IsEmpty());
  532. if (Str.IsEmpty() || !Str.IsDelimiter(DirectoryMaskDelimiters, Str.Length()))
  533. {
  534. int D = Str.LastDelimiter(DirectoryMaskDelimiters);
  535. // if there's any [back]slash anywhere in str,
  536. // add the same [back]slash at the end, otherwise add slash
  537. wchar_t Delimiter = (D > 0) ? Str[D] : DirectoryMaskDelimiters[1];
  538. Str += Delimiter;
  539. }
  540. return Str;
  541. }
  542. //---------------------------------------------------------------------------
  543. void __fastcall TFileMasks::CreateMask(
  544. const UnicodeString & MaskStr, int MaskStart, int /*MaskEnd*/, bool Include)
  545. {
  546. bool Directory = false; // shut up
  547. TMask Mask;
  548. Mask.MaskStr = MaskStr;
  549. Mask.UserStr = MaskStr;
  550. Mask.FileNameMaskKind = TMask::TKind::Any;
  551. Mask.FileNameMask = NULL;
  552. Mask.DirectoryMaskKind = TMask::TKind::Any;
  553. Mask.RemoteDirectoryMask = NULL;
  554. Mask.LocalDirectoryMask = NULL;
  555. Mask.HighSizeMask = TMask::None;
  556. Mask.LowSizeMask = TMask::None;
  557. Mask.HighModificationMask = TMask::None;
  558. Mask.LowModificationMask = TMask::None;
  559. wchar_t NextPartDelimiter = L'\0';
  560. int NextPartFrom = 1;
  561. while (NextPartFrom <= MaskStr.Length())
  562. {
  563. wchar_t PartDelimiter = NextPartDelimiter;
  564. int PartFrom = NextPartFrom;
  565. UnicodeString PartStr = CopyToChars(MaskStr, NextPartFrom, L"<>", false, &NextPartDelimiter, true);
  566. int PartStart = MaskStart + PartFrom - 1;
  567. int PartEnd = MaskStart + NextPartFrom - 1 - 2;
  568. TrimEx(PartStr, PartStart, PartEnd);
  569. if (PartDelimiter != L'\0')
  570. {
  571. bool Low = (PartDelimiter == L'>');
  572. TMask::TMaskBoundary Boundary;
  573. if ((PartStr.Length() >= 1) && (PartStr[1] == L'='))
  574. {
  575. Boundary = TMask::Close;
  576. PartStr.Delete(1, 1);
  577. }
  578. else
  579. {
  580. Boundary = TMask::Open;
  581. }
  582. TDateTime Modification;
  583. __int64 DummySize;
  584. if ((!TryStrToInt64(PartStr, DummySize) && TryStrToDateTimeStandard(PartStr, Modification)) ||
  585. TryRelativeStrToDateTime(PartStr, Modification, false))
  586. {
  587. TMask::TMaskBoundary & ModificationMask =
  588. (Low ? Mask.LowModificationMask : Mask.HighModificationMask);
  589. if ((ModificationMask != TMask::None) || Directory)
  590. {
  591. // include delimiter into size part
  592. ThrowError(PartStart - 1, PartEnd);
  593. }
  594. ModificationMask = Boundary;
  595. (Low ? Mask.LowModification : Mask.HighModification) = Modification;
  596. }
  597. else
  598. {
  599. TMask::TMaskBoundary & SizeMask = (Low ? Mask.LowSizeMask : Mask.HighSizeMask);
  600. __int64 & Size = (Low ? Mask.LowSize : Mask.HighSize);
  601. if ((SizeMask != TMask::None) || Directory)
  602. {
  603. // include delimiter into size part
  604. ThrowError(PartStart - 1, PartEnd);
  605. }
  606. SizeMask = Boundary;
  607. if (!TryStrToSize(PartStr, Size))
  608. {
  609. ThrowError(PartStart, PartEnd);
  610. }
  611. }
  612. }
  613. else if (!PartStr.IsEmpty())
  614. {
  615. int D = PartStr.LastDelimiter(DirectoryMaskDelimiters);
  616. Directory = (D > 0) && (D == PartStr.Length());
  617. if (Directory)
  618. {
  619. do
  620. {
  621. PartStr.SetLength(PartStr.Length() - 1);
  622. Mask.UserStr.Delete(PartStart - MaskStart + D, 1);
  623. D--;
  624. }
  625. while (PartStr.IsDelimiter(DirectoryMaskDelimiters, PartStr.Length()));
  626. D = PartStr.LastDelimiter(DirectoryMaskDelimiters);
  627. if (FForceDirectoryMasks == 0)
  628. {
  629. Directory = false;
  630. Mask.MaskStr = Mask.UserStr;
  631. }
  632. }
  633. else if (FForceDirectoryMasks > 0)
  634. {
  635. Directory = true;
  636. Mask.MaskStr.Insert(DirectoryMaskDelimiters[1], PartStart - MaskStart + PartStr.Length());
  637. }
  638. if (D > 0)
  639. {
  640. // make sure sole "/" (root dir) is preserved as is
  641. UnicodeString DirectoryMaskStr = SimpleUnixExcludeTrailingBackslash(ToUnixPath(PartStr.SubString(1, D)));
  642. UnicodeString RemoteDirectoryMaskStr = DirectoryMaskStr;
  643. UnicodeString LocalDirectoryMaskStr = DirectoryMaskStr;
  644. const UnicodeString RelativePrefix = L".";
  645. const UnicodeString RelativePrefixWithSlash = RelativePrefix + L"/";
  646. if (DirectoryMaskStr == RelativePrefix)
  647. {
  648. FAnyRelative = true;
  649. if (!FRemoteRoot.IsEmpty())
  650. {
  651. RemoteDirectoryMaskStr = SimpleUnixExcludeTrailingBackslash(FRemoteRoot);
  652. }
  653. if (!FLocalRoot.IsEmpty())
  654. {
  655. LocalDirectoryMaskStr = SimpleUnixExcludeTrailingBackslash(FLocalRoot);
  656. }
  657. }
  658. else if (StartsStr(RelativePrefixWithSlash, DirectoryMaskStr))
  659. {
  660. FAnyRelative = true;
  661. DirectoryMaskStr.Delete(1, RelativePrefixWithSlash.Length());
  662. if (!FRemoteRoot.IsEmpty())
  663. {
  664. RemoteDirectoryMaskStr = FRemoteRoot + DirectoryMaskStr;
  665. }
  666. if (!FLocalRoot.IsEmpty())
  667. {
  668. LocalDirectoryMaskStr = FLocalRoot + DirectoryMaskStr;
  669. }
  670. }
  671. CreateMaskMask(
  672. RemoteDirectoryMaskStr, PartStart, PartStart + D - 1, false,
  673. Mask.DirectoryMaskKind, Mask.RemoteDirectoryMask);
  674. if (Mask.RemoteDirectoryMask != NULL)
  675. {
  676. Mask.LocalDirectoryMask = DoCreateMaskMask(LocalDirectoryMaskStr);
  677. }
  678. CreateMaskMask(
  679. PartStr.SubString(D + 1, PartStr.Length() - D),
  680. PartStart + D, PartEnd, true,
  681. Mask.FileNameMaskKind, Mask.FileNameMask);
  682. }
  683. else
  684. {
  685. CreateMaskMask(PartStr, PartStart, PartEnd, true, Mask.FileNameMaskKind, Mask.FileNameMask);
  686. }
  687. }
  688. }
  689. FMasks[MASK_INDEX(Directory, Include)].push_back(Mask);
  690. }
  691. //---------------------------------------------------------------------------
  692. TStrings * __fastcall TFileMasks::GetMasksStr(int Index) const
  693. {
  694. if (FMasksStr[Index] == NULL)
  695. {
  696. FMasksStr[Index] = new TStringList();
  697. TMasks::const_iterator I = FMasks[Index].begin();
  698. while (I != FMasks[Index].end())
  699. {
  700. FMasksStr[Index]->Add((*I).UserStr);
  701. I++;
  702. }
  703. }
  704. return FMasksStr[Index];
  705. }
  706. //---------------------------------------------------------------------------
  707. void __fastcall TFileMasks::TrimEx(UnicodeString & Str, int & Start, int & End)
  708. {
  709. UnicodeString Buf = TrimLeft(Str);
  710. Start += Str.Length() - Buf.Length();
  711. Str = TrimRight(Buf);
  712. End -= Buf.Length() - Str.Length();
  713. }
  714. //---------------------------------------------------------------------------
  715. bool TFileMasks::MatchesMaskMask(TMask::TKind MaskKind, Masks::TMask * MaskMask, const UnicodeString & Str)
  716. {
  717. bool Result;
  718. if (MaskKind == TMask::TKind::Any)
  719. {
  720. Result = true;
  721. }
  722. else if ((MaskKind == TMask::TKind::NoExt) && (Str.Pos(L".") == 0))
  723. {
  724. Result = true;
  725. }
  726. else
  727. {
  728. Result = MaskMask->Matches(Str);
  729. }
  730. return Result;
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TFileMasks::SetMasks(const UnicodeString value)
  734. {
  735. if (FStr != value)
  736. {
  737. SetStr(value, false);
  738. }
  739. }
  740. //---------------------------------------------------------------------------
  741. void __fastcall TFileMasks::SetMask(const UnicodeString & Mask)
  742. {
  743. SetStr(Mask, true);
  744. }
  745. //---------------------------------------------------------------------------
  746. void __fastcall TFileMasks::SetStr(const UnicodeString Str, bool SingleMask)
  747. {
  748. FAnyRelative = false;
  749. UnicodeString Backup = FStr;
  750. try
  751. {
  752. FStr = Str;
  753. Clear();
  754. int NextMaskFrom = 1;
  755. bool Include = true;
  756. while (NextMaskFrom <= Str.Length())
  757. {
  758. int MaskStart = NextMaskFrom;
  759. wchar_t NextMaskDelimiter;
  760. UnicodeString MaskStr;
  761. if (SingleMask)
  762. {
  763. MaskStr = Str;
  764. NextMaskFrom = Str.Length() + 1;
  765. NextMaskDelimiter = L'\0';
  766. }
  767. else
  768. {
  769. MaskStr = CopyToChars(Str, NextMaskFrom, AllFileMasksDelimiters, false, &NextMaskDelimiter, true);
  770. }
  771. int MaskEnd = NextMaskFrom - 2;
  772. TrimEx(MaskStr, MaskStart, MaskEnd);
  773. if (!MaskStr.IsEmpty())
  774. {
  775. CreateMask(MaskStr, MaskStart, MaskEnd, Include);
  776. }
  777. if (NextMaskDelimiter == IncludeExcludeFileMasksDelimiter)
  778. {
  779. if (Include)
  780. {
  781. Include = false;
  782. }
  783. else
  784. {
  785. ThrowError(NextMaskFrom - 1, Str.Length());
  786. }
  787. }
  788. }
  789. }
  790. catch(...)
  791. {
  792. // this does not work correctly if previous mask was set using SetMask.
  793. // this should not fail (the mask was validated before),
  794. // otherwise we end in an infinite loop
  795. SetStr(Backup, false);
  796. throw;
  797. }
  798. }
  799. //---------------------------------------------------------------------------
  800. void TFileMasks::SetRoots(const UnicodeString & LocalRoot, const UnicodeString & RemoteRoot)
  801. {
  802. if (FAnyRelative) // optimization
  803. {
  804. FLocalRoot = EscapeMask(UnixIncludeTrailingBackslash(ToUnixPath(LocalRoot)));
  805. FRemoteRoot = EscapeMask(UnixIncludeTrailingBackslash(RemoteRoot));
  806. SetStr(FStr, false);
  807. }
  808. }
  809. //---------------------------------------------------------------------------
  810. void TFileMasks::SetRoots(TStrings * LocalFileList, const UnicodeString & RemoteRoot)
  811. {
  812. if (FAnyRelative) // optimization
  813. {
  814. UnicodeString LocalRoot;
  815. ExtractCommonPath(LocalFileList, LocalRoot);
  816. SetRoots(LocalRoot, RemoteRoot);
  817. }
  818. }
  819. //---------------------------------------------------------------------------
  820. void TFileMasks::SetRoots(const UnicodeString & LocalRoot, TStrings * RemoteFileList)
  821. {
  822. if (FAnyRelative) // optimization
  823. {
  824. UnicodeString RemoteRoot;
  825. UnixExtractCommonPath(RemoteFileList, RemoteRoot);
  826. SetRoots(LocalRoot, RemoteRoot);
  827. }
  828. }
  829. //---------------------------------------------------------------------------
  830. //---------------------------------------------------------------------------
  831. #define TEXT_TOKEN L'\255'
  832. //---------------------------------------------------------------------------
  833. const wchar_t TCustomCommand::NoQuote = L'\0';
  834. const UnicodeString TCustomCommand::Quotes = L"\"'";
  835. //---------------------------------------------------------------------------
  836. UnicodeString __fastcall TCustomCommand::Escape(const UnicodeString & S)
  837. {
  838. return ReplaceStr(S, L"!", L"!!");
  839. }
  840. //---------------------------------------------------------------------------
  841. TCustomCommand::TCustomCommand()
  842. {
  843. }
  844. //---------------------------------------------------------------------------
  845. void __fastcall TCustomCommand::GetToken(
  846. const UnicodeString & Command, int Index, int & Len, wchar_t & PatternCmd)
  847. {
  848. DebugAssert(Index <= Command.Length());
  849. const wchar_t * Ptr = Command.c_str() + Index - 1;
  850. if (Ptr[0] == L'!')
  851. {
  852. PatternCmd = Ptr[1];
  853. if (PatternCmd == L'\0')
  854. {
  855. Len = 1;
  856. }
  857. else if (PatternCmd == L'!')
  858. {
  859. Len = 2;
  860. }
  861. else
  862. {
  863. Len = PatternLen(Command, Index);
  864. }
  865. if (Len <= 0)
  866. {
  867. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNKNOWN, (PatternCmd, Index)));
  868. }
  869. else
  870. {
  871. if ((Command.Length() - Index + 1) < Len)
  872. {
  873. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (PatternCmd, Index)));
  874. }
  875. }
  876. }
  877. else
  878. {
  879. PatternCmd = TEXT_TOKEN;
  880. const wchar_t * NextPattern = wcschr(Ptr, L'!');
  881. if (NextPattern == NULL)
  882. {
  883. Len = Command.Length() - Index + 1;
  884. }
  885. else
  886. {
  887. Len = NextPattern - Ptr;
  888. }
  889. }
  890. }
  891. //---------------------------------------------------------------------------
  892. void __fastcall TCustomCommand::PatternHint(int /*Index*/, const UnicodeString & /*Pattern*/)
  893. {
  894. // noop
  895. }
  896. //---------------------------------------------------------------------------
  897. UnicodeString __fastcall TCustomCommand::Complete(const UnicodeString & Command,
  898. bool LastPass)
  899. {
  900. int Index = 1;
  901. int PatternIndex = 0;
  902. while (Index <= Command.Length())
  903. {
  904. int Len;
  905. wchar_t PatternCmd;
  906. GetToken(Command, Index, Len, PatternCmd);
  907. if (PatternCmd == TEXT_TOKEN)
  908. {
  909. }
  910. else if (PatternCmd == L'!')
  911. {
  912. }
  913. else
  914. {
  915. UnicodeString Pattern = Command.SubString(Index, Len);
  916. PatternHint(PatternIndex, Pattern);
  917. PatternIndex++;
  918. }
  919. Index += Len;
  920. }
  921. UnicodeString Result;
  922. Index = 1;
  923. PatternIndex = 0;
  924. while (Index <= Command.Length())
  925. {
  926. int Len;
  927. wchar_t PatternCmd;
  928. GetToken(Command, Index, Len, PatternCmd);
  929. if (PatternCmd == TEXT_TOKEN)
  930. {
  931. Result += Command.SubString(Index, Len);
  932. }
  933. else if (PatternCmd == L'!')
  934. {
  935. if (LastPass)
  936. {
  937. Result += L'!';
  938. }
  939. else
  940. {
  941. Result += Command.SubString(Index, Len);
  942. }
  943. }
  944. else
  945. {
  946. wchar_t Quote = NoQuote;
  947. if ((Index > 1) && (Index + Len - 1 < Command.Length()) &&
  948. Command.IsDelimiter(Quotes, Index - 1) &&
  949. Command.IsDelimiter(Quotes, Index + Len) &&
  950. (Command[Index - 1] == Command[Index + Len]))
  951. {
  952. Quote = Command[Index - 1];
  953. }
  954. UnicodeString Pattern = Command.SubString(Index, Len);
  955. UnicodeString Replacement;
  956. bool Delimit = true;
  957. if (PatternReplacement(PatternIndex, Pattern, Replacement, Delimit))
  958. {
  959. if (!LastPass)
  960. {
  961. Replacement = Escape(Replacement);
  962. }
  963. if (Delimit)
  964. {
  965. DelimitReplacement(Replacement, Quote);
  966. }
  967. Result += Replacement;
  968. }
  969. else
  970. {
  971. Result += Pattern;
  972. }
  973. PatternIndex++;
  974. }
  975. Index += Len;
  976. }
  977. return Result;
  978. }
  979. //---------------------------------------------------------------------------
  980. void __fastcall TCustomCommand::DelimitReplacement(UnicodeString & Replacement, wchar_t Quote)
  981. {
  982. Replacement = ShellDelimitStr(Replacement, Quote);
  983. }
  984. //---------------------------------------------------------------------------
  985. void __fastcall TCustomCommand::Validate(const UnicodeString & Command)
  986. {
  987. CustomValidate(Command, NULL);
  988. }
  989. //---------------------------------------------------------------------------
  990. void __fastcall TCustomCommand::CustomValidate(const UnicodeString & Command,
  991. void * Arg)
  992. {
  993. int Index = 1;
  994. while (Index <= Command.Length())
  995. {
  996. int Len;
  997. wchar_t PatternCmd;
  998. GetToken(Command, Index, Len, PatternCmd);
  999. ValidatePattern(Command, Index, Len, PatternCmd, Arg);
  1000. Index += Len;
  1001. }
  1002. }
  1003. //---------------------------------------------------------------------------
  1004. bool __fastcall TCustomCommand::FindPattern(const UnicodeString & Command,
  1005. wchar_t PatternCmd)
  1006. {
  1007. bool Result = false;
  1008. int Index = 1;
  1009. while (!Result && (Index <= Command.Length()))
  1010. {
  1011. int Len;
  1012. wchar_t APatternCmd;
  1013. GetToken(Command, Index, Len, APatternCmd);
  1014. if (((PatternCmd != L'!') && (tolower(PatternCmd) == tolower(APatternCmd))) ||
  1015. ((PatternCmd == L'!') && (Len == 1) && (APatternCmd != TEXT_TOKEN)) ||
  1016. ((PatternCmd == L'\0') && (APatternCmd != TEXT_TOKEN)))
  1017. {
  1018. Result = true;
  1019. }
  1020. Index += Len;
  1021. }
  1022. return Result;
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. bool __fastcall TCustomCommand::HasAnyPatterns(const UnicodeString & Command)
  1026. {
  1027. return FindPattern(Command, L'\0');
  1028. }
  1029. //---------------------------------------------------------------------------
  1030. void __fastcall TCustomCommand::ValidatePattern(const UnicodeString & /*Command*/,
  1031. int /*Index*/, int /*Len*/, wchar_t /*PatternCmd*/, void * /*Arg*/)
  1032. {
  1033. }
  1034. //---------------------------------------------------------------------------
  1035. //---------------------------------------------------------------------------
  1036. TInteractiveCustomCommand::TInteractiveCustomCommand(
  1037. TCustomCommand * ChildCustomCommand)
  1038. {
  1039. FChildCustomCommand = ChildCustomCommand;
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. void __fastcall TInteractiveCustomCommand::Prompt(
  1043. int /*Index*/, const UnicodeString & /*Prompt*/, UnicodeString & Value)
  1044. {
  1045. Value = L"";
  1046. }
  1047. //---------------------------------------------------------------------------
  1048. void __fastcall TInteractiveCustomCommand::Execute(
  1049. const UnicodeString & /*Command*/, UnicodeString & Value)
  1050. {
  1051. Value = L"";
  1052. }
  1053. //---------------------------------------------------------------------------
  1054. int __fastcall TInteractiveCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  1055. {
  1056. int Len;
  1057. wchar_t PatternCmd = (Index < Command.Length()) ? Command[Index + 1] : L'\0';
  1058. switch (PatternCmd)
  1059. {
  1060. case L'?':
  1061. {
  1062. const wchar_t * Ptr = Command.c_str() + Index - 1;
  1063. const wchar_t * PatternEnd = wcschr(Ptr + 1, L'!');
  1064. if (PatternEnd == NULL)
  1065. {
  1066. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  1067. }
  1068. Len = PatternEnd - Ptr + 1;
  1069. }
  1070. break;
  1071. case L'`':
  1072. {
  1073. const wchar_t * Ptr = Command.c_str() + Index - 1;
  1074. const wchar_t * PatternEnd = wcschr(Ptr + 2, L'`');
  1075. if (PatternEnd == NULL)
  1076. {
  1077. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  1078. }
  1079. Len = PatternEnd - Ptr + 1;
  1080. }
  1081. break;
  1082. default:
  1083. Len = FChildCustomCommand->PatternLen(Command, Index);
  1084. break;
  1085. }
  1086. return Len;
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. bool __fastcall TInteractiveCustomCommand::IsPromptPattern(const UnicodeString & Pattern)
  1090. {
  1091. return (Pattern.Length() >= 3) && (Pattern[2] == L'?');
  1092. }
  1093. //---------------------------------------------------------------------------
  1094. void __fastcall TInteractiveCustomCommand::ParsePromptPattern(
  1095. const UnicodeString & Pattern, UnicodeString & Prompt, UnicodeString & Default, bool & Delimit)
  1096. {
  1097. int Pos = Pattern.SubString(3, Pattern.Length() - 2).Pos(L"?");
  1098. if (Pos > 0)
  1099. {
  1100. Default = Pattern.SubString(3 + Pos, Pattern.Length() - 3 - Pos);
  1101. if ((Pos > 1) && (Pattern[3 + Pos - 2] == L'\\'))
  1102. {
  1103. Delimit = false;
  1104. Pos--;
  1105. }
  1106. Prompt = Pattern.SubString(3, Pos - 1);
  1107. }
  1108. else
  1109. {
  1110. Prompt = Pattern.SubString(3, Pattern.Length() - 3);
  1111. }
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. bool __fastcall TInteractiveCustomCommand::PatternReplacement(int Index, const UnicodeString & Pattern,
  1115. UnicodeString & Replacement, bool & Delimit)
  1116. {
  1117. bool Result;
  1118. if (IsPromptPattern(Pattern))
  1119. {
  1120. UnicodeString PromptStr;
  1121. // The PromptStr and Replacement are actually never used
  1122. // as the only implementation (TWinInteractiveCustomCommand) uses
  1123. // prompts and defaults from PatternHint.
  1124. ParsePromptPattern(Pattern, PromptStr, Replacement, Delimit);
  1125. Prompt(Index, PromptStr, Replacement);
  1126. Result = true;
  1127. }
  1128. else if ((Pattern.Length() >= 3) && (Pattern[2] == L'`'))
  1129. {
  1130. UnicodeString Command = Pattern.SubString(3, Pattern.Length() - 3);
  1131. Command = FChildCustomCommand->Complete(Command, true);
  1132. Execute(Command, Replacement);
  1133. Delimit = false;
  1134. Result = true;
  1135. }
  1136. else
  1137. {
  1138. Result = false;
  1139. }
  1140. return Result;
  1141. }
  1142. //---------------------------------------------------------------------------
  1143. //---------------------------------------------------------------------------
  1144. __fastcall TCustomCommandData::TCustomCommandData()
  1145. {
  1146. Init(NULL);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. __fastcall TCustomCommandData::TCustomCommandData(TTerminal * Terminal)
  1150. {
  1151. // Should use FillSessionDataForCode as in TCustomScpExplorerForm::SessionDataForCode
  1152. Init(Terminal->SessionData, Terminal->UserName, Terminal->Password,
  1153. Terminal->GetSessionInfo().HostKeyFingerprintSHA256);
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. __fastcall TCustomCommandData::TCustomCommandData(TSessionData * SessionData)
  1157. {
  1158. Init(SessionData);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. __fastcall TCustomCommandData::TCustomCommandData(
  1162. TSessionData * SessionData, const UnicodeString & UserName, const UnicodeString & Password)
  1163. {
  1164. Init(SessionData, UserName, Password, UnicodeString());
  1165. }
  1166. //---------------------------------------------------------------------------
  1167. void __fastcall TCustomCommandData::Init(TSessionData * ASessionData)
  1168. {
  1169. FSessionData.reset(new TSessionData(L""));
  1170. if (ASessionData != NULL)
  1171. {
  1172. FSessionData->Assign(ASessionData);
  1173. }
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TCustomCommandData::Init(
  1177. TSessionData * ASessionData, const UnicodeString & AUserName,
  1178. const UnicodeString & APassword, const UnicodeString & AHostKey)
  1179. {
  1180. Init(ASessionData);
  1181. FSessionData->UserName = AUserName;
  1182. FSessionData->Password = APassword;
  1183. FSessionData->HostKey = AHostKey;
  1184. }
  1185. //---------------------------------------------------------------------------
  1186. void __fastcall TCustomCommandData::operator=(const TCustomCommandData & Data)
  1187. {
  1188. DebugAssert(Data.SessionData != NULL);
  1189. FSessionData.reset(new TSessionData(L""));
  1190. FSessionData->Assign(Data.SessionData);
  1191. }
  1192. //---------------------------------------------------------------------------
  1193. TSessionData * __fastcall TCustomCommandData::GetSesssionData() const
  1194. {
  1195. return FSessionData.get();
  1196. }
  1197. //---------------------------------------------------------------------------
  1198. //---------------------------------------------------------------------------
  1199. TFileCustomCommand::TFileCustomCommand()
  1200. {
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1204. const UnicodeString & Path)
  1205. {
  1206. FData = Data;
  1207. FPath = Path;
  1208. }
  1209. //---------------------------------------------------------------------------
  1210. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1211. const UnicodeString & Path, const UnicodeString & FileName,
  1212. const UnicodeString & FileList) :
  1213. TCustomCommand()
  1214. {
  1215. FData = Data;
  1216. FPath = Path;
  1217. FFileName = FileName;
  1218. FFileList = FileList;
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. int __fastcall TFileCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  1222. {
  1223. int Len;
  1224. wchar_t PatternCmd = (Index < Command.Length()) ? tolower(Command[Index + 1]) : L'\0';
  1225. switch (PatternCmd)
  1226. {
  1227. case L's':
  1228. case L'e':
  1229. case L'@':
  1230. case L'u':
  1231. case L'p':
  1232. case L'#':
  1233. case L'/':
  1234. case L'&':
  1235. case L'n':
  1236. Len = 2;
  1237. break;
  1238. default:
  1239. Len = 1;
  1240. break;
  1241. }
  1242. return Len;
  1243. }
  1244. //---------------------------------------------------------------------------
  1245. bool __fastcall TFileCustomCommand::PatternReplacement(
  1246. int /*Index*/, const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  1247. {
  1248. // keep consistent with TSessionLog::OpenLogFile
  1249. if (SameText(Pattern, L"!s"))
  1250. {
  1251. if (FData.SessionData != NULL)
  1252. {
  1253. Replacement = FData.SessionData->GenerateSessionUrl(sufSession);
  1254. }
  1255. }
  1256. else if (SameText(Pattern, L"!e"))
  1257. {
  1258. if (FData.SessionData != NULL)
  1259. {
  1260. Replacement = FData.SessionData->GenerateSessionUrl(sufComplete);
  1261. }
  1262. }
  1263. else if (Pattern == L"!@")
  1264. {
  1265. if (FData.SessionData != NULL)
  1266. {
  1267. Replacement = FData.SessionData->HostNameExpanded;
  1268. }
  1269. }
  1270. else if (SameText(Pattern, L"!u"))
  1271. {
  1272. if (FData.SessionData != NULL)
  1273. {
  1274. Replacement = FData.SessionData->UserName;
  1275. }
  1276. }
  1277. else if (SameText(Pattern, L"!p"))
  1278. {
  1279. if (FData.SessionData != NULL)
  1280. {
  1281. Replacement = NormalizeString(FData.SessionData->Password);
  1282. }
  1283. }
  1284. else if (SameText(Pattern, L"!#"))
  1285. {
  1286. if (FData.SessionData != NULL)
  1287. {
  1288. Replacement = IntToStr(FData.SessionData->PortNumber);
  1289. }
  1290. }
  1291. else if (Pattern == L"!/")
  1292. {
  1293. Replacement = UnixIncludeTrailingBackslash(FPath);
  1294. }
  1295. else if (Pattern == L"!&")
  1296. {
  1297. Replacement = FFileList;
  1298. // already delimited
  1299. Delimit = false;
  1300. }
  1301. else if (SameText(Pattern, L"!n"))
  1302. {
  1303. if (FData.SessionData != NULL)
  1304. {
  1305. Replacement = FData.SessionData->SessionName;
  1306. }
  1307. }
  1308. else
  1309. {
  1310. DebugAssert(Pattern.Length() == 1);
  1311. Replacement = FFileName;
  1312. }
  1313. return true;
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. void __fastcall TFileCustomCommand::Validate(const UnicodeString & Command)
  1317. {
  1318. int Found[2] = { 0, 0 };
  1319. CustomValidate(Command, &Found);
  1320. if ((Found[0] > 0) && (Found[1] > 0))
  1321. {
  1322. throw Exception(FMTLOAD(CUSTOM_COMMAND_FILELIST_ERROR,
  1323. (Found[1], Found[0])));
  1324. }
  1325. }
  1326. //---------------------------------------------------------------------------
  1327. void __fastcall TFileCustomCommand::ValidatePattern(const UnicodeString & Command,
  1328. int Index, int /*Len*/, wchar_t PatternCmd, void * Arg)
  1329. {
  1330. int * Found = static_cast<int *>(Arg);
  1331. DebugAssert(Index > 0);
  1332. if (PatternCmd == L'&')
  1333. {
  1334. Found[0] = Index;
  1335. }
  1336. else if ((PatternCmd != TEXT_TOKEN) && (PatternLen(Command, Index) == 1))
  1337. {
  1338. Found[1] = Index;
  1339. }
  1340. }
  1341. //---------------------------------------------------------------------------
  1342. bool __fastcall TFileCustomCommand::IsFileListCommand(const UnicodeString & Command)
  1343. {
  1344. return FindPattern(Command, L'&');
  1345. }
  1346. //---------------------------------------------------------------------------
  1347. bool __fastcall TFileCustomCommand::IsRemoteFileCommand(const UnicodeString & Command)
  1348. {
  1349. return FindPattern(Command, L'!') || FindPattern(Command, L'&');
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. bool __fastcall TFileCustomCommand::IsFileCommand(const UnicodeString & Command)
  1353. {
  1354. return IsRemoteFileCommand(Command);
  1355. }
  1356. //---------------------------------------------------------------------------
  1357. bool __fastcall TFileCustomCommand::IsSiteCommand(const UnicodeString & Command)
  1358. {
  1359. return FindPattern(Command, L'@') || FindPattern(Command, L'S') || FindPattern(Command, L'E');
  1360. }
  1361. //---------------------------------------------------------------------------
  1362. bool __fastcall TFileCustomCommand::IsSessionCommand(const UnicodeString & Command)
  1363. {
  1364. return
  1365. IsSiteCommand(Command) || IsPasswordCommand(Command) ||
  1366. FindPattern(Command, L'U') || FindPattern(Command, L'#') || FindPattern(Command, L'N') ||
  1367. FindPattern(Command, L'/');
  1368. }
  1369. //---------------------------------------------------------------------------
  1370. bool __fastcall TFileCustomCommand::IsPasswordCommand(const UnicodeString & Command)
  1371. {
  1372. return FindPattern(Command, L'p');
  1373. }
  1374. //---------------------------------------------------------------------------