FileMasks.cpp 44 KB

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