File indexing completed on 2025-01-05 04:26:47

0001 /*
0002  * Replacement fot QT Bindings that were removed from QT5
0003  * Copyright (C) 2020  Pedro de Carvalho Gomes <pedro.gomes@ipsoft.com>
0004  * 
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  * 
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  * 
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 #ifndef COREBYTEARRAY_H 
0019 #define COREBYTEARRAY_H
0020 
0021 #include "QtBinding.h"
0022 
0023 #include <QObject>
0024 #include <QByteArray>
0025 
0026 class QJSEngine;
0027 
0028 namespace QtBindings
0029 {
0030     namespace Core
0031     {
0032         class ByteArray : public QObject, public QByteArray, public QtBindings::Base<ByteArray>
0033         {
0034         Q_OBJECT
0035         public:
0036             Q_INVOKABLE ByteArray();
0037             Q_INVOKABLE explicit ByteArray(const char *data, int size = -1);
0038             Q_INVOKABLE ByteArray(int size, char ch);
0039             Q_INVOKABLE explicit ByteArray(const QByteArray &other);
0040             Q_INVOKABLE ByteArray(const ByteArray &other);
0041             Q_INVOKABLE ByteArray(ByteArray &&other) noexcept;
0042             Q_INVOKABLE explicit ByteArray(const QString &other);
0043             Q_INVOKABLE static ByteArray fromBase64(const QByteArray &base64);
0044             Q_INVOKABLE static ByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options);
0045             Q_INVOKABLE static ByteArray fromHex(const QByteArray &hexEncoded);
0046             Q_INVOKABLE static ByteArray fromPercentEncoding(const QByteArray &input, char percent = '%');
0047             Q_INVOKABLE static ByteArray fromRawData(const char *data, int size);
0048             Q_INVOKABLE static ByteArray fromStdString(const std::string &str);
0049             Q_INVOKABLE static ByteArray number(int n, int base = 10);
0050             Q_INVOKABLE static ByteArray number(uint n, int base = 10);
0051             Q_INVOKABLE static ByteArray number(qlonglong n, int base = 10);
0052             Q_INVOKABLE static ByteArray number(qulonglong n, int base = 10);
0053             Q_INVOKABLE static ByteArray number(double n, char f = 'g', int prec = 6);
0054             ByteArray &operator=(const ByteArray &other);
0055         public slots:
0056             ByteArray &append(const QByteArray &ba);
0057             ByteArray &append(int count, char ch);
0058             ByteArray &append(const char *str);
0059             ByteArray &append(const char *str, int len);
0060             ByteArray &append(char ch);
0061             char at(int i) const;
0062             QByteArray::iterator begin();
0063             QByteArray::const_iterator begin() const;
0064             int capacity() const;
0065             QByteArray::const_iterator cbegin() const;
0066             QByteArray::const_iterator cend() const;
0067             void chop(int n);
0068             void clear();
0069             QByteArray::const_iterator constBegin() const;
0070             const char *constData() const;
0071             QByteArray::const_iterator constEnd() const;
0072             bool contains(const QByteArray &ba) const;
0073             bool contains(const char *str) const;
0074             bool contains(char ch) const;
0075             int count(const QByteArray &ba) const;
0076             int count(const char *str) const;
0077             int count(char ch) const;
0078             int count() const;
0079             QByteArray::const_reverse_iterator crbegin() const;
0080             QByteArray::const_reverse_iterator crend() const;
0081             char *data();
0082             const char *data() const;
0083             QByteArray::iterator end();
0084             QByteArray::const_iterator end() const;
0085             bool endsWith(const QByteArray &ba) const;
0086             bool endsWith(char ch) const;
0087             bool endsWith(const char *str) const;
0088             QByteArray &fill(char ch, int size = -1);
0089             int indexOf(const QByteArray &ba, int from = 0) const;
0090             int indexOf(const char *str, int from = 0) const;
0091             int indexOf(char ch, int from = 0) const;
0092             ByteArray &insert(int i, const QByteArray &ba);
0093             ByteArray &insert(int i, int count, char ch);
0094             ByteArray &insert(int i, const char *str);
0095             ByteArray &insert(int i, const char *str, int len);
0096             ByteArray &insert(int i, char ch);
0097             bool isEmpty() const;
0098             bool isNull() const;
0099             int lastIndexOf(const QByteArray &ba, int from = -1) const;
0100             int lastIndexOf(const char *str, int from = -1) const;
0101             int lastIndexOf(char ch, int from = -1) const;
0102             QByteArray left(int len) const;
0103             QByteArray leftJustified(int width, char fill = ' ', bool truncate = false) const;
0104             int length() const;
0105             QByteArray mid(int pos, int len = -1) const;
0106             ByteArray &prepend(const QByteArray &ba);
0107             ByteArray &prepend(int count, char ch);
0108             ByteArray &prepend(const char *str);
0109             ByteArray &prepend(const char *str, int len);
0110             ByteArray &prepend(char ch);
0111             void push_back(const QByteArray &other);
0112             void push_back(const char *str);
0113             void push_back(char ch);
0114             void push_front(const QByteArray &other);
0115             void push_front(const char *str);
0116             void push_front(char ch);
0117             QByteArray::reverse_iterator rbegin();
0118             QByteArray::const_reverse_iterator rbegin() const;
0119             QByteArray &remove(int pos, int len);
0120             QByteArray::reverse_iterator rend();
0121             QByteArray::const_reverse_iterator rend() const;
0122             QByteArray repeated(int times) const;
0123             QByteArray &replace(int pos, int len, const QByteArray &after);
0124             QByteArray &replace(int pos, int len, const char *after, int alen);
0125             QByteArray &replace(int pos, int len, const char *after);
0126             QByteArray &replace(char before, const char *after);
0127             QByteArray &replace(char before, const QByteArray &after);
0128             QByteArray &replace(const char *before, const char *after);
0129             QByteArray &replace(const char *before, int bsize, const char *after, int asize);
0130             QByteArray &replace(const QByteArray &before, const QByteArray &after);
0131             QByteArray &replace(const QByteArray &before, const char *after);
0132             QByteArray &replace(const char *before, const QByteArray &after);
0133             QByteArray &replace(char before, char after);
0134             void reserve(int size);
0135             void resize(int size);
0136             QByteArray right(int len) const;
0137             QByteArray rightJustified(int width, char fill = ' ', bool truncate = false) const;
0138             QByteArray &setNum(int n, int base = 10);
0139             QByteArray &setNum(ushort n, int base = 10);
0140             QByteArray &setNum(short n, int base = 10);
0141             QByteArray &setNum(uint n, int base = 10);
0142             QByteArray &setNum(qlonglong n, int base = 10);
0143             QByteArray &setNum(qulonglong n, int base = 10);
0144             QByteArray &setNum(float n, char f = 'g', int prec = 6);
0145             QByteArray &setNum(double n, char f = 'g', int prec = 6);
0146             QByteArray &setRawData(const char *data, uint size);
0147             QByteArray simplified() const;
0148             int size() const;
0149             QList<QByteArray> split(char sep) const;
0150             void squeeze();
0151             bool startsWith(const QByteArray &ba) const;
0152             bool startsWith(char ch) const;
0153             bool startsWith(const char *str) const;
0154             void swap(QByteArray &other);
0155             QByteArray toBase64() const;
0156             QByteArray toBase64(QByteArray::Base64Options options) const;
0157             double toDouble(bool *ok = Q_NULLPTR) const;
0158             float toFloat(bool *ok = Q_NULLPTR) const;
0159             QByteArray toHex() const;
0160             QByteArray toHex(char separator) const;
0161             int toInt(bool *ok = Q_NULLPTR, int base = 10) const;
0162             long toLong(bool *ok = Q_NULLPTR, int base = 10) const;
0163             qlonglong toLongLong(bool *ok = Q_NULLPTR, int base = 10) const;
0164             QByteArray toLower() const;
0165             QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(), const QByteArray &include = QByteArray(), char percent = '%') const;
0166             short toShort(bool *ok = Q_NULLPTR, int base = 10) const;
0167             std::string toStdString() const;
0168             uint toUInt(bool *ok = Q_NULLPTR, int base = 10) const;
0169             ulong toULong(bool *ok = Q_NULLPTR, int base = 10) const;
0170             qulonglong toULongLong(bool *ok = Q_NULLPTR, int base = 10) const;
0171             ushort toUShort(bool *ok = Q_NULLPTR, int base = 10) const;
0172             QByteArray toUpper() const;
0173             QByteArray trimmed() const;
0174             void truncate(int pos);
0175         };
0176     }
0177 }
0178 Q_DECLARE_METATYPE(QtBindings::Core::ByteArray)
0179 #endif // COREBYTEARRAY_H