mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(core): add new class for easy string conversion
This commit is contained in:
parent
bf0e6817af
commit
1f0467bb1b
|
@ -229,6 +229,8 @@ set(${PROJECT_NAME}_SOURCES
|
|||
src/core/toxid.h
|
||||
src/core/toxpk.cpp
|
||||
src/core/toxpk.h
|
||||
src/core/toxstring.cpp
|
||||
src/core/toxstring.h
|
||||
src/friend.cpp
|
||||
src/friend.h
|
||||
src/friendlist.cpp
|
||||
|
|
2
qtox.pro
2
qtox.pro
|
@ -444,6 +444,7 @@ HEADERS += \
|
|||
src/core/toxencrypt.h \
|
||||
src/core/toxid.h \
|
||||
src/core/toxpk.h \
|
||||
src/core/toxstring.h \
|
||||
src/friend.h \
|
||||
src/friendlist.h \
|
||||
src/group.h \
|
||||
|
@ -564,6 +565,7 @@ SOURCES += \
|
|||
src/core/toxencrypt.cpp \
|
||||
src/core/toxid.cpp \
|
||||
src/core/toxpk.cpp \
|
||||
src/core/toxstring.cpp \
|
||||
src/friend.cpp \
|
||||
src/friendlist.cpp \
|
||||
src/group.cpp \
|
||||
|
|
96
src/core/toxstring.cpp
Normal file
96
src/core/toxstring.cpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
Copyright © 2017 by The qTox Project Contributors
|
||||
|
||||
This file is part of qTox, a Qt-based graphical interface for Tox.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
qTox is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "toxstring.h"
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
|
||||
/**
|
||||
* @class ToxString
|
||||
* @brief Helper to convert safely between strings in the c-toxcore representation and QString.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Creates a ToxString from a QString.
|
||||
* @param string Input text.
|
||||
*/
|
||||
ToxString::ToxString(const QString& text)
|
||||
: ToxString(text.toUtf8())
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates a ToxString from bytes in a QByteArray.
|
||||
* @param text Input text.
|
||||
*/
|
||||
ToxString::ToxString(const QByteArray& text)
|
||||
: string(text)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates a ToxString from the representation used by c-toxcore.
|
||||
* @param text Pointer to the beginning of the text.
|
||||
* @param length Number of bytes to read from the beginning.
|
||||
*/
|
||||
ToxString::ToxString(const uint8_t* text, size_t length)
|
||||
{
|
||||
assert(length <= INT_MAX);
|
||||
string = QByteArray(reinterpret_cast<const char*>(text), length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a pointer to the beginning of the string data.
|
||||
* @return Pointer to the beginning of the string data.
|
||||
*/
|
||||
const uint8_t* ToxString::data() const
|
||||
{
|
||||
return reinterpret_cast<const uint8_t*>(string.constData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the number of bytes in the string.
|
||||
* @return Number of bytes in the string.
|
||||
*/
|
||||
size_t ToxString::size() const
|
||||
{
|
||||
return string.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets the string as QString.
|
||||
* @return QString representation of the string.
|
||||
*/
|
||||
QString ToxString::getQString() const
|
||||
{
|
||||
return QString::fromUtf8(string);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief getBytes Gets the bytes of the string.
|
||||
* @return Bytes of the string as QByteArray.
|
||||
*/
|
||||
QByteArray ToxString::getBytes() const
|
||||
{
|
||||
return QByteArray(string);
|
||||
}
|
43
src/core/toxstring.h
Normal file
43
src/core/toxstring.h
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
Copyright © 2017 by The qTox Project Contributors
|
||||
|
||||
This file is part of qTox, a Qt-based graphical interface for Tox.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
qTox is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TOXSTRING_H
|
||||
#define TOXSTRING_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
class ToxString
|
||||
{
|
||||
public:
|
||||
explicit ToxString(const QString& text);
|
||||
explicit ToxString(const QByteArray& text);
|
||||
ToxString(const uint8_t* text, size_t length);
|
||||
|
||||
const uint8_t* data() const;
|
||||
size_t size() const;
|
||||
QString getQString() const;
|
||||
QByteArray getBytes() const;
|
||||
|
||||
private:
|
||||
QByteArray string;
|
||||
};
|
||||
#endif // TOXSTRING_H
|
Loading…
Reference in New Issue
Block a user