FileMasks.cpp 44 KB

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