FileMasks.cpp 44 KB

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