From 76e346c8e5ac7067cc49063e0c11d88c23871115 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 2 Oct 2018 13:24:45 +0200 Subject: Add Util namespace - Util::files lists files in specified .path --- src/util.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/util.cpp (limited to 'src/util.cpp') diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..5b1478b --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,45 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "util.h" +#include +#include + +#define ListSeparator QLatin1Literal(";") + +QStringList Util::files(const QString &location, const QStringList &nameFilters) +{ + if(location.isEmpty()) + return QStringList(); + + QStringList filelist; + + // check if location is a list of locations (contains a ';') + if(location.contains(ListSeparator)) { + const QStringList locations = location.split(ListSeparator); + + for(const QString &l : locations) { + filelist.append(Util::files(l, nameFilters)); + } + + return filelist; + } + + const QFileInfo info(location); + + // check if location is a folder + if(info.isDir()) { + for(const QFileInfo &entryInfo : QDir(info.absoluteFilePath()).entryInfoList(nameFilters, QDir::Files | QDir::Readable, QDir::Time)) { + filelist.append(entryInfo.absoluteFilePath()); + } + } else if(info.isFile()) { + filelist.append(info.absoluteFilePath()); + } + + return filelist; +} \ No newline at end of file -- cgit v1.2.1