File indexing completed on 2024-11-24 04:49:38

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     kleo/predicates.h
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include <gpgme++/key.h>
0013 
0014 #include <algorithm>
0015 #include <cstring>
0016 #include <functional>
0017 #include <iterator>
0018 #include <string>
0019 
0020 namespace Kleo
0021 {
0022 namespace _detail
0023 {
0024 
0025 inline int mystrcmp(const char *s1, const char *s2)
0026 {
0027     using namespace std;
0028     return s1 ? s2 ? strcmp(s1, s2) : 1 : s2 ? -1 : 0;
0029 }
0030 
0031 #define make_comparator_str_impl(Name, expr, cmp)                                                                                                              \
0032     template<template<typename U> class Op>                                                                                                                    \
0033     struct Name {                                                                                                                                              \
0034         typedef bool result_type;                                                                                                                              \
0035                                                                                                                                                                \
0036         bool operator()(const char *lhs, const char *rhs) const                                                                                                \
0037         {                                                                                                                                                      \
0038             return Op<int>()(cmp, 0);                                                                                                                          \
0039         }                                                                                                                                                      \
0040                                                                                                                                                                \
0041         bool operator()(const std::string &lhs, const std::string &rhs) const                                                                                  \
0042         {                                                                                                                                                      \
0043             return operator()(lhs.c_str(), rhs.c_str());                                                                                                       \
0044         }                                                                                                                                                      \
0045         bool operator()(const char *lhs, const std::string &rhs) const                                                                                         \
0046         {                                                                                                                                                      \
0047             return operator()(lhs, rhs.c_str());                                                                                                               \
0048         }                                                                                                                                                      \
0049         bool operator()(const std::string &lhs, const char *rhs) const                                                                                         \
0050         {                                                                                                                                                      \
0051             return operator()(lhs.c_str(), rhs);                                                                                                               \
0052         }                                                                                                                                                      \
0053                                                                                                                                                                \
0054         template<typename T>                                                                                                                                   \
0055         bool operator()(const T &lhs, const T &rhs) const                                                                                                      \
0056         {                                                                                                                                                      \
0057             return operator()((lhs expr), (rhs expr));                                                                                                         \
0058         }                                                                                                                                                      \
0059         template<typename T>                                                                                                                                   \
0060         bool operator()(const T &lhs, const char *rhs) const                                                                                                   \
0061         {                                                                                                                                                      \
0062             return operator()((lhs expr), rhs);                                                                                                                \
0063         }                                                                                                                                                      \
0064         template<typename T>                                                                                                                                   \
0065         bool operator()(const char *lhs, const T &rhs) const                                                                                                   \
0066         {                                                                                                                                                      \
0067             return operator()(lhs, (rhs expr));                                                                                                                \
0068         }                                                                                                                                                      \
0069         template<typename T>                                                                                                                                   \
0070         bool operator()(const T &lhs, const std::string &rhs) const                                                                                            \
0071         {                                                                                                                                                      \
0072             return operator()((lhs expr), rhs);                                                                                                                \
0073         }                                                                                                                                                      \
0074         template<typename T>                                                                                                                                   \
0075         bool operator()(const std::string &lhs, const T &rhs) const                                                                                            \
0076         {                                                                                                                                                      \
0077             return operator()(lhs, (rhs expr));                                                                                                                \
0078         }                                                                                                                                                      \
0079     }
0080 
0081 #define make_comparator_str_fast(Name, expr) make_comparator_str_impl(Name, expr, _detail::mystrcmp(lhs, rhs))
0082 #define make_comparator_str(Name, expr) make_comparator_str_impl(Name, expr, qstricmp(lhs, rhs))
0083 
0084 make_comparator_str_fast(ByFingerprint, .primaryFingerprint());
0085 make_comparator_str_fast(ByKeyID, .keyID());
0086 make_comparator_str_fast(ByShortKeyID, .shortKeyID());
0087 make_comparator_str_fast(ByChainID, .chainID());
0088 make_comparator_str_fast(ByKeyGrip, .keyGrip());
0089 
0090 template<typename T>
0091 void sort_by_fpr(T &t)
0092 {
0093     std::sort(t.begin(), t.end(), ByFingerprint<std::less>());
0094 }
0095 
0096 template<typename T>
0097 void remove_duplicates_by_fpr(T &t)
0098 {
0099     t.erase(std::unique(t.begin(), t.end(), ByFingerprint<std::equal_to>()), t.end());
0100 }
0101 
0102 template<typename T>
0103 T union_by_fpr(const T &t1, const T &t2)
0104 {
0105     T result;
0106     result.reserve(t1.size() + t2.size());
0107     std::set_union(t1.begin(), //
0108                    t1.end(),
0109                    t2.begin(),
0110                    t2.end(),
0111                    std::back_inserter(result),
0112                    ByFingerprint<std::less>());
0113     return result;
0114 }
0115 }
0116 }