FileMasks.cpp 41 KB

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