FileMasks.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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 = DelimitStr(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'k':
  1229. case L'#':
  1230. case L'/':
  1231. case L'&':
  1232. case L'n':
  1233. Len = 2;
  1234. break;
  1235. default:
  1236. Len = 1;
  1237. break;
  1238. }
  1239. return Len;
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. bool __fastcall TFileCustomCommand::PatternReplacement(
  1243. int /*Index*/, const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  1244. {
  1245. // keep consistent with TSessionLog::OpenLogFile
  1246. if (SameText(Pattern, L"!s"))
  1247. {
  1248. if (FData.SessionData != NULL)
  1249. {
  1250. Replacement = FData.SessionData->GenerateSessionUrl(sufSession);
  1251. }
  1252. }
  1253. else if (SameText(Pattern, L"!e"))
  1254. {
  1255. if (FData.SessionData != NULL)
  1256. {
  1257. Replacement = FData.SessionData->GenerateSessionUrl(sufComplete);
  1258. }
  1259. }
  1260. else if (Pattern == L"!@")
  1261. {
  1262. if (FData.SessionData != NULL)
  1263. {
  1264. Replacement = FData.SessionData->HostNameExpanded;
  1265. }
  1266. }
  1267. else if (SameText(Pattern, L"!u"))
  1268. {
  1269. if (FData.SessionData != NULL)
  1270. {
  1271. Replacement = FData.SessionData->UserName;
  1272. }
  1273. }
  1274. else if (SameText(Pattern, L"!p"))
  1275. {
  1276. if (FData.SessionData != NULL)
  1277. {
  1278. Replacement = NormalizeString(FData.SessionData->Password);
  1279. }
  1280. }
  1281. else if (SameText(Pattern, L"!#"))
  1282. {
  1283. if (FData.SessionData != NULL)
  1284. {
  1285. Replacement = IntToStr(FData.SessionData->PortNumber);
  1286. }
  1287. }
  1288. else if (SameText(Pattern, L"!k"))
  1289. {
  1290. if (FData.SessionData != NULL)
  1291. {
  1292. Replacement = FData.SessionData->ResolvePublicKeyFile();
  1293. }
  1294. }
  1295. else if (Pattern == L"!/")
  1296. {
  1297. Replacement = UnixIncludeTrailingBackslash(FPath);
  1298. }
  1299. else if (Pattern == L"!&")
  1300. {
  1301. Replacement = FFileList;
  1302. // already delimited
  1303. Delimit = false;
  1304. }
  1305. else if (SameText(Pattern, L"!n"))
  1306. {
  1307. if (FData.SessionData != NULL)
  1308. {
  1309. Replacement = FData.SessionData->SessionName;
  1310. }
  1311. }
  1312. else
  1313. {
  1314. DebugAssert(Pattern.Length() == 1);
  1315. Replacement = FFileName;
  1316. }
  1317. return true;
  1318. }
  1319. //---------------------------------------------------------------------------
  1320. void __fastcall TFileCustomCommand::Validate(const UnicodeString & Command)
  1321. {
  1322. int Found[2] = { 0, 0 };
  1323. CustomValidate(Command, &Found);
  1324. if ((Found[0] > 0) && (Found[1] > 0))
  1325. {
  1326. throw Exception(FMTLOAD(CUSTOM_COMMAND_FILELIST_ERROR,
  1327. (Found[1], Found[0])));
  1328. }
  1329. }
  1330. //---------------------------------------------------------------------------
  1331. void __fastcall TFileCustomCommand::ValidatePattern(const UnicodeString & Command,
  1332. int Index, int /*Len*/, wchar_t PatternCmd, void * Arg)
  1333. {
  1334. int * Found = static_cast<int *>(Arg);
  1335. DebugAssert(Index > 0);
  1336. if (PatternCmd == L'&')
  1337. {
  1338. Found[0] = Index;
  1339. }
  1340. else if ((PatternCmd != TEXT_TOKEN) && (PatternLen(Command, Index) == 1))
  1341. {
  1342. Found[1] = Index;
  1343. }
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. bool __fastcall TFileCustomCommand::IsFileListCommand(const UnicodeString & Command)
  1347. {
  1348. return FindPattern(Command, L'&');
  1349. }
  1350. //---------------------------------------------------------------------------
  1351. bool __fastcall TFileCustomCommand::IsRemoteFileCommand(const UnicodeString & Command)
  1352. {
  1353. return FindPattern(Command, L'!') || FindPattern(Command, L'&');
  1354. }
  1355. //---------------------------------------------------------------------------
  1356. bool __fastcall TFileCustomCommand::IsFileCommand(const UnicodeString & Command)
  1357. {
  1358. return IsRemoteFileCommand(Command);
  1359. }
  1360. //---------------------------------------------------------------------------
  1361. bool __fastcall TFileCustomCommand::IsSiteCommand(const UnicodeString & Command)
  1362. {
  1363. return FindPattern(Command, L'@') || FindPattern(Command, L'S') || FindPattern(Command, L'E');
  1364. }
  1365. //---------------------------------------------------------------------------
  1366. bool __fastcall TFileCustomCommand::IsSessionCommand(const UnicodeString & Command)
  1367. {
  1368. return
  1369. IsSiteCommand(Command) || IsPasswordCommand(Command) ||
  1370. FindPattern(Command, L'U') || FindPattern(Command, L'#') || FindPattern(Command, L'N') ||
  1371. FindPattern(Command, L'/');
  1372. }
  1373. //---------------------------------------------------------------------------
  1374. bool __fastcall TFileCustomCommand::IsPasswordCommand(const UnicodeString & Command)
  1375. {
  1376. return FindPattern(Command, L'p');
  1377. }
  1378. //---------------------------------------------------------------------------