File indexing completed on 2024-05-12 05:55:42

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2023 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "bytearraycodingconfigentry.hpp"
0010 
0011 // Std
0012 #include <algorithm>
0013 #include <array>
0014 #include <iterator>
0015 
0016 // Matching Kasten::ByteArrayCoding
0017 static constexpr int ByteArrayCodingCount = 6;
0018 static const std::array<QString, ByteArrayCodingCount> byteArrayCodingConfigValueList = {
0019     QStringLiteral("Hexadecimal"),
0020     QStringLiteral("Decimal"),
0021     QStringLiteral("Octal"),
0022     QStringLiteral("Binary"),
0023     QStringLiteral("Char"),
0024     QStringLiteral("UTF-8"),
0025 };
0026 
0027 template <>
0028 Kasten::ByteArrayCoding
0029 KConfigGroup::readEntry(const char *key,
0030                         const Kasten::ByteArrayCoding &defaultValue) const
0031 {
0032     const QString entry = readEntry(key, QString());
0033 
0034     auto it = std::find(byteArrayCodingConfigValueList.cbegin(), byteArrayCodingConfigValueList.cend(), entry);
0035     if (it == byteArrayCodingConfigValueList.cend()) {
0036         return defaultValue;
0037     }
0038 
0039     const int listIndex = std::distance(byteArrayCodingConfigValueList.cbegin(), it);
0040     return static_cast<Kasten::ByteArrayCoding>(listIndex);
0041 }
0042 
0043 template <>
0044 void KConfigGroup::writeEntry(const char *key,
0045                               const Kasten::ByteArrayCoding &value,
0046                               KConfigBase::WriteConfigFlags flags)
0047 {
0048     QString configValue;
0049     if (value == Kasten::ByteArrayInvalidCoding) {
0050         configValue = QStringLiteral("Invalid");
0051     } else {
0052         const int listIndex = static_cast<int>(value);
0053         configValue = byteArrayCodingConfigValueList[listIndex];
0054     }
0055     writeEntry(key, configValue, flags);
0056 }