FileMasks.cpp 41 KB

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