Something went wrong. Try again.
The agentic engineering control plane for the posthuman future
Something went wrong. Try again.
2.8 kB · 99 lines
C++
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100#include "PathFuzzy.h"
#include <QDir>#include <QFile>#include <QFileInfo>
PathFuzzy::PathFuzzy(QObject* parent) : QObject(parent) {}
QString PathFuzzy::normalize(const QString& input) { QString value = input; // Expand a leading `~` to the home directory. if (value.startsWith(QLatin1Char('~'))) { value.replace(0, 1, QDir::homePath()); } // Resolve relative paths against the current working directory. if (!QFileInfo(value).isAbsolute()) { value = QDir::currentPath() + QLatin1Char('/') + value; } return QDir::cleanPath(value);}
bool PathFuzzy::namesDirectory(const QString& input){ if (input.isEmpty()) { return false; } // A trailing slash asks for the directory outright, whether or not it exists: // "the contents of this path" is what the text says. if (input.endsWith(QLatin1Char('/'))) { return true; } // Without a slash the path must exist as a directory, and it must contain a // slash somewhere. A bare name stays a filter: normalize() would resolve it // against the process working directory, so a name that happens to exist // there would jump the listing somewhere the user never asked for. if (!input.contains(QLatin1Char('/'))) { return false; } return QFileInfo(normalize(input)).isDir();}
QString PathFuzzy::queryBase(const QString& input){ if (input.isEmpty()) { return QDir::homePath(); } if (namesDirectory(input)) { return normalize(input); } // Otherwise the listing comes from the input's parent directory. normalize // expands `~` and resolves a relative path against the working directory. return QFileInfo(normalize(input)).absolutePath();}
QString PathFuzzy::queryPartial(const QString& input){ if (namesDirectory(input)) { return {}; } const qsizetype slash = input.lastIndexOf(QLatin1Char('/')); return slash < 0 ? input : input.mid(slash + 1);}
bool PathFuzzy::isDir(const QString& path) { return QFileInfo(path).isDir();}
bool PathFuzzy::exists(const QString& path) { return !path.isEmpty() && QFileInfo::exists(normalize(path));}
bool PathFuzzy::createEmpty(const QString& path) { const QString target = normalize(path); if (target.isEmpty()) { return false; } const QFileInfo info(target); if (info.exists()) { return info.isFile(); } if (!info.dir().exists()) { return false; } QFile file(target); // QFile::open with WriteOnly creates the file. Closing immediately leaves it // empty, which is what "create a file" means here. return file.open(QIODevice::WriteOnly);}
QString PathFuzzy::homeDir() { return QDir::homePath();}
QString PathFuzzy::canonical(const QString& path) { if (path.isEmpty()) { return {}; } return QFileInfo(path).canonicalFilePath();}