summaryrefslogtreecommitdiff
path: root/library/filter.cpp
blob: d9ba8c5f6f104a1e13f586a65681530699cd9c73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "filter.h"
#include "zstring.h"
#include <wx/string.h>
#include <set>
#include <vector>
#include "resources.h"

using FreeFileSync::FilterProcess;


void compoundStringToTable(const Zstring& compoundInput, const DefaultChar* delimiter, std::vector<Zstring>& output)
{
    output.clear();
    Zstring input(compoundInput);

    //make sure input ends with delimiter - no problem with empty strings here
    if (!input.EndsWith(delimiter))
        input += delimiter;

    unsigned int indexStart = 0;
    unsigned int indexEnd   = 0;
    while ((indexEnd = input.find(delimiter, indexStart)) != Zstring::npos)
    {
        if (indexStart != indexEnd) //do not add empty strings
        {
            Zstring newEntry = input.substr(indexStart, indexEnd - indexStart);

            newEntry.Trim(true);  //remove whitespace characters from right
            newEntry.Trim(false); //remove whitespace characters from left

            if (!newEntry.empty())
                output.push_back(newEntry);
        }
        indexStart = indexEnd + 1;
    }
}


inline
void mergeVectors(std::vector<Zstring>& changing, const std::vector<Zstring>& input)
{
    for (std::vector<Zstring>::const_iterator i = input.begin(); i != input.end(); ++i)
        changing.push_back(*i);
}


std::vector<Zstring> compoundStringToFilter(const Zstring& filterString)
{
    //delimiters may be ';' or '\n'
    std::vector<Zstring> filterList;
    std::vector<Zstring> filterPreProcessing;
    compoundStringToTable(filterString, wxT(";"), filterPreProcessing);

    for (std::vector<Zstring>::const_iterator i = filterPreProcessing.begin(); i != filterPreProcessing.end(); ++i)
    {
        std::vector<Zstring> newEntries;
        compoundStringToTable(*i, wxT("\n"), newEntries);
        mergeVectors(filterList, newEntries);
    }

    return filterList;
}


inline
void addFilterEntry(const Zstring& filtername, std::set<Zstring>& fileFilter, std::set<Zstring>& directoryFilter)
{
    Zstring filterFormatted = filtername;

#ifdef FFS_WIN
    //Windows does NOT distinguish between upper/lower-case
    filterFormatted.MakeLower();
#elif defined FFS_LINUX
    //Linux DOES distinguish between upper/lower-case
//nothing to do here
#endif

    //remove leading separators (keep BEFORE test for Zstring::empty()!)
    if (filterFormatted.length() > 0 && *filterFormatted.c_str() == GlobalResources::FILE_NAME_SEPARATOR)
        filterFormatted = Zstring(filterFormatted.c_str() + 1);

    if (!filterFormatted.empty())
    {
        //Test if filterFormatted ends with GlobalResources::FILE_NAME_SEPARATOR, ignoring '*' and '?'.
        //If so, treat as filter for directory and add to directoryFilter.
        const DefaultChar* filter = filterFormatted.c_str();
        int i = filterFormatted.length() - 1;
        while (filter[i] == DefaultChar('*') || filter[i] == DefaultChar('?'))
        {
            --i;

            if (i == -1)
                break;
        }

        if (i >= 0 && filter[i] == GlobalResources::FILE_NAME_SEPARATOR) //last FILE_NAME_SEPARATOR found
        {
            if (i != int(filterFormatted.length()) - 1)  // "name\*"
            {
                fileFilter.insert(filterFormatted);
                directoryFilter.insert(filterFormatted);
            }
            //else // "name\"

            if (i > 0) // "name\*" or "name\": add "name" to directory filter
                directoryFilter.insert(Zstring(filterFormatted.c_str(), i));
        }
        else
        {
            fileFilter.insert(filterFormatted);
            directoryFilter.insert(filterFormatted);
        }
    }
}


inline
bool matchesFilter(const DefaultChar* name, const std::set<Zstring>& filter)
{
#ifdef FFS_WIN //Windows does NOT distinguish between upper/lower-case
    Zstring nameFormatted = name;
    nameFormatted.MakeLower();
#elif defined FFS_LINUX //Linux DOES distinguish between upper/lower-case
    const DefaultChar* const nameFormatted = name; //nothing to do here
#endif

    for (std::set<Zstring>::iterator j = filter.begin(); j != filter.end(); ++j)
        if (Zstring::Matches(nameFormatted, *j))
            return true;

    return false;
}

//##############################################################


FilterProcess::FilterProcess(const wxString& includeFilter, const wxString& excludeFilter)
{
    //no need for regular expressions! In tests wxRegex was by factor of 10 slower than wxString::Matches()!!

    //load filter into vectors of strings
    //delimiters may be ';' or '\n'
    std::vector<Zstring> includeList = compoundStringToFilter(includeFilter.c_str());
    std::vector<Zstring> excludeList = compoundStringToFilter(excludeFilter.c_str());

    //setup include/exclude filters for files and directories
    for (std::vector<Zstring>::iterator i = includeList.begin(); i != includeList.end(); ++i)
        addFilterEntry(*i, filterFileIn, filterFolderIn);

    for (std::vector<Zstring>::iterator i = excludeList.begin(); i != excludeList.end(); ++i)
        addFilterEntry(*i, filterFileEx, filterFolderEx);
}


bool FilterProcess::matchesFileFilter(const DefaultChar* relFilename) const
{
    if (    matchesFilter(relFilename, filterFileIn) &&  //process include filters
            !matchesFilter(relFilename, filterFileEx))   //process exclude filters
        return true;
    else
        return false;
}


bool FilterProcess::matchesDirFilter(const DefaultChar* relDirname) const
{
    if (    matchesFilter(relDirname, filterFolderIn) &&  //process include filters
            !matchesFilter(relDirname, filterFolderEx))   //process exclude filters
        return true;
    else
        return false;
}


void FilterProcess::filterGridData(FolderComparison& folderCmp) const
{
    //execute filtering...
    for (FolderComparison::iterator j = folderCmp.begin(); j != folderCmp.end(); ++j)
    {
        FileComparison& fileCmp = j->fileCmp;

        for (FileComparison::iterator i = fileCmp.begin(); i != fileCmp.end(); ++i)
        {
            //left hand side
            if (i->fileDescrLeft.objType == FileDescrLine::TYPE_FILE)
            {
                if (!matchesFileFilter(i->fileDescrLeft.relativeName.c_str()))
                {
                    i->selectedForSynchronization = false;
                    continue;
                }
            }
            else if (i->fileDescrLeft.objType == FileDescrLine::TYPE_DIRECTORY)
            {
                if (!matchesDirFilter(i->fileDescrLeft.relativeName.c_str()))
                {
                    i->selectedForSynchronization = false;
                    continue;
                }
            }
            //right hand side
            else if (i->fileDescrRight.objType == FileDescrLine::TYPE_FILE)
            {
                if (!matchesFileFilter(i->fileDescrRight.relativeName.c_str()))
                {
                    i->selectedForSynchronization = false;
                    continue;
                }
            }
            else if (i->fileDescrRight.objType == FileDescrLine::TYPE_DIRECTORY)
            {
                if (!matchesDirFilter(i->fileDescrRight.relativeName.c_str()))
                {
                    i->selectedForSynchronization = false;
                    continue;
                }
            }

            i->selectedForSynchronization = true;
        }
    }
}


template <bool includeRows>
inline
void inOrExcludeAllRows(FreeFileSync::FolderComparison& folderCmp)
{
    //remove all filters on folderCmp
    for (FreeFileSync::FolderComparison::iterator j = folderCmp.begin(); j != folderCmp.end(); ++j)
    {
        FreeFileSync::FileComparison& fileCmp = j->fileCmp;
        for (FreeFileSync::FileComparison::iterator i = fileCmp.begin(); i != fileCmp.end(); ++i)
            i->selectedForSynchronization = includeRows;
    }
}


void FilterProcess::includeAllRowsOnGrid(FolderComparison& folderCmp)
{
    //remove all filters on currentGridData
    inOrExcludeAllRows<true>(folderCmp);
}


void FilterProcess::excludeAllRowsOnGrid(FolderComparison& folderCmp)
{
    //exclude all rows on currentGridData
    inOrExcludeAllRows<false>(folderCmp);
}
bgstack15