Skip to content

Commit

Permalink
Merge parse lines implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrugman committed Dec 28, 2023
1 parent 51e44ab commit 7a55a6a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 51 deletions.
13 changes: 2 additions & 11 deletions include/pfs/parsers/lines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template <typename Inserter>
using inserted_type = typename Inserter::container_type::value_type;

template <typename Inserter>
void parse_and_filter_lines(
void parse_file_lines(
const std::string& path,
Inserter inserter,
std::function<inserted_type<Inserter>(const std::string&)> parser,
Expand All @@ -58,6 +58,7 @@ void parse_and_filter_lines(
}

auto inserted = parser(line);

if (filter && filter(inserted))
{
continue;
Expand All @@ -67,16 +68,6 @@ void parse_and_filter_lines(
}
}

template <typename Inserter>
void parse_lines(
const std::string& path,
Inserter inserter,
std::function<inserted_type<Inserter>(const std::string&)> parser,
size_t lines_to_skip = 0)
{
parse_and_filter_lines(path, inserter, parser, nullptr, lines_to_skip);
}

} // namespace parsers
} // namespace impl
} // namespace pfs
Expand Down
33 changes: 17 additions & 16 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ std::vector<net_device> net::get_dev(net_device_filter filter) const
static const size_t HEADER_LINES = 2;

std::vector<net_device> output;
parsers::parse_and_filter_lines(path, std::back_inserter(output),
parsers::parse_net_device_line, filter, HEADER_LINES);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_net_device_line,
filter, HEADER_LINES);
return output;
}

Expand Down Expand Up @@ -117,9 +118,9 @@ std::vector<netlink_socket> net::get_netlink(netlink_socket_filter filter) const
static const size_t HEADER_LINES = 1;

std::vector<netlink_socket> output;
parsers::parse_and_filter_lines(path, std::back_inserter(output),
parsers::parse_netlink_socket_line,
filter, HEADER_LINES);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_netlink_socket_line,
filter, HEADER_LINES);
return output;
}

Expand All @@ -131,9 +132,9 @@ std::vector<unix_socket> net::get_unix(unix_socket_filter filter) const
static const size_t HEADER_LINES = 1;

std::vector<unix_socket> output;
parsers::parse_and_filter_lines(path, std::back_inserter(output),
parsers::parse_unix_socket_line,
filter, HEADER_LINES);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_unix_socket_line,
filter, HEADER_LINES);
return output;
}

Expand All @@ -145,23 +146,23 @@ std::vector<net_socket> net::get_net_sockets(const std::string& file,
static const size_t HEADER_LINES = 1;

std::vector<net_socket> output;
parsers::parse_and_filter_lines(path, std::back_inserter(output),
parsers::parse_net_socket_line,
filter, HEADER_LINES);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_net_socket_line,
filter, HEADER_LINES);
return output;
}

std::vector<net_route> net::get_route(net_route_filter filter) const
{
static const size_t HEADER_LINES = 1;

static const std::string ROUTES_FILE("route");
auto path = _net_root + ROUTES_FILE;

static const size_t HEADER_LINES = 1;

std::vector<net_route> output;
parsers::parse_and_filter_lines(path, std::back_inserter(output),
parsers::parse_net_route_line,
filter, HEADER_LINES);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_net_route_line,
filter, HEADER_LINES);
return output;
}

Expand Down
25 changes: 13 additions & 12 deletions src/procfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,22 @@ std::vector<zone> procfs::get_buddyinfo() const
auto path = _root + BUDDYINFO_FILE;

std::vector<zone> output;
parsers::parse_lines(path, std::back_inserter(output),
parsers::parse_buddyinfo_line);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_buddyinfo_line);
return output;
}

std::vector<cgroup_controller> procfs::get_cgroups() const
{
static const size_t HEADER_LINES = 1;

static const std::string CGROUPS_FILE("cgroups");
auto path = _root + CGROUPS_FILE;

static const size_t HEADER_LINES = 1;

std::vector<cgroup_controller> output;
parsers::parse_lines(path, std::back_inserter(output),
parsers::parse_cgroup_controller_line, HEADER_LINES);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_cgroup_controller_line,
/* filter = */ nullptr, HEADER_LINES);
return output;
}

Expand All @@ -124,8 +125,8 @@ std::unordered_map<std::string, bool> procfs::get_filesystems() const
auto path = _root + FILESYSTEMS_FILE;

std::unordered_map<std::string, bool> output;
parsers::parse_lines(path, std::inserter(output, output.begin()),
parsers::parse_filesystems_line);
parsers::parse_file_lines(path, std::inserter(output, output.begin()),
parsers::parse_filesystems_line);
return output;
}

Expand All @@ -135,8 +136,8 @@ std::unordered_map<std::string, size_t> procfs::get_meminfo() const
auto path = _root + MEMINFO_FILE;

std::unordered_map<std::string, size_t> output;
parsers::parse_lines(path, std::inserter(output, output.begin()),
parsers::parse_meminfo_line);
parsers::parse_file_lines(path, std::inserter(output, output.begin()),
parsers::parse_meminfo_line);
return output;
}

Expand Down Expand Up @@ -172,8 +173,8 @@ std::vector<module> procfs::get_modules() const
auto path = _root + MODULES_FILE;

std::vector<module> output;
parsers::parse_lines(path, std::back_inserter(output),
parsers::parse_modules_line);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_modules_line);
return output;
}

Expand Down
20 changes: 10 additions & 10 deletions src/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ std::vector<cgroup> task::get_cgroups() const
auto path = _task_root + CGROUP_FILE;

std::vector<cgroup> output;
parsers::parse_lines(path, std::back_inserter(output),
parsers::parse_cgroup_line);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_cgroup_line);
return output;
}

Expand Down Expand Up @@ -345,8 +345,8 @@ std::vector<mem_region> task::get_maps() const
auto path = _task_root + MAPS_FILE;

std::vector<mem_region> output;
parsers::parse_lines(path, std::back_inserter(output),
parsers::parse_maps_line);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_maps_line);
return output;
}

Expand All @@ -364,8 +364,8 @@ std::vector<mount> task::get_mountinfo() const
auto path = _task_root + MOUNTINFO_FILE;

std::vector<mount> output;
parsers::parse_lines(path, std::back_inserter(output),
parsers::parse_mountinfo_line);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_mountinfo_line);
return output;
}

Expand Down Expand Up @@ -473,8 +473,8 @@ std::vector<id_map> task::get_uid_map() const
auto path = _task_root + UID_MAP_FILE;

std::vector<id_map> output;
parsers::parse_lines(path, std::back_inserter(output),
parsers::parse_id_map_line);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_id_map_line);
return output;
}

Expand All @@ -484,8 +484,8 @@ std::vector<id_map> task::get_gid_map() const
auto path = _task_root + GID_MAP_FILE;

std::vector<id_map> output;
parsers::parse_lines(path, std::back_inserter(output),
parsers::parse_id_map_line);
parsers::parse_file_lines(path, std::back_inserter(output),
parsers::parse_id_map_line);
return output;
}

Expand Down
4 changes: 2 additions & 2 deletions test/test_parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TEST_CASE("Parse lines move()-s", "[parsers]")
pfs::impl::defer unlink_temp_file([&file] { unlink(file.c_str()); });

std::vector<obj> output;
parse_lines(file, std::back_inserter(output), parser);
parse_file_lines(file, std::back_inserter(output), parser);

REQUIRE(output.size() == content.size());
for (size_t i = 0; i < output.size(); ++i)
Expand Down Expand Up @@ -105,6 +105,6 @@ TEST_CASE("Parse lines functionality", "[parsers]")
}

file = create_temp_file(content);
parse_and_filter_lines(file, std::back_inserter(output), parser, filter, skipped);
parse_file_lines(file, std::back_inserter(output), parser, filter, skipped);
REQUIRE(output == expected);
}

0 comments on commit 7a55a6a

Please sign in to comment.