Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / modules / tizen / Filesystem / NodeFilterMatcher.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @author          Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
18  */
19
20 #include "NodeFilterMatcher.h"
21
22 #include <pcrecpp.h>
23
24 namespace WrtDeviceApis {
25 namespace Filesystem {
26 using namespace Api;
27
28 bool NodeFilterMatcher::match(const INodePtr& value,
29                               const NodeFilterPtr& filter)
30 {
31     if (filter) {
32         if (!matchString(value->getPath()->getName(), filter->getName())) {
33             return false;
34         }
35
36         if (!matchRange(value->getCreated(),
37                         filter->getMinCreated(),
38                         filter->getMaxCreated()))
39         {
40             return false;
41         }
42
43         if (!matchRange(value->getModified(),
44                         filter->getMinModified(),
45                         filter->getMaxModified()))
46         {
47             return false;
48         }
49
50         if (!matchRange(value->getSize(),
51                         filter->getMinSize(),
52                         filter->getMaxSize()))
53         {
54             return false;
55         }
56     }
57     return true;
58 }
59
60 bool NodeFilterMatcher::matchString(const std::string& value,
61                                     const OptionalString& filter)
62 {
63     if (!filter.IsNull()) {
64         return pcrecpp::RE(*filter).PartialMatch(value);
65     }
66     return true;
67 }
68
69 template<typename Type>
70 bool NodeFilterMatcher::matchRange(const Type& value,
71                                    const DPL::Optional<Type>& min,
72                                    const DPL::Optional<Type>& max)
73 {
74     if ((!min.IsNull() && (value < *min)) ||
75         (!max.IsNull() && (value > *max)))
76     {
77         return false;
78     }
79     return true;
80 }
81 } // Filesystem
82 } // WrtDeviceApis