Skip to content

Commit

Permalink
Split parsers/generic.hpp
Browse files Browse the repository at this point in the history
- Move parse lines into lines.hpp
- Move to_number into number.hpp
- Move to_sequence into proc_stat.cpp
  • Loading branch information
dtrugman committed Dec 27, 2023
1 parent 2bcec87 commit 51e44ab
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 70 deletions.
62 changes: 0 additions & 62 deletions include/pfs/parsers/generic.hpp → include/pfs/parsers/lines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,68 +77,6 @@ void parse_lines(
parse_and_filter_lines(path, inserter, parser, nullptr, lines_to_skip);
}

template <typename T>
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 <typename T>
static void to_sequence(const std::string& value, proc_stat::sequence<T>& 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<T> 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
Expand Down
51 changes: 51 additions & 0 deletions include/pfs/parsers/number.hpp
Original file line number Diff line number Diff line change
@@ -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 <string>

#include "pfs/parser_error.hpp"
#include "pfs/utils.hpp"

namespace pfs {
namespace impl {
namespace parsers {

template <typename T>
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
2 changes: 1 addition & 1 deletion src/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion src/block_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
46 changes: 45 additions & 1 deletion src/parsers/proc_stat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <chrono>
#include <cstring>

#include "pfs/parsers/generic.hpp"
#include "pfs/parsers/number.hpp"
#include "pfs/parsers/proc_stat.hpp"
#include "pfs/utils.hpp"

Expand All @@ -27,6 +27,50 @@ namespace parsers {

namespace {

template <typename T>
static void to_sequence(const std::string& value, proc_stat::sequence<T>& 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<T> 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:
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/task_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/


#include "pfs/parsers/generic.hpp"
#include "pfs/parsers/number.hpp"
#include "pfs/parsers/task_io.hpp"
#include "pfs/utils.hpp"

Expand Down
2 changes: 1 addition & 1 deletion src/procfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/test_parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 51e44ab

Please sign in to comment.