diff --git a/src/applib/CMakeLists.txt b/src/applib/CMakeLists.txt index 96ca7a9..3ffc368 100644 --- a/src/applib/CMakeLists.txt +++ b/src/applib/CMakeLists.txt @@ -39,7 +39,7 @@ target_sources(applib PRIVATE smartctl_executor.cpp smartctl_executor_gui.h smartctl_executor.h - smartctl_output_type.h + smartctl_parser_types.h smartctl_text_parser_helper.cpp smartctl_text_parser_helper.h smartctl_version_parser.cpp diff --git a/src/applib/selftest.cpp b/src/applib/selftest.cpp index b3d22ef..e254c3a 100644 --- a/src/applib/selftest.cpp +++ b/src/applib/selftest.cpp @@ -243,7 +243,7 @@ std::string SelfTest::update(const std::shared_ptr& smartctl_ex return error_msg; AtaStorageAttribute::DiskType disk_type = drive_->get_is_hdd() ? AtaStorageAttribute::DiskType::Hdd : AtaStorageAttribute::DiskType::Ssd; - auto parser = SmartctlParser::create(SmartctlOutputParserType::Text); + auto parser = SmartctlParser::create(SmartctlParserType::Text); DBG_ASSERT_RETURN(parser, "Cannot create parser"); if (!parser->parse_full(output)) { // try to parse it diff --git a/src/applib/smartctl_ata_text_parser.cpp b/src/applib/smartctl_ata_text_parser.cpp index 555287e..da78bef 100644 --- a/src/applib/smartctl_ata_text_parser.cpp +++ b/src/applib/smartctl_ata_text_parser.cpp @@ -227,7 +227,7 @@ bool SmartctlAtaTextParser::parse_full(const std::string& full) add_property(p); } - if (!SmartctlVersionParser::check_parsed_version(SmartctlOutputParserType::Text, version)) { + if (!SmartctlVersionParser::check_parsed_version(SmartctlParserType::Text, version)) { set_error_msg("Incompatible smartctl version."); debug_out_warn("app", DBG_FUNC_MSG << "Incompatible smartctl version. Returning.\n"); return false; diff --git a/src/applib/smartctl_output_type.h b/src/applib/smartctl_output_type.h deleted file mode 100644 index bda0ba3..0000000 --- a/src/applib/smartctl_output_type.h +++ /dev/null @@ -1,53 +0,0 @@ -/****************************************************************************** -License: GNU General Public License v3.0 only -Copyright: - (C) 2022 Alexander Shaduri -******************************************************************************/ -/// \file -/// \author Alexander Shaduri -/// \ingroup applib -/// \weakgroup applib -/// @{ - -#ifndef SMARTCTL_OUTPUT_TYPE_H -#define SMARTCTL_OUTPUT_TYPE_H - -#include "local_glibmm.h" - -#include "hz/enum_helper.h" - - -enum class SmartctlOutputParserType { - Auto, - Json, - Text, -}; - - - -/// Helper structure for enum-related functions -struct SmartctlOutputParserTypeExt - : public hz::EnumHelper< - SmartctlOutputParserType, - SmartctlOutputParserTypeExt, - Glib::ustring> -{ - static constexpr inline SmartctlOutputParserType default_value = SmartctlOutputParserType::Auto; - - static std::unordered_map> build_enum_map() - { - return { - {SmartctlOutputParserType::Auto, {"auto", _("Automatic")}}, - {SmartctlOutputParserType::Json, {"json", _("JSON")}}, - {SmartctlOutputParserType::Text, {"text", _("Text")}}, - }; - } - -}; - - - - -#endif - -/// @} diff --git a/src/applib/smartctl_parser.cpp b/src/applib/smartctl_parser.cpp index bef18d0..ae842ce 100644 --- a/src/applib/smartctl_parser.cpp +++ b/src/applib/smartctl_parser.cpp @@ -20,14 +20,12 @@ Copyright: -std::unique_ptr SmartctlParser::create(SmartctlOutputParserType type) +std::unique_ptr SmartctlParser::create(SmartctlParserType type) { switch(type) { - case SmartctlOutputParserType::Auto: - break; - case SmartctlOutputParserType::Json: + case SmartctlParserType::Json: return std::make_unique(); - case SmartctlOutputParserType::Text: + case SmartctlParserType::Text: return std::make_unique(); } return nullptr; @@ -35,18 +33,18 @@ std::unique_ptr SmartctlParser::create(SmartctlOutputParserType -std::optional SmartctlParser::detect_output_type(const std::string& output) const -{ +// std::optional SmartctlParser::detect_output_type(const std::string& output) const +// { // Look for the first non-whitespace symbol - auto first_symbol = std::find_if(output.begin(), output.end(), [&](char c) { - return !std::isspace(c, std::locale::classic()); - }); - if (first_symbol != output.end() && *first_symbol == '-' + // auto first_symbol = std::find_if(output.begin(), output.end(), [&](char c) { + // return !std::isspace(c, std::locale::classic()); + // }); + // if (first_symbol != output.end() && *first_symbol == '-' -} +// } diff --git a/src/applib/smartctl_parser.h b/src/applib/smartctl_parser.h index 395d56e..594075c 100644 --- a/src/applib/smartctl_parser.h +++ b/src/applib/smartctl_parser.h @@ -16,8 +16,9 @@ Copyright: #include #include +// #include "leaf_ns.h" #include "ata_storage_property.h" -#include "smartctl_output_type.h" +#include "smartctl_parser_types.h" @@ -51,12 +52,12 @@ class SmartctlParser { /// Create an instance of this class. /// \return nullptr if no such class exists - static std::unique_ptr create(SmartctlOutputParserType type); + static std::unique_ptr create(SmartctlParserType type); /// Create an instance of this class. /// \return nullptr if no such class exists - static std::unique_ptr detect_and_parse(SmartctlOutputParserType type); + // static std::unique_ptr detect_and_parse(SmartctlParserType type); /// Parse full "smartctl -x" output. @@ -65,8 +66,7 @@ class SmartctlParser { /// Detect smartctl output type (text, json). - /// Return - [[nodiscard]] std::optional detect_output_type(const std::string& output) const; + // [[nodiscard]] leaf::result detect_output_type(const std::string& output) const; /// Get "full" data, as passed to parse_full(). diff --git a/src/applib/smartctl_parser_types.h b/src/applib/smartctl_parser_types.h new file mode 100644 index 0000000..9b3f450 --- /dev/null +++ b/src/applib/smartctl_parser_types.h @@ -0,0 +1,83 @@ +/****************************************************************************** +License: GNU General Public License v3.0 only +Copyright: + (C) 2022 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup applib +/// \weakgroup applib +/// @{ + +#ifndef SMARTCTL_PARSER_TYPES_H +#define SMARTCTL_PARSER_TYPES_H + +#include "local_glibmm.h" + +#include "hz/enum_helper.h" + + + +enum class SmartctlParserType { + Json, + Text, +}; + + + +/// Helper structure for enum-related functions +struct SmartctlParserTypeExt + : public hz::EnumHelper< + SmartctlParserType, + SmartctlParserTypeExt, + Glib::ustring> +{ + static constexpr inline SmartctlParserType default_value = SmartctlParserType::Json; + + static std::unordered_map> build_enum_map() + { + return { + {SmartctlParserType::Json, {"json", _("JSON")}}, + {SmartctlParserType::Text, {"text", _("Text")}}, + }; + } + +}; + + + + +enum class SmartctlParserSettingType { + Auto, + Json, + Text, +}; + + + +/// Helper structure for enum-related functions +struct SmartctlParserSettingTypeExt + : public hz::EnumHelper< + SmartctlParserSettingType, + SmartctlParserSettingTypeExt, + Glib::ustring> +{ + static constexpr inline SmartctlParserSettingType default_value = SmartctlParserSettingType::Auto; + + static std::unordered_map> build_enum_map() + { + return { + {SmartctlParserSettingType::Auto, {"auto", _("Automatic")}}, + {SmartctlParserSettingType::Json, {"json", _("JSON")}}, + {SmartctlParserSettingType::Text, {"text", _("Text")}}, + }; + } + +}; + + + + +#endif + +/// @} diff --git a/src/applib/smartctl_version_parser.cpp b/src/applib/smartctl_version_parser.cpp index 9311d6c..b23ab91 100644 --- a/src/applib/smartctl_version_parser.cpp +++ b/src/applib/smartctl_version_parser.cpp @@ -53,15 +53,13 @@ std::optional SmartctlVersionParser::get_numeric_version(const std::stri -bool SmartctlVersionParser::check_parsed_version(SmartctlOutputParserType parser_type, const std::string& version_only) +bool SmartctlVersionParser::check_parsed_version(SmartctlParserType parser_type, const std::string& version_only) { if (auto numeric_version = get_numeric_version(version_only); numeric_version.has_value()) { switch(parser_type) { - case SmartctlOutputParserType::Auto: - return numeric_version.value() >= minimum_req_text_version || numeric_version.value() >= minimum_req_json_version; - case SmartctlOutputParserType::Json: + case SmartctlParserType::Json: return numeric_version.value() >= minimum_req_json_version; - case SmartctlOutputParserType::Text: + case SmartctlParserType::Text: return numeric_version.value() >= minimum_req_text_version; } } @@ -70,13 +68,13 @@ bool SmartctlVersionParser::check_parsed_version(SmartctlOutputParserType parser -std::optional SmartctlVersionParser::detect_supported_parser_type(const std::string& version_only) +std::optional SmartctlVersionParser::detect_supported_parser_type(const std::string& version_only) { - if (check_parsed_version(SmartctlOutputParserType::Json, version_only)) { - return SmartctlOutputParserType::Json; + if (check_parsed_version(SmartctlParserType::Json, version_only)) { + return SmartctlParserType::Json; } - if (check_parsed_version(SmartctlOutputParserType::Text, version_only)) { - return SmartctlOutputParserType::Text; + if (check_parsed_version(SmartctlParserType::Text, version_only)) { + return SmartctlParserType::Text; } return std::nullopt; } diff --git a/src/applib/smartctl_version_parser.h b/src/applib/smartctl_version_parser.h index 5c1b29c..14c11f1 100644 --- a/src/applib/smartctl_version_parser.h +++ b/src/applib/smartctl_version_parser.h @@ -18,7 +18,7 @@ Copyright: #include #include -#include "smartctl_output_type.h" +#include "smartctl_parser_types.h" @@ -42,11 +42,11 @@ class SmartctlVersionParser { /// Check that the version of smartctl output can be parsed with a parser. - static bool check_parsed_version(SmartctlOutputParserType parser_type, const std::string& version_only); + static bool check_parsed_version(SmartctlParserType parser_type, const std::string& version_only); /// Detect smartctl parser type based on smartctl version - static std::optional detect_supported_parser_type(const std::string& version_only); + static std::optional detect_supported_parser_type(const std::string& version_only); private: diff --git a/src/applib/storage_device.cpp b/src/applib/storage_device.cpp index 3f3127b..33d0122 100644 --- a/src/applib/storage_device.cpp +++ b/src/applib/storage_device.cpp @@ -241,7 +241,7 @@ std::string StorageDevice::parse_basic_data(bool do_set_properties, bool emit_si disk_type = hdd_.value() ? AtaStorageAttribute::DiskType::Hdd : AtaStorageAttribute::DiskType::Ssd; } - auto parser = SmartctlParser::create(SmartctlOutputParserType::Text); + auto parser = SmartctlParser::create(SmartctlParserType::Text); DBG_ASSERT_RETURN(parser, "Cannot create parser"); if (parser->parse_full(this->info_output_)) { // try to parse it @@ -308,7 +308,7 @@ std::string StorageDevice::parse_data() disk_type = hdd_.value() ? AtaStorageAttribute::DiskType::Hdd : AtaStorageAttribute::DiskType::Ssd; } - auto parser = SmartctlParser::create(SmartctlOutputParserType::Text); + auto parser = SmartctlParser::create(SmartctlParserType::Text); DBG_ASSERT_RETURN(parser, "Cannot create parser"); if (parser->parse_full(this->full_output_)) { // try to parse it (parse only, set the properties after basic parsing).