From 809807e984b8161a156735bf66496e81bfc666e3 Mon Sep 17 00:00:00 2001 From: Daniel Trugman Date: Wed, 27 Dec 2023 23:48:06 +0000 Subject: [PATCH] Split parsers/generic.hpp - Move parse lines into lines.hpp - Move to_number into number.hpp - Move to_sequence into proc_stat.cpp --- .../pfs/parsers/{generic.hpp => lines.hpp} | 62 ------------------- include/pfs/parsers/number.hpp | 51 +++++++++++++++ src/block.cpp | 2 +- src/block_queue.cpp | 2 +- src/net.cpp | 2 +- src/parsers/proc_stat.cpp | 46 +++++++++++++- src/parsers/task_io.cpp | 2 +- src/procfs.cpp | 2 +- src/task.cpp | 2 +- test/test_parsers.cpp | 2 +- 10 files changed, 103 insertions(+), 70 deletions(-) rename include/pfs/parsers/{generic.hpp => lines.hpp} (57%) create mode 100644 include/pfs/parsers/number.hpp diff --git a/include/pfs/parsers/generic.hpp b/include/pfs/parsers/lines.hpp similarity index 57% rename from include/pfs/parsers/generic.hpp rename to include/pfs/parsers/lines.hpp index a34c5d8..086d1cd 100644 --- a/include/pfs/parsers/generic.hpp +++ b/include/pfs/parsers/lines.hpp @@ -77,68 +77,6 @@ void parse_lines( parse_and_filter_lines(path, inserter, parser, nullptr, lines_to_skip); } -template -static void to_number(const std::string& value, T& out, - utils::base base = utils::base::decimal) -{ - try - { - utils::stot(value, out, base); - } - catch (const std::invalid_argument& ex) - { - throw parser_error("Corrupted number - Invalid argument", value); - } - catch (const std::out_of_range& ex) - { - throw parser_error("Corrupted number - Out of range", value); - } -} - -template -static void to_sequence(const std::string& value, proc_stat::sequence& out) -{ - // Some examples: - // clang-format off - // 975101428 40707218 345522235 433770 2054357 19668 0 1807723 381659448 33954 202863055 - // clang-format on - - enum token - { - TOTAL = 0, - MIN_COUNT = 1, - }; - - auto tokens = utils::split(value); - if (tokens.size() < MIN_COUNT) - { - throw parser_error("Corrupted sequence - Unexpected tokens count", - value); - } - - try - { - proc_stat::sequence sequence; - - utils::stot(tokens[TOTAL], out.total); - - for (size_t i = MIN_COUNT; i < tokens.size(); i++) - { - unsigned long long value; - utils::stot(tokens[i], value); - out.per_item.push_back(value); - } - } - catch (const std::invalid_argument& ex) - { - throw parser_error("Corrupted sequence - Invalid argument", value); - } - catch (const std::out_of_range& ex) - { - throw parser_error("Corrupted sequence - Out of range", value); - } -} - } // namespace parsers } // namespace impl } // namespace pfs diff --git a/include/pfs/parsers/number.hpp b/include/pfs/parsers/number.hpp new file mode 100644 index 0000000..8f7ff30 --- /dev/null +++ b/include/pfs/parsers/number.hpp @@ -0,0 +1,51 @@ +/* + * Copyright 2020-present Daniel Trugman + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PFS_PARSERS_NUMBER_HPP +#define PFS_PARSERS_NUMBER_HPP + +#include + +#include "pfs/parser_error.hpp" +#include "pfs/utils.hpp" + +namespace pfs { +namespace impl { +namespace parsers { + +template +static void to_number(const std::string& value, T& out, + utils::base base = utils::base::decimal) +{ + try + { + utils::stot(value, out, base); + } + catch (const std::invalid_argument& ex) + { + throw parser_error("Corrupted number - Invalid argument", value); + } + catch (const std::out_of_range& ex) + { + throw parser_error("Corrupted number - Out of range", value); + } +} + +} // namespace parsers +} // namespace impl +} // namespace pfs + +#endif // PFS_PARSERS_NUMBER_HPP diff --git a/src/block.cpp b/src/block.cpp index d1835ae..806b2a6 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -19,7 +19,7 @@ #include "pfs/defer.hpp" #include "pfs/parsers/block_stat.hpp" #include "pfs/parsers/common.hpp" -#include "pfs/parsers/generic.hpp" +#include "pfs/parsers/number.hpp" #include "pfs/block.hpp" #include "pfs/utils.hpp" diff --git a/src/block_queue.cpp b/src/block_queue.cpp index 3ea582a..9e2db54 100644 --- a/src/block_queue.cpp +++ b/src/block_queue.cpp @@ -18,7 +18,7 @@ #include "pfs/defer.hpp" #include "pfs/parsers/common.hpp" -#include "pfs/parsers/generic.hpp" +#include "pfs/parsers/number.hpp" #include "pfs/block_queue.hpp" namespace pfs { diff --git a/src/net.cpp b/src/net.cpp index 2b3f878..484837e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -20,7 +20,7 @@ #include "pfs/parsers/net_socket.hpp" #include "pfs/parsers/unix_socket.hpp" #include "pfs/parsers/netlink_socket.hpp" -#include "pfs/parsers/generic.hpp" +#include "pfs/parsers/lines.hpp" namespace pfs { diff --git a/src/parsers/proc_stat.cpp b/src/parsers/proc_stat.cpp index 4dc795b..2a575c1 100644 --- a/src/parsers/proc_stat.cpp +++ b/src/parsers/proc_stat.cpp @@ -17,7 +17,7 @@ #include #include -#include "pfs/parsers/generic.hpp" +#include "pfs/parsers/number.hpp" #include "pfs/parsers/proc_stat.hpp" #include "pfs/utils.hpp" @@ -27,6 +27,50 @@ namespace parsers { namespace { +template +static void to_sequence(const std::string& value, proc_stat::sequence& out) +{ + // Some examples: + // clang-format off + // 975101428 40707218 345522235 433770 2054357 19668 0 1807723 381659448 33954 202863055 + // clang-format on + + enum token + { + TOTAL = 0, + MIN_COUNT = 1, + }; + + auto tokens = utils::split(value); + if (tokens.size() < MIN_COUNT) + { + throw parser_error("Corrupted sequence - Unexpected tokens count", + value); + } + + try + { + proc_stat::sequence sequence; + + utils::stot(tokens[TOTAL], out.total); + + for (size_t i = MIN_COUNT; i < tokens.size(); i++) + { + unsigned long long value; + utils::stot(tokens[i], value); + out.per_item.push_back(value); + } + } + catch (const std::invalid_argument& ex) + { + throw parser_error("Corrupted sequence - Invalid argument", value); + } + catch (const std::out_of_range& ex) + { + throw parser_error("Corrupted sequence - Out of range", value); + } +} + static void to_cpu(const std::string& value, proc_stat::cpu& out) { // Some examples: diff --git a/src/parsers/task_io.cpp b/src/parsers/task_io.cpp index 5e6cbec..cb210fc 100644 --- a/src/parsers/task_io.cpp +++ b/src/parsers/task_io.cpp @@ -15,7 +15,7 @@ */ -#include "pfs/parsers/generic.hpp" +#include "pfs/parsers/number.hpp" #include "pfs/parsers/task_io.hpp" #include "pfs/utils.hpp" diff --git a/src/procfs.cpp b/src/procfs.cpp index eaebe1e..0d808ac 100644 --- a/src/procfs.cpp +++ b/src/procfs.cpp @@ -27,7 +27,7 @@ #include "pfs/parsers/loadavg.hpp" #include "pfs/parsers/uptime.hpp" #include "pfs/parsers/modules.hpp" -#include "pfs/parsers/generic.hpp" +#include "pfs/parsers/lines.hpp" #include "pfs/parsers/proc_stat.hpp" #include "pfs/procfs.hpp" #include "pfs/utils.hpp" diff --git a/src/task.cpp b/src/task.cpp index 9cc3d75..a5a0dbc 100644 --- a/src/task.cpp +++ b/src/task.cpp @@ -31,7 +31,7 @@ #include "pfs/parsers/cgroup.hpp" #include "pfs/parsers/maps.hpp" #include "pfs/parsers/mountinfo.hpp" -#include "pfs/parsers/generic.hpp" +#include "pfs/parsers/lines.hpp" #include "pfs/parsers/common.hpp" #include "pfs/parsers/task_io.hpp" #include "pfs/parsers/task_status.hpp" diff --git a/test/test_parsers.cpp b/test/test_parsers.cpp index d123a99..d599ea4 100644 --- a/test/test_parsers.cpp +++ b/test/test_parsers.cpp @@ -2,7 +2,7 @@ #include "test_utils.hpp" #include "pfs/defer.hpp" -#include "pfs/parsers/generic.hpp" +#include "pfs/parsers/lines.hpp" using namespace pfs::impl::parsers;