diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-22 01:59:09 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-22 01:59:09 +0300 |
commit | a8e5ae38e88530eabfa06d63ecc5f321bb607e7c (patch) | |
tree | ac3ae869d1831959bf3ec1897fafeb56be6d77ff /utils | |
parent | utils: refactor command-line arguments parsing (diff) | |
download | aes-tools-a8e5ae38e88530eabfa06d63ecc5f321bb607e7c.tar.gz aes-tools-a8e5ae38e88530eabfa06d63ecc5f321bb607e7c.zip |
utils: code style
Diffstat (limited to '')
-rw-r--r-- | utils/block_cmd_parser.hpp | 175 | ||||
-rw-r--r-- | utils/file_cmd_parser.hpp | 117 | ||||
-rw-r--r-- | utils/helpers/command_line.hpp | 1 |
3 files changed, 150 insertions, 143 deletions
diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp index 58f86d8..c748b3d 100644 --- a/utils/block_cmd_parser.hpp +++ b/utils/block_cmd_parser.hpp @@ -19,106 +19,109 @@ #include <utility> #include <vector> -class BlockSettings : public command_line::SettingsParser +namespace { -public: - aes::Algorithm algorithm = AES_AES128; - aes::Mode mode = AES_ECB; + class BlockSettings : public command_line::SettingsParser + { + public: + aes::Algorithm algorithm = AES_AES128; + aes::Mode mode = AES_ECB; - bool use_boxes = false; - bool verbose = false; + bool use_boxes = false; + bool verbose = false; - std::vector<Input> inputs; + std::vector<Input> inputs; - explicit BlockSettings(const std::string& argv0) - : SettingsParser{argv0} - { - visible.add_options() - ("verbose,v", - boost::program_options::bool_switch(&verbose), - "enable verbose output") - ("algorithm,a", - boost::program_options::value<aes::Algorithm>(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value<aes::Mode>(&mode) - ->required() - ->value_name("MODE"), - "set mode of operation") - ("use-boxes,b", - boost::program_options::bool_switch(&use_boxes), - "use the \"boxes\" interface"); - hidden.add_options() - ("args", - boost::program_options::value<std::vector<std::string>>(&args), - "shouldn't be visible"); - positional.add("args", -1); - } - - const char* get_short_description() const override - { - return "[-h|--help] [-v|--verbose] [-a|--algorithm NAME] [-m|--mode MODE]" - " [-- KEY [IV] [BLOCK]...]..."; - } + explicit BlockSettings(const std::string& argv0) + : SettingsParser{argv0} + { + visible.add_options() + ("verbose,v", + boost::program_options::bool_switch(&verbose), + "enable verbose output") + ("algorithm,a", + boost::program_options::value<aes::Algorithm>(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm") + ("mode,m", + boost::program_options::value<aes::Mode>(&mode) + ->required() + ->value_name("MODE"), + "set mode of operation") + ("use-boxes,b", + boost::program_options::bool_switch(&use_boxes), + "use the \"boxes\" interface"); + hidden.add_options() + ("args", + boost::program_options::value<std::vector<std::string>>(&args), + "shouldn't be visible"); + positional.add("args", -1); + } - void parse(int argc, char* argv[]) override - { - SettingsParser::parse(argc, argv); - parse_inputs(std::deque<std::string>{ - std::make_move_iterator(args.begin()), - std::make_move_iterator(args.end())}); - } - -private: - void parse_inputs(std::deque<std::string>&& src) - { - while (!src.empty()) - inputs.emplace_back(parse_input(src)); - } + const char* get_short_description() const override + { + return "[-h|--help] [-v|--verbose] [-a|--algorithm NAME] [-m|--mode MODE]" + " [-- KEY [IV] [BLOCK]...]..."; + } - Input parse_input(std::deque<std::string>& src) const - { - std::string key{std::move(src.front())}; - src.pop_front(); + void parse(int argc, char* argv[]) override + { + SettingsParser::parse(argc, argv); + parse_inputs(std::deque<std::string>{ + std::make_move_iterator(args.begin()), + std::make_move_iterator(args.end())}); + } - std::string iv; + private: + void parse_inputs(std::deque<std::string>&& src) + { + while (!src.empty()) + inputs.emplace_back(parse_input(src)); + } - if (aes::mode_requires_init_vector(mode)) + Input parse_input(std::deque<std::string>& src) const { - if (src.empty()) - { - throw boost::program_options::error{ - "an initialization vector is required for the selected mode of operation"}; - } - iv = std::move(src.front()); + std::string key{std::move(src.front())}; src.pop_front(); - } - auto blocks = parse_blocks(src); + std::string iv; - if (aes::mode_requires_init_vector(mode)) - return {key, iv, std::move(blocks)}; - else - return {key, std::move(blocks)}; - } + if (aes::mode_requires_init_vector(mode)) + { + if (src.empty()) + { + throw boost::program_options::error{ + "an initialization vector is required for the selected mode of operation"}; + } + iv = std::move(src.front()); + src.pop_front(); + } - static std::vector<std::string> parse_blocks(std::deque<std::string>& src) - { - std::vector<std::string> blocks; + auto blocks = parse_blocks(src); - while (!src.empty()) - { - std::string block{std::move(src.front())}; - src.pop_front(); - if (block == "--") - break; - blocks.emplace_back(std::move(block)); + if (aes::mode_requires_init_vector(mode)) + return {key, iv, std::move(blocks)}; + else + return {key, std::move(blocks)}; } - return blocks; - } + static std::vector<std::string> parse_blocks(std::deque<std::string>& src) + { + std::vector<std::string> blocks; + + while (!src.empty()) + { + std::string block{std::move(src.front())}; + src.pop_front(); + if (block == "--") + break; + blocks.emplace_back(std::move(block)); + } + + return blocks; + } - std::vector<std::string> args; -}; + std::vector<std::string> args; + }; +} diff --git a/utils/file_cmd_parser.hpp b/utils/file_cmd_parser.hpp index e199409..6d7a962 100644 --- a/utils/file_cmd_parser.hpp +++ b/utils/file_cmd_parser.hpp @@ -17,67 +17,70 @@ #include <string> #include <utility> -class FileSettings : public command_line::SettingsParser +namespace { -public: - aes::Algorithm algorithm = AES_AES128; - aes::Mode mode = AES_ECB; - - std::string input_path; - std::string output_path; - std::string key; - std::string iv; - - explicit FileSettings(const std::string& argv0) - : SettingsParser{argv0} + class FileSettings : public command_line::SettingsParser { - visible.add_options() - ("algorithm,a", - boost::program_options::value<aes::Algorithm>(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value<aes::Mode>(&mode) - ->required() - ->value_name("MODE"), - "set mode of operation") - ("key,k", - boost::program_options::value<std::string>(&key) - ->required() - ->value_name("KEY"), - "set encryption key") - ("iv,v", - boost::program_options::value<std::string>(&iv) - ->value_name("BLOCK"), - "set initialization vector") - ("input,i", - boost::program_options::value<std::string>(&input_path) - ->required() - ->value_name("PATH"), - "set input file path") - ("output,o", - boost::program_options::value<std::string>(&output_path) - ->required() - ->value_name("PATH"), - "set output file path"); - } + public: + aes::Algorithm algorithm = AES_AES128; + aes::Mode mode = AES_ECB; - const char* get_short_description() const override - { - return "[-h|--help] [-a|--algorithm NAME] [-m|--mode MODE]" - " [-k|--key KEY] [-v|--iv BLOCK]" - " [-i|--input PATH] [-o|--output PATH]"; - } + std::string input_path; + std::string output_path; + std::string key; + std::string iv; - void parse(int argc, char** argv) override - { - SettingsParser::parse(argc, argv); + explicit FileSettings(const std::string& argv0) + : SettingsParser{argv0} + { + visible.add_options() + ("algorithm,a", + boost::program_options::value<aes::Algorithm>(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm") + ("mode,m", + boost::program_options::value<aes::Mode>(&mode) + ->required() + ->value_name("MODE"), + "set mode of operation") + ("key,k", + boost::program_options::value<std::string>(&key) + ->required() + ->value_name("KEY"), + "set encryption key") + ("iv,v", + boost::program_options::value<std::string>(&iv) + ->value_name("BLOCK"), + "set initialization vector") + ("input,i", + boost::program_options::value<std::string>(&input_path) + ->required() + ->value_name("PATH"), + "set input file path") + ("output,o", + boost::program_options::value<std::string>(&output_path) + ->required() + ->value_name("PATH"), + "set output file path"); + } - if (aes::mode_requires_init_vector(mode) && iv.empty()) + const char* get_short_description() const override { - throw boost::program_options::error{ - "an initialization vector is required for the selected mode of operation"}; + return "[-h|--help] [-a|--algorithm NAME] [-m|--mode MODE]" + " [-k|--key KEY] [-v|--iv BLOCK]" + " [-i|--input PATH] [-o|--output PATH]"; + } + + void parse(int argc, char** argv) override + { + SettingsParser::parse(argc, argv); + + if (aes::mode_requires_init_vector(mode) && iv.empty()) + { + throw boost::program_options::error{ + "an initialization vector is required for the selected mode of operation"}; + } } - } -}; + }; +} diff --git a/utils/helpers/command_line.hpp b/utils/helpers/command_line.hpp index 4843a06..2e4d803 100644 --- a/utils/helpers/command_line.hpp +++ b/utils/helpers/command_line.hpp @@ -68,6 +68,7 @@ namespace command_line boost::program_options::options_description visible; boost::program_options::positional_options_description positional; + private: static std::string extract_filename(const std::string& path) { return boost::filesystem::path{path}.filename().string(); |