1 /////////////////////////////////////////////////////////////////////////////
2 // FileFilterMgr.cpp : implementation file
3 // see FileFilterMgr.h for description
4 /////////////////////////////////////////////////////////////////////////////
6 // This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
7 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8 // You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
9 /////////////////////////////////////////////////////////////////////////////
13 #include "FileFilterMgr.h"
19 static char THIS_FILE[] = __FILE__;
22 // I think that CRegExp doesn't copy correctly (I get heap corruption in CRegList::program)
23 // so I'm using pointers to avoid its copy constructor
26 typedef CTypedPtrList<CPtrList, CRegExp*>RegList;
27 static void DeleteRegList(RegList & reglist)
29 while (!reglist.IsEmpty())
31 CRegExp * regexp = reglist.RemoveHead();
36 // For example, this might be a GNU C filter, excluding *.o files and CVS directories
37 // That is to say, a filter is a set of file masks and directory masks
45 FileFilter() : default_include(true) { }
48 FileFilter::~FileFilter()
50 DeleteRegList(filefilters);
51 DeleteRegList(dirfilters);
54 FileFilterMgr::~FileFilterMgr()
60 void FileFilterMgr::LoadFromDirectory(LPCTSTR szPattern)
64 BOOL bWorking = finder.FindFile(szPattern);
67 bWorking = finder.FindNextFile();
68 if (finder.IsDots() || finder.IsDirectory())
70 LoadFilterFile(finder.GetFilePath(), finder.GetFileName());
74 void FileFilterMgr::DeleteAllFilters()
76 for (int i=0; i<m_filters.GetSize(); ++i)
81 m_filters.RemoveAll();
84 // Add a single pattern (if nonempty & valid) to a pattern list
85 static void AddFilterPattern(RegList & reglist, CString & str)
89 if (str.IsEmpty()) return;
90 CRegExp * regexp = new CRegExp;
91 if (regexp->RegComp(str))
92 reglist.AddTail(regexp);
97 // Parse a filter file, and add it to array if valid
98 void FileFilterMgr::LoadFilterFile(LPCTSTR szFilepath, LPCTSTR szFilename)
101 if (!file.Open(szFilepath, CFile::modeRead))
103 FileFilter *pfilter = new FileFilter;
104 pfilter->fullpath = szFilepath;
105 pfilter->name = szFilename; // default if no name
107 while (file.ReadString(sLine))
109 if (0 == _tcsncmp(sLine, _T("name:"), 5))
111 // specifies display name
112 CString str = sLine.Mid(5);
117 else if (0 == _tcsncmp(sLine, _T("def:"), 4))
120 CString str = sLine.Mid(4);
122 if (str == _T("0") || str == _T("no") || str == _T("exclude"))
123 pfilter->default_include = false;
124 else if (str == _T("1") || str == _T("yes") || str == _T("include"))
125 pfilter->default_include = true;
127 else if (0 == _tcsncmp(sLine, _T("f:"), 2))
130 CString str = sLine.Mid(2);
131 AddFilterPattern(pfilter->filefilters, str);
133 else if (0 == _tcsncmp(sLine, _T("d:"), 2))
136 CString str = sLine.Mid(2);
137 AddFilterPattern(pfilter->dirfilters, str);
140 m_filters.Add(pfilter);
143 // Give client back a pointer to the actual filter
144 // We just do a linear search, because this is seldom called
145 FileFilter * FileFilterMgr::GetFilter(LPCTSTR szFilterName)
147 for (int i=0; i<m_filters.GetSize(); ++i)
149 if (m_filters[i]->name == szFilterName)
155 // return TRUE if string passes
156 BOOL TestAgainstRegList(const RegList & reglist, LPCTSTR szTest)
158 CString str = szTest;
160 for (POSITION pos = reglist.GetHeadPosition(); pos; )
162 CRegExp * regexp = reglist.GetNext(pos);
163 if (regexp->RegFind(str) != -1)
169 // return TRUE if file passes the filter
170 BOOL FileFilterMgr::TestFileNameAgainstFilter(FileFilter * pFilter, LPCTSTR szFileName)
172 if (!pFilter) return TRUE;
173 if (TestAgainstRegList(pFilter->filefilters, szFileName))
174 return !pFilter->default_include;
175 return pFilter->default_include;
178 // return TRUE if directory passes the filter
179 BOOL FileFilterMgr::TestDirNameAgainstFilter(FileFilter * pFilter, LPCTSTR szDirName)
181 if (!pFilter) return TRUE;
182 if (TestAgainstRegList(pFilter->dirfilters, szDirName))
183 return !pFilter->default_include;
184 return pFilter->default_include;
187 CString FileFilterMgr::GetFilterName(int i)
189 return m_filters[i]->name;