Warning, file /utilities/okteta/kasten/gui/liboktetawidgets/bytearrayvalidator.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006, 2009, 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 "bytearrayvalidator.hpp"
0010 
0011 // Okteta core
0012 #include <Okteta/ValueCodec>
0013 #include <Okteta/Character>
0014 #include <Okteta/CharCodec>
0015 
0016 namespace Okteta {
0017 
0018 ByteArrayValidator::ByteArrayValidator(QObject* parent, Coding codecId, int charCodecId)
0019     : QValidator(parent)
0020     , mCharCodec(CharCodec::createCodec(Okteta::LocalEncoding))
0021 {
0022     Q_UNUSED(charCodecId)
0023     setCodec(codecId);
0024 }
0025 
0026 ByteArrayValidator::~ByteArrayValidator()
0027 {
0028     delete mValueCodec;
0029     delete mCharCodec;
0030 }
0031 
0032 void ByteArrayValidator::setCharCodec(const QString& charCodecName)
0033 {
0034     if (charCodecName == mCharCodec->name()) {
0035         return;
0036     }
0037 
0038     delete mCharCodec;
0039     mCharCodec = CharCodec::createCodec(charCodecName);
0040 }
0041 
0042 void ByteArrayValidator::setCodec(Coding codecId)
0043 {
0044     if (codecId == mCodecId) {
0045         return;
0046     }
0047 
0048     mCodecId = codecId;
0049 
0050     if (mCodecId != CharCoding
0051         && mCodecId != Utf8Coding) {
0052         delete mValueCodec;
0053         mValueCodec = ValueCodec::createCodec((Okteta::ValueCoding)mCodecId);
0054     }
0055 }
0056 
0057 void ByteArrayValidator::setMaxLength(int maxLength)
0058 {
0059     mMaxLength = maxLength;
0060     if (maxLength < mMinLength) {
0061         mMinLength = maxLength;
0062     }
0063 }
0064 
0065 void ByteArrayValidator::setMinLength(int minLength)
0066 {
0067     mMinLength = minLength;
0068     if (minLength > mMaxLength) {
0069         mMaxLength = minLength;
0070     }
0071 }
0072 
0073 QValidator::State ByteArrayValidator::validate(QString& string, int& pos) const
0074 {
0075     Q_UNUSED(pos)
0076 
0077     State result = QValidator::Acceptable;
0078 
0079     int stringLength = string.length();
0080 
0081     if (mCodecId == CharCoding) {
0082         if (stringLength > mMaxLength) {
0083             string.truncate(mMaxLength);
0084             stringLength = mMaxLength;
0085         }
0086 
0087         for (int i = 0; i < stringLength; ++i) {
0088             const QChar c = string.at(i);
0089             if (!mCharCodec->canEncode(c)) {
0090                 result = QValidator::Invalid;
0091                 break;
0092             }
0093         }
0094     } else if (mCodecId != Utf8Coding) {
0095         const int encodingWidth = mValueCodec->encodingWidth();
0096         int byteCount = 0;
0097         for (int i = 0; i < stringLength;) {
0098             Okteta::Byte dummyByte;
0099             const int usedCharCount = mValueCodec->decode(&dummyByte, string, i);
0100 
0101             // could not decode?
0102             if (usedCharCount == 0) {
0103                 result = QValidator::Invalid;
0104                 break;
0105             }
0106             i += usedCharCount;
0107             ++byteCount;
0108 
0109             if (byteCount >= mMaxLength) {
0110                 string.truncate(i);
0111                 break;
0112             }
0113         }
0114 
0115         if (byteCount < mMinLength) {
0116             const int paddingCount = (mMinLength - byteCount) * encodingWidth;
0117             string += QString(paddingCount, QLatin1Char('0'));
0118         }
0119     }
0120 
0121     return result;
0122 }
0123 
0124 QByteArray ByteArrayValidator::toByteArray(const QString& string) const
0125 {
0126     QByteArray result;
0127 
0128     const int stringLength = string.length();
0129     if (mCodecId == CharCoding) {
0130         result.resize(stringLength);
0131         for (int i = 0; i < stringLength; ++i) {
0132             Byte byte;
0133             const bool success = mCharCodec->encode(&byte, string[i]);
0134             result[i] = success ? byte : '?'; // TODO: define unknown symbol
0135         }
0136     } else if (mCodecId == Utf8Coding) {
0137         result = string.toUtf8();
0138     } else {
0139         int i = 0;
0140         while (i < stringLength) {
0141             Byte byte;
0142             const int readChars = mValueCodec->decode(&byte, string, i);
0143             if (readChars > 0) {
0144                 i += readChars;
0145                 result.append(byte);
0146             } else {
0147                 while (i < stringLength && !mValueCodec->isValidDigit(string[i].toLatin1()))
0148                     ++i;
0149             }
0150         }
0151     }
0152 
0153     return result;
0154 }
0155 
0156 QString ByteArrayValidator::toString(const QByteArray& byteArray) const
0157 {
0158     QString result;
0159 
0160     const int byteArraySize = byteArray.size();
0161     if (mCodecId == Utf8Coding) {
0162         result = QString::fromUtf8(byteArray.constData(), byteArraySize);
0163     } else if (mCodecId == CharCoding) {
0164         result.resize(byteArraySize);
0165         for (int i = 0; i < byteArraySize; ++i) {
0166             Character c = mCharCodec->decode(byteArray[i]);
0167             result[i] = c.isUndefined() ? QChar::fromLatin1('?') : c; // TODO: define unknown symbol
0168         }
0169     } else {
0170         const int encodingWidth = mValueCodec->encodingWidth();
0171         result.resize(byteArraySize * encodingWidth);
0172         int r = 0;
0173         for (int i = 0; i < byteArraySize; ++i, r += encodingWidth) {
0174             mValueCodec->encode(&result, r, byteArray[i]);
0175         }
0176     }
0177     return result;
0178 }
0179 
0180 }
0181 
0182 #include "moc_bytearrayvalidator.cpp"