File indexing completed on 2024-04-21 05:53:01
0001 /* 0002 This file is part of the Okteta Core library, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2003 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 OKTETA_VALUECODEC_HPP 0010 #define OKTETA_VALUECODEC_HPP 0011 0012 // lib 0013 #include "oktetacore.hpp" 0014 #include "oktetacore_export.hpp" 0015 #include "byte.hpp" 0016 0017 class QString; 0018 0019 namespace Okteta { 0020 0021 /** Class that is able to convert codings to and from 0022 * hexadecimal, decimal, octal, and binary. 0023 * 0024 * The buffer will be always filled up to CodingWidth, if not using shortCodingFunction. 0025 * 0026 * @author Friedrich W. H. Kossebau 0027 */ 0028 0029 class OKTETACORE_EXPORT ValueCodec 0030 { 0031 public: 0032 static ValueCodec* createCodec(ValueCoding valueCoding); 0033 0034 public: 0035 virtual ~ValueCodec(); 0036 0037 protected: 0038 ValueCodec(); 0039 0040 public: // API to be implemented 0041 /** 0042 * @return number of digits the greatest byte gets decoded into 0043 */ 0044 virtual unsigned int encodingWidth() const = 0; 0045 /** 0046 * @return largest value which could get another digit appended and stay below 256. 0047 */ 0048 virtual Byte digitsFilledLimit() const = 0; 0049 0050 /** 0051 * Encodes the byte using full coding width, prefixing with 0s if needed, 0052 * and writes the result to digits 0053 * @param digits string to write the digits into 0054 * @param pos offset in digits where to write the code to 0055 * @param byte data to encode 0056 */ 0057 virtual void encode(QString* digits, unsigned int pos, Byte byte) const = 0; 0058 /** 0059 * Encodes the byte and writes the result to digits, no leading 0s. 0060 * @param digits string to write the digits into 0061 * @param pos offset in digits where to write the code to 0062 * @param byte data to encode 0063 */ 0064 virtual void encodeShort(QString* digits, unsigned int pos, Byte byte) const = 0; 0065 0066 /** 0067 * Tries to increase the byte value by appending a digit to the digits of 0068 * the current encoding. 0069 * @param digit digit to append to encoding of current value. 0070 * @param byte offset in digits where to write the code to 0071 * @return true if successful, false otherwise 0072 */ 0073 virtual bool appendDigit(Byte* byte, unsigned char digit) const = 0; 0074 /** 0075 * Tries to remove the last (least significant) digit from byte. 0076 * @param byte value to remove the last digit from 0077 * @return true if successful, false otherwise 0078 */ 0079 virtual void removeLastDigit(Byte* byte) const = 0; 0080 /** 0081 * Checks if the given digit is used in the encoding. 0082 * @param digit value to check 0083 * @return true if digit is valid, false otherwise 0084 */ 0085 virtual bool isValidDigit(unsigned char digit) const = 0; 0086 /** 0087 * Turns the digit into a byte with the value of the digit. 0088 * @param digit digit to turn into the value 0089 * @return true if successful, false otherwise 0090 */ 0091 virtual bool turnToValue(unsigned char* digit) const = 0; 0092 0093 public: 0094 /** 0095 * Tries to decode the digits in the text into a byte. 0096 * @param byte pointer to the variable to store the result in 0097 * @param text string to turn into the value 0098 * @param pos offset in the text to start with decoding 0099 * @return used chars of the string for the decoding, beginning with pos 0100 */ 0101 unsigned int decode(Byte* byte, const QString& digits, unsigned int pos = 0) const; 0102 }; 0103 0104 inline ValueCodec::ValueCodec() = default; 0105 0106 } 0107 0108 #endif