1
0

FileMasks.cpp 39 KB

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