File indexing completed on 2024-05-05 17:57:59

0001 /*
0002     This file is part of the Okteta Core library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2004, 2011 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 "valuecodec.hpp"
0010 
0011 // lib
0012 #include "binarybytecodec.hpp"
0013 #include "octalbytecodec.hpp"
0014 #include "decimalbytecodec.hpp"
0015 #include "hexadecimalbytecodec.hpp"
0016 // Qt
0017 #include <QString>
0018 
0019 namespace Okteta {
0020 
0021 ValueCodec::~ValueCodec() = default;
0022 
0023 ValueCodec* ValueCodec::createCodec(ValueCoding valueCoding)
0024 {
0025     ValueCodec* result;
0026     switch (valueCoding)
0027     {
0028     case DecimalCoding: result = new DecimalByteCodec(); break;
0029     case OctalCoding:   result = new OctalByteCodec();   break;
0030     case BinaryCoding:  result = new BinaryByteCodec();  break;
0031     case HexadecimalCoding:
0032     default:            result = new HexadecimalByteCodec();
0033     }
0034     return result;
0035 }
0036 
0037 unsigned int ValueCodec::decode(unsigned char* byte, const QString& digits, unsigned int pos) const
0038 {
0039     const unsigned int oldPos = pos;
0040     const unsigned int left = digits.size() - pos;
0041 
0042     unsigned int d = encodingWidth();
0043     if (left < d) {
0044         d = left;
0045     }
0046 
0047     unsigned char result = 0;
0048     while (d > 0) {
0049         if (!appendDigit(&result, digits.at(pos).toLatin1())) { // TODO: use QChar.digitValue()
0050             break;
0051         }
0052 
0053         ++pos;
0054         --d;
0055     }
0056 
0057     *byte = result;
0058     return pos - oldPos;
0059 }
0060 
0061 }