FileFilter.h
Go to the documentation of this file.
1 
2 // This source file is part of the ZipArchive Library Open Source distribution
3 // and is Copyrighted 2000 - 2022 by Artpol Software - Tadeusz Dracz
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // For the licensing details refer to the License.txt file.
11 //
12 // Web Site: https://www.artpol-software.com
14 
22 #if !defined(ZIPARCHIVE_FILEFILTER_DOT_H)
23 #define ZIPARCHIVE_FILEFILTER_DOT_H
24 
25 #if _MSC_VER > 1000
26  #pragma once
27  #pragma warning( push )
28  #pragma warning (disable : 4100) // unreferenced formal parameter
29 #endif
30 
31 #include "stdafx.h"
32 #include "ZipExport.h"
33 #include "FileInfo.h"
34 #include "Wildcard.h"
35 #include "ZipPlatform.h"
36 #include "ZipCollections.h"
37 
38 namespace ZipArchiveLib
39 {
48  class ZIP_API CFileFilter
49  {
50  public:
51 
61  CFileFilter(bool bInverted = false)
62  :m_bInverted(bInverted)
63  {
64  }
65 
106  bool Evaluate(LPCTSTR lpszParentDir, LPCTSTR lpszName, const CFileInfo& info)
107  {
108  bool ret = Accept(lpszParentDir, lpszName, info);
109  if (!HandlesInversion())
110  return m_bInverted ? !ret : ret;
111  return ret;
112  }
113 
130  void SetInverted(bool bInverted = true) { m_bInverted = bInverted;}
131 
143  bool IsInverted() const {return m_bInverted;}
144 
169  virtual bool HandlesFile(const CFileInfo& info)
170  {
171  return !info.IsDirectory();
172  }
173 
174  virtual ~CFileFilter()
175  {
176  }
177 protected:
210  virtual bool Accept(LPCTSTR lpszParentDir, LPCTSTR lpszName, const CFileInfo& info)
211  {
212  return true;
213  }
214 
236  virtual bool HandlesInversion() const
237  {
238  return false;
239  }
240  bool m_bInverted;
241 
242  };
243 
244 
255  class ZIP_API CNameFileFilter : public CFileFilter
256  {
257  CWildcard m_matcher;
258  int m_iAppliesToTypes;
259  public:
260 
271  {
272  toFile = 0x1,
273  toDirectory = 0x2,
274  toAll = toFile | toDirectory
275  };
276 
306  CNameFileFilter(LPCTSTR lpszPattern = _T("*"), bool bInverted = false, int iAppliesToTypes = toFile, bool bCaseSensitive = ZipPlatform::GetSystemCaseSensitivity())
307  :CFileFilter(bInverted), m_matcher(lpszPattern, bCaseSensitive)
308  {
309  m_iAppliesToTypes = iAppliesToTypes;
310  }
311 
326  bool AppliesToType(int iType)
327  {
328  return (m_iAppliesToTypes & iType) == iType;
329  }
330 
341  void SetAppliesToTypes(int iType) { m_iAppliesToTypes = iType; }
342 
352  int GetAppliesToTypes() {return m_iAppliesToTypes;}
353 
374  bool HandlesFile(const CFileInfo& info)
375  {
376  return info.IsDirectory() ? AppliesToType(toDirectory) : AppliesToType(toFile);
377  }
378  protected:
379  virtual bool Accept(LPCTSTR, LPCTSTR lpszName, const CFileInfo& info)
380  {
381  return m_matcher.IsMatch(lpszName);
382  }
383  };
384 
393  class ZIP_API CGroupFileFilter : public CFileFilter
394  {
395 
396  public:
397 
402  {
403  And,
404  Or
405  };
406 
431  CGroupFileFilter(GroupType groupType = CGroupFileFilter::And, bool bAutoDelete = true, bool bInverted = false)
432  :CFileFilter(bInverted), m_iType(groupType), m_bAutoDelete(bAutoDelete)
433  {
434  }
435 
442  void Add(CFileFilter* pFilter)
443  {
444  m_filters.Add(pFilter);
445  }
446 
453  CFileFilter* GetAt(ZIP_ARRAY_SIZE_TYPE uIndex)
454  {
455  return m_filters[uIndex];
456  }
457 
464  const CFileFilter* GetAt(ZIP_ARRAY_SIZE_TYPE uIndex) const
465  {
466  return m_filters[uIndex];
467  }
468 
475  const CFileFilter* operator[] (ZIP_ARRAY_SIZE_TYPE uIndex) const
476  {
477  return GetAt(uIndex);
478  }
479 
486  CFileFilter* operator[] (ZIP_ARRAY_SIZE_TYPE uIndex)
487  {
488  return GetAt(uIndex);
489  }
490 
502  void RemoveAt(ZIP_ARRAY_SIZE_TYPE uIndex)
503  {
504  CFileFilter* filter = m_filters[uIndex];
505  // first remove, then delete
506  m_filters.RemoveAt(uIndex);
507  if (m_bAutoDelete)
508  delete filter;
509  }
510 
511 
522  void Clear()
523  {
524  if (m_filters.GetSize() == 0)
525  return;
526 
527  ZIP_ARRAY_SIZE_TYPE i = m_filters.GetSize() - 1;
528  for (; ;)
529  {
530  RemoveAt(i);
531  if (i == 0)
532  break;
533  i--;
534  }
535  }
536 
544  ZIP_ARRAY_SIZE_TYPE GetSize()
545  {
546  return m_filters.GetSize();
547  }
548 
558  void SetType(GroupType iType) {m_iType = iType;}
559 
569  GroupType GetType() const {return m_iType;}
570 
583  void SetAutoDelete(bool bAutoDelete) {m_bAutoDelete = bAutoDelete;}
584 
594  bool IsAutoDelete() const {return m_bAutoDelete;}
595 
607  bool HandlesFile(const CFileInfo& info)
608  {
609  for (ZIP_ARRAY_SIZE_TYPE i = 0; i < m_filters.GetSize(); i++)
610  // it is enough that one filter handles it
611  if (m_filters[i]->HandlesFile(info))
612  return true;
613  return false;
614  }
615 
616 
618  {
619  Clear();
620  }
621 
622  protected:
623 
624  virtual bool Accept(LPCTSTR lpszParentDir, LPCTSTR lpszName, const CFileInfo& info);
634  bool HandlesInversion() const
635  {
636  return true;
637  }
640 
641  private:
642 
643 #if (_MSC_VER > 1000) && (defined ZIP_HAS_DLL)
644  #pragma warning (push)
645  #pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class
646 #endif
647 
648  CZipArray<CFileFilter*> m_filters;
649 
650 #if (_MSC_VER > 1000) && (defined ZIP_HAS_DLL)
651  #pragma warning( pop)
652 #endif
653 
654  };
655 }
656 
657 #if _MSC_VER > 1000
658  #pragma warning( pop )
659 #endif
660 
661 #endif

The ZipArchive Library Copyright © 2000 - 2022 Artpol Software - Tadeusz Dracz. Generated at Sat Dec 17 2022 19:57:03.