459 |
andreas |
1 |
/*
|
|
|
2 |
* Copyright (C) 2024 by Andreas Theofilu <andreas@theosys.at>
|
|
|
3 |
*
|
|
|
4 |
* This program is free software; you can redistribute it and/or modify
|
|
|
5 |
* it under the terms of the GNU General Public License as published by
|
|
|
6 |
* the Free Software Foundation; either version 3 of the License, or
|
|
|
7 |
* (at your option) any later version.
|
|
|
8 |
*
|
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
* GNU General Public License for more details.
|
|
|
13 |
*
|
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
|
15 |
* along with this program; if not, write to the Free Software Foundation,
|
|
|
16 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
#ifndef TBYTEARRAY_H
|
|
|
20 |
#define TBYTEARRAY_H
|
|
|
21 |
|
|
|
22 |
#include <string>
|
|
|
23 |
#include <cstddef>
|
|
|
24 |
|
|
|
25 |
class TByteArray
|
|
|
26 |
{
|
|
|
27 |
public:
|
|
|
28 |
TByteArray();
|
|
|
29 |
TByteArray(TByteArray& arr);
|
|
|
30 |
TByteArray(const unsigned char *data, size_t len);
|
|
|
31 |
TByteArray(const std::string& data);
|
|
|
32 |
~TByteArray();
|
|
|
33 |
|
|
|
34 |
void assign(unsigned char *data, size_t len);
|
|
|
35 |
void assign(const std::string& data);
|
|
|
36 |
void assign(const TByteArray& arr);
|
|
|
37 |
|
|
|
38 |
void append(unsigned char *data, size_t len);
|
|
|
39 |
void append(const std::string& data);
|
|
|
40 |
void append(const TByteArray& arr);
|
|
|
41 |
|
|
|
42 |
size_t size() { return mSize; }
|
|
|
43 |
bool empty() { return mSize == 0; }
|
|
|
44 |
std::string toString();
|
|
|
45 |
unsigned char *toSerial(unsigned char *erg=nullptr, bool bigEndian=false);
|
|
|
46 |
static unsigned char *hexStrToByte(const std::string& hstr, size_t *size=nullptr);
|
|
|
47 |
size_t serialSize();
|
|
|
48 |
unsigned char *data() { return mBuffer; };
|
|
|
49 |
unsigned char at(size_t pos) const;
|
|
|
50 |
void clear();
|
|
|
51 |
|
|
|
52 |
TByteArray& operator=(const TByteArray& other);
|
|
|
53 |
TByteArray& operator+=(const TByteArray& other);
|
|
|
54 |
bool operator==(const TByteArray& other) const;
|
|
|
55 |
bool operator!=(const TByteArray& other) const;
|
|
|
56 |
unsigned char operator[](size_t pos) const;
|
|
|
57 |
|
|
|
58 |
protected:
|
|
|
59 |
bool isBigEndian();
|
|
|
60 |
unsigned char *swapBytes(unsigned char *b, size_t size);
|
|
|
61 |
|
|
|
62 |
private:
|
|
|
63 |
size_t mSize{0};
|
|
|
64 |
unsigned char *mBuffer{nullptr};
|
|
|
65 |
};
|
|
|
66 |
|
|
|
67 |
#endif // BYTEARRAY_H
|