FileMasks.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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, UnicodeString(), UnicodeString(), UnicodeString());
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. __fastcall TCustomCommandData::TCustomCommandData(TTerminal * Terminal)
  1054. {
  1055. Init(Terminal->SessionData, Terminal->UserName, Terminal->Password,
  1056. Terminal->GetSessionInfo().HostKeyFingerprintSHA256);
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. __fastcall TCustomCommandData::TCustomCommandData(
  1060. TSessionData * SessionData, const UnicodeString & UserName, const UnicodeString & Password)
  1061. {
  1062. Init(SessionData, UserName, Password, UnicodeString());
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. void __fastcall TCustomCommandData::Init(
  1066. TSessionData * ASessionData, const UnicodeString & AUserName,
  1067. const UnicodeString & APassword, const UnicodeString & AHostKey)
  1068. {
  1069. FSessionData.reset(new TSessionData(L""));
  1070. if (ASessionData != NULL)
  1071. {
  1072. FSessionData->Assign(ASessionData);
  1073. }
  1074. FSessionData->UserName = AUserName;
  1075. FSessionData->Password = APassword;
  1076. FSessionData->HostKey = AHostKey;
  1077. }
  1078. //---------------------------------------------------------------------------
  1079. void __fastcall TCustomCommandData::operator=(const TCustomCommandData & Data)
  1080. {
  1081. DebugAssert(Data.SessionData != NULL);
  1082. FSessionData.reset(new TSessionData(L""));
  1083. FSessionData->Assign(Data.SessionData);
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. TSessionData * __fastcall TCustomCommandData::GetSesssionData() const
  1087. {
  1088. return FSessionData.get();
  1089. }
  1090. //---------------------------------------------------------------------------
  1091. //---------------------------------------------------------------------------
  1092. TFileCustomCommand::TFileCustomCommand()
  1093. {
  1094. }
  1095. //---------------------------------------------------------------------------
  1096. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1097. const UnicodeString & Path)
  1098. {
  1099. FData = Data;
  1100. FPath = Path;
  1101. }
  1102. //---------------------------------------------------------------------------
  1103. TFileCustomCommand::TFileCustomCommand(const TCustomCommandData & Data,
  1104. const UnicodeString & Path, const UnicodeString & FileName,
  1105. const UnicodeString & FileList) :
  1106. TCustomCommand()
  1107. {
  1108. FData = Data;
  1109. FPath = Path;
  1110. FFileName = FileName;
  1111. FFileList = FileList;
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. int __fastcall TFileCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  1115. {
  1116. int Len;
  1117. wchar_t PatternCmd = (Index < Command.Length()) ? tolower(Command[Index + 1]) : L'\0';
  1118. switch (PatternCmd)
  1119. {
  1120. case L's':
  1121. case L'e':
  1122. case L'@':
  1123. case L'u':
  1124. case L'p':
  1125. case L'#':
  1126. case L'/':
  1127. case L'&':
  1128. case L'n':
  1129. Len = 2;
  1130. break;
  1131. default:
  1132. Len = 1;
  1133. break;
  1134. }
  1135. return Len;
  1136. }
  1137. //---------------------------------------------------------------------------
  1138. bool __fastcall TFileCustomCommand::PatternReplacement(
  1139. int /*Index*/, const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  1140. {
  1141. // keep consistent with TSessionLog::OpenLogFile
  1142. if (SameText(Pattern, L"!s"))
  1143. {
  1144. if (FData.SessionData != NULL)
  1145. {
  1146. Replacement = FData.SessionData->GenerateSessionUrl(sufSession);
  1147. }
  1148. }
  1149. else if (SameText(Pattern, L"!e"))
  1150. {
  1151. if (FData.SessionData != NULL)
  1152. {
  1153. Replacement = FData.SessionData->GenerateSessionUrl(sufComplete);
  1154. }
  1155. }
  1156. else if (Pattern == L"!@")
  1157. {
  1158. if (FData.SessionData != NULL)
  1159. {
  1160. Replacement = FData.SessionData->HostNameExpanded;
  1161. }
  1162. }
  1163. else if (SameText(Pattern, L"!u"))
  1164. {
  1165. if (FData.SessionData != NULL)
  1166. {
  1167. Replacement = FData.SessionData->UserName;
  1168. }
  1169. }
  1170. else if (SameText(Pattern, L"!p"))
  1171. {
  1172. if (FData.SessionData != NULL)
  1173. {
  1174. Replacement = NormalizeString(FData.SessionData->Password);
  1175. }
  1176. }
  1177. else if (SameText(Pattern, L"!#"))
  1178. {
  1179. if (FData.SessionData != NULL)
  1180. {
  1181. Replacement = IntToStr(FData.SessionData->PortNumber);
  1182. }
  1183. }
  1184. else if (Pattern == L"!/")
  1185. {
  1186. Replacement = UnixIncludeTrailingBackslash(FPath);
  1187. }
  1188. else if (Pattern == L"!&")
  1189. {
  1190. Replacement = FFileList;
  1191. // already delimited
  1192. Delimit = false;
  1193. }
  1194. else if (SameText(Pattern, L"!n"))
  1195. {
  1196. if (FData.SessionData != NULL)
  1197. {
  1198. Replacement = FData.SessionData->SessionName;
  1199. }
  1200. }
  1201. else
  1202. {
  1203. DebugAssert(Pattern.Length() == 1);
  1204. Replacement = FFileName;
  1205. }
  1206. return true;
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. void __fastcall TFileCustomCommand::Validate(const UnicodeString & Command)
  1210. {
  1211. int Found[2] = { 0, 0 };
  1212. CustomValidate(Command, &Found);
  1213. if ((Found[0] > 0) && (Found[1] > 0))
  1214. {
  1215. throw Exception(FMTLOAD(CUSTOM_COMMAND_FILELIST_ERROR,
  1216. (Found[1], Found[0])));
  1217. }
  1218. }
  1219. //---------------------------------------------------------------------------
  1220. void __fastcall TFileCustomCommand::ValidatePattern(const UnicodeString & Command,
  1221. int Index, int /*Len*/, wchar_t PatternCmd, void * Arg)
  1222. {
  1223. int * Found = static_cast<int *>(Arg);
  1224. DebugAssert(Index > 0);
  1225. if (PatternCmd == L'&')
  1226. {
  1227. Found[0] = Index;
  1228. }
  1229. else if ((PatternCmd != TEXT_TOKEN) && (PatternLen(Command, Index) == 1))
  1230. {
  1231. Found[1] = Index;
  1232. }
  1233. }
  1234. //---------------------------------------------------------------------------
  1235. bool __fastcall TFileCustomCommand::IsFileListCommand(const UnicodeString & Command)
  1236. {
  1237. return FindPattern(Command, L'&');
  1238. }
  1239. //---------------------------------------------------------------------------
  1240. bool __fastcall TFileCustomCommand::IsRemoteFileCommand(const UnicodeString & Command)
  1241. {
  1242. return FindPattern(Command, L'!') || FindPattern(Command, L'&');
  1243. }
  1244. //---------------------------------------------------------------------------
  1245. bool __fastcall TFileCustomCommand::IsFileCommand(const UnicodeString & Command)
  1246. {
  1247. return IsRemoteFileCommand(Command);
  1248. }
  1249. //---------------------------------------------------------------------------
  1250. bool __fastcall TFileCustomCommand::IsSiteCommand(const UnicodeString & Command)
  1251. {
  1252. return FindPattern(Command, L'@') || FindPattern(Command, L'S') || FindPattern(Command, L'E');
  1253. }
  1254. //---------------------------------------------------------------------------
  1255. bool __fastcall TFileCustomCommand::IsSessionCommand(const UnicodeString & Command)
  1256. {
  1257. return
  1258. IsSiteCommand(Command) || IsPasswordCommand(Command) ||
  1259. FindPattern(Command, L'U') || FindPattern(Command, L'#') || FindPattern(Command, L'N') ||
  1260. FindPattern(Command, L'/');
  1261. }
  1262. //---------------------------------------------------------------------------
  1263. bool __fastcall TFileCustomCommand::IsPasswordCommand(const UnicodeString & Command)
  1264. {
  1265. return FindPattern(Command, L'p');
  1266. }
  1267. //---------------------------------------------------------------------------