Skip to content

Commit 4e1276e

Browse files
committedJan 4, 2024
Use an enum for the filter return type
1 parent 9b86977 commit 4e1276e

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed
 

‎include/pfs/filter.hpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2020-present Daniel Trugman
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+
#ifndef PFS_FILTER_HPP
18+
#define PFS_FILTER_HPP
19+
20+
namespace pfs {
21+
namespace filter {
22+
23+
enum class action {
24+
drop,
25+
keep,
26+
};
27+
28+
} // namespace filter
29+
} // namespace pfs
30+
31+
#endif // PFS_FILTER_HPP

‎include/pfs/net.hpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include "types.hpp"
25+
#include "filter.hpp"
2526

2627
namespace pfs {
2728

@@ -42,11 +43,11 @@ class net final
4243
net& operator=(net&&) = delete;
4344

4445
public:
45-
using net_device_filter = std::function<bool(const net_device&)>;
46-
using net_socket_filter = std::function<bool(const net_socket&)>;
47-
using netlink_socket_filter = std::function<bool(const netlink_socket&)>;
48-
using unix_socket_filter = std::function<bool(const unix_socket&)>;
49-
using net_route_filter = std::function<bool(const net_route&)>;
46+
using net_device_filter = std::function<filter::action(const net_device&)>;
47+
using net_socket_filter = std::function<filter::action(const net_socket&)>;
48+
using netlink_socket_filter = std::function<filter::action(const netlink_socket&)>;
49+
using unix_socket_filter = std::function<filter::action(const unix_socket&)>;
50+
using net_route_filter = std::function<filter::action(const net_route&)>;
5051

5152
public:
5253
std::vector<net_device> get_dev(net_device_filter filter = nullptr) const;

‎include/pfs/parsers/lines.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "pfs/parser_error.hpp"
2424
#include "pfs/utils.hpp"
25+
#include "pfs/filter.hpp"
2526

2627
namespace pfs {
2728
namespace impl {
@@ -35,7 +36,7 @@ void parse_file_lines(
3536
const std::string& path,
3637
Inserter inserter,
3738
std::function<inserted_type<Inserter>(const std::string&)> parser,
38-
std::function<bool(const inserted_type<Inserter>&)> filter = nullptr,
39+
std::function<filter::action(const inserted_type<Inserter>&)> filter = nullptr,
3940
size_t lines_to_skip = 0)
4041
{
4142
std::ifstream in(path);
@@ -59,7 +60,7 @@ void parse_file_lines(
5960

6061
auto inserted = parser(line);
6162

62-
if (filter && filter(inserted))
63+
if (filter && filter(inserted) != filter::action::keep)
6364
{
6465
continue;
6566
}

‎sample/tool_netstat.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ void task_netstat(const pfs::task& task, const std::string& type)
3131

3232
auto inodes = task.get_fds_inodes();
3333
pfs::net::net_socket_filter filter = [&inodes](const pfs::net_socket& sock){
34-
return inodes.find(sock.inode) == inodes.end();
34+
return inodes.find(sock.inode) != inodes.end()
35+
? pfs::filter::action::keep
36+
: pfs::filter::action::drop;
3537
};
3638

3739
auto net = task.get_net();

‎test/test_parsers.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ TEST_CASE("Parse lines functionality", "[parsers]")
7070
std::vector<std::string> content;
7171
std::vector<std::string> expected;
7272
std::vector<std::string> output;
73-
std::function<bool(const std::string&)> filter = nullptr;
73+
std::function<pfs::filter::action(const std::string&)> filter = nullptr;
7474
size_t skipped = 0;
7575

7676
std::string file;
@@ -101,7 +101,9 @@ TEST_CASE("Parse lines functionality", "[parsers]")
101101
{
102102
content = {"a", "x", "x", "b", "x", "c"};
103103
expected = {"a", "b", "c"};
104-
filter = [](const std::string& entry) { return entry == "x"; };
104+
filter = [](const std::string& entry) {
105+
return entry == "x" ? pfs::filter::action::drop : pfs::filter::action::keep;
106+
};
105107
}
106108

107109
file = create_temp_file(content);

0 commit comments

Comments
 (0)
Please sign in to comment.