FileMasks.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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. static UnicodeString FileMasksDelimiters = L";,";
  14. static UnicodeString AllFileMasksDelimiters = FileMasksDelimiters + IncludeExcludeFileMasksDelimiter;
  15. static UnicodeString DirectoryMaskDelimiters = L"/\\";
  16. static UnicodeString FileMasksDelimiterStr = UnicodeString(FileMasksDelimiters[1]) + L' ';
  17. //---------------------------------------------------------------------------
  18. __fastcall EFileMasksException::EFileMasksException(
  19. UnicodeString Message, int AErrorStart, int AErrorLen) :
  20. Exception(Message)
  21. {
  22. ErrorStart = AErrorStart;
  23. ErrorLen = AErrorLen;
  24. }
  25. //---------------------------------------------------------------------------
  26. UnicodeString __fastcall MaskFilePart(const UnicodeString Part, const UnicodeString Mask, bool& Masked)
  27. {
  28. UnicodeString Result;
  29. int RestStart = 1;
  30. bool Delim = false;
  31. for (int Index = 1; Index <= Mask.Length(); Index++)
  32. {
  33. switch (Mask[Index])
  34. {
  35. case L'\\':
  36. if (!Delim)
  37. {
  38. Delim = true;
  39. Masked = true;
  40. break;
  41. }
  42. case L'*':
  43. if (!Delim)
  44. {
  45. Result += Part.SubString(RestStart, Part.Length() - RestStart + 1);
  46. RestStart = Part.Length() + 1;
  47. Masked = true;
  48. break;
  49. }
  50. case L'?':
  51. if (!Delim)
  52. {
  53. if (RestStart <= Part.Length())
  54. {
  55. Result += Part[RestStart];
  56. RestStart++;
  57. }
  58. Masked = true;
  59. break;
  60. }
  61. default:
  62. Result += Mask[Index];
  63. RestStart++;
  64. Delim = false;
  65. break;
  66. }
  67. }
  68. return Result;
  69. }
  70. //---------------------------------------------------------------------------
  71. UnicodeString __fastcall MaskFileName(UnicodeString FileName, const UnicodeString Mask)
  72. {
  73. if (IsEffectiveFileNameMask(Mask))
  74. {
  75. bool Masked;
  76. int P = Mask.LastDelimiter(L".");
  77. if (P > 0)
  78. {
  79. int P2 = FileName.LastDelimiter(".");
  80. // only dot at beginning of file name is not considered as
  81. // name/ext separator
  82. UnicodeString FileExt = P2 > 1 ?
  83. FileName.SubString(P2 + 1, FileName.Length() - P2) : UnicodeString();
  84. FileExt = MaskFilePart(FileExt, Mask.SubString(P + 1, Mask.Length() - P), Masked);
  85. if (P2 > 1)
  86. {
  87. FileName.SetLength(P2 - 1);
  88. }
  89. FileName = MaskFilePart(FileName, Mask.SubString(1, P - 1), Masked);
  90. if (!FileExt.IsEmpty())
  91. {
  92. FileName += L"." + FileExt;
  93. }
  94. }
  95. else
  96. {
  97. FileName = MaskFilePart(FileName, Mask, Masked);
  98. }
  99. }
  100. return FileName;
  101. }
  102. //---------------------------------------------------------------------------
  103. bool __fastcall IsEffectiveFileNameMask(const UnicodeString & Mask)
  104. {
  105. return !Mask.IsEmpty() && (Mask != L"*") && (Mask != L"*.*");
  106. }
  107. //---------------------------------------------------------------------------
  108. UnicodeString __fastcall DelimitFileNameMask(UnicodeString Mask)
  109. {
  110. for (int i = 1; i <= Mask.Length(); i++)
  111. {
  112. if (wcschr(L"\\*?", Mask[i]) != NULL)
  113. {
  114. Mask.Insert(L"\\", i);
  115. i++;
  116. }
  117. }
  118. return Mask;
  119. }
  120. //---------------------------------------------------------------------------
  121. //---------------------------------------------------------------------------
  122. TFileMasks::TParams::TParams() :
  123. Size(0)
  124. {
  125. }
  126. //---------------------------------------------------------------------------
  127. UnicodeString TFileMasks::TParams::ToString() const
  128. {
  129. return UnicodeString(L"[") + IntToStr(Size) + L"/" + DateTimeToStr(Modification) + L"]";
  130. }
  131. //---------------------------------------------------------------------------
  132. //---------------------------------------------------------------------------
  133. bool __fastcall TFileMasks::IsMask(const UnicodeString Mask)
  134. {
  135. return (Mask.LastDelimiter(L"?*[") > 0);
  136. }
  137. //---------------------------------------------------------------------------
  138. bool __fastcall TFileMasks::IsAnyMask(const UnicodeString & Mask)
  139. {
  140. return Mask.IsEmpty() || (Mask == L"*.*") || (Mask == L"*");
  141. }
  142. //---------------------------------------------------------------------------
  143. UnicodeString __fastcall TFileMasks::NormalizeMask(const UnicodeString & Mask, const UnicodeString & AnyMask)
  144. {
  145. if (IsAnyMask(Mask))
  146. {
  147. return AnyMask;
  148. }
  149. else
  150. {
  151. return Mask;
  152. }
  153. }
  154. //---------------------------------------------------------------------------
  155. UnicodeString __fastcall TFileMasks::ComposeMaskStr(
  156. TStrings * MasksStr, bool Directory)
  157. {
  158. UnicodeString Result;
  159. UnicodeString ResultNoDirMask;
  160. for (int I = 0; I < MasksStr->Count; I++)
  161. {
  162. UnicodeString Str = MasksStr->Strings[I].Trim();
  163. if (!Str.IsEmpty())
  164. {
  165. for (int P = 1; P <= Str.Length(); P++)
  166. {
  167. if (Str.IsDelimiter(AllFileMasksDelimiters, P))
  168. {
  169. Str.Insert(Str[P], P);
  170. P++;
  171. }
  172. }
  173. UnicodeString StrNoDirMask;
  174. if (Directory)
  175. {
  176. StrNoDirMask = Str;
  177. Str = MakeDirectoryMask(Str);
  178. }
  179. else
  180. {
  181. while (Str.IsDelimiter(DirectoryMaskDelimiters, Str.Length()))
  182. {
  183. Str.SetLength(Str.Length() - 1);
  184. }
  185. StrNoDirMask = Str;
  186. }
  187. AddToList(Result, Str, FileMasksDelimiterStr);
  188. AddToList(ResultNoDirMask, StrNoDirMask, FileMasksDelimiterStr);
  189. }
  190. }
  191. // For directories, the above will add slash ay the end of masks,
  192. // breaking size and time masks and thus circumverting their validation.
  193. // This performes as hoc validation to cover the scenario.
  194. // For files this makes no difference, but no harm either
  195. TFileMasks Temp(Directory ? 1 : 0);
  196. Temp = ResultNoDirMask;
  197. return Result;
  198. }
  199. //---------------------------------------------------------------------------
  200. UnicodeString __fastcall TFileMasks::ComposeMaskStr(
  201. TStrings * IncludeFileMasksStr, TStrings * ExcludeFileMasksStr,
  202. TStrings * IncludeDirectoryMasksStr, TStrings * ExcludeDirectoryMasksStr)
  203. {
  204. UnicodeString IncludeMasks = ComposeMaskStr(IncludeFileMasksStr, false);
  205. AddToList(IncludeMasks, ComposeMaskStr(IncludeDirectoryMasksStr, true), FileMasksDelimiterStr);
  206. UnicodeString ExcludeMasks = ComposeMaskStr(ExcludeFileMasksStr, false);
  207. AddToList(ExcludeMasks, ComposeMaskStr(ExcludeDirectoryMasksStr, true), FileMasksDelimiterStr);
  208. UnicodeString Result = IncludeMasks;
  209. if (!ExcludeMasks.IsEmpty())
  210. {
  211. if (!Result.IsEmpty())
  212. {
  213. Result += L' ';
  214. }
  215. Result += UnicodeString(IncludeExcludeFileMasksDelimiter) + L' ' + ExcludeMasks;
  216. }
  217. return Result;
  218. }
  219. //---------------------------------------------------------------------------
  220. __fastcall TFileMasks::TFileMasks()
  221. {
  222. Init();
  223. }
  224. //---------------------------------------------------------------------------
  225. __fastcall TFileMasks::TFileMasks(int ForceDirectoryMasks)
  226. {
  227. Init();
  228. FForceDirectoryMasks = ForceDirectoryMasks;
  229. }
  230. //---------------------------------------------------------------------------
  231. __fastcall TFileMasks::TFileMasks(const TFileMasks & Source)
  232. {
  233. Init();
  234. FForceDirectoryMasks = Source.FForceDirectoryMasks;
  235. SetStr(Source.Masks, false);
  236. }
  237. //---------------------------------------------------------------------------
  238. __fastcall TFileMasks::TFileMasks(const UnicodeString & AMasks)
  239. {
  240. Init();
  241. SetStr(AMasks, false);
  242. }
  243. //---------------------------------------------------------------------------
  244. __fastcall TFileMasks::~TFileMasks()
  245. {
  246. Clear();
  247. }
  248. //---------------------------------------------------------------------------
  249. void __fastcall TFileMasks::Init()
  250. {
  251. FForceDirectoryMasks = -1;
  252. DoInit(false);
  253. }
  254. //---------------------------------------------------------------------------
  255. void __fastcall TFileMasks::DoInit(bool Delete)
  256. {
  257. for (int Index = 0; Index < 4; Index++)
  258. {
  259. if (Delete)
  260. {
  261. delete FMasksStr[Index];
  262. }
  263. FMasksStr[Index] = NULL;
  264. }
  265. }
  266. //---------------------------------------------------------------------------
  267. void __fastcall TFileMasks::Clear()
  268. {
  269. DoInit(true);
  270. for (int Index = 0; Index < 4; Index++)
  271. {
  272. Clear(FMasks[Index]);
  273. }
  274. }
  275. //---------------------------------------------------------------------------
  276. void __fastcall TFileMasks::Clear(TMasks & Masks)
  277. {
  278. TMasks::iterator I = Masks.begin();
  279. while (I != Masks.end())
  280. {
  281. ReleaseMaskMask((*I).FileNameMask);
  282. ReleaseMaskMask((*I).DirectoryMask);
  283. I++;
  284. }
  285. Masks.clear();
  286. }
  287. //---------------------------------------------------------------------------
  288. bool __fastcall TFileMasks::MatchesMasks(const UnicodeString FileName, bool Directory,
  289. const UnicodeString Path, const TParams * Params, const TMasks & Masks, bool Recurse)
  290. {
  291. bool Result = false;
  292. TMasks::const_iterator I = Masks.begin();
  293. while (!Result && (I != Masks.end()))
  294. {
  295. const TMask & Mask = *I;
  296. Result =
  297. MatchesMaskMask(Mask.DirectoryMask, Path) &&
  298. MatchesMaskMask(Mask.FileNameMask, FileName);
  299. if (Result)
  300. {
  301. bool HasSize = (Params != NULL);
  302. switch (Mask.HighSizeMask)
  303. {
  304. case TMask::None:
  305. Result = true;
  306. break;
  307. case TMask::Open:
  308. Result = HasSize && (Params->Size < Mask.HighSize);
  309. break;
  310. case TMask::Close:
  311. Result = HasSize && (Params->Size <= Mask.HighSize);
  312. break;
  313. }
  314. if (Result)
  315. {
  316. switch (Mask.LowSizeMask)
  317. {
  318. case TMask::None:
  319. Result = true;
  320. break;
  321. case TMask::Open:
  322. Result = HasSize && (Params->Size > Mask.LowSize);
  323. break;
  324. case TMask::Close:
  325. Result = HasSize && (Params->Size >= Mask.LowSize);
  326. break;
  327. }
  328. }
  329. bool HasModification = (Params != NULL);
  330. if (Result)
  331. {
  332. switch (Mask.HighModificationMask)
  333. {
  334. case TMask::None:
  335. Result = true;
  336. break;
  337. case TMask::Open:
  338. Result = HasModification && (Params->Modification < Mask.HighModification);
  339. break;
  340. case TMask::Close:
  341. Result = HasModification && (Params->Modification <= Mask.HighModification);
  342. break;
  343. }
  344. }
  345. if (Result)
  346. {
  347. switch (Mask.LowModificationMask)
  348. {
  349. case TMask::None:
  350. Result = true;
  351. break;
  352. case TMask::Open:
  353. Result = HasModification && (Params->Modification > Mask.LowModification);
  354. break;
  355. case TMask::Close:
  356. Result = HasModification && (Params->Modification >= Mask.LowModification);
  357. break;
  358. }
  359. }
  360. }
  361. I++;
  362. }
  363. if (!Result && Directory && !IsUnixRootPath(Path) && Recurse)
  364. {
  365. UnicodeString ParentFileName = UnixExtractFileName(Path);
  366. UnicodeString ParentPath = UnixExcludeTrailingBackslash(UnixExtractFilePath(Path));
  367. // Pass Params down or not?
  368. // Currently it includes Size/Time only, what is not used for directories.
  369. // So it depends of future use. Possibly we should make a copy
  370. // and pass on only relevant fields.
  371. Result = MatchesMasks(ParentFileName, true, ParentPath, Params, Masks, Recurse);
  372. }
  373. return Result;
  374. }
  375. //---------------------------------------------------------------------------
  376. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Directory,
  377. const UnicodeString Path, const TParams * Params) const
  378. {
  379. bool ImplicitMatch;
  380. return Matches(FileName, Directory, Path, Params, ImplicitMatch);
  381. }
  382. //---------------------------------------------------------------------------
  383. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Directory,
  384. const UnicodeString Path, const TParams * Params,
  385. bool & ImplicitMatch) const
  386. {
  387. bool ImplicitIncludeMatch = FMasks[MASK_INDEX(Directory, true)].empty();
  388. bool ExplicitIncludeMatch = MatchesMasks(FileName, Directory, Path, Params, FMasks[MASK_INDEX(Directory, true)], true);
  389. bool Result =
  390. (ImplicitIncludeMatch || ExplicitIncludeMatch) &&
  391. !MatchesMasks(FileName, Directory, Path, Params, FMasks[MASK_INDEX(Directory, false)], false);
  392. ImplicitMatch =
  393. Result && ImplicitIncludeMatch && !ExplicitIncludeMatch &&
  394. FMasks[MASK_INDEX(Directory, false)].empty();
  395. return Result;
  396. }
  397. //---------------------------------------------------------------------------
  398. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Local,
  399. bool Directory, const TParams * Params) const
  400. {
  401. bool ImplicitMatch;
  402. return Matches(FileName, Local, Directory, Params, ImplicitMatch);
  403. }
  404. //---------------------------------------------------------------------------
  405. bool __fastcall TFileMasks::Matches(const UnicodeString FileName, bool Local,
  406. bool Directory, const TParams * Params, bool & ImplicitMatch) const
  407. {
  408. bool Result;
  409. if (Local)
  410. {
  411. UnicodeString Path = ExtractFilePath(FileName);
  412. if (!Path.IsEmpty())
  413. {
  414. Path = ToUnixPath(ExcludeTrailingBackslash(Path));
  415. }
  416. Result = Matches(ExtractFileName(FileName), Directory, Path, Params,
  417. ImplicitMatch);
  418. }
  419. else
  420. {
  421. Result = Matches(UnixExtractFileName(FileName), Directory,
  422. UnixExcludeTrailingBackslash(UnixExtractFilePath(FileName)), Params,
  423. ImplicitMatch);
  424. }
  425. return Result;
  426. }
  427. //---------------------------------------------------------------------------
  428. bool __fastcall TFileMasks::operator ==(const TFileMasks & rhm) const
  429. {
  430. return (Masks == rhm.Masks);
  431. }
  432. //---------------------------------------------------------------------------
  433. TFileMasks & __fastcall TFileMasks::operator =(const UnicodeString & rhs)
  434. {
  435. Masks = rhs;
  436. return *this;
  437. }
  438. //---------------------------------------------------------------------------
  439. TFileMasks & __fastcall TFileMasks::operator =(const TFileMasks & rhm)
  440. {
  441. FForceDirectoryMasks = rhm.FForceDirectoryMasks;
  442. Masks = rhm.Masks;
  443. return *this;
  444. }
  445. //---------------------------------------------------------------------------
  446. bool __fastcall TFileMasks::operator ==(const UnicodeString & rhs) const
  447. {
  448. return (Masks == rhs);
  449. }
  450. //---------------------------------------------------------------------------
  451. void __fastcall TFileMasks::ThrowError(int Start, int End)
  452. {
  453. throw EFileMasksException(
  454. FMTLOAD(MASK_ERROR, (Masks.SubString(Start, End - Start + 1))),
  455. Start, End - Start + 1);
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall TFileMasks::CreateMaskMask(const UnicodeString & Mask, int Start, int End,
  459. bool Ex, TMaskMask & MaskMask)
  460. {
  461. try
  462. {
  463. assert(MaskMask.Mask == NULL);
  464. if (Ex && IsAnyMask(Mask))
  465. {
  466. MaskMask.Kind = TMaskMask::Any;
  467. MaskMask.Mask = NULL;
  468. }
  469. else
  470. {
  471. MaskMask.Kind = (Ex && (Mask == L"*.")) ? TMaskMask::NoExt : TMaskMask::Regular;
  472. MaskMask.Mask = new Masks::TMask(Mask);
  473. }
  474. }
  475. catch(...)
  476. {
  477. ThrowError(Start, End);
  478. }
  479. }
  480. //---------------------------------------------------------------------------
  481. UnicodeString __fastcall TFileMasks::MakeDirectoryMask(UnicodeString Str)
  482. {
  483. assert(!Str.IsEmpty());
  484. if (Str.IsEmpty() || !Str.IsDelimiter(DirectoryMaskDelimiters, Str.Length()))
  485. {
  486. int D = Str.LastDelimiter(DirectoryMaskDelimiters);
  487. // if there's any [back]slash anywhere in str,
  488. // add the same [back]slash at the end, otherwise add slash
  489. wchar_t Delimiter = (D > 0) ? Str[D] : DirectoryMaskDelimiters[1];
  490. Str += Delimiter;
  491. }
  492. return Str;
  493. }
  494. //---------------------------------------------------------------------------
  495. void __fastcall TFileMasks::CreateMask(
  496. const UnicodeString & MaskStr, int MaskStart, int /*MaskEnd*/, bool Include)
  497. {
  498. bool Directory = false; // shut up
  499. TMask Mask;
  500. Mask.MaskStr = MaskStr;
  501. Mask.UserStr = MaskStr;
  502. Mask.FileNameMask.Kind = TMaskMask::Any;
  503. Mask.FileNameMask.Mask = NULL;
  504. Mask.DirectoryMask.Kind = TMaskMask::Any;
  505. Mask.DirectoryMask.Mask = NULL;
  506. Mask.HighSizeMask = TMask::None;
  507. Mask.LowSizeMask = TMask::None;
  508. Mask.HighModificationMask = TMask::None;
  509. Mask.LowModificationMask = TMask::None;
  510. wchar_t NextPartDelimiter = L'\0';
  511. int NextPartFrom = 1;
  512. while (NextPartFrom <= MaskStr.Length())
  513. {
  514. wchar_t PartDelimiter = NextPartDelimiter;
  515. int PartFrom = NextPartFrom;
  516. UnicodeString PartStr = CopyToChars(MaskStr, NextPartFrom, L"<>", false, &NextPartDelimiter, true);
  517. int PartStart = MaskStart + PartFrom - 1;
  518. int PartEnd = MaskStart + NextPartFrom - 1 - 2;
  519. TrimEx(PartStr, PartStart, PartEnd);
  520. if (PartDelimiter != L'\0')
  521. {
  522. bool Low = (PartDelimiter == L'>');
  523. TMask::TMaskBoundary Boundary;
  524. if ((PartStr.Length() >= 1) && (PartStr[1] == L'='))
  525. {
  526. Boundary = TMask::Close;
  527. PartStr.Delete(1, 1);
  528. }
  529. else
  530. {
  531. Boundary = TMask::Open;
  532. }
  533. TFormatSettings FormatSettings = TFormatSettings::Create(GetDefaultLCID());
  534. FormatSettings.DateSeparator = L'-';
  535. FormatSettings.TimeSeparator = L':';
  536. FormatSettings.ShortDateFormat = "yyyy/mm/dd";
  537. FormatSettings.ShortTimeFormat = "hh:nn:ss";
  538. TDateTime Modification;
  539. if (TryStrToDateTime(PartStr, Modification, FormatSettings) ||
  540. TryRelativeStrToDateTime(PartStr, Modification))
  541. {
  542. TMask::TMaskBoundary & ModificationMask =
  543. (Low ? Mask.LowModificationMask : Mask.HighModificationMask);
  544. if ((ModificationMask != TMask::None) || Directory)
  545. {
  546. // include delimiter into size part
  547. ThrowError(PartStart - 1, PartEnd);
  548. }
  549. ModificationMask = Boundary;
  550. (Low ? Mask.LowModification : Mask.HighModification) = Modification;
  551. }
  552. else
  553. {
  554. TMask::TMaskBoundary & SizeMask = (Low ? Mask.LowSizeMask : Mask.HighSizeMask);
  555. __int64 & Size = (Low ? Mask.LowSize : Mask.HighSize);
  556. if ((SizeMask != TMask::None) || Directory)
  557. {
  558. // include delimiter into size part
  559. ThrowError(PartStart - 1, PartEnd);
  560. }
  561. SizeMask = Boundary;
  562. Size = ParseSize(PartStr);
  563. }
  564. }
  565. else if (!PartStr.IsEmpty())
  566. {
  567. int D = PartStr.LastDelimiter(DirectoryMaskDelimiters);
  568. Directory = (D > 0) && (D == PartStr.Length());
  569. if (Directory)
  570. {
  571. do
  572. {
  573. PartStr.SetLength(PartStr.Length() - 1);
  574. Mask.UserStr.Delete(PartStart - MaskStart + D, 1);
  575. D--;
  576. }
  577. while (PartStr.IsDelimiter(DirectoryMaskDelimiters, PartStr.Length()));
  578. D = PartStr.LastDelimiter(DirectoryMaskDelimiters);
  579. if (FForceDirectoryMasks == 0)
  580. {
  581. Directory = false;
  582. Mask.MaskStr = Mask.UserStr;
  583. }
  584. }
  585. else if (FForceDirectoryMasks > 0)
  586. {
  587. Directory = true;
  588. Mask.MaskStr.Insert(DirectoryMaskDelimiters[1], PartStart - MaskStart + PartStr.Length());
  589. }
  590. if (D > 0)
  591. {
  592. // make sure sole "/" (root dir) is preserved as is
  593. CreateMaskMask(
  594. UnixExcludeTrailingBackslash(ToUnixPath(PartStr.SubString(1, D))),
  595. PartStart, PartStart + D - 1, false,
  596. Mask.DirectoryMask);
  597. CreateMaskMask(
  598. PartStr.SubString(D + 1, PartStr.Length() - D),
  599. PartStart + D, PartEnd, true,
  600. Mask.FileNameMask);
  601. }
  602. else
  603. {
  604. CreateMaskMask(PartStr, PartStart, PartEnd, true, Mask.FileNameMask);
  605. }
  606. }
  607. }
  608. FMasks[MASK_INDEX(Directory, Include)].push_back(Mask);
  609. }
  610. //---------------------------------------------------------------------------
  611. TStrings * __fastcall TFileMasks::GetMasksStr(int Index) const
  612. {
  613. if (FMasksStr[Index] == NULL)
  614. {
  615. FMasksStr[Index] = new TStringList();
  616. TMasks::const_iterator I = FMasks[Index].begin();
  617. while (I != FMasks[Index].end())
  618. {
  619. FMasksStr[Index]->Add((*I).UserStr);
  620. I++;
  621. }
  622. }
  623. return FMasksStr[Index];
  624. }
  625. //---------------------------------------------------------------------------
  626. void __fastcall TFileMasks::ReleaseMaskMask(TMaskMask & MaskMask)
  627. {
  628. delete MaskMask.Mask;
  629. }
  630. //---------------------------------------------------------------------------
  631. void __fastcall TFileMasks::TrimEx(UnicodeString & Str, int & Start, int & End)
  632. {
  633. UnicodeString Buf = TrimLeft(Str);
  634. Start += Str.Length() - Buf.Length();
  635. Str = TrimRight(Buf);
  636. End -= Buf.Length() - Str.Length();
  637. }
  638. //---------------------------------------------------------------------------
  639. bool __fastcall TFileMasks::MatchesMaskMask(const TMaskMask & MaskMask, const UnicodeString & Str)
  640. {
  641. bool Result;
  642. if (MaskMask.Kind == TMaskMask::Any)
  643. {
  644. Result = true;
  645. }
  646. else if ((MaskMask.Kind == TMaskMask::NoExt) && (Str.Pos(L".") == 0))
  647. {
  648. Result = true;
  649. }
  650. else
  651. {
  652. Result = MaskMask.Mask->Matches(Str);
  653. }
  654. return Result;
  655. }
  656. //---------------------------------------------------------------------------
  657. void __fastcall TFileMasks::SetMasks(const UnicodeString value)
  658. {
  659. if (FStr != value)
  660. {
  661. SetStr(value, false);
  662. }
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TFileMasks::SetMask(const UnicodeString & Mask)
  666. {
  667. SetStr(Mask, true);
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall TFileMasks::SetStr(const UnicodeString Str, bool SingleMask)
  671. {
  672. UnicodeString Backup = FStr;
  673. try
  674. {
  675. FStr = Str;
  676. Clear();
  677. int NextMaskFrom = 1;
  678. bool Include = true;
  679. while (NextMaskFrom <= Str.Length())
  680. {
  681. int MaskStart = NextMaskFrom;
  682. wchar_t NextMaskDelimiter;
  683. UnicodeString MaskStr;
  684. if (SingleMask)
  685. {
  686. MaskStr = Str;
  687. NextMaskFrom = Str.Length() + 1;
  688. NextMaskDelimiter = L'\0';
  689. }
  690. else
  691. {
  692. MaskStr = CopyToChars(Str, NextMaskFrom, AllFileMasksDelimiters, false, &NextMaskDelimiter, true);
  693. }
  694. int MaskEnd = NextMaskFrom - 2;
  695. TrimEx(MaskStr, MaskStart, MaskEnd);
  696. if (!MaskStr.IsEmpty())
  697. {
  698. CreateMask(MaskStr, MaskStart, MaskEnd, Include);
  699. }
  700. if (NextMaskDelimiter == IncludeExcludeFileMasksDelimiter)
  701. {
  702. if (Include)
  703. {
  704. Include = false;
  705. }
  706. else
  707. {
  708. ThrowError(NextMaskFrom - 1, Str.Length());
  709. }
  710. }
  711. }
  712. }
  713. catch(...)
  714. {
  715. // this does not work correctly if previous mask was set using SetMask.
  716. // this should not fail (the mask was validated before),
  717. // otherwise we end in an infinite loop
  718. SetStr(Backup, false);
  719. throw;
  720. }
  721. }
  722. //---------------------------------------------------------------------------
  723. //---------------------------------------------------------------------------
  724. #define TEXT_TOKEN L'\255'
  725. //---------------------------------------------------------------------------
  726. const wchar_t TCustomCommand::NoQuote = L'\0';
  727. const UnicodeString TCustomCommand::Quotes = L"\"'";
  728. //---------------------------------------------------------------------------
  729. TCustomCommand::TCustomCommand()
  730. {
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TCustomCommand::GetToken(
  734. const UnicodeString & Command, int Index, int & Len, wchar_t & PatternCmd)
  735. {
  736. assert(Index <= Command.Length());
  737. const wchar_t * Ptr = Command.c_str() + Index - 1;
  738. if (Ptr[0] == L'!')
  739. {
  740. PatternCmd = Ptr[1];
  741. if (PatternCmd == L'\0')
  742. {
  743. Len = 1;
  744. }
  745. else if (PatternCmd == L'!')
  746. {
  747. Len = 2;
  748. }
  749. else
  750. {
  751. Len = PatternLen(Command, Index);
  752. }
  753. if (Len <= 0)
  754. {
  755. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNKNOWN, (PatternCmd, Index)));
  756. }
  757. else
  758. {
  759. if ((Command.Length() - Index + 1) < Len)
  760. {
  761. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (PatternCmd, Index)));
  762. }
  763. }
  764. }
  765. else
  766. {
  767. PatternCmd = TEXT_TOKEN;
  768. const wchar_t * NextPattern = wcschr(Ptr, L'!');
  769. if (NextPattern == NULL)
  770. {
  771. Len = Command.Length() - Index + 1;
  772. }
  773. else
  774. {
  775. Len = NextPattern - Ptr;
  776. }
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. UnicodeString __fastcall TCustomCommand::Complete(const UnicodeString & Command,
  781. bool LastPass)
  782. {
  783. UnicodeString Result;
  784. int Index = 1;
  785. while (Index <= Command.Length())
  786. {
  787. int Len;
  788. wchar_t PatternCmd;
  789. GetToken(Command, Index, Len, PatternCmd);
  790. if (PatternCmd == TEXT_TOKEN)
  791. {
  792. Result += Command.SubString(Index, Len);
  793. }
  794. else if (PatternCmd == L'!')
  795. {
  796. if (LastPass)
  797. {
  798. Result += L'!';
  799. }
  800. else
  801. {
  802. Result += Command.SubString(Index, Len);
  803. }
  804. }
  805. else
  806. {
  807. wchar_t Quote = NoQuote;
  808. if ((Index > 1) && (Index + Len - 1 < Command.Length()) &&
  809. Command.IsDelimiter(Quotes, Index - 1) &&
  810. Command.IsDelimiter(Quotes, Index + Len) &&
  811. (Command[Index - 1] == Command[Index + Len]))
  812. {
  813. Quote = Command[Index - 1];
  814. }
  815. UnicodeString Pattern = Command.SubString(Index, Len);
  816. UnicodeString Replacement;
  817. bool Delimit = true;
  818. if (PatternReplacement(Pattern, Replacement, Delimit))
  819. {
  820. if (!LastPass)
  821. {
  822. Replacement = ReplaceStr(Replacement, L"!", L"!!");
  823. }
  824. if (Delimit)
  825. {
  826. DelimitReplacement(Replacement, Quote);
  827. }
  828. Result += Replacement;
  829. }
  830. else
  831. {
  832. Result += Pattern;
  833. }
  834. }
  835. Index += Len;
  836. }
  837. return Result;
  838. }
  839. //---------------------------------------------------------------------------
  840. void __fastcall TCustomCommand::DelimitReplacement(UnicodeString & Replacement, wchar_t Quote)
  841. {
  842. Replacement = ShellDelimitStr(Replacement, Quote);
  843. }
  844. //---------------------------------------------------------------------------
  845. void __fastcall TCustomCommand::Validate(const UnicodeString & Command)
  846. {
  847. CustomValidate(Command, NULL);
  848. }
  849. //---------------------------------------------------------------------------
  850. void __fastcall TCustomCommand::CustomValidate(const UnicodeString & Command,
  851. void * Arg)
  852. {
  853. int Index = 1;
  854. while (Index <= Command.Length())
  855. {
  856. int Len;
  857. wchar_t PatternCmd;
  858. GetToken(Command, Index, Len, PatternCmd);
  859. ValidatePattern(Command, Index, Len, PatternCmd, Arg);
  860. Index += Len;
  861. }
  862. }
  863. //---------------------------------------------------------------------------
  864. bool __fastcall TCustomCommand::FindPattern(const UnicodeString & Command,
  865. wchar_t PatternCmd)
  866. {
  867. bool Result = false;
  868. int Index = 1;
  869. while (!Result && (Index <= Command.Length()))
  870. {
  871. int Len;
  872. wchar_t APatternCmd;
  873. GetToken(Command, Index, Len, APatternCmd);
  874. if (((PatternCmd != L'!') && (PatternCmd == APatternCmd)) ||
  875. ((PatternCmd == L'!') && (Len == 1) && (APatternCmd != TEXT_TOKEN)))
  876. {
  877. Result = true;
  878. }
  879. Index += Len;
  880. }
  881. return Result;
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomCommand::ValidatePattern(const UnicodeString & /*Command*/,
  885. int /*Index*/, int /*Len*/, wchar_t /*PatternCmd*/, void * /*Arg*/)
  886. {
  887. }
  888. //---------------------------------------------------------------------------
  889. //---------------------------------------------------------------------------
  890. TInteractiveCustomCommand::TInteractiveCustomCommand(
  891. TCustomCommand * ChildCustomCommand)
  892. {
  893. FChildCustomCommand = ChildCustomCommand;
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TInteractiveCustomCommand::Prompt(
  897. const UnicodeString & /*Prompt*/, UnicodeString & Value)
  898. {
  899. Value = L"";
  900. }
  901. //---------------------------------------------------------------------------
  902. void __fastcall TInteractiveCustomCommand::Execute(
  903. const UnicodeString & /*Command*/, UnicodeString & Value)
  904. {
  905. Value = L"";
  906. }
  907. //---------------------------------------------------------------------------
  908. int __fastcall TInteractiveCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  909. {
  910. int Len;
  911. switch (Command[Index + 1])
  912. {
  913. case L'?':
  914. {
  915. const wchar_t * Ptr = Command.c_str() + Index - 1;
  916. const wchar_t * PatternEnd = wcschr(Ptr + 1, L'!');
  917. if (PatternEnd == NULL)
  918. {
  919. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  920. }
  921. Len = PatternEnd - Ptr + 1;
  922. }
  923. break;
  924. case L'`':
  925. {
  926. const wchar_t * Ptr = Command.c_str() + Index - 1;
  927. const wchar_t * PatternEnd = wcschr(Ptr + 2, L'`');
  928. if (PatternEnd == NULL)
  929. {
  930. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  931. }
  932. Len = PatternEnd - Ptr + 1;
  933. }
  934. break;
  935. default:
  936. Len = FChildCustomCommand->PatternLen(Command, Index);
  937. break;
  938. }
  939. return Len;
  940. }
  941. //---------------------------------------------------------------------------
  942. bool __fastcall TInteractiveCustomCommand::PatternReplacement(const UnicodeString & Pattern,
  943. UnicodeString & Replacement, bool & Delimit)
  944. {
  945. bool Result;
  946. if ((Pattern.Length() >= 3) && (Pattern[2] == L'?'))
  947. {
  948. UnicodeString PromptStr;
  949. int Pos = Pattern.SubString(3, Pattern.Length() - 2).Pos(L"?");
  950. if (Pos > 0)
  951. {
  952. Replacement = Pattern.SubString(3 + Pos, Pattern.Length() - 3 - Pos);
  953. if ((Pos > 1) && (Pattern[3 + Pos - 2] == L'\\'))
  954. {
  955. Delimit = false;
  956. Pos--;
  957. }
  958. PromptStr = Pattern.SubString(3, Pos - 1);
  959. }
  960. else
  961. {
  962. PromptStr = Pattern.SubString(3, Pattern.Length() - 3);
  963. }
  964. Prompt(PromptStr, Replacement);
  965. Result = true;
  966. }
  967. else if ((Pattern.Length() >= 3) && (Pattern[2] == L'`'))
  968. {
  969. UnicodeString Command = Pattern.SubString(3, Pattern.Length() - 3);
  970. Command = FChildCustomCommand->Complete(Command, true);
  971. Execute(Command, Replacement);
  972. Delimit = false;
  973. Result = true;
  974. }
  975. else
  976. {
  977. Result = false;
  978. }
  979. return Result;
  980. }
  981. //---------------------------------------------------------------------------
  982. //---------------------------------------------------------------------------
  983. __fastcall TCustomCommandData::TCustomCommandData()
  984. {
  985. }
  986. //---------------------------------------------------------------------------
  987. __fastcall TCustomCommandData::TCustomCommandData(TTerminal * Terminal)
  988. {
  989. Init(Terminal->SessionData, Terminal->Password);
  990. }
  991. //---------------------------------------------------------------------------
  992. __fastcall TCustomCommandData::TCustomCommandData(
  993. TSessionData * SessionData, const UnicodeString & Password)
  994. {
  995. Init(SessionData, Password);
  996. }
  997. //---------------------------------------------------------------------------
  998. void __fastcall TCustomCommandData::Init(
  999. TSessionData * SessionData, const UnicodeString & APassword)
  1000. {
  1001. HostName = SessionData->HostNameExpanded;
  1002. UserName = SessionData->UserNameExpanded;
  1003. Password = APassword;
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. //---------------------------------------------------------------------------
  1007. TFileCustomCommand::TFileCustomCommand()
  1008. {
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1012. const UnicodeString & Path)
  1013. {
  1014. FData = Data;
  1015. FPath = Path;
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1019. const UnicodeString & Path, const UnicodeString & FileName,
  1020. const UnicodeString & FileList) :
  1021. TCustomCommand()
  1022. {
  1023. FData = Data;
  1024. FPath = Path;
  1025. FFileName = FileName;
  1026. FFileList = FileList;
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. int __fastcall TFileCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  1030. {
  1031. int Len;
  1032. switch (toupper(Command[Index + 1]))
  1033. {
  1034. case L'@':
  1035. case L'U':
  1036. case L'P':
  1037. case L'/':
  1038. case L'&':
  1039. Len = 2;
  1040. break;
  1041. default:
  1042. Len = 1;
  1043. break;
  1044. }
  1045. return Len;
  1046. }
  1047. //---------------------------------------------------------------------------
  1048. bool __fastcall TFileCustomCommand::PatternReplacement(
  1049. const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  1050. {
  1051. // keep consistent with TSessionLog::OpenLogFile
  1052. if (Pattern == L"!@")
  1053. {
  1054. Replacement = FData.HostName;
  1055. }
  1056. else if (AnsiSameText(Pattern, L"!u"))
  1057. {
  1058. Replacement = FData.UserName;
  1059. }
  1060. else if (AnsiSameText(Pattern, L"!p"))
  1061. {
  1062. Replacement = FData.Password;
  1063. }
  1064. else if (Pattern == L"!/")
  1065. {
  1066. Replacement = UnixIncludeTrailingBackslash(FPath);
  1067. }
  1068. else if (Pattern == L"!&")
  1069. {
  1070. Replacement = FFileList;
  1071. // already delimited
  1072. Delimit = false;
  1073. }
  1074. else
  1075. {
  1076. assert(Pattern.Length() == 1);
  1077. Replacement = FFileName;
  1078. }
  1079. return true;
  1080. }
  1081. //---------------------------------------------------------------------------
  1082. void __fastcall TFileCustomCommand::Validate(const UnicodeString & Command)
  1083. {
  1084. int Found[2] = { 0, 0 };
  1085. CustomValidate(Command, &Found);
  1086. if ((Found[0] > 0) && (Found[1] > 0))
  1087. {
  1088. throw Exception(FMTLOAD(CUSTOM_COMMAND_FILELIST_ERROR,
  1089. (Found[1], Found[0])));
  1090. }
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. void __fastcall TFileCustomCommand::ValidatePattern(const UnicodeString & Command,
  1094. int Index, int /*Len*/, wchar_t PatternCmd, void * Arg)
  1095. {
  1096. int * Found = static_cast<int *>(Arg);
  1097. assert(Index > 0);
  1098. if (PatternCmd == L'&')
  1099. {
  1100. Found[0] = Index;
  1101. }
  1102. else if ((PatternCmd != TEXT_TOKEN) && (PatternLen(Command, Index) == 1))
  1103. {
  1104. Found[1] = Index;
  1105. }
  1106. }
  1107. //---------------------------------------------------------------------------
  1108. bool __fastcall TFileCustomCommand::IsFileListCommand(const UnicodeString & Command)
  1109. {
  1110. return FindPattern(Command, L'&');
  1111. }
  1112. //---------------------------------------------------------------------------
  1113. bool __fastcall TFileCustomCommand::IsFileCommand(const UnicodeString & Command)
  1114. {
  1115. return FindPattern(Command, L'!') || FindPattern(Command, L'&');
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. bool __fastcall TFileCustomCommand::IsSiteCommand(const UnicodeString & Command)
  1119. {
  1120. return FindPattern(Command, L'@');
  1121. }
  1122. //---------------------------------------------------------------------------
  1123. bool __fastcall TFileCustomCommand::IsPasswordCommand(const UnicodeString & Command)
  1124. {
  1125. return FindPattern(Command, L'p');
  1126. }
  1127. //---------------------------------------------------------------------------