1
0

FileMasks.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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 = SimpleUnixExcludeTrailingBackslash(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. SimpleUnixExcludeTrailingBackslash(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. SimpleUnixExcludeTrailingBackslash(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. wchar_t PatternCmd = (Index < Command.Length()) ? Command[Index + 1] : L'\0';
  912. switch (PatternCmd)
  913. {
  914. case L'?':
  915. {
  916. const wchar_t * Ptr = Command.c_str() + Index - 1;
  917. const wchar_t * PatternEnd = wcschr(Ptr + 1, L'!');
  918. if (PatternEnd == NULL)
  919. {
  920. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  921. }
  922. Len = PatternEnd - Ptr + 1;
  923. }
  924. break;
  925. case L'`':
  926. {
  927. const wchar_t * Ptr = Command.c_str() + Index - 1;
  928. const wchar_t * PatternEnd = wcschr(Ptr + 2, L'`');
  929. if (PatternEnd == NULL)
  930. {
  931. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (Command[Index + 1], Index)));
  932. }
  933. Len = PatternEnd - Ptr + 1;
  934. }
  935. break;
  936. default:
  937. Len = FChildCustomCommand->PatternLen(Command, Index);
  938. break;
  939. }
  940. return Len;
  941. }
  942. //---------------------------------------------------------------------------
  943. bool __fastcall TInteractiveCustomCommand::PatternReplacement(const UnicodeString & Pattern,
  944. UnicodeString & Replacement, bool & Delimit)
  945. {
  946. bool Result;
  947. if ((Pattern.Length() >= 3) && (Pattern[2] == L'?'))
  948. {
  949. UnicodeString PromptStr;
  950. int Pos = Pattern.SubString(3, Pattern.Length() - 2).Pos(L"?");
  951. if (Pos > 0)
  952. {
  953. Replacement = Pattern.SubString(3 + Pos, Pattern.Length() - 3 - Pos);
  954. if ((Pos > 1) && (Pattern[3 + Pos - 2] == L'\\'))
  955. {
  956. Delimit = false;
  957. Pos--;
  958. }
  959. PromptStr = Pattern.SubString(3, Pos - 1);
  960. }
  961. else
  962. {
  963. PromptStr = Pattern.SubString(3, Pattern.Length() - 3);
  964. }
  965. Prompt(PromptStr, Replacement);
  966. Result = true;
  967. }
  968. else if ((Pattern.Length() >= 3) && (Pattern[2] == L'`'))
  969. {
  970. UnicodeString Command = Pattern.SubString(3, Pattern.Length() - 3);
  971. Command = FChildCustomCommand->Complete(Command, true);
  972. Execute(Command, Replacement);
  973. Delimit = false;
  974. Result = true;
  975. }
  976. else
  977. {
  978. Result = false;
  979. }
  980. return Result;
  981. }
  982. //---------------------------------------------------------------------------
  983. //---------------------------------------------------------------------------
  984. __fastcall TCustomCommandData::TCustomCommandData()
  985. {
  986. }
  987. //---------------------------------------------------------------------------
  988. __fastcall TCustomCommandData::TCustomCommandData(TTerminal * Terminal)
  989. {
  990. Init(Terminal->SessionData, Terminal->UserName, Terminal->Password,
  991. Terminal->GetSessionInfo().HostKeyFingerprint);
  992. }
  993. //---------------------------------------------------------------------------
  994. __fastcall TCustomCommandData::TCustomCommandData(
  995. TSessionData * SessionData, const UnicodeString & UserName, const UnicodeString & Password)
  996. {
  997. Init(SessionData, UserName, Password, UnicodeString());
  998. }
  999. //---------------------------------------------------------------------------
  1000. void __fastcall TCustomCommandData::Init(
  1001. TSessionData * ASessionData, const UnicodeString & AUserName,
  1002. const UnicodeString & APassword, const UnicodeString & AHostKey)
  1003. {
  1004. FSessionData.reset(new TSessionData(L""));
  1005. FSessionData->Assign(ASessionData);
  1006. FSessionData->UserName = AUserName;
  1007. FSessionData->Password = APassword;
  1008. FSessionData->HostKey = AHostKey;
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. void __fastcall TCustomCommandData::operator=(const TCustomCommandData & Data)
  1012. {
  1013. assert(Data.SessionData != NULL);
  1014. FSessionData.reset(new TSessionData(L""));
  1015. FSessionData->Assign(Data.SessionData);
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. TSessionData * __fastcall TCustomCommandData::GetSesssionData() const
  1019. {
  1020. return FSessionData.get();
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. //---------------------------------------------------------------------------
  1024. TFileCustomCommand::TFileCustomCommand()
  1025. {
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1029. const UnicodeString & Path)
  1030. {
  1031. FData = Data;
  1032. FPath = Path;
  1033. }
  1034. //---------------------------------------------------------------------------
  1035. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1036. const UnicodeString & Path, const UnicodeString & FileName,
  1037. const UnicodeString & FileList) :
  1038. TCustomCommand()
  1039. {
  1040. FData = Data;
  1041. FPath = Path;
  1042. FFileName = FileName;
  1043. FFileList = FileList;
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. int __fastcall TFileCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  1047. {
  1048. int Len;
  1049. wchar_t PatternCmd = (Index < Command.Length()) ? Command[Index + 1] : L'\0';
  1050. switch (PatternCmd)
  1051. {
  1052. case L'S':
  1053. case L'@':
  1054. case L'U':
  1055. case L'P':
  1056. case L'/':
  1057. case L'&':
  1058. Len = 2;
  1059. break;
  1060. default:
  1061. Len = 1;
  1062. break;
  1063. }
  1064. return Len;
  1065. }
  1066. //---------------------------------------------------------------------------
  1067. bool __fastcall TFileCustomCommand::PatternReplacement(
  1068. const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  1069. {
  1070. // keep consistent with TSessionLog::OpenLogFile
  1071. if (AnsiSameText(Pattern, L"!s"))
  1072. {
  1073. Replacement = FData.SessionData->GenerateSessionUrl(sufComplete);
  1074. }
  1075. else if (Pattern == L"!@")
  1076. {
  1077. Replacement = FData.SessionData->HostNameExpanded;
  1078. }
  1079. else if (AnsiSameText(Pattern, L"!u"))
  1080. {
  1081. Replacement = FData.SessionData->UserName;
  1082. }
  1083. else if (AnsiSameText(Pattern, L"!p"))
  1084. {
  1085. Replacement = FData.SessionData->Password;
  1086. }
  1087. else if (Pattern == L"!/")
  1088. {
  1089. Replacement = UnixIncludeTrailingBackslash(FPath);
  1090. }
  1091. else if (Pattern == L"!&")
  1092. {
  1093. Replacement = FFileList;
  1094. // already delimited
  1095. Delimit = false;
  1096. }
  1097. else
  1098. {
  1099. assert(Pattern.Length() == 1);
  1100. Replacement = FFileName;
  1101. }
  1102. return true;
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. void __fastcall TFileCustomCommand::Validate(const UnicodeString & Command)
  1106. {
  1107. int Found[2] = { 0, 0 };
  1108. CustomValidate(Command, &Found);
  1109. if ((Found[0] > 0) && (Found[1] > 0))
  1110. {
  1111. throw Exception(FMTLOAD(CUSTOM_COMMAND_FILELIST_ERROR,
  1112. (Found[1], Found[0])));
  1113. }
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TFileCustomCommand::ValidatePattern(const UnicodeString & Command,
  1117. int Index, int /*Len*/, wchar_t PatternCmd, void * Arg)
  1118. {
  1119. int * Found = static_cast<int *>(Arg);
  1120. assert(Index > 0);
  1121. if (PatternCmd == L'&')
  1122. {
  1123. Found[0] = Index;
  1124. }
  1125. else if ((PatternCmd != TEXT_TOKEN) && (PatternLen(Command, Index) == 1))
  1126. {
  1127. Found[1] = Index;
  1128. }
  1129. }
  1130. //---------------------------------------------------------------------------
  1131. bool __fastcall TFileCustomCommand::IsFileListCommand(const UnicodeString & Command)
  1132. {
  1133. return FindPattern(Command, L'&');
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. bool __fastcall TFileCustomCommand::IsRemoteFileCommand(const UnicodeString & Command)
  1137. {
  1138. return FindPattern(Command, L'!') || FindPattern(Command, L'&');
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. bool __fastcall TFileCustomCommand::IsFileCommand(const UnicodeString & Command)
  1142. {
  1143. return IsRemoteFileCommand(Command);
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. bool __fastcall TFileCustomCommand::IsSiteCommand(const UnicodeString & Command)
  1147. {
  1148. return FindPattern(Command, L'@');
  1149. }
  1150. //---------------------------------------------------------------------------
  1151. bool __fastcall TFileCustomCommand::IsPasswordCommand(const UnicodeString & Command)
  1152. {
  1153. return FindPattern(Command, L'p');
  1154. }
  1155. //---------------------------------------------------------------------------