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 @@ @@ -33,15 +33,12 @@
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,
// we only return a partial view
const QByteArray midView(const QByteArray &in, int pos, int len = -1);
}
// Mimic QByteArray::mid(pos, len) but instead of returning a full-copy,
// we only return a partial view
const QByteArray midView(const QByteArray &in, int pos, int len = -1);
}

21
src/base/utils/foreignapps.h

@ -33,21 +33,18 @@ @@ -33,21 +33,18 @@
#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 isSupportedVersion() const;
bool isValid() const;
bool isSupportedVersion() const;
QString executableName;
Version version;
};
QString executableName;
Version version;
};
PythonInfo pythonInfo();
}
PythonInfo pythonInfo();
}

69
src/base/utils/fs.h

@ -34,47 +34,44 @@ @@ -34,47 +34,44 @@
#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
* with the OS being run.
*/
QString toNativePath(const QString &path);
/**
* Converts a path to a string suitable for processing.
* This function makes sure the directory separator used is independent
* 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);
/**
* Converts a path to a string suitable for display.
* This function makes sure the directory separator used is consistent
* with the OS being run.
*/
QString toNativePath(const QString &path);
/**
* Converts a path to a string suitable for processing.
* This function makes sure the directory separator used is independent
* 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 fileExtension(const QString &filename);
QString fileName(const QString &filePath);
QString folderName(const QString &filePath);
qint64 computePathSize(const QString &path);
bool sameFiles(const QString &path1, const QString &path2);
QString toValidFileSystemName(const QString &name, bool allowSeparators = false
, const QString &pad = QLatin1String(" "));
bool isValidFileSystemName(const QString &name, bool allowSeparators = false);
qint64 freeDiskSpaceOnPath(const QString &path);
QString branchPath(const QString &filePath, QString *removed = nullptr);
bool sameFileNames(const QString &first, const QString &second);
QString expandPath(const QString &path);
QString expandPathAbs(const QString &path);
bool isRegularFile(const QString &path);
QString fileExtension(const QString &filename);
QString fileName(const QString &filePath);
QString folderName(const QString &filePath);
qint64 computePathSize(const QString &path);
bool sameFiles(const QString &path1, const QString &path2);
QString toValidFileSystemName(const QString &name, bool allowSeparators = false
, const QString &pad = QLatin1String(" "));
bool isValidFileSystemName(const QString &name, bool allowSeparators = false);
qint64 freeDiskSpaceOnPath(const QString &path);
QString branchPath(const QString &filePath, QString *removed = nullptr);
bool sameFileNames(const QString &first, const QString &second);
QString expandPath(const QString &path);
QString expandPathAbs(const QString &path);
bool isRegularFile(const QString &path);
bool smartRemoveEmptyFolderTree(const QString &path);
bool forceRemove(const QString &filePath);
void removeDirRecursive(const QString &path);
bool smartRemoveEmptyFolderTree(const QString &path);
bool forceRemove(const QString &filePath);
void removeDirRecursive(const QString &path);
QString tempPath();
QString tempPath();
#if !defined Q_OS_HAIKU
bool isNetworkFileSystem(const QString &path);
bool isNetworkFileSystem(const QString &path);
#endif
}
}

9
src/base/utils/gzip.h

@ -31,11 +31,8 @@ @@ -31,11 +31,8 @@
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 @@ @@ -34,47 +34,44 @@
class QByteArray;
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
class FileDeviceOutputIterator
{
public:
// std::iterator_traits
using iterator_category = std::output_iterator_tag;
using difference_type = void;
using value_type = void;
using pointer = void;
using reference = void;
public:
// std::iterator_traits
using iterator_category = std::output_iterator_tag;
using difference_type = void;
using value_type = void;
using pointer = void;
using reference = void;
explicit FileDeviceOutputIterator(QFileDevice &device, const int bufferSize = (4 * 1024));
FileDeviceOutputIterator(const FileDeviceOutputIterator &other) = default;
~FileDeviceOutputIterator();
explicit FileDeviceOutputIterator(QFileDevice &device, const int bufferSize = (4 * 1024));
FileDeviceOutputIterator(const FileDeviceOutputIterator &other) = default;
~FileDeviceOutputIterator();
// mimic std::ostream_iterator behavior
FileDeviceOutputIterator &operator=(char c);
// mimic std::ostream_iterator behavior
FileDeviceOutputIterator &operator=(char c);
constexpr FileDeviceOutputIterator &operator*()
{
return *this;
}
constexpr FileDeviceOutputIterator &operator*()
{
return *this;
}
constexpr FileDeviceOutputIterator &operator++()
{
return *this;
}
constexpr FileDeviceOutputIterator &operator++()
{
return *this;
}
constexpr FileDeviceOutputIterator &operator++(int)
{
return *this;
}
constexpr FileDeviceOutputIterator &operator++(int)
{
return *this;
}
private:
QFileDevice *m_device;
std::shared_ptr<QByteArray> m_buffer;
int m_bufferSize;
};
}
private:
QFileDevice *m_device;
std::shared_ptr<QByteArray> m_buffer;
int m_bufferSize;
};
}

109
src/base/utils/misc.h

@ -41,69 +41,66 @@ enum class ShutdownDialogAction; @@ -41,69 +41,66 @@ enum class ShutdownDialogAction;
/* 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
// see http://en.wikipedia.org/wiki/Kilobyte
enum class SizeUnit
{
Byte, // 1024^0,
KibiByte, // 1024^1,
MebiByte, // 1024^2,
GibiByte, // 1024^3,
TebiByte, // 1024^4,
PebiByte, // 1024^5,
ExbiByte // 1024^6,
// int64 is used for sizes and thus the next units can not be handled
// ZebiByte, // 1024^7,
// YobiByte, // 1024^8
};
QString parseHtmlLinks(const QString &rawText);
void shutdownComputer(const ShutdownDialogAction &action);
QString osName();
QString boostVersionString();
QString libtorrentVersionString();
QString opensslVersionString();
QString zlibVersionString();
QString unitString(SizeUnit unit, bool isSpeed = false);
// return the best user friendly storage unit (B, KiB, MiB, GiB, TiB)
// value must be given in bytes
QString friendlyUnit(qint64 bytesValue, bool isSpeed = false);
int friendlyUnitPrecision(SizeUnit unit);
qint64 sizeInBytes(qreal size, SizeUnit unit);
bool isPreviewable(const QString &extension);
// 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();
Byte, // 1024^0,
KibiByte, // 1024^1,
MebiByte, // 1024^2,
GibiByte, // 1024^3,
TebiByte, // 1024^4,
PebiByte, // 1024^5,
ExbiByte // 1024^6,
// int64 is used for sizes and thus the next units can not be handled
// ZebiByte, // 1024^7,
// YobiByte, // 1024^8
};
QString parseHtmlLinks(const QString &rawText);
void shutdownComputer(const ShutdownDialogAction &action);
QString osName();
QString boostVersionString();
QString libtorrentVersionString();
QString opensslVersionString();
QString zlibVersionString();
QString unitString(SizeUnit unit, bool isSpeed = false);
// return the best user friendly storage unit (B, KiB, MiB, GiB, TiB)
// value must be given in bytes
QString friendlyUnit(qint64 bytesValue, bool isSpeed = false);
int friendlyUnitPrecision(SizeUnit unit);
qint64 sizeInBytes(qreal size, SizeUnit unit);
bool isPreviewable(const QString &extension);
// 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
QString windowsSystemPath();
QString windowsSystemPath();
template <typename T>
T loadWinAPI(const QString &source, const char *funcName)
{
QString path = windowsSystemPath();
if (!path.endsWith('\\'))
path += '\\';
template <typename T>
T loadWinAPI(const QString &source, const char *funcName)
{
QString path = windowsSystemPath();
if (!path.endsWith('\\'))
path += '\\';
path += source;
path += source;
auto pathWchar = std::make_unique<wchar_t[]>(path.length() + 1);
path.toWCharArray(pathWchar.get());
auto pathWchar = std::make_unique<wchar_t[]>(path.length() + 1);
path.toWCharArray(pathWchar.get());
return reinterpret_cast<T>(
::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName));
}
#endif // Q_OS_WIN
return reinterpret_cast<T>(
::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName));
}
#endif // Q_OS_WIN
}

31
src/base/utils/net.h

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

23
src/base/utils/password.h

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

7
src/base/utils/random.h

@ -31,10 +31,7 @@ @@ -31,10 +31,7 @@
#include <cstdint>
#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 @@ @@ -37,59 +37,56 @@
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);
template <Qt::CaseSensitivity caseSensitivity>
bool naturalLessThan(const QString &left, const QString &right)
{
return (naturalCompare(left, right, caseSensitivity) < 0);
}
QString wildcardToRegex(const QString &pattern);
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>
T unquote(const T &str, const QString &quotes = QChar('"'))
for (const QChar quote : quotes)
{
if (str.length() < 2) return str;
for (const QChar quote : quotes)
{
if (str.startsWith(quote) && str.endsWith(quote))
return str.mid(1, (str.length() - 2));
}
return str;
if (str.startsWith(quote) && str.endsWith(quote))
return str.mid(1, (str.length() - 2));
}
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>
QString fromEnum(const T &value)
{
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int.");
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
QString fromEnum(const T &value)
{
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int.");
const auto metaEnum = QMetaEnum::fromType<T>();
return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value)));
}
const auto metaEnum = QMetaEnum::fromType<T>();
return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value)));
}
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
T toEnum(const QString &serializedValue, const T &defaultValue)
{
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int.");
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
T toEnum(const QString &serializedValue, const T &defaultValue)
{
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int.");
const auto metaEnum = QMetaEnum::fromType<T>();
bool ok = false;
const T value = static_cast<T>(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok));
return (ok ? value : defaultValue);
}
const auto metaEnum = QMetaEnum::fromType<T>();
bool ok = false;
const T value = static_cast<T>(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok));
return (ok ? value : defaultValue);
}
}

Loading…
Cancel
Save