FileMasks.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "FileMasks.h"
  5. #include <Masks.hpp>
  6. #include "Common.h"
  7. //---------------------------------------------------------------------------
  8. AnsiString __fastcall MaskFilePart(const AnsiString Part, const AnsiString Mask)
  9. {
  10. AnsiString Result;
  11. int RestStart = 1;
  12. for (int Index = 1; Index <= Mask.Length(); Index++)
  13. {
  14. switch (Mask[Index])
  15. {
  16. case '*':
  17. Result += Part.SubString(RestStart, Part.Length() - RestStart + 1);
  18. RestStart = Part.Length() + 1;
  19. break;
  20. case '?':
  21. if (RestStart <= Part.Length())
  22. {
  23. Result += Part[RestStart];
  24. RestStart++;
  25. }
  26. break;
  27. default:
  28. Result += Mask[Index];
  29. RestStart++;
  30. break;
  31. }
  32. }
  33. return Result;
  34. }
  35. //---------------------------------------------------------------------------
  36. AnsiString __fastcall MaskFileName(AnsiString FileName, const AnsiString Mask)
  37. {
  38. if (!Mask.IsEmpty() && (Mask != "*") && (Mask != "*.*"))
  39. {
  40. int P = Mask.LastDelimiter(".");
  41. if (P > 0)
  42. {
  43. int P2 = FileName.LastDelimiter(".");
  44. // only dot at beginning of file name is not considered as
  45. // name/ext separator
  46. AnsiString FileExt = P2 > 1 ?
  47. FileName.SubString(P2 + 1, FileName.Length() - P2) : AnsiString();
  48. FileExt = MaskFilePart(FileExt, Mask.SubString(P + 1, Mask.Length() - P));
  49. if (P2 > 1)
  50. {
  51. FileName.SetLength(P2 - 1);
  52. }
  53. FileName = MaskFilePart(FileName, Mask.SubString(1, P - 1));
  54. if (!FileExt.IsEmpty())
  55. {
  56. FileName += "." + FileExt;
  57. }
  58. }
  59. else
  60. {
  61. FileName = MaskFilePart(FileName, Mask);
  62. }
  63. }
  64. return FileName;
  65. }
  66. //---------------------------------------------------------------------------
  67. //---------------------------------------------------------------------------
  68. __fastcall TFileMasks::TFileMasks()
  69. {
  70. FMasks = "";
  71. }
  72. //---------------------------------------------------------------------------
  73. __fastcall TFileMasks::TFileMasks(const TFileMasks & Source)
  74. {
  75. Masks = Source.Masks;
  76. }
  77. //---------------------------------------------------------------------------
  78. __fastcall TFileMasks::TFileMasks(const AnsiString AMasks)
  79. {
  80. FMasks = AMasks;
  81. }
  82. //---------------------------------------------------------------------------
  83. bool __fastcall TFileMasks::Matches(AnsiString FileName) const
  84. {
  85. AnsiString S = Masks;
  86. FileName = ExtractFileName(FileName);
  87. while (!S.IsEmpty())
  88. {
  89. AnsiString M;
  90. M = CutToChar(S, ';', True);
  91. if (MatchesMask(FileName, M)) return true;
  92. }
  93. return false;
  94. }
  95. //---------------------------------------------------------------------------
  96. bool __fastcall TFileMasks::IsValid()
  97. {
  98. int Start, Length;
  99. return IsValid(Start, Length);
  100. }
  101. //---------------------------------------------------------------------------
  102. bool __fastcall TFileMasks::IsValid(int & Start, int & Length)
  103. {
  104. AnsiString S = Masks;
  105. int IStart = 1;
  106. while (!S.IsEmpty())
  107. {
  108. AnsiString M;
  109. int P = S.Pos(';');
  110. M = CutToChar(S, ';', False);
  111. if (!M.IsEmpty())
  112. {
  113. try
  114. {
  115. TMask * Mask = new TMask(Trim(M));
  116. try
  117. {
  118. Mask->Matches("*.*");
  119. }
  120. __finally
  121. {
  122. delete Mask;
  123. }
  124. }
  125. catch (Exception &E)
  126. {
  127. // Ignore leading/trainling spaces
  128. while (!M.IsEmpty() && (M[1] == ' '))
  129. {
  130. IStart++;
  131. M.Delete(1, 1);
  132. }
  133. Start = IStart-1;
  134. Length = M.Trim().Length();
  135. return False;
  136. }
  137. }
  138. if (P) IStart += P;
  139. }
  140. return true;
  141. }
  142. //---------------------------------------------------------------------------
  143. bool __fastcall TFileMasks::operator ==(const TFileMasks & rhm) const
  144. {
  145. return (Masks == rhm.Masks);
  146. }
  147. //---------------------------------------------------------------------------
  148. TFileMasks & __fastcall TFileMasks::operator =(const AnsiString rhs)
  149. {
  150. Masks = rhs;
  151. return *this;
  152. }
  153. //---------------------------------------------------------------------------
  154. TFileMasks & __fastcall TFileMasks::operator =(const TFileMasks & rhm)
  155. {
  156. Masks = rhm.Masks;
  157. return *this;
  158. }
  159. //---------------------------------------------------------------------------
  160. bool __fastcall TFileMasks::operator ==(const AnsiString rhs) const
  161. {
  162. return (Masks == rhs);
  163. }
  164. //---------------------------------------------------------------------------
  165. TFileMasks & __fastcall TFileMasks::operator =(const char * rhs)
  166. {
  167. Masks = rhs;
  168. return *this;
  169. }