From 188460a91bd37ca6f18fb89e448819b642953be7 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 4 May 2017 04:08:15 +0300 Subject: code style & refactoring --- utils/block_cmd_parser.hpp | 90 +++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 46 deletions(-) (limited to 'utils/block_cmd_parser.hpp') diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp index 70452e3..9c9d61c 100644 --- a/utils/block_cmd_parser.hpp +++ b/utils/block_cmd_parser.hpp @@ -13,8 +13,8 @@ #include #include -#include #include +#include #include #include #include @@ -86,9 +86,9 @@ namespace po::notify(vm); - parse_inputs(settings, inputs, std::deque( + inputs = parse_inputs(settings, std::deque{ std::make_move_iterator(args.begin()), - std::make_move_iterator(args.end()))); + std::make_move_iterator(args.end())}); return settings; } @@ -96,57 +96,55 @@ namespace bool exit_with_usage() const { return help_flag; } private: - static void parse_inputs( + static std::vector parse_inputs( const Settings& settings, - std::vector& inputs, std::deque&& args) { + std::vector inputs; while (!args.empty()) + inputs.emplace_back(parse_input(settings, args)); + return inputs; + } + + static Input parse_input( + const Settings& settings, + std::deque& args) + { + std::string key{std::move(args.front())}; + args.pop_front(); + + std::string iv; + + if (aes::mode_requires_init_vector(settings.mode)) { - auto key_string = std::move(args.front()); + if (args.empty()) + throw boost::program_options::error{"an initialization vector is required for the selected mode of operation"}; + iv = std::move(args.front()); args.pop_front(); + } - std::string iv_string; - - if (aes::mode_requires_init_vector(settings.mode)) - { - if (args.empty()) - { - throw boost::program_options::error( - "an initialization vector is required for the selected mode of operation"); - } - iv_string = std::move(args.front()); - args.pop_front(); - } - - std::vector input_block_strings; - - while (!args.empty()) - { - if (args.front() == "--") - { - args.pop_front(); - break; - } - - input_block_strings.emplace_back(std::move(args.front())); - args.pop_front(); - } - - if (aes::mode_requires_init_vector(settings.mode)) - { - inputs.emplace_back( - std::move(key_string), - std::move(iv_string), - std::move(input_block_strings)); - } - else - { - inputs.emplace_back( - std::move(key_string), - std::move(input_block_strings)); - } + auto blocks = parse_blocks(args); + + if (aes::mode_requires_init_vector(settings.mode)) + return {key, iv, std::move(blocks)}; + else + return {key, std::move(blocks)}; + } + + static std::vector parse_blocks(std::deque& args) + { + std::vector blocks; + + while (!args.empty()) + { + std::string block{std::move(args.front())}; + args.pop_front(); + if (block == "--") + break; + blocks.emplace_back(std::move(block)); } + + return blocks; } const std::string prog_name; -- cgit v1.2.3