File indexing completed on 2025-01-05 05:23:52

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2007-2008 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 #ifndef KASTEN_BYTEARRAYSOURCECODESTREAMENCODER_HPP
0010 #define KASTEN_BYTEARRAYSOURCECODESTREAMENCODER_HPP
0011 
0012 // lib
0013 #include "abstractbytearraystreamencoder.hpp"
0014 // Qt
0015 #include <QString>
0016 
0017 class KConfigGroup;
0018 
0019 namespace Kasten {
0020 
0021 class SourceCodeStreamEncoderSettings
0022 {
0023 public:
0024     enum class PrimitiveDataType
0025     {
0026         Char = 0,
0027         UnsignedChar,
0028         Short,
0029         UnsignedShort,
0030         Integer,
0031         UnsignedInteger,
0032         Float,
0033         Double,
0034         _Count,
0035     };
0036 
0037 private:
0038     static const QString DefaultVariableName;
0039 
0040     static inline constexpr PrimitiveDataType DefaultDataType = PrimitiveDataType::UnsignedChar;
0041     static inline constexpr int DefaultElementsPerLine = 4;
0042     static inline constexpr bool DefaultUnsignedAsHexadecimal = true;
0043 
0044     static inline constexpr char VariableNameConfigKey[] = "VariableName";
0045     static inline constexpr char DataTypeConfigKey[] = "DataType";
0046     static inline constexpr char ElementsPerLineConfigKey[] = "ElementsPerLine";
0047     static inline constexpr char UnsignedAsHexadecimalConfigKey[] = "UnsignedAsHexadecimal";
0048 
0049 public:
0050     SourceCodeStreamEncoderSettings();
0051     SourceCodeStreamEncoderSettings(const SourceCodeStreamEncoderSettings&) = default;
0052 
0053     ~SourceCodeStreamEncoderSettings() = default;
0054 
0055     SourceCodeStreamEncoderSettings& operator=(const SourceCodeStreamEncoderSettings&) = default;
0056 
0057     bool operator==(const SourceCodeStreamEncoderSettings& other) const;
0058 
0059 public:
0060     void loadConfig(const KConfigGroup& configGroup);
0061     void saveConfig(KConfigGroup& configGroup) const;
0062 
0063 public:
0064     QString variableName;
0065     PrimitiveDataType dataType = PrimitiveDataType::UnsignedChar;
0066     int elementsPerLine = 4;
0067     bool unsignedAsHexadecimal = true;
0068 };
0069 
0070 // TODO: General synchronizer would load matching encoder and decoder
0071 // manually defined by desktopfile
0072 class ByteArraySourceCodeStreamEncoder : public AbstractByteArrayStreamEncoder
0073 {
0074     Q_OBJECT
0075 
0076 private:
0077     static inline constexpr char ConfigGroupId[] = "ByteArraySourceCodeStreamEncoder";
0078 
0079 public:
0080     ByteArraySourceCodeStreamEncoder();
0081     ~ByteArraySourceCodeStreamEncoder() override;
0082 
0083 public:
0084     SourceCodeStreamEncoderSettings settings() const;
0085     void setSettings(const SourceCodeStreamEncoderSettings& settings);
0086 
0087 public:
0088     const char* const* dataTypeNames() const;
0089     int dataTypesCount() const;
0090 
0091 protected: // AbstractByteArrayStreamEncoder API
0092     bool encodeDataToStream(QIODevice* device,
0093                             const ByteArrayView* byteArrayView,
0094                             const Okteta::AbstractByteArrayModel* byteArrayModel,
0095                             const Okteta::AddressRange& range) override;
0096 
0097 private:
0098     QString printFormatted(const Okteta::AbstractByteArrayModel* byteArrayModel, Okteta::Address offset, unsigned int dataSize) const;
0099 
0100 private:
0101     SourceCodeStreamEncoderSettings mSettings;
0102 };
0103 
0104 inline SourceCodeStreamEncoderSettings ByteArraySourceCodeStreamEncoder::settings() const { return mSettings; }
0105 
0106 }
0107 
0108 #endif