From 7c51bc68d4b1efacad3a725deff4cb20b51cee9b Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 16 Apr 2022 13:16:29 +0300 Subject: [PATCH] option: rename to to_string_opt and to_string_view_opt to_option_string -> to_string_opt to_string_view -> to_string_view_opt --- lib/message/mu-message-part.cc | 5 +- lib/message/mu-message.cc | 16 ++++--- lib/message/mu-mime-object.hh | 84 +++++++++++++++++----------------- lib/utils/Makefile.am | 1 + lib/utils/meson.build | 1 + lib/utils/mu-option.hh | 27 +++++++++++ lib/utils/mu-utils.cc | 9 ++-- lib/utils/mu-utils.hh | 30 ++++++------ mu/mu-cmd.cc | 8 ++-- 9 files changed, 104 insertions(+), 77 deletions(-) diff --git a/lib/message/mu-message-part.cc b/lib/message/mu-message-part.cc index b9f4cb00..735b27c4 100644 --- a/lib/message/mu-message-part.cc +++ b/lib/message/mu-message-part.cc @@ -41,9 +41,6 @@ MessagePart::mime_object() const noexcept return *mime_obj; } - - - Option MessagePart::cooked_filename() const noexcept { @@ -123,7 +120,7 @@ MessagePart::to_string() const noexcept if (mime_object().is_part()) return MimePart{mime_object()}.to_string(); else - return mime_object().object_to_string(); + return mime_object().to_string_opt(); } diff --git a/lib/message/mu-message.cc b/lib/message/mu-message.cc index 6929e5c7..caa3750c 100644 --- a/lib/message/mu-message.cc +++ b/lib/message/mu-message.cc @@ -89,8 +89,10 @@ Message::Message(const std::string& path, const std::string& mdir): else priv_->mime_msg = std::move(msg.value()); - priv_->doc.add(Field::Id::Path, - Mu::from_gchars(g_canonicalize_filename(path.c_str(), NULL))); + auto xpath{to_string_opt_gchar(g_canonicalize_filename(path.c_str(), NULL))}; + if (xpath) + priv_->doc.add(Field::Id::Path, std::move(xpath.value())); + if (!mdir.empty()) priv_->doc.add(Field::Id::Maildir, mdir); priv_->doc.add(Field::Id::Size, static_cast(statbuf.st_size)); @@ -103,10 +105,10 @@ Message::Message(const std::string& text, const std::string& path, const std::string& mdir): priv_{std::make_unique()} { - if (!path.empty()) - priv_->doc.add(Field::Id::Path, - Mu::from_gchars( - g_canonicalize_filename(path.c_str(), NULL))); + auto xpath{to_string_opt_gchar(g_canonicalize_filename(path.c_str(), NULL))}; + if (xpath) + priv_->doc.add(Field::Id::Path, std::move(xpath.value())); + if (!mdir.empty()) priv_->doc.add(Field::Id::Maildir, mdir); @@ -246,7 +248,7 @@ get_mailing_list(const MimeMessage& mime_msg) g_free(dechdr); - return from_gchars(std::move(res)); + return to_string_opt_gchar(std::move(res)); } static bool /* heuristic */ diff --git a/lib/message/mu-mime-object.hh b/lib/message/mu-mime-object.hh index 1617c9c7..43818396 100644 --- a/lib/message/mu-mime-object.hh +++ b/lib/message/mu-mime-object.hh @@ -153,7 +153,7 @@ struct MimeContentType: public Object { } Option mime_type() const noexcept { - return to_option_string(g_mime_content_type_get_mime_type(self())); + return to_string_opt(g_mime_content_type_get_mime_type(self())); } bool is_type(const std::string& type, const std::string& subtype) const { @@ -193,7 +193,7 @@ struct MimeStream: public Object { }; template -constexpr Option to_string_view(const S& seq, T t) { +constexpr Option to_string_view_opt(const S& seq, T t) { auto&& it = seq_find_if(seq, [&](auto&& item){return item.first == t;}); if (it == seq.cend()) return Nothing; @@ -283,27 +283,27 @@ struct MimeCertificate: public Object { } Option issuer_serial() const { - return to_option_string(g_mime_certificate_get_issuer_serial(self())); + return to_string_opt(g_mime_certificate_get_issuer_serial(self())); } Option issuer_name() const { - return to_option_string(g_mime_certificate_get_issuer_name(self())); + return to_string_opt(g_mime_certificate_get_issuer_name(self())); } Option fingerprint() const { - return to_option_string(g_mime_certificate_get_fingerprint(self())); + return to_string_opt(g_mime_certificate_get_fingerprint(self())); } Option key_id() const { - return to_option_string(g_mime_certificate_get_key_id(self())); + return to_string_opt(g_mime_certificate_get_key_id(self())); } Option name() const { - return to_option_string(g_mime_certificate_get_name(self())); + return to_string_opt(g_mime_certificate_get_name(self())); } Option user_id() const { - return to_option_string(g_mime_certificate_get_user_id(self())); + return to_string_opt(g_mime_certificate_get_user_id(self())); } Option<::time_t> created() const { @@ -341,8 +341,8 @@ AllPubkeyAlgos = {{ { MimeCertificate::PubkeyAlgo::EdDsa, "elliptic-curve+dsa-2"} }}; -constexpr Option to_string_view(MimeCertificate::PubkeyAlgo algo) { - return to_string_view(AllPubkeyAlgos, algo); +constexpr Option to_string_view_opt(MimeCertificate::PubkeyAlgo algo) { + return to_string_view_opt(AllPubkeyAlgos, algo); }; @@ -365,8 +365,8 @@ AllDigestAlgos = {{ { MimeCertificate::DigestAlgo::Crc32Rfc2440, "crc32-rfc2440"}, }}; -constexpr Option to_string_view(MimeCertificate::DigestAlgo algo) { - return to_string_view(AllDigestAlgos, algo); +constexpr Option to_string_view_opt(MimeCertificate::DigestAlgo algo) { + return to_string_view_opt(AllDigestAlgos, algo); }; constexpr std::array, 6> @@ -379,8 +379,8 @@ AllTrusts = {{ { MimeCertificate::Trust::TrustUltimate,"trust-ultimate" }, }}; -constexpr Option to_string_view(MimeCertificate::Trust trust) { - return to_string_view(AllTrusts, trust); +constexpr Option to_string_view_opt(MimeCertificate::Trust trust) { + return to_string_view_opt(AllTrusts, trust); }; constexpr std::array, 6> @@ -393,8 +393,8 @@ AllValidities = {{ { MimeCertificate::Validity::Ultimate, "ultimate" }, }}; -constexpr Option to_string_view(MimeCertificate::Validity val) { - return to_string_view(AllValidities, val); +constexpr Option to_string_view_opt(MimeCertificate::Validity val) { + return to_string_view_opt(AllValidities, val); }; @@ -520,7 +520,7 @@ struct MimeDecryptResult: public Object { } Option session_key() const noexcept { - return to_option_string(g_mime_decrypt_result_get_session_key(self())); + return to_string_opt(g_mime_decrypt_result_get_session_key(self())); } @@ -532,22 +532,22 @@ private: constexpr std::array, 12> AllCipherAlgos= {{ - { MimeDecryptResult::CipherAlgo::Default , "default"}, - { MimeDecryptResult::CipherAlgo::Idea , "idea"}, - { MimeDecryptResult::CipherAlgo::Des3 , "3des"}, - { MimeDecryptResult::CipherAlgo::Cast5 , "cast5"}, - { MimeDecryptResult::CipherAlgo::Blowfish , "blowfish"}, - { MimeDecryptResult::CipherAlgo::Aes , "aes"}, - { MimeDecryptResult::CipherAlgo::Aes192 , "aes192"}, - { MimeDecryptResult::CipherAlgo::Aes256 , "aes256"}, - { MimeDecryptResult::CipherAlgo::TwoFish , "twofish"}, - { MimeDecryptResult::CipherAlgo::Camellia128, "camellia128"}, - { MimeDecryptResult::CipherAlgo::Camellia192, "camellia192"}, - { MimeDecryptResult::CipherAlgo::Camellia256, "camellia256"}, + {MimeDecryptResult::CipherAlgo::Default, "default"}, + {MimeDecryptResult::CipherAlgo::Idea, "idea"}, + {MimeDecryptResult::CipherAlgo::Des3, "3des"}, + {MimeDecryptResult::CipherAlgo::Cast5, "cast5"}, + {MimeDecryptResult::CipherAlgo::Blowfish, "blowfish"}, + {MimeDecryptResult::CipherAlgo::Aes, "aes"}, + {MimeDecryptResult::CipherAlgo::Aes192, "aes192"}, + {MimeDecryptResult::CipherAlgo::Aes256, "aes256"}, + {MimeDecryptResult::CipherAlgo::TwoFish, "twofish"}, + {MimeDecryptResult::CipherAlgo::Camellia128, "camellia128"}, + {MimeDecryptResult::CipherAlgo::Camellia192, "camellia192"}, + {MimeDecryptResult::CipherAlgo::Camellia256, "camellia256"}, }}; -constexpr Option to_string_view(MimeDecryptResult::CipherAlgo algo) { - return to_string_view(AllCipherAlgos, algo); +constexpr Option to_string_view_opt(MimeDecryptResult::CipherAlgo algo) { + return to_string_view_opt(AllCipherAlgos, algo); }; @@ -581,13 +581,13 @@ struct MimeCryptoContext : public Object { Option encryption_protocol() { - return to_option_string(g_mime_crypto_context_get_encryption_protocol(self())); + return to_string_opt(g_mime_crypto_context_get_encryption_protocol(self())); } Option signature_protocol() { - return to_option_string(g_mime_crypto_context_get_signature_protocol(self())); + return to_string_opt(g_mime_crypto_context_get_signature_protocol(self())); } Option key_exchange_protocol() { - return to_option_string(g_mime_crypto_context_get_key_exchange_protocol(self())); + return to_string_opt(g_mime_crypto_context_get_key_exchange_protocol(self())); } /** @@ -685,7 +685,7 @@ public: } Option content_type_parameter(const std::string& param) const noexcept { - return to_option_string( + return to_string_opt( g_mime_object_get_content_type_parameter(self(), param.c_str())); } @@ -832,7 +832,7 @@ public: * @return string or nullopt */ Option message_id() const noexcept { - return to_option_string(g_mime_message_get_message_id(self())); + return to_string_opt(g_mime_message_get_message_id(self())); } /** @@ -841,7 +841,7 @@ public: * @return string or nullopt */ Option subject() const noexcept { - return to_option_string(g_mime_message_get_subject(self())); + return to_string_opt(g_mime_message_get_subject(self())); } /** @@ -914,7 +914,7 @@ public: * @return string or nullopt */ Option content_description() const noexcept { - return to_option_string(g_mime_part_get_content_description(self())); + return to_string_opt(g_mime_part_get_content_description(self())); } /** @@ -924,7 +924,7 @@ public: * @return string or nullopt */ Option content_id() const noexcept { - return to_option_string(g_mime_part_get_content_id(self())); + return to_string_opt(g_mime_part_get_content_id(self())); } /** @@ -934,7 +934,7 @@ public: * @return string or nullopt */ Option content_md5() const noexcept { - return to_option_string(g_mime_part_get_content_md5(self())); + return to_string_opt(g_mime_part_get_content_md5(self())); } @@ -955,7 +955,7 @@ public: * @return string or nullopt */ Option content_location() const noexcept { - return to_option_string(g_mime_part_get_content_location(self())); + return to_string_opt(g_mime_part_get_content_location(self())); } /** @@ -965,7 +965,7 @@ public: * @return string or nullopt */ Option filename() const noexcept { - return to_option_string(g_mime_part_get_filename(self())); + return to_string_opt(g_mime_part_get_filename(self())); } /** diff --git a/lib/utils/Makefile.am b/lib/utils/Makefile.am index 8191b651..f6855d5e 100644 --- a/lib/utils/Makefile.am +++ b/lib/utils/Makefile.am @@ -58,6 +58,7 @@ libmu_utils_la_SOURCES= \ mu-logger.cc \ mu-logger.hh \ mu-option.hh \ + mu-option.cc \ mu-readline.cc \ mu-readline.hh \ mu-result.hh \ diff --git a/lib/utils/meson.build b/lib/utils/meson.build index 8997873d..b8119cf0 100644 --- a/lib/utils/meson.build +++ b/lib/utils/meson.build @@ -23,6 +23,7 @@ lib_mu_utils=static_library('mu-utils', [ 'mu-logger.cc', 'mu-logger.hh', 'mu-option.hh', + 'mu-option.cc', 'mu-readline.cc', 'mu-readline.hh', 'mu-result.hh', diff --git a/lib/utils/mu-option.hh b/lib/utils/mu-option.hh index fb4a64a9..13f9a9a1 100644 --- a/lib/utils/mu-option.hh +++ b/lib/utils/mu-option.hh @@ -15,6 +15,7 @@ #define MU_OPTION__ #include "optional.hpp" +#include namespace Mu { @@ -29,5 +30,31 @@ Some(T&& t) } constexpr auto Nothing = tl::nullopt; // 'None' is take already +/** + * Maybe create a string from a const char pointer. + * + * @param str a char pointer or NULL + * + * @return option with either the string or nothing if str was NULL. + */ +Option +static inline to_string_opt(const char* str) { + if (str) + return std::string{str}; + else + return Nothing; +} + +/** + * Like maybe_string that takes a const char*, but additionally, + * g_free() the string. + * + * @param str char pointer or NULL (consumed) + * + * @return option with either the string or nothing if str was NULL. + */ +Option to_string_opt_gchar(char*&& str); + + } // namespace Mu #endif /*MU_OPTION__*/ diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc index 04f1a5eb..ee4a10c2 100644 --- a/lib/utils/mu-utils.cc +++ b/lib/utils/mu-utils.cc @@ -43,6 +43,7 @@ #include "mu-util.h" #include "mu-str.h" #include "mu-error.hh" +#include "mu-option.hh" using namespace Mu; @@ -352,14 +353,12 @@ Mu::time_to_string(const std::string& frm, time_t t, bool utc) return {}; } - char* str = g_date_time_format(dt, frm.c_str()); /* always utf8 */ + auto datestr{to_string_opt_gchar(g_date_time_format(dt, frm.c_str()))}; g_date_time_unref(dt); - if (!str) { + if (!datestr) g_warning("failed to format time with format '%s'", frm.c_str()); - return {}; - } - return from_gchars(std::move(str)/*consumed*/); + return datestr.value_or(""); } static std::string diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index bea94186..8e3d1c3c 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -174,22 +174,22 @@ std::string date_to_time_t_string(int64_t t); std::string time_to_string(const std::string& frm, time_t t, bool utc = false) G_GNUC_CONST; -/** - * Create a std::string by consuming a gchar* array; this takes ownership - * of str which should no longer be used. - * - * @param str a gchar* or NULL (latter taken as "") - * - * @return a std::string - */ -static inline std::string -from_gchars(gchar*&& str) -{ - std::string s{str ? str : ""}; - g_free(str); +// /** +// * Create a std::string by consuming a gchar* array; this takes ownership +// * of str which should no longer be used. +// * +// * @param str a gchar* or NULL (latter taken as "") +// * +// * @return a std::string +// */ +// static inline std::string +// from_gchars(gchar*&& str) +// { +// std::string s{str ? str : ""}; +// g_free(str); - return s; -} +// return s; +// } // https://stackoverflow.com/questions/19053351/how-do-i-use-a-custom-deleter-with-a-stdunique-ptr-member template diff --git a/mu/mu-cmd.cc b/mu/mu-cmd.cc index 8f895ae3..cac06c35 100644 --- a/mu/mu-cmd.cc +++ b/mu/mu-cmd.cc @@ -366,13 +366,13 @@ print_signature(const Mu::MimeSignature& sig, const MuConfig *opts) const auto cert{sig.certificate()}; key_val(col, "public-key algo", - to_string_view(cert.pubkey_algo()).value_or("unknown")); + to_string_view_opt(cert.pubkey_algo()).value_or("unknown")); key_val(col, "digest algo", - to_string_view(cert.digest_algo()).value_or("unknown")); + to_string_view_opt(cert.digest_algo()).value_or("unknown")); key_val(col, "id-validity", - to_string_view(cert.id_validity()).value_or("unknown")); + to_string_view_opt(cert.id_validity()).value_or("unknown")); key_val(col, "trust", - to_string_view(cert.trust()).value_or("unknown")); + to_string_view_opt(cert.trust()).value_or("unknown")); key_val(col, "issuer-serial", cert.issuer_serial().value_or("unknown")); key_val(col, "issuer-name", cert.issuer_name().value_or("unknown")); key_val(col, "finger-print", cert.fingerprint().value_or("unknown"));