OSDN Git Service

Refactor file filtering code to use String instead of CString.
[winmerge-jp/winmerge-jp.git] / Src / FileFilterMgr.h
1 /////////////////////////////////////////////////////////////////////////////
2 //    License (GPLv2+):
3 //    This program is free software; you can redistribute it and/or modify
4 //    it under the terms of the GNU General Public License as published by
5 //    the Free Software Foundation; either version 2 of the License, or
6 //    (at your option) any later version.
7 //    This program is distributed in the hope that it will be useful, but
8 //    WITHOUT ANY WARRANTY; without even the implied warranty of
9 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 //    General Public License for more details.
11 //    You should have received a copy of the GNU General Public License
12 //    along with this program; if not, write to the Free Software
13 //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
14 /////////////////////////////////////////////////////////////////////////////
15 /**
16  *  @file FileFilterMgr.h
17  *
18  *  @brief Declaration file for FileFilterMgr
19  */ 
20 // ID line follows -- this is updated by SVN
21 // $Id$
22
23 #ifndef FileFilterMgr_h_included
24 #define FileFilterMgr_h_included
25
26 // Uses MFC C++ template containers
27 #include <afxtempl.h>
28 #include <vector>
29 #include "UnicodeString.h"
30 #include "pcre.h"
31
32 struct FileFilterElement;
33 struct FileFilter;
34
35 /**
36  * @brief Return values for many filter functions.
37  */
38 enum FILTER_RETVALUE
39 {
40         FILTER_OK = 0,  /**< Success */
41         FILTER_ERROR_FILEACCESS,  /**< File could not be opened etc. */
42         FILTER_NOTFOUND, /**< Filter not found */
43 };
44
45 /**
46  * @brief File filter manager for handling filefilters.
47  *
48  * The FileFilterMgr loads a collection of named file filters from disk,
49  * and provides lookup access by name, or array access by index, to these
50  * named filters. It also provides test functions for actually using the
51  * filters.
52  *
53  * We are using PCRE for regular expressions. Nice thing in PCRE is it supports
54  * UTF-8 unicode, unlike many other libs. For ANSI builds we use just ansi
55  * strings, and for unicode we must first convert strings to UTF-8.
56  */
57 class FileFilterMgr
58 {
59 private:
60
61 public:
62         ~FileFilterMgr();
63         // Reload filter array from specified directory (passed to CFileFind)
64         void LoadFromDirectory(LPCTSTR szPattern, LPCTSTR szExt);
65         // Reload an edited filter
66         int ReloadFilterFromDisk(FileFilter * pfilter);
67         int ReloadFilterFromDisk(LPCTSTR szFullPath);
68         // Load a filter from a string
69         void LoadFilterString(LPCTSTR szFilterString);
70         int AddFilter(LPCTSTR szFilterFile);
71         void RemoveFilter(LPCTSTR szFilterFile);
72
73         // access to array of filters
74         int GetFilterCount() const { return m_filters.size(); }
75         String GetFilterName(int i) const;
76         String GetFilterName(const FileFilter *pFilter) const;
77         String GetFilterPath(int i) const;
78         String GetFilterDesc(int i) const;
79         String GetFilterDesc(const FileFilter *pFilter) const;
80         FileFilter * GetFilterByPath(LPCTSTR szFilterName);
81         String GetFullpath(FileFilter * pfilter) const;
82
83         // methods to actually use filter
84         BOOL TestFileNameAgainstFilter(const FileFilter * pFilter, LPCTSTR szFileName) const;
85         BOOL TestDirNameAgainstFilter(const FileFilter * pFilter, LPCTSTR szDirName) const;
86
87         void DeleteAllFilters();
88
89 // Implementation methods
90 protected:
91         // Clear the list of known filters
92         // Load a filter from a file (if syntax is valid)
93         FileFilter * LoadFilterFile(LPCTSTR szFilepath, int & errorcode);
94
95 // Implementation data
96 private:
97         std::vector<FileFilter*> m_filters; /*< List of filters loaded */
98 };
99
100
101 BOOL TestAgainstRegList(const std::vector<FileFilterElement*> *filterList, LPCTSTR szTest);
102 void EmptyFilterList(std::vector<FileFilterElement*> *filterList);
103
104
105 #endif // FileFilterMgr_h_included