1 /*
2  * Replacement fot QT Bindings that were removed from QT5
3  * Copyright (C) 2020  Pedro de Carvalho Gomes <pedro.gomes@ipsoft.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #ifndef COREBYTEARRAY_H
19 #define COREBYTEARRAY_H
20 
21 #include "QtBinding.h"
22 
23 #include <QObject>
24 #include <QByteArray>
25 
26 class QJSEngine;
27 
28 namespace QtBindings
29 {
30     namespace Core
31     {
32         class ByteArray : public QObject, public QByteArray, public QtBindings::Base<ByteArray>
33         {
34         Q_OBJECT
35         public:
36             Q_INVOKABLE ByteArray();
37             Q_INVOKABLE explicit ByteArray(const char *data, int size = -1);
38             Q_INVOKABLE ByteArray(int size, char ch);
39             Q_INVOKABLE explicit ByteArray(const QByteArray &other);
40             Q_INVOKABLE ByteArray(const ByteArray &other);
41             Q_INVOKABLE ByteArray(ByteArray &&other) noexcept;
42             Q_INVOKABLE explicit ByteArray(const QString &other);
43             Q_INVOKABLE static ByteArray fromBase64(const QByteArray &base64);
44             Q_INVOKABLE static ByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options);
45             Q_INVOKABLE static ByteArray fromHex(const QByteArray &hexEncoded);
46             Q_INVOKABLE static ByteArray fromPercentEncoding(const QByteArray &input, char percent = '%');
47             Q_INVOKABLE static ByteArray fromRawData(const char *data, int size);
48             Q_INVOKABLE static ByteArray fromStdString(const std::string &str);
49             Q_INVOKABLE static ByteArray number(int n, int base = 10);
50             Q_INVOKABLE static ByteArray number(uint n, int base = 10);
51             Q_INVOKABLE static ByteArray number(qlonglong n, int base = 10);
52             Q_INVOKABLE static ByteArray number(qulonglong n, int base = 10);
53             Q_INVOKABLE static ByteArray number(double n, char f = 'g', int prec = 6);
54             ByteArray &operator=(const ByteArray &other);
55         public slots:
56             ByteArray &append(const QByteArray &ba);
57             ByteArray &append(int count, char ch);
58             ByteArray &append(const char *str);
59             ByteArray &append(const char *str, int len);
60             ByteArray &append(char ch);
61             ByteArray &append(const QString &str);
62             char at(int i) const;
63             QByteArray::iterator begin();
64             QByteArray::const_iterator begin() const;
65             int capacity() const;
66             QByteArray::const_iterator cbegin() const;
67             QByteArray::const_iterator cend() const;
68             void chop(int n);
69             void clear();
70             QByteArray::const_iterator constBegin() const;
71             const char *constData() const;
72             QByteArray::const_iterator constEnd() const;
73             bool contains(const QByteArray &ba) const;
74             bool contains(const char *str) const;
75             bool contains(char ch) const;
76             int count(const QByteArray &ba) const;
77             int count(const char *str) const;
78             int count(char ch) const;
79             int count() const;
80             QByteArray::const_reverse_iterator crbegin() const;
81             QByteArray::const_reverse_iterator crend() const;
82             char *data();
83             const char *data() const;
84             QByteArray::iterator end();
85             QByteArray::const_iterator end() const;
86             bool endsWith(const QByteArray &ba) const;
87             bool endsWith(char ch) const;
88             bool endsWith(const char *str) const;
89             QByteArray &fill(char ch, int size = -1);
90             int indexOf(const QByteArray &ba, int from = 0) const;
91             int indexOf(const char *str, int from = 0) const;
92             int indexOf(char ch, int from = 0) const;
93             int indexOf(const QString &str, int from = 0) const;
94             ByteArray &insert(int i, const QByteArray &ba);
95             ByteArray &insert(int i, int count, char ch);
96             ByteArray &insert(int i, const char *str);
97             ByteArray &insert(int i, const char *str, int len);
98             ByteArray &insert(int i, char ch);
99             ByteArray &insert(int i, const QString &str);
100             bool isEmpty() const;
101             bool isNull() const;
102             int lastIndexOf(const QByteArray &ba, int from = -1) const;
103             int lastIndexOf(const char *str, int from = -1) const;
104             int lastIndexOf(char ch, int from = -1) const;
105             int lastIndexOf(const QString &str, int from = -1) const;
106             QByteArray left(int len) const;
107             QByteArray leftJustified(int width, char fill = ' ', bool truncate = false) const;
108             int length() const;
109             QByteArray mid(int pos, int len = -1) const;
110             ByteArray &prepend(const QByteArray &ba);
111             ByteArray &prepend(int count, char ch);
112             ByteArray &prepend(const char *str);
113             ByteArray &prepend(const char *str, int len);
114             ByteArray &prepend(char ch);
115             void push_back(const QByteArray &other);
116             void push_back(const char *str);
117             void push_back(char ch);
118             void push_front(const QByteArray &other);
119             void push_front(const char *str);
120             void push_front(char ch);
121             QByteArray::reverse_iterator rbegin();
122             QByteArray::const_reverse_iterator rbegin() const;
123             QByteArray &remove(int pos, int len);
124             QByteArray::reverse_iterator rend();
125             QByteArray::const_reverse_iterator rend() const;
126             QByteArray repeated(int times) const;
127             QByteArray &replace(int pos, int len, const QByteArray &after);
128             QByteArray &replace(int pos, int len, const char *after, int alen);
129             QByteArray &replace(int pos, int len, const char *after);
130             QByteArray &replace(char before, const char *after);
131             QByteArray &replace(char before, const QByteArray &after);
132             QByteArray &replace(const char *before, const char *after);
133             QByteArray &replace(const char *before, int bsize, const char *after, int asize);
134             QByteArray &replace(const QByteArray &before, const QByteArray &after);
135             QByteArray &replace(const QByteArray &before, const char *after);
136             QByteArray &replace(const char *before, const QByteArray &after);
137             QByteArray &replace(char before, char after);
138             QByteArray &replace(const QString &before, const char *after);
139             QByteArray &replace(char before, const QString &after);
140             QByteArray &replace(const QString &before, const QByteArray &after);
141             void reserve(int size);
142             void resize(int size);
143             QByteArray right(int len) const;
144             QByteArray rightJustified(int width, char fill = ' ', bool truncate = false) const;
145             QByteArray &setNum(int n, int base = 10);
146             QByteArray &setNum(ushort n, int base = 10);
147             QByteArray &setNum(short n, int base = 10);
148             QByteArray &setNum(uint n, int base = 10);
149             QByteArray &setNum(qlonglong n, int base = 10);
150             QByteArray &setNum(qulonglong n, int base = 10);
151             QByteArray &setNum(float n, char f = 'g', int prec = 6);
152             QByteArray &setNum(double n, char f = 'g', int prec = 6);
153             QByteArray &setRawData(const char *data, uint size);
154             QByteArray simplified() const;
155             int size() const;
156             QList<QByteArray> split(char sep) const;
157             void squeeze();
158             bool startsWith(const QByteArray &ba) const;
159             bool startsWith(char ch) const;
160             bool startsWith(const char *str) const;
161             void swap(QByteArray &other);
162             QByteArray toBase64() const;
163             QByteArray toBase64(QByteArray::Base64Options options) const;
164             double toDouble(bool *ok = Q_NULLPTR) const;
165             float toFloat(bool *ok = Q_NULLPTR) const;
166             QByteArray toHex() const;
167             QByteArray toHex(char separator) const;
168             int toInt(bool *ok = Q_NULLPTR, int base = 10) const;
169             long toLong(bool *ok = Q_NULLPTR, int base = 10) const;
170             qlonglong toLongLong(bool *ok = Q_NULLPTR, int base = 10) const;
171             QByteArray toLower() const;
172             QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(), const QByteArray &include = QByteArray(), char percent = '%') const;
173             short toShort(bool *ok = Q_NULLPTR, int base = 10) const;
174             std::string toStdString() const;
175             uint toUInt(bool *ok = Q_NULLPTR, int base = 10) const;
176             ulong toULong(bool *ok = Q_NULLPTR, int base = 10) const;
177             qulonglong toULongLong(bool *ok = Q_NULLPTR, int base = 10) const;
178             ushort toUShort(bool *ok = Q_NULLPTR, int base = 10) const;
179             QByteArray toUpper() const;
180             QByteArray trimmed() const;
181             void truncate(int pos);
182         };
183     }
184 }
185 Q_DECLARE_METATYPE(QtBindings::Core::ByteArray)
186 #endif // COREBYTEARRAY_H
187