Browse Source

Use nested namespaces definition syntax

adaptive-webui-19844
Vladimir Golovnev (Glassez) 4 years ago
parent
commit
552ff0489d
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 15
      src/base/utils/bytearray.h
  2. 21
      src/base/utils/foreignapps.h
  3. 69
      src/base/utils/fs.h
  4. 9
      src/base/utils/gzip.h
  5. 67
      src/base/utils/io.h
  6. 109
      src/base/utils/misc.h
  7. 31
      src/base/utils/net.h
  8. 23
      src/base/utils/password.h
  9. 7
      src/base/utils/random.h
  10. 79
      src/base/utils/string.h

15
src/base/utils/bytearray.h

@ -33,15 +33,12 @@
class QByteArray; class QByteArray;
namespace Utils namespace Utils::ByteArray
{ {
namespace ByteArray // Mimic QString::splitRef(sep, behavior)
{ QVector<QByteArray> splitToViews(const QByteArray &in, const QByteArray &sep, const QString::SplitBehavior behavior = QString::KeepEmptyParts);
// Mimic QString::splitRef(sep, behavior)
QVector<QByteArray> splitToViews(const QByteArray &in, const QByteArray &sep, const QString::SplitBehavior behavior = QString::KeepEmptyParts);
// Mimic QByteArray::mid(pos, len) but instead of returning a full-copy, // Mimic QByteArray::mid(pos, len) but instead of returning a full-copy,
// we only return a partial view // we only return a partial view
const QByteArray midView(const QByteArray &in, int pos, int len = -1); const QByteArray midView(const QByteArray &in, int pos, int len = -1);
}
} }

21
src/base/utils/foreignapps.h

@ -33,21 +33,18 @@
#include "base/utils/version.h" #include "base/utils/version.h"
namespace Utils namespace Utils::ForeignApps
{ {
namespace ForeignApps struct PythonInfo
{ {
struct PythonInfo using Version = Utils::Version<quint8, 3, 1>;
{
using Version = Utils::Version<quint8, 3, 1>;
bool isValid() const; bool isValid() const;
bool isSupportedVersion() const; bool isSupportedVersion() const;
QString executableName; QString executableName;
Version version; Version version;
}; };
PythonInfo pythonInfo(); PythonInfo pythonInfo();
}
} }

69
src/base/utils/fs.h

@ -34,47 +34,44 @@
#include <QString> #include <QString>
namespace Utils namespace Utils::Fs
{ {
namespace Fs /**
{ * Converts a path to a string suitable for display.
/** * This function makes sure the directory separator used is consistent
* Converts a path to a string suitable for display. * with the OS being run.
* This function makes sure the directory separator used is consistent */
* with the OS being run. QString toNativePath(const QString &path);
*/ /**
QString toNativePath(const QString &path); * Converts a path to a string suitable for processing.
/** * This function makes sure the directory separator used is independent
* Converts a path to a string suitable for processing. * from the OS being run so it is the same on all supported platforms.
* This function makes sure the directory separator used is independent * Slash ('/') is used as "uniform" directory separator.
* from the OS being run so it is the same on all supported platforms. */
* Slash ('/') is used as "uniform" directory separator. QString toUniformPath(const QString &path);
*/
QString toUniformPath(const QString &path);
QString fileExtension(const QString &filename); QString fileExtension(const QString &filename);
QString fileName(const QString &filePath); QString fileName(const QString &filePath);
QString folderName(const QString &filePath); QString folderName(const QString &filePath);
qint64 computePathSize(const QString &path); qint64 computePathSize(const QString &path);
bool sameFiles(const QString &path1, const QString &path2); bool sameFiles(const QString &path1, const QString &path2);
QString toValidFileSystemName(const QString &name, bool allowSeparators = false QString toValidFileSystemName(const QString &name, bool allowSeparators = false
, const QString &pad = QLatin1String(" ")); , const QString &pad = QLatin1String(" "));
bool isValidFileSystemName(const QString &name, bool allowSeparators = false); bool isValidFileSystemName(const QString &name, bool allowSeparators = false);
qint64 freeDiskSpaceOnPath(const QString &path); qint64 freeDiskSpaceOnPath(const QString &path);
QString branchPath(const QString &filePath, QString *removed = nullptr); QString branchPath(const QString &filePath, QString *removed = nullptr);
bool sameFileNames(const QString &first, const QString &second); bool sameFileNames(const QString &first, const QString &second);
QString expandPath(const QString &path); QString expandPath(const QString &path);
QString expandPathAbs(const QString &path); QString expandPathAbs(const QString &path);
bool isRegularFile(const QString &path); bool isRegularFile(const QString &path);
bool smartRemoveEmptyFolderTree(const QString &path); bool smartRemoveEmptyFolderTree(const QString &path);
bool forceRemove(const QString &filePath); bool forceRemove(const QString &filePath);
void removeDirRecursive(const QString &path); void removeDirRecursive(const QString &path);
QString tempPath(); QString tempPath();
#if !defined Q_OS_HAIKU #if !defined Q_OS_HAIKU
bool isNetworkFileSystem(const QString &path); bool isNetworkFileSystem(const QString &path);
#endif #endif
}
} }

9
src/base/utils/gzip.h

@ -31,11 +31,8 @@
class QByteArray; class QByteArray;
namespace Utils namespace Utils::Gzip
{ {
namespace Gzip QByteArray compress(const QByteArray &data, int level = 6, bool *ok = nullptr);
{ QByteArray decompress(const QByteArray &data, bool *ok = nullptr);
QByteArray compress(const QByteArray &data, int level = 6, bool *ok = nullptr);
QByteArray decompress(const QByteArray &data, bool *ok = nullptr);
}
} }

67
src/base/utils/io.h

@ -34,47 +34,44 @@
class QByteArray; class QByteArray;
class QFileDevice; class QFileDevice;
namespace Utils namespace Utils::IO
{ {
namespace IO // A wrapper class that satisfy LegacyOutputIterator requirement
class FileDeviceOutputIterator
{ {
// A wrapper class that satisfy LegacyOutputIterator requirement public:
class FileDeviceOutputIterator // std::iterator_traits
{ using iterator_category = std::output_iterator_tag;
public: using difference_type = void;
// std::iterator_traits using value_type = void;
using iterator_category = std::output_iterator_tag; using pointer = void;
using difference_type = void; using reference = void;
using value_type = void;
using pointer = void;
using reference = void;
explicit FileDeviceOutputIterator(QFileDevice &device, const int bufferSize = (4 * 1024)); explicit FileDeviceOutputIterator(QFileDevice &device, const int bufferSize = (4 * 1024));
FileDeviceOutputIterator(const FileDeviceOutputIterator &other) = default; FileDeviceOutputIterator(const FileDeviceOutputIterator &other) = default;
~FileDeviceOutputIterator(); ~FileDeviceOutputIterator();
// mimic std::ostream_iterator behavior // mimic std::ostream_iterator behavior
FileDeviceOutputIterator &operator=(char c); FileDeviceOutputIterator &operator=(char c);
constexpr FileDeviceOutputIterator &operator*() constexpr FileDeviceOutputIterator &operator*()
{ {
return *this; return *this;
} }
constexpr FileDeviceOutputIterator &operator++() constexpr FileDeviceOutputIterator &operator++()
{ {
return *this; return *this;
} }
constexpr FileDeviceOutputIterator &operator++(int) constexpr FileDeviceOutputIterator &operator++(int)
{ {
return *this; return *this;
} }
private: private:
QFileDevice *m_device; QFileDevice *m_device;
std::shared_ptr<QByteArray> m_buffer; std::shared_ptr<QByteArray> m_buffer;
int m_bufferSize; int m_bufferSize;
}; };
}
} }

109
src/base/utils/misc.h

@ -41,69 +41,66 @@ enum class ShutdownDialogAction;
/* Miscellaneous functions that can be useful */ /* Miscellaneous functions that can be useful */
namespace Utils namespace Utils::Misc
{ {
namespace Misc // use binary prefix standards from IEC 60027-2
// see http://en.wikipedia.org/wiki/Kilobyte
enum class SizeUnit
{ {
// use binary prefix standards from IEC 60027-2 Byte, // 1024^0,
// see http://en.wikipedia.org/wiki/Kilobyte KibiByte, // 1024^1,
enum class SizeUnit MebiByte, // 1024^2,
{ GibiByte, // 1024^3,
Byte, // 1024^0, TebiByte, // 1024^4,
KibiByte, // 1024^1, PebiByte, // 1024^5,
MebiByte, // 1024^2, ExbiByte // 1024^6,
GibiByte, // 1024^3, // int64 is used for sizes and thus the next units can not be handled
TebiByte, // 1024^4, // ZebiByte, // 1024^7,
PebiByte, // 1024^5, // YobiByte, // 1024^8
ExbiByte // 1024^6, };
// int64 is used for sizes and thus the next units can not be handled
// ZebiByte, // 1024^7, QString parseHtmlLinks(const QString &rawText);
// YobiByte, // 1024^8
}; void shutdownComputer(const ShutdownDialogAction &action);
QString parseHtmlLinks(const QString &rawText); QString osName();
QString boostVersionString();
void shutdownComputer(const ShutdownDialogAction &action); QString libtorrentVersionString();
QString opensslVersionString();
QString osName(); QString zlibVersionString();
QString boostVersionString();
QString libtorrentVersionString(); QString unitString(SizeUnit unit, bool isSpeed = false);
QString opensslVersionString();
QString zlibVersionString(); // return the best user friendly storage unit (B, KiB, MiB, GiB, TiB)
// value must be given in bytes
QString unitString(SizeUnit unit, bool isSpeed = false); QString friendlyUnit(qint64 bytesValue, bool isSpeed = false);
int friendlyUnitPrecision(SizeUnit unit);
// return the best user friendly storage unit (B, KiB, MiB, GiB, TiB) qint64 sizeInBytes(qreal size, SizeUnit unit);
// value must be given in bytes
QString friendlyUnit(qint64 bytesValue, bool isSpeed = false); bool isPreviewable(const QString &extension);
int friendlyUnitPrecision(SizeUnit unit);
qint64 sizeInBytes(qreal size, SizeUnit unit); // Take a number of seconds and return a user-friendly
// time duration like "1d 2h 10m".
bool isPreviewable(const QString &extension); QString userFriendlyDuration(qlonglong seconds, qlonglong maxCap = -1);
QString getUserIDString();
// Take a number of seconds and return a user-friendly
// time duration like "1d 2h 10m".
QString userFriendlyDuration(qlonglong seconds, qlonglong maxCap = -1);
QString getUserIDString();
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QString windowsSystemPath(); QString windowsSystemPath();
template <typename T> template <typename T>
T loadWinAPI(const QString &source, const char *funcName) T loadWinAPI(const QString &source, const char *funcName)
{ {
QString path = windowsSystemPath(); QString path = windowsSystemPath();
if (!path.endsWith('\\')) if (!path.endsWith('\\'))
path += '\\'; path += '\\';
path += source; path += source;
auto pathWchar = std::make_unique<wchar_t[]>(path.length() + 1); auto pathWchar = std::make_unique<wchar_t[]>(path.length() + 1);
path.toWCharArray(pathWchar.get()); path.toWCharArray(pathWchar.get());
return reinterpret_cast<T>( return reinterpret_cast<T>(
::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName)); ::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName));
}
#endif // Q_OS_WIN
} }
#endif // Q_OS_WIN
} }

31
src/base/utils/net.h

@ -35,24 +35,21 @@ class QSslCertificate;
class QSslKey; class QSslKey;
class QString; class QString;
namespace Utils namespace Utils::Net
{ {
namespace Net using Subnet = QPair<QHostAddress, int>;
{
using Subnet = QPair<QHostAddress, int>;
bool isValidIP(const QString &ip); bool isValidIP(const QString &ip);
Subnet parseSubnet(const QString &subnetStr, bool *ok = nullptr); Subnet parseSubnet(const QString &subnetStr, bool *ok = nullptr);
bool canParseSubnet(const QString &subnetStr); bool canParseSubnet(const QString &subnetStr);
bool isLoopbackAddress(const QHostAddress &addr); bool isLoopbackAddress(const QHostAddress &addr);
bool isIPInRange(const QHostAddress &addr, const QVector<Subnet> &subnets); bool isIPInRange(const QHostAddress &addr, const QVector<Subnet> &subnets);
QString subnetToString(const Subnet &subnet); QString subnetToString(const Subnet &subnet);
QHostAddress canonicalIPv6Addr(const QHostAddress &addr); QHostAddress canonicalIPv6Addr(const QHostAddress &addr);
const int MAX_SSL_FILE_SIZE = 1024 * 1024; const int MAX_SSL_FILE_SIZE = 1024 * 1024;
QList<QSslCertificate> loadSSLCertificate(const QByteArray &data); QList<QSslCertificate> loadSSLCertificate(const QByteArray &data);
bool isSSLCertificatesValid(const QByteArray &data); bool isSSLCertificatesValid(const QByteArray &data);
QSslKey loadSSLKey(const QByteArray &data); QSslKey loadSSLKey(const QByteArray &data);
bool isSSLKeyValid(const QByteArray &data); bool isSSLKeyValid(const QByteArray &data);
}
} }

23
src/base/utils/password.h

@ -31,21 +31,18 @@
class QByteArray; class QByteArray;
class QString; class QString;
namespace Utils namespace Utils::Password
{ {
namespace Password // Implements constant-time comparison to protect against timing attacks
{ // Taken from https://crackstation.net/hashing-security.htm
// Implements constant-time comparison to protect against timing attacks bool slowEquals(const QByteArray &a, const QByteArray &b);
// Taken from https://crackstation.net/hashing-security.htm
bool slowEquals(const QByteArray &a, const QByteArray &b);
namespace PBKDF2 namespace PBKDF2
{ {
QByteArray generate(const QString &password); QByteArray generate(const QString &password);
QByteArray generate(const QByteArray &password); QByteArray generate(const QByteArray &password);
bool verify(const QByteArray &secret, const QString &password); bool verify(const QByteArray &secret, const QString &password);
bool verify(const QByteArray &secret, const QByteArray &password); bool verify(const QByteArray &secret, const QByteArray &password);
}
} }
} }

7
src/base/utils/random.h

@ -31,10 +31,7 @@
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
namespace Utils namespace Utils::Random
{ {
namespace Random uint32_t rand(uint32_t min = 0, uint32_t max = std::numeric_limits<uint32_t>::max());
{
uint32_t rand(uint32_t min = 0, uint32_t max = std::numeric_limits<uint32_t>::max());
}
} }

79
src/base/utils/string.h

@ -37,59 +37,56 @@
class QStringRef; class QStringRef;
namespace Utils namespace Utils::String
{ {
namespace String QString fromDouble(double n, int precision);
int naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity);
template <Qt::CaseSensitivity caseSensitivity>
bool naturalLessThan(const QString &left, const QString &right)
{ {
QString fromDouble(double n, int precision); return (naturalCompare(left, right, caseSensitivity) < 0);
}
int naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity); QString wildcardToRegex(const QString &pattern);
template <Qt::CaseSensitivity caseSensitivity>
bool naturalLessThan(const QString &left, const QString &right)
{
return (naturalCompare(left, right, caseSensitivity) < 0);
}
QString wildcardToRegex(const QString &pattern); template <typename T>
T unquote(const T &str, const QString &quotes = QChar('"'))
{
if (str.length() < 2) return str;
template <typename T> for (const QChar quote : quotes)
T unquote(const T &str, const QString &quotes = QChar('"'))
{ {
if (str.length() < 2) return str; if (str.startsWith(quote) && str.endsWith(quote))
return str.mid(1, (str.length() - 2));
for (const QChar quote : quotes)
{
if (str.startsWith(quote) && str.endsWith(quote))
return str.mid(1, (str.length() - 2));
}
return str;
} }
bool parseBool(const QString &string, bool defaultValue); return str;
}
bool parseBool(const QString &string, bool defaultValue);
QString join(const QVector<QStringRef> &strings, const QString &separator); QString join(const QVector<QStringRef> &strings, const QString &separator);
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0> template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
QString fromEnum(const T &value) QString fromEnum(const T &value)
{ {
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>, static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int."); "Enumeration underlying type has to be int.");
const auto metaEnum = QMetaEnum::fromType<T>(); const auto metaEnum = QMetaEnum::fromType<T>();
return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value))); return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value)));
} }
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0> template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
T toEnum(const QString &serializedValue, const T &defaultValue) T toEnum(const QString &serializedValue, const T &defaultValue)
{ {
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>, static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int."); "Enumeration underlying type has to be int.");
const auto metaEnum = QMetaEnum::fromType<T>(); const auto metaEnum = QMetaEnum::fromType<T>();
bool ok = false; bool ok = false;
const T value = static_cast<T>(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok)); const T value = static_cast<T>(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok));
return (ok ? value : defaultValue); return (ok ? value : defaultValue);
}
} }
} }

Loading…
Cancel
Save