tizen beta release
[platform/framework/web/wrt-plugins-common.git] / src / 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
27 using namespace Api;
28
29 bool NodeFilterMatcher::match(const INodePtr& value,
30                               const NodeFilterPtr& filter)
31 {
32     if (filter) {
33         if (!matchString(value->getPath()->getName(), filter->getName())) {
34             return false;
35         }
36
37         if (!matchRange(value->getCreated(),
38                         filter->getMinCreated(),
39                         filter->getMaxCreated())) {
40             return false;
41         }
42
43         if (!matchRange(value->getModified(),
44                         filter->getMinModified(),
45                         filter->getMaxModified())) {
46             return false;
47         }
48
49         if (!matchRange(value->getSize(),
50                         filter->getMinSize(),
51                         filter->getMaxSize())) {
52             return false;
53         }
54     }
55     return true;
56 }
57
58 bool NodeFilterMatcher::matchString(const std::string& value,
59         const OptionalString& filter)
60 {
61     if (!filter.IsNull()) {
62         return pcrecpp::RE(*filter).PartialMatch(value);
63     }
64     return true;
65 }
66
67 template<typename Type>
68 bool NodeFilterMatcher::matchRange(const Type& value,
69         const DPL::Optional<Type>& min,
70         const DPL::Optional<Type>& max)
71 {
72     if ((!min.IsNull() && (value < *min)) ||
73         (!max.IsNull() && (value > *max))) {
74         return false;
75     }
76     return true;
77 }
78
79 } // Filesystem
80 } // WrtDeviceApis