FileMasks.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. TFormatSettings FormatSettings = TFormatSettings::Create(GetDefaultLCID());
  540. FormatSettings.DateSeparator = L'-';
  541. FormatSettings.TimeSeparator = L':';
  542. FormatSettings.ShortDateFormat = "yyyy/mm/dd";
  543. FormatSettings.ShortTimeFormat = "hh:nn:ss";
  544. TDateTime Modification;
  545. __int64 DummySize;
  546. if ((!TryStrToInt64(PartStr, DummySize) && TryStrToDateTime(PartStr, Modification, FormatSettings)) ||
  547. TryRelativeStrToDateTime(PartStr, Modification, false))
  548. {
  549. TMask::TMaskBoundary & ModificationMask =
  550. (Low ? Mask.LowModificationMask : Mask.HighModificationMask);
  551. if ((ModificationMask != TMask::None) || Directory)
  552. {
  553. // include delimiter into size part
  554. ThrowError(PartStart - 1, PartEnd);
  555. }
  556. ModificationMask = Boundary;
  557. (Low ? Mask.LowModification : Mask.HighModification) = Modification;
  558. }
  559. else
  560. {
  561. TMask::TMaskBoundary & SizeMask = (Low ? Mask.LowSizeMask : Mask.HighSizeMask);
  562. __int64 & Size = (Low ? Mask.LowSize : Mask.HighSize);
  563. if ((SizeMask != TMask::None) || Directory)
  564. {
  565. // include delimiter into size part
  566. ThrowError(PartStart - 1, PartEnd);
  567. }
  568. SizeMask = Boundary;
  569. if (!TryStrToSize(PartStr, Size))
  570. {
  571. ThrowError(PartStart, PartEnd);
  572. }
  573. }
  574. }
  575. else if (!PartStr.IsEmpty())
  576. {
  577. int D = PartStr.LastDelimiter(DirectoryMaskDelimiters);
  578. Directory = (D > 0) && (D == PartStr.Length());
  579. if (Directory)
  580. {
  581. do
  582. {
  583. PartStr.SetLength(PartStr.Length() - 1);
  584. Mask.UserStr.Delete(PartStart - MaskStart + D, 1);
  585. D--;
  586. }
  587. while (PartStr.IsDelimiter(DirectoryMaskDelimiters, PartStr.Length()));
  588. D = PartStr.LastDelimiter(DirectoryMaskDelimiters);
  589. if (FForceDirectoryMasks == 0)
  590. {
  591. Directory = false;
  592. Mask.MaskStr = Mask.UserStr;
  593. }
  594. }
  595. else if (FForceDirectoryMasks > 0)
  596. {
  597. Directory = true;
  598. Mask.MaskStr.Insert(DirectoryMaskDelimiters[1], PartStart - MaskStart + PartStr.Length());
  599. }
  600. if (D > 0)
  601. {
  602. // make sure sole "/" (root dir) is preserved as is
  603. CreateMaskMask(
  604. SimpleUnixExcludeTrailingBackslash(ToUnixPath(PartStr.SubString(1, D))),
  605. PartStart, PartStart + D - 1, false,
  606. Mask.DirectoryMask);
  607. CreateMaskMask(
  608. PartStr.SubString(D + 1, PartStr.Length() - D),
  609. PartStart + D, PartEnd, true,
  610. Mask.FileNameMask);
  611. }
  612. else
  613. {
  614. CreateMaskMask(PartStr, PartStart, PartEnd, true, Mask.FileNameMask);
  615. }
  616. }
  617. }
  618. FMasks[MASK_INDEX(Directory, Include)].push_back(Mask);
  619. }
  620. //---------------------------------------------------------------------------
  621. TStrings * __fastcall TFileMasks::GetMasksStr(int Index) const
  622. {
  623. if (FMasksStr[Index] == NULL)
  624. {
  625. FMasksStr[Index] = new TStringList();
  626. TMasks::const_iterator I = FMasks[Index].begin();
  627. while (I != FMasks[Index].end())
  628. {
  629. FMasksStr[Index]->Add((*I).UserStr);
  630. I++;
  631. }
  632. }
  633. return FMasksStr[Index];
  634. }
  635. //---------------------------------------------------------------------------
  636. void __fastcall TFileMasks::ReleaseMaskMask(TMaskMask & MaskMask)
  637. {
  638. delete MaskMask.Mask;
  639. }
  640. //---------------------------------------------------------------------------
  641. void __fastcall TFileMasks::TrimEx(UnicodeString & Str, int & Start, int & End)
  642. {
  643. UnicodeString Buf = TrimLeft(Str);
  644. Start += Str.Length() - Buf.Length();
  645. Str = TrimRight(Buf);
  646. End -= Buf.Length() - Str.Length();
  647. }
  648. //---------------------------------------------------------------------------
  649. bool __fastcall TFileMasks::MatchesMaskMask(const TMaskMask & MaskMask, const UnicodeString & Str)
  650. {
  651. bool Result;
  652. if (MaskMask.Kind == TMaskMask::Any)
  653. {
  654. Result = true;
  655. }
  656. else if ((MaskMask.Kind == TMaskMask::NoExt) && (Str.Pos(L".") == 0))
  657. {
  658. Result = true;
  659. }
  660. else
  661. {
  662. Result = MaskMask.Mask->Matches(Str);
  663. }
  664. return Result;
  665. }
  666. //---------------------------------------------------------------------------
  667. void __fastcall TFileMasks::SetMasks(const UnicodeString value)
  668. {
  669. if (FStr != value)
  670. {
  671. SetStr(value, false);
  672. }
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TFileMasks::SetMask(const UnicodeString & Mask)
  676. {
  677. SetStr(Mask, true);
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TFileMasks::SetStr(const UnicodeString Str, bool SingleMask)
  681. {
  682. UnicodeString Backup = FStr;
  683. try
  684. {
  685. FStr = Str;
  686. Clear();
  687. int NextMaskFrom = 1;
  688. bool Include = true;
  689. while (NextMaskFrom <= Str.Length())
  690. {
  691. int MaskStart = NextMaskFrom;
  692. wchar_t NextMaskDelimiter;
  693. UnicodeString MaskStr;
  694. if (SingleMask)
  695. {
  696. MaskStr = Str;
  697. NextMaskFrom = Str.Length() + 1;
  698. NextMaskDelimiter = L'\0';
  699. }
  700. else
  701. {
  702. MaskStr = CopyToChars(Str, NextMaskFrom, AllFileMasksDelimiters, false, &NextMaskDelimiter, true);
  703. }
  704. int MaskEnd = NextMaskFrom - 2;
  705. TrimEx(MaskStr, MaskStart, MaskEnd);
  706. if (!MaskStr.IsEmpty())
  707. {
  708. CreateMask(MaskStr, MaskStart, MaskEnd, Include);
  709. }
  710. if (NextMaskDelimiter == IncludeExcludeFileMasksDelimiter)
  711. {
  712. if (Include)
  713. {
  714. Include = false;
  715. }
  716. else
  717. {
  718. ThrowError(NextMaskFrom - 1, Str.Length());
  719. }
  720. }
  721. }
  722. }
  723. catch(...)
  724. {
  725. // this does not work correctly if previous mask was set using SetMask.
  726. // this should not fail (the mask was validated before),
  727. // otherwise we end in an infinite loop
  728. SetStr(Backup, false);
  729. throw;
  730. }
  731. }
  732. //---------------------------------------------------------------------------
  733. //---------------------------------------------------------------------------
  734. #define TEXT_TOKEN L'\255'
  735. //---------------------------------------------------------------------------
  736. const wchar_t TCustomCommand::NoQuote = L'\0';
  737. const UnicodeString TCustomCommand::Quotes = L"\"'";
  738. //---------------------------------------------------------------------------
  739. UnicodeString __fastcall TCustomCommand::Escape(const UnicodeString & S)
  740. {
  741. return ReplaceStr(S, L"!", L"!!");
  742. }
  743. //---------------------------------------------------------------------------
  744. TCustomCommand::TCustomCommand()
  745. {
  746. }
  747. //---------------------------------------------------------------------------
  748. void __fastcall TCustomCommand::GetToken(
  749. const UnicodeString & Command, int Index, int & Len, wchar_t & PatternCmd)
  750. {
  751. DebugAssert(Index <= Command.Length());
  752. const wchar_t * Ptr = Command.c_str() + Index - 1;
  753. if (Ptr[0] == L'!')
  754. {
  755. PatternCmd = Ptr[1];
  756. if (PatternCmd == L'\0')
  757. {
  758. Len = 1;
  759. }
  760. else if (PatternCmd == L'!')
  761. {
  762. Len = 2;
  763. }
  764. else
  765. {
  766. Len = PatternLen(Command, Index);
  767. }
  768. if (Len <= 0)
  769. {
  770. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNKNOWN, (PatternCmd, Index)));
  771. }
  772. else
  773. {
  774. if ((Command.Length() - Index + 1) < Len)
  775. {
  776. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (PatternCmd, Index)));
  777. }
  778. }
  779. }
  780. else
  781. {
  782. PatternCmd = TEXT_TOKEN;
  783. const wchar_t * NextPattern = wcschr(Ptr, L'!');
  784. if (NextPattern == NULL)
  785. {
  786. Len = Command.Length() - Index + 1;
  787. }
  788. else
  789. {
  790. Len = NextPattern - Ptr;
  791. }
  792. }
  793. }
  794. //---------------------------------------------------------------------------
  795. void __fastcall TCustomCommand::PatternHint(int /*Index*/, const UnicodeString & /*Pattern*/)
  796. {
  797. // noop
  798. }
  799. //---------------------------------------------------------------------------
  800. UnicodeString __fastcall TCustomCommand::Complete(const UnicodeString & Command,
  801. bool LastPass)
  802. {
  803. int Index = 1;
  804. int PatternIndex = 0;
  805. while (Index <= Command.Length())
  806. {
  807. int Len;
  808. wchar_t PatternCmd;
  809. GetToken(Command, Index, Len, PatternCmd);
  810. if (PatternCmd == TEXT_TOKEN)
  811. {
  812. }
  813. else if (PatternCmd == L'!')
  814. {
  815. }
  816. else
  817. {
  818. UnicodeString Pattern = Command.SubString(Index, Len);
  819. PatternHint(PatternIndex, Pattern);
  820. PatternIndex++;
  821. }
  822. Index += Len;
  823. }
  824. UnicodeString Result;
  825. Index = 1;
  826. PatternIndex = 0;
  827. while (Index <= Command.Length())
  828. {
  829. int Len;
  830. wchar_t PatternCmd;
  831. GetToken(Command, Index, Len, PatternCmd);
  832. if (PatternCmd == TEXT_TOKEN)
  833. {
  834. Result += Command.SubString(Index, Len);
  835. }
  836. else if (PatternCmd == L'!')
  837. {
  838. if (LastPass)
  839. {
  840. Result += L'!';
  841. }
  842. else
  843. {
  844. Result += Command.SubString(Index, Len);
  845. }
  846. }
  847. else
  848. {
  849. wchar_t Quote = NoQuote;
  850. if ((Index > 1) && (Index + Len - 1 < Command.Length()) &&
  851. Command.IsDelimiter(Quotes, Index - 1) &&
  852. Command.IsDelimiter(Quotes, Index + Len) &&
  853. (Command[Index - 1] == Command[Index + Len]))
  854. {
  855. Quote = Command[Index - 1];
  856. }
  857. UnicodeString Pattern = Command.SubString(Index, Len);
  858. UnicodeString Replacement;
  859. bool Delimit = true;
  860. if (PatternReplacement(PatternIndex, Pattern, Replacement, Delimit))
  861. {
  862. if (!LastPass)
  863. {
  864. Replacement = Escape(Replacement);
  865. }
  866. if (Delimit)
  867. {
  868. DelimitReplacement(Replacement, Quote);
  869. }
  870. Result += Replacement;
  871. }
  872. else
  873. {
  874. Result += Pattern;
  875. }
  876. PatternIndex++;
  877. }
  878. Index += Len;
  879. }
  880. return Result;
  881. }
  882. //---------------------------------------------------------------------------
  883. void __fastcall TCustomCommand::DelimitReplacement(UnicodeString & Replacement, wchar_t Quote)
  884. {
  885. Replacement = ShellDelimitStr(Replacement, Quote);
  886. }
  887. //---------------------------------------------------------------------------
  888. void __fastcall TCustomCommand::Validate(const UnicodeString & Command)
  889. {
  890. CustomValidate(Command, NULL);
  891. }
  892. //---------------------------------------------------------------------------
  893. void __fastcall TCustomCommand::CustomValidate(const UnicodeString & Command,
  894. void * Arg)
  895. {
  896. int Index = 1;
  897. while (Index <= Command.Length())
  898. {
  899. int Len;
  900. wchar_t PatternCmd;
  901. GetToken(Command, Index, Len, PatternCmd);
  902. ValidatePattern(Command, Index, Len, PatternCmd, Arg);
  903. Index += Len;
  904. }
  905. }
  906. //---------------------------------------------------------------------------
  907. bool __fastcall TCustomCommand::FindPattern(const UnicodeString & Command,
  908. wchar_t PatternCmd)
  909. {
  910. bool Result = false;
  911. int Index = 1;
  912. while (!Result && (Index <= Command.Length()))
  913. {
  914. int Len;
  915. wchar_t APatternCmd;
  916. GetToken(Command, Index, Len, APatternCmd);
  917. if (((PatternCmd != L'!') && (tolower(PatternCmd) == tolower(APatternCmd))) ||
  918. ((PatternCmd == L'!') && (Len == 1) && (APatternCmd != TEXT_TOKEN)) ||
  919. ((PatternCmd == L'\0') && (APatternCmd != TEXT_TOKEN)))
  920. {
  921. Result = true;
  922. }
  923. Index += Len;
  924. }
  925. return Result;
  926. }
  927. //---------------------------------------------------------------------------
  928. bool __fastcall TCustomCommand::HasAnyPatterns(const UnicodeString & Command)
  929. {
  930. return FindPattern(Command, L'\0');
  931. }
  932. //---------------------------------------------------------------------------
  933. void __fastcall TCustomCommand::ValidatePattern(const UnicodeString & /*Command*/,
  934. int /*Index*/, int /*Len*/, wchar_t /*PatternCmd*/, void * /*Arg*/)
  935. {
  936. }
  937. //---------------------------------------------------------------------------
  938. //---------------------------------------------------------------------------
  939. TInteractiveCustomCommand::TInteractiveCustomCommand(
  940. TCustomCommand * ChildCustomCommand)
  941. {
  942. FChildCustomCommand = ChildCustomCommand;
  943. }
  944. //---------------------------------------------------------------------------
  945. void __fastcall TInteractiveCustomCommand::Prompt(
  946. int /*Index*/, const UnicodeString & /*Prompt*/, UnicodeString & Value)
  947. {
  948. Value = L"";
  949. }
  950. //---------------------------------------------------------------------------
  951. void __fastcall TInteractiveCustomCommand::Execute(
  952. const UnicodeString & /*Command*/, UnicodeString & Value)
  953. {
  954. Value = L"";
  955. }
  956. //---------------------------------------------------------------------------
  957. int __fastcall TInteractiveCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  958. {
  959. int Len;
  960. wchar_t PatternCmd = (Index < Command.Length()) ? Command[Index + 1] : L'\0';
  961. switch (PatternCmd)
  962. {
  963. case L'?':
  964. {
  965. const wchar_t * Ptr = Command.c_str() + Index - 1;
  966. const wchar_t * PatternEnd = wcschr(Ptr + 1, L'!');
  967. if (PatternEnd == NULL)
  968. {
  969. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  970. }
  971. Len = PatternEnd - Ptr + 1;
  972. }
  973. break;
  974. case L'`':
  975. {
  976. const wchar_t * Ptr = Command.c_str() + Index - 1;
  977. const wchar_t * PatternEnd = wcschr(Ptr + 2, L'`');
  978. if (PatternEnd == NULL)
  979. {
  980. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  981. }
  982. Len = PatternEnd - Ptr + 1;
  983. }
  984. break;
  985. default:
  986. Len = FChildCustomCommand->PatternLen(Command, Index);
  987. break;
  988. }
  989. return Len;
  990. }
  991. //---------------------------------------------------------------------------
  992. bool __fastcall TInteractiveCustomCommand::IsPromptPattern(const UnicodeString & Pattern)
  993. {
  994. return (Pattern.Length() >= 3) && (Pattern[2] == L'?');
  995. }
  996. //---------------------------------------------------------------------------
  997. void __fastcall TInteractiveCustomCommand::ParsePromptPattern(
  998. const UnicodeString & Pattern, UnicodeString & Prompt, UnicodeString & Default, bool & Delimit)
  999. {
  1000. int Pos = Pattern.SubString(3, Pattern.Length() - 2).Pos(L"?");
  1001. if (Pos > 0)
  1002. {
  1003. Default = Pattern.SubString(3 + Pos, Pattern.Length() - 3 - Pos);
  1004. if ((Pos > 1) && (Pattern[3 + Pos - 2] == L'\\'))
  1005. {
  1006. Delimit = false;
  1007. Pos--;
  1008. }
  1009. Prompt = Pattern.SubString(3, Pos - 1);
  1010. }
  1011. else
  1012. {
  1013. Prompt = Pattern.SubString(3, Pattern.Length() - 3);
  1014. }
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. bool __fastcall TInteractiveCustomCommand::PatternReplacement(int Index, const UnicodeString & Pattern,
  1018. UnicodeString & Replacement, bool & Delimit)
  1019. {
  1020. bool Result;
  1021. if (IsPromptPattern(Pattern))
  1022. {
  1023. UnicodeString PromptStr;
  1024. // The PromptStr and Replacement are actually never used
  1025. // as the only implementation (TWinInteractiveCustomCommand) uses
  1026. // prompts and defaults from PatternHint.
  1027. ParsePromptPattern(Pattern, PromptStr, Replacement, Delimit);
  1028. Prompt(Index, PromptStr, Replacement);
  1029. Result = true;
  1030. }
  1031. else if ((Pattern.Length() >= 3) && (Pattern[2] == L'`'))
  1032. {
  1033. UnicodeString Command = Pattern.SubString(3, Pattern.Length() - 3);
  1034. Command = FChildCustomCommand->Complete(Command, true);
  1035. Execute(Command, Replacement);
  1036. Delimit = false;
  1037. Result = true;
  1038. }
  1039. else
  1040. {
  1041. Result = false;
  1042. }
  1043. return Result;
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. //---------------------------------------------------------------------------
  1047. __fastcall TCustomCommandData::TCustomCommandData()
  1048. {
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. __fastcall TCustomCommandData::TCustomCommandData(TTerminal * Terminal)
  1052. {
  1053. Init(Terminal->SessionData, Terminal->UserName, Terminal->Password,
  1054. Terminal->GetSessionInfo().HostKeyFingerprint);
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. __fastcall TCustomCommandData::TCustomCommandData(
  1058. TSessionData * SessionData, const UnicodeString & UserName, const UnicodeString & Password)
  1059. {
  1060. Init(SessionData, UserName, Password, UnicodeString());
  1061. }
  1062. //---------------------------------------------------------------------------
  1063. void __fastcall TCustomCommandData::Init(
  1064. TSessionData * ASessionData, const UnicodeString & AUserName,
  1065. const UnicodeString & APassword, const UnicodeString & AHostKey)
  1066. {
  1067. FSessionData.reset(new TSessionData(L""));
  1068. FSessionData->Assign(ASessionData);
  1069. FSessionData->UserName = AUserName;
  1070. FSessionData->Password = APassword;
  1071. FSessionData->HostKey = AHostKey;
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. void __fastcall TCustomCommandData::operator=(const TCustomCommandData & Data)
  1075. {
  1076. DebugAssert(Data.SessionData != NULL);
  1077. FSessionData.reset(new TSessionData(L""));
  1078. FSessionData->Assign(Data.SessionData);
  1079. }
  1080. //---------------------------------------------------------------------------
  1081. TSessionData * __fastcall TCustomCommandData::GetSesssionData() const
  1082. {
  1083. return FSessionData.get();
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. //---------------------------------------------------------------------------
  1087. TFileCustomCommand::TFileCustomCommand()
  1088. {
  1089. }
  1090. //---------------------------------------------------------------------------
  1091. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1092. const UnicodeString & Path)
  1093. {
  1094. FData = Data;
  1095. FPath = Path;
  1096. }
  1097. //---------------------------------------------------------------------------
  1098. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1099. const UnicodeString & Path, const UnicodeString & FileName,
  1100. const UnicodeString & FileList) :
  1101. TCustomCommand()
  1102. {
  1103. FData = Data;
  1104. FPath = Path;
  1105. FFileName = FileName;
  1106. FFileList = FileList;
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. int __fastcall TFileCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  1110. {
  1111. int Len;
  1112. wchar_t PatternCmd = (Index < Command.Length()) ? tolower(Command[Index + 1]) : L'\0';
  1113. switch (PatternCmd)
  1114. {
  1115. case L's':
  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(sufComplete);
  1141. }
  1142. }
  1143. else if (Pattern == L"!@")
  1144. {
  1145. if (FData.SessionData != NULL)
  1146. {
  1147. Replacement = FData.SessionData->HostNameExpanded;
  1148. }
  1149. }
  1150. else if (SameText(Pattern, L"!u"))
  1151. {
  1152. if (FData.SessionData != NULL)
  1153. {
  1154. Replacement = FData.SessionData->UserName;
  1155. }
  1156. }
  1157. else if (SameText(Pattern, L"!p"))
  1158. {
  1159. if (FData.SessionData != NULL)
  1160. {
  1161. Replacement = FData.SessionData->Password;
  1162. }
  1163. }
  1164. else if (SameText(Pattern, L"!#"))
  1165. {
  1166. if (FData.SessionData != NULL)
  1167. {
  1168. Replacement = IntToStr(FData.SessionData->PortNumber);
  1169. }
  1170. }
  1171. else if (Pattern == L"!/")
  1172. {
  1173. Replacement = UnixIncludeTrailingBackslash(FPath);
  1174. }
  1175. else if (Pattern == L"!&")
  1176. {
  1177. Replacement = FFileList;
  1178. // already delimited
  1179. Delimit = false;
  1180. }
  1181. else if (SameText(Pattern, L"!n"))
  1182. {
  1183. if (FData.SessionData != NULL)
  1184. {
  1185. Replacement = FData.SessionData->SessionName;
  1186. }
  1187. }
  1188. else
  1189. {
  1190. DebugAssert(Pattern.Length() == 1);
  1191. Replacement = FFileName;
  1192. }
  1193. return true;
  1194. }
  1195. //---------------------------------------------------------------------------
  1196. void __fastcall TFileCustomCommand::Validate(const UnicodeString & Command)
  1197. {
  1198. int Found[2] = { 0, 0 };
  1199. CustomValidate(Command, &Found);
  1200. if ((Found[0] > 0) && (Found[1] > 0))
  1201. {
  1202. throw Exception(FMTLOAD(CUSTOM_COMMAND_FILELIST_ERROR,
  1203. (Found[1], Found[0])));
  1204. }
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. void __fastcall TFileCustomCommand::ValidatePattern(const UnicodeString & Command,
  1208. int Index, int /*Len*/, wchar_t PatternCmd, void * Arg)
  1209. {
  1210. int * Found = static_cast<int *>(Arg);
  1211. DebugAssert(Index > 0);
  1212. if (PatternCmd == L'&')
  1213. {
  1214. Found[0] = Index;
  1215. }
  1216. else if ((PatternCmd != TEXT_TOKEN) && (PatternLen(Command, Index) == 1))
  1217. {
  1218. Found[1] = Index;
  1219. }
  1220. }
  1221. //---------------------------------------------------------------------------
  1222. bool __fastcall TFileCustomCommand::IsFileListCommand(const UnicodeString & Command)
  1223. {
  1224. return FindPattern(Command, L'&');
  1225. }
  1226. //---------------------------------------------------------------------------
  1227. bool __fastcall TFileCustomCommand::IsRemoteFileCommand(const UnicodeString & Command)
  1228. {
  1229. return FindPattern(Command, L'!') || FindPattern(Command, L'&');
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. bool __fastcall TFileCustomCommand::IsFileCommand(const UnicodeString & Command)
  1233. {
  1234. return IsRemoteFileCommand(Command);
  1235. }
  1236. //---------------------------------------------------------------------------
  1237. bool __fastcall TFileCustomCommand::IsSiteCommand(const UnicodeString & Command)
  1238. {
  1239. return FindPattern(Command, L'@');
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. bool __fastcall TFileCustomCommand::IsPasswordCommand(const UnicodeString & Command)
  1243. {
  1244. return FindPattern(Command, L'p');
  1245. }
  1246. //---------------------------------------------------------------------------