File indexing completed on 2024-06-23 05:14:15

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     utils/detail_p.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 <assuan.h>
0013 
0014 #ifdef _WIN32
0015 #include <io.h>
0016 #endif
0017 
0018 namespace Kleo
0019 {
0020 namespace _detail
0021 {
0022 
0023 template<template<typename U> class Op>
0024 struct ByName {
0025     using result_type = bool;
0026 
0027     template<typename T>
0028     bool operator()(const T &lhs, const T &rhs) const
0029     {
0030         return Op<int>()(qstricmp(lhs->name(), rhs->name()), 0);
0031     }
0032     template<typename T>
0033     bool operator()(const T &lhs, const char *rhs) const
0034     {
0035         return Op<int>()(qstricmp(lhs->name(), rhs), 0);
0036     }
0037     template<typename T>
0038     bool operator()(const char *lhs, const T &rhs) const
0039     {
0040         return Op<int>()(qstricmp(lhs, rhs->name()), 0);
0041     }
0042     bool operator()(const char *lhs, const char *rhs) const
0043     {
0044         return Op<int>()(qstricmp(lhs, rhs), 0);
0045     }
0046 };
0047 
0048 // inspired by GnuPG's translate_sys2libc_fd, this converts a HANDLE
0049 // to int fd on Windows, and is a NO-OP on POSIX:
0050 static inline int translate_sys2libc_fd(assuan_fd_t fd, bool for_write)
0051 {
0052     if (fd == ASSUAN_INVALID_FD) {
0053         return -1;
0054     }
0055 #if defined(_WIN32)
0056     return _open_osfhandle((intptr_t)fd, for_write);
0057 #else
0058     (void)for_write;
0059     return fd;
0060 #endif
0061 }
0062 
0063 static inline assuan_fd_t translate_libc2sys_fd(int fd)
0064 {
0065     if (fd == -1) {
0066         return ASSUAN_INVALID_FD;
0067     }
0068 #if defined(_WIN32)
0069     return (assuan_fd_t)_get_osfhandle(fd);
0070 #else
0071     return fd;
0072 #endif
0073 }
0074 
0075 // returns an integer representation of the assuan_fd_t,
0076 // suitable for debug output
0077 static inline qulonglong assuanFD2int(assuan_fd_t fd)
0078 {
0079 #ifdef _WIN32
0080     return reinterpret_cast<qulonglong>(fd);
0081 #else
0082     return static_cast<qulonglong>(fd);
0083 #endif
0084 }
0085 }
0086 }