Coverage Report

Created: 2025-06-13 06:45

/src/Fast-DDS/src/cpp/utils/StringMatching.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
 * @file StringMatching.cpp
17
 *
18
 */
19
20
#include <utils/StringMatching.hpp>
21
#include <limits.h>
22
#include <errno.h>
23
24
#if defined(__cplusplus_winrt)
25
#include <algorithm>
26
#include <regex>
27
#elif defined(_WIN32)
28
#include "Shlwapi.h"
29
#else
30
#include <fnmatch.h>
31
#endif // if defined(__cplusplus_winrt)
32
33
namespace eprosima {
34
namespace fastdds {
35
namespace rtps {
36
37
StringMatching::StringMatching()
38
0
{
39
    // TODO Auto-generated constructor stub
40
41
0
}
42
43
StringMatching::~StringMatching()
44
0
{
45
    // TODO Auto-generated destructor stub
46
0
}
47
48
#if defined(__cplusplus_winrt)
49
void replace_all(
50
        std::string& subject,
51
        const std::string& search,
52
        const std::string& replace)
53
{
54
    size_t pos = 0;
55
    while ((pos = subject.find(search, pos)) != std::string::npos)
56
    {
57
        subject.replace(pos, search.length(), replace);
58
        pos += replace.length();
59
    }
60
}
61
62
bool StringMatching::matchPattern(
63
        const char* pattern,
64
        const char* str)
65
{
66
    std::string path(pattern);
67
    std::string spec(str);
68
69
    replace_all(pattern, "*", ".*");
70
    replace_all(pattern, "?", ".");
71
72
    std::regex path_regex(path);
73
    std::smatch spec_match;
74
    if (std::regex_match(spec, spec_match, path_regex))
75
    {
76
        return true;
77
    }
78
79
    return false;
80
}
81
82
bool StringMatching::matchString(
83
        const char* str1,
84
        const char* str2)
85
{
86
    if (StringMatching::matchPattern(str1, str2))
87
    {
88
        return true;
89
    }
90
91
    if (StringMatching::matchPattern(str2, str1))
92
    {
93
        return true;
94
    }
95
96
    return false;
97
}
98
99
#elif defined(_WIN32)
100
bool StringMatching::matchPattern(
101
        const char* pattern,
102
        const char* str)
103
{
104
    if (PathMatchSpecA(str, pattern))
105
    {
106
        return true;
107
    }
108
    return false;
109
}
110
111
bool StringMatching::matchString(
112
        const char* str1,
113
        const char* str2)
114
{
115
    if (PathMatchSpecA(str1, str2))
116
    {
117
        return true;
118
    }
119
    if (PathMatchSpecA(str2, str1))
120
    {
121
        return true;
122
    }
123
    return false;
124
}
125
126
#else
127
bool StringMatching::matchPattern(
128
        const char* pattern,
129
        const char* str)
130
0
{
131
0
    if (fnmatch(pattern, str, FNM_NOESCAPE) == 0)
132
0
    {
133
0
        return true;
134
0
    }
135
0
    return false;
136
0
}
137
138
bool StringMatching::matchString(
139
        const char* str1,
140
        const char* str2)
141
0
{
142
0
    if (fnmatch(str1, str2, FNM_NOESCAPE) == 0)
143
0
    {
144
0
        return true;
145
0
    }
146
0
    if (fnmatch(str2, str1, FNM_NOESCAPE) == 0)
147
0
    {
148
0
        return true;
149
0
    }
150
0
    return false;
151
0
}
152
153
#endif // if defined(__cplusplus_winrt)
154
155
} /* namespace rtps */
156
} /* namespace fastdds */
157
} /* namespace eprosima */