FileMasks.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "FileMasks.h"
  5. namespace Masks
  6. {
  7. bool __fastcall MatchesMask(const AnsiString Filename, const AnsiString Mask);
  8. }
  9. using namespace Masks;
  10. #include "Common.h"
  11. #include "TextsCore.h"
  12. #include "RemoteFiles.h"
  13. //---------------------------------------------------------------------------
  14. AnsiString __fastcall MaskFilePart(const AnsiString Part, const AnsiString Mask, bool& Masked)
  15. {
  16. AnsiString Result;
  17. int RestStart = 1;
  18. bool Delim = false;
  19. for (int Index = 1; Index <= Mask.Length(); Index++)
  20. {
  21. switch (Mask[Index])
  22. {
  23. case '\\':
  24. if (!Delim)
  25. {
  26. Delim = true;
  27. Masked = true;
  28. break;
  29. }
  30. case '*':
  31. if (!Delim)
  32. {
  33. Result += Part.SubString(RestStart, Part.Length() - RestStart + 1);
  34. RestStart = Part.Length() + 1;
  35. Masked = true;
  36. break;
  37. }
  38. case '?':
  39. if (!Delim)
  40. {
  41. if (RestStart <= Part.Length())
  42. {
  43. Result += Part[RestStart];
  44. RestStart++;
  45. }
  46. Masked = true;
  47. break;
  48. }
  49. default:
  50. Result += Mask[Index];
  51. RestStart++;
  52. Delim = false;
  53. break;
  54. }
  55. }
  56. return Result;
  57. }
  58. //---------------------------------------------------------------------------
  59. AnsiString __fastcall MaskFileName(AnsiString FileName, const AnsiString Mask)
  60. {
  61. if (!Mask.IsEmpty() && (Mask != "*") && (Mask != "*.*"))
  62. {
  63. bool Masked;
  64. int P = Mask.LastDelimiter(".");
  65. if (P > 0)
  66. {
  67. int P2 = FileName.LastDelimiter(".");
  68. // only dot at beginning of file name is not considered as
  69. // name/ext separator
  70. AnsiString FileExt = P2 > 1 ?
  71. FileName.SubString(P2 + 1, FileName.Length() - P2) : AnsiString();
  72. FileExt = MaskFilePart(FileExt, Mask.SubString(P + 1, Mask.Length() - P), Masked);
  73. if (P2 > 1)
  74. {
  75. FileName.SetLength(P2 - 1);
  76. }
  77. FileName = MaskFilePart(FileName, Mask.SubString(1, P - 1), Masked);
  78. if (!FileExt.IsEmpty())
  79. {
  80. FileName += "." + FileExt;
  81. }
  82. }
  83. else
  84. {
  85. FileName = MaskFilePart(FileName, Mask, Masked);
  86. }
  87. }
  88. return FileName;
  89. }
  90. //---------------------------------------------------------------------------
  91. bool __fastcall IsFileNameMask(const AnsiString Mask)
  92. {
  93. bool Masked = false;
  94. MaskFilePart("", Mask, Masked);
  95. return Masked;
  96. }
  97. //---------------------------------------------------------------------------
  98. AnsiString __fastcall DelimitFileNameMask(AnsiString Mask)
  99. {
  100. for (int i = 1; i <= Mask.Length(); i++)
  101. {
  102. if (strchr("\\*?", Mask[i]) != NULL)
  103. {
  104. Mask.Insert("\\", i);
  105. i++;
  106. }
  107. }
  108. return Mask;
  109. }
  110. //---------------------------------------------------------------------------
  111. //---------------------------------------------------------------------------
  112. bool __fastcall TFileMasks::IsMask(const AnsiString Mask)
  113. {
  114. return (Mask.LastDelimiter("?*[") > 0);
  115. }
  116. //---------------------------------------------------------------------------
  117. bool __fastcall TFileMasks::SingleMaskMatch(const AnsiString Mask, const AnsiString FileName)
  118. {
  119. return MatchesMask(FileName, Mask);
  120. }
  121. //---------------------------------------------------------------------------
  122. __fastcall TFileMasks::TFileMasks()
  123. {
  124. FMasks = "";
  125. }
  126. //---------------------------------------------------------------------------
  127. __fastcall TFileMasks::TFileMasks(const TFileMasks & Source)
  128. {
  129. Masks = Source.Masks;
  130. }
  131. //---------------------------------------------------------------------------
  132. __fastcall TFileMasks::TFileMasks(const AnsiString AMasks)
  133. {
  134. FMasks = AMasks;
  135. }
  136. //---------------------------------------------------------------------------
  137. bool __fastcall TFileMasks::Matches(AnsiString FileName, AnsiString Path) const
  138. {
  139. AnsiString S = Masks;
  140. while (!S.IsEmpty())
  141. {
  142. AnsiString M;
  143. M = CutToChar(S, ';', True);
  144. int D = M.LastDelimiter("\\/");
  145. bool PathMatch = true;
  146. if (D > 0)
  147. {
  148. AnsiString MP = ToUnixPath(M.SubString(1, D - 1));
  149. // 'Path' must already have unix slashes
  150. PathMatch = MatchesMask(Path, MP);
  151. M = M.SubString(D + 1, M.Length() - D);
  152. }
  153. if (PathMatch && MatchesMask(FileName, M)) return true;
  154. }
  155. return false;
  156. }
  157. //---------------------------------------------------------------------------
  158. bool __fastcall TFileMasks::Matches(AnsiString FileName, bool Local) const
  159. {
  160. AnsiString Path;
  161. if (Local)
  162. {
  163. Path = ExtractFilePath(FileName);
  164. if (!Path.IsEmpty())
  165. {
  166. Path = ToUnixPath(ExcludeTrailingBackslash(Path));
  167. }
  168. FileName = ExtractFileName(FileName);
  169. }
  170. else
  171. {
  172. Path = UnixExcludeTrailingBackslash(UnixExtractFilePath(FileName));
  173. FileName = UnixExtractFileName(FileName);
  174. }
  175. return Matches(FileName, Path);
  176. }
  177. //---------------------------------------------------------------------------
  178. bool __fastcall TFileMasks::IsValid()
  179. {
  180. int Start, Length;
  181. return IsValid(Start, Length);
  182. }
  183. //---------------------------------------------------------------------------
  184. bool __fastcall TFileMasks::IsValid(int & Start, int & Length)
  185. {
  186. AnsiString S = Masks;
  187. int IStart = 1;
  188. while (!S.IsEmpty())
  189. {
  190. AnsiString M;
  191. int P = S.Pos(';');
  192. M = CutToChar(S, ';', False);
  193. if (!M.IsEmpty())
  194. {
  195. try
  196. {
  197. MatchesMask("*.*", Trim(M));
  198. }
  199. catch (Exception &E)
  200. {
  201. // Ignore leading/trainling spaces
  202. while (!M.IsEmpty() && (M[1] == ' '))
  203. {
  204. IStart++;
  205. M.Delete(1, 1);
  206. }
  207. Start = IStart-1;
  208. Length = M.Trim().Length();
  209. return False;
  210. }
  211. }
  212. if (P) IStart += P;
  213. }
  214. return true;
  215. }
  216. //---------------------------------------------------------------------------
  217. bool __fastcall TFileMasks::operator ==(const TFileMasks & rhm) const
  218. {
  219. return (Masks == rhm.Masks);
  220. }
  221. //---------------------------------------------------------------------------
  222. TFileMasks & __fastcall TFileMasks::operator =(const AnsiString rhs)
  223. {
  224. Masks = rhs;
  225. return *this;
  226. }
  227. //---------------------------------------------------------------------------
  228. TFileMasks & __fastcall TFileMasks::operator =(const TFileMasks & rhm)
  229. {
  230. Masks = rhm.Masks;
  231. return *this;
  232. }
  233. //---------------------------------------------------------------------------
  234. bool __fastcall TFileMasks::operator ==(const AnsiString rhs) const
  235. {
  236. return (Masks == rhs);
  237. }
  238. //---------------------------------------------------------------------------
  239. TFileMasks & __fastcall TFileMasks::operator =(const char * rhs)
  240. {
  241. Masks = rhs;
  242. return *this;
  243. }
  244. //---------------------------------------------------------------------------
  245. //---------------------------------------------------------------------------
  246. #define TEXT_TOKEN '\255'
  247. //---------------------------------------------------------------------------
  248. TCustomCommand::TCustomCommand()
  249. {
  250. }
  251. //---------------------------------------------------------------------------
  252. void __fastcall TCustomCommand::GetToken(
  253. const AnsiString & Command, int Index, int & Len, char & PatternCmd)
  254. {
  255. assert(Index <= Command.Length());
  256. const char * Ptr = Command.c_str() + Index - 1;
  257. if (Ptr[0] == '!')
  258. {
  259. PatternCmd = Ptr[1];
  260. if (PatternCmd == '!')
  261. {
  262. Len = 2;
  263. }
  264. else
  265. {
  266. Len = PatternLen(Index, PatternCmd);
  267. }
  268. if (Len < 0)
  269. {
  270. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNKNOWN, (PatternCmd, Index)));
  271. }
  272. else if (Len > 0)
  273. {
  274. if ((Command.Length() - Index + 1) < Len)
  275. {
  276. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (PatternCmd, Index)));
  277. }
  278. }
  279. else if (Len == 0)
  280. {
  281. const char * PatternEnd = strchr(Ptr + 1, '!');
  282. if (PatternEnd == NULL)
  283. {
  284. throw Exception(FMTLOAD(CUSTOM_COMMAND_UNTERMINATED, (PatternCmd, Index)));
  285. }
  286. Len = PatternEnd - Ptr + 1;
  287. }
  288. }
  289. else
  290. {
  291. PatternCmd = TEXT_TOKEN;
  292. const char * NextPattern = strchr(Ptr, '!');
  293. if (NextPattern == NULL)
  294. {
  295. Len = Command.Length() - Index + 1;
  296. }
  297. else
  298. {
  299. Len = NextPattern - Ptr;
  300. }
  301. }
  302. }
  303. //---------------------------------------------------------------------------
  304. AnsiString __fastcall TCustomCommand::Complete(const AnsiString & Command,
  305. bool LastPass)
  306. {
  307. AnsiString Result;
  308. int Index = 1;
  309. while (Index <= Command.Length())
  310. {
  311. int Len;
  312. char PatternCmd;
  313. GetToken(Command, Index, Len, PatternCmd);
  314. if (PatternCmd == TEXT_TOKEN)
  315. {
  316. Result += Command.SubString(Index, Len);
  317. }
  318. else if (PatternCmd == '!')
  319. {
  320. if (LastPass)
  321. {
  322. Result += '!';
  323. }
  324. else
  325. {
  326. Result += Command.SubString(Index, Len);
  327. }
  328. }
  329. else
  330. {
  331. AnsiString Pattern = Command.SubString(Index, Len);
  332. AnsiString Replacement;
  333. if (PatternReplacement(Index, Pattern, Replacement))
  334. {
  335. if (!LastPass)
  336. {
  337. Replacement = StringReplace(Replacement, "!", "!!",
  338. TReplaceFlags() << rfReplaceAll);
  339. }
  340. Result += Replacement;
  341. }
  342. else
  343. {
  344. Result += Pattern;
  345. }
  346. }
  347. Index += Len;
  348. }
  349. return Result;
  350. }
  351. //---------------------------------------------------------------------------
  352. void __fastcall TCustomCommand::Validate(const AnsiString & Command)
  353. {
  354. CustomValidate(Command, NULL);
  355. }
  356. //---------------------------------------------------------------------------
  357. void __fastcall TCustomCommand::CustomValidate(const AnsiString & Command,
  358. void * Arg)
  359. {
  360. int Index = 1;
  361. while (Index <= Command.Length())
  362. {
  363. int Len;
  364. char PatternCmd;
  365. GetToken(Command, Index, Len, PatternCmd);
  366. ValidatePattern(Command, Index, Len, PatternCmd, Arg);
  367. Index += Len;
  368. }
  369. }
  370. //---------------------------------------------------------------------------
  371. bool __fastcall TCustomCommand::FindPattern(const AnsiString & Command,
  372. char PatternCmd)
  373. {
  374. bool Result = false;
  375. int Index = 1;
  376. while (!Result && (Index <= Command.Length()))
  377. {
  378. int Len;
  379. char APatternCmd;
  380. GetToken(Command, Index, Len, APatternCmd);
  381. if (((PatternCmd != '!') && (PatternCmd == APatternCmd)) ||
  382. ((PatternCmd == '!') && (Len == 1) && (APatternCmd != TEXT_TOKEN)))
  383. {
  384. Result = true;
  385. }
  386. Index += Len;
  387. }
  388. return Result;
  389. }
  390. //---------------------------------------------------------------------------
  391. void __fastcall TCustomCommand::ValidatePattern(const AnsiString & /*Command*/,
  392. int /*Index*/, int /*Len*/, char /*PatternCmd*/, void * /*Arg*/)
  393. {
  394. }
  395. //---------------------------------------------------------------------------
  396. //---------------------------------------------------------------------------
  397. TInteractiveCustomCommand::TInteractiveCustomCommand(
  398. TCustomCommand * ChildCustomCommand)
  399. {
  400. FChildCustomCommand = ChildCustomCommand;
  401. }
  402. //---------------------------------------------------------------------------
  403. void __fastcall TInteractiveCustomCommand::Prompt(int /*Index*/,
  404. const AnsiString & /*Prompt*/, AnsiString & Value)
  405. {
  406. Value = "";
  407. }
  408. //---------------------------------------------------------------------------
  409. int __fastcall TInteractiveCustomCommand::PatternLen(int Index, char PatternCmd)
  410. {
  411. int Len;
  412. switch (PatternCmd)
  413. {
  414. case '?':
  415. Len = 0;
  416. break;
  417. default:
  418. Len = FChildCustomCommand->PatternLen(Index, PatternCmd);
  419. break;
  420. }
  421. return Len;
  422. }
  423. //---------------------------------------------------------------------------
  424. bool __fastcall TInteractiveCustomCommand::PatternReplacement(int Index, const AnsiString & Pattern,
  425. AnsiString & Replacement)
  426. {
  427. bool Result;
  428. if ((Pattern.Length() >= 3) && (Pattern[2] == '?'))
  429. {
  430. AnsiString PromptStr;
  431. int Pos = Pattern.SubString(3, Pattern.Length() - 2).Pos("?");
  432. if (Pos > 0)
  433. {
  434. PromptStr = Pattern.SubString(3, Pos - 1);
  435. Replacement = Pattern.SubString(3 + Pos, Pattern.Length() - 3 - Pos);
  436. }
  437. else
  438. {
  439. PromptStr = Pattern.SubString(3, Pattern.Length() - 3);
  440. }
  441. Prompt(Index, PromptStr, Replacement);
  442. Result = true;
  443. }
  444. else
  445. {
  446. Result = false;
  447. }
  448. return Result;
  449. }
  450. //---------------------------------------------------------------------------
  451. //---------------------------------------------------------------------------
  452. TFileCustomCommand::TFileCustomCommand() :
  453. TCustomCommand()
  454. {
  455. }
  456. //---------------------------------------------------------------------------
  457. TFileCustomCommand::TFileCustomCommand(const AnsiString & FileName, const AnsiString & FileList) :
  458. TCustomCommand()
  459. {
  460. FFileName = FileName;
  461. FFileList = FileList;
  462. }
  463. //---------------------------------------------------------------------------
  464. int __fastcall TFileCustomCommand::PatternLen(int /*Index*/, char PatternCmd)
  465. {
  466. int Len;
  467. switch (PatternCmd)
  468. {
  469. case '&':
  470. Len = 2;
  471. break;
  472. default:
  473. Len = 1;
  474. break;
  475. }
  476. return Len;
  477. }
  478. //---------------------------------------------------------------------------
  479. bool __fastcall TFileCustomCommand::PatternReplacement(int /*Index*/,
  480. const AnsiString & Pattern, AnsiString & Replacement)
  481. {
  482. if (Pattern == "!&")
  483. {
  484. Replacement = FFileList;
  485. }
  486. else
  487. {
  488. assert(Pattern.Length() == 1);
  489. Replacement = FFileName;
  490. }
  491. return true;
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall TFileCustomCommand::Validate(const AnsiString & Command)
  495. {
  496. int Found[2] = { 0, 0 };
  497. CustomValidate(Command, &Found);
  498. if ((Found[0] > 0) && (Found[1] > 0))
  499. {
  500. throw Exception(FMTLOAD(CUSTOM_COMMAND_FILELIST_ERROR,
  501. (Found[1], Found[0])));
  502. }
  503. }
  504. //---------------------------------------------------------------------------
  505. void __fastcall TFileCustomCommand::ValidatePattern(const AnsiString & /*Command*/,
  506. int Index, int /*Len*/, char PatternCmd, void * Arg)
  507. {
  508. int * Found = static_cast<int *>(Arg);
  509. assert(Index > 0);
  510. if (PatternCmd == '&')
  511. {
  512. Found[0] = Index;
  513. }
  514. else if ((PatternCmd != TEXT_TOKEN) && (PatternLen(Index, PatternCmd) == 1))
  515. {
  516. Found[1] = Index;
  517. }
  518. }
  519. //---------------------------------------------------------------------------
  520. bool __fastcall TFileCustomCommand::IsFileListCommand(const AnsiString & Command)
  521. {
  522. return FindPattern(Command, '&');
  523. }
  524. //---------------------------------------------------------------------------
  525. bool __fastcall TFileCustomCommand::IsFileCommand(const AnsiString & Command)
  526. {
  527. return FindPattern(Command, '!') || FindPattern(Command, '&');
  528. }
  529. //---------------------------------------------------------------------------