101 lines
2.8 KiB
C++
101 lines
2.8 KiB
C++
#include "PathJsObject.hpp"
|
|
|
|
#include <QDir>
|
|
#include <QList>
|
|
|
|
#include "FileJsObject.hpp"
|
|
#include "exception/FileNotFoundException.hpp"
|
|
|
|
|
|
PathJsObject::PathJsObject(AtlasJsEngine* engine, const std::filesystem::path& virtualPath) {
|
|
this->engine = engine;
|
|
this->virtualPath = virtualPath;
|
|
}
|
|
|
|
QJSValue PathJsObject::getParent() const {
|
|
// return the parent directory
|
|
return this->engine->newQObject(new PathJsObject(
|
|
this->engine,
|
|
this->virtualPath.parent_path()
|
|
));
|
|
}
|
|
|
|
QList<QJSValue> PathJsObject::getChildrens() const {
|
|
// check if the path is iterable
|
|
if (!this->isDirectory()) {
|
|
this->engine->throwError(QJSValue::ErrorType::TypeError, "Only directories can have children.");
|
|
return {};
|
|
}
|
|
|
|
// get the corresponding real directory path
|
|
const std::filesystem::path realPath = this->getRealPath();
|
|
|
|
// prepare the list of children paths
|
|
auto childsPath = QList<QJSValue>();
|
|
|
|
// iterate through the childrens of the directory
|
|
for (const auto& childRealEntry : std::filesystem::directory_iterator(realPath)) {
|
|
// get the corresponding path object
|
|
const auto& childRealPath = childRealEntry.path();
|
|
|
|
// get the virtual path
|
|
std::filesystem::path childVirtualPath = this->virtualPath / childRealPath.filename();
|
|
// add it to the list of childrens
|
|
const auto pathJs = this->engine->newQObject(new PathJsObject(
|
|
this->engine,
|
|
childVirtualPath
|
|
));
|
|
childsPath.append(pathJs);
|
|
}
|
|
|
|
return childsPath;
|
|
}
|
|
|
|
bool PathJsObject::isFile() const {
|
|
return is_regular_file(this->getRealPath());
|
|
}
|
|
|
|
bool PathJsObject::isDirectory() const {
|
|
return is_directory(this->getRealPath());
|
|
}
|
|
|
|
Q_INVOKABLE QJSValue PathJsObject::open(const QString& mode, const QString& encoding) {
|
|
// create a new file object
|
|
auto* file = new FileJsObject(
|
|
this->engine,
|
|
this->virtualPath
|
|
);
|
|
// open it with the given mode
|
|
file->open(mode);
|
|
|
|
// convert it into a QJSValue
|
|
auto fileJs = this->engine->newQObject(file);
|
|
|
|
return fileJs;
|
|
|
|
// TODO(Faraphel): handle encoding
|
|
}
|
|
|
|
QString PathJsObject::getPath() const {
|
|
return QString::fromStdString(this->virtualPath.string());
|
|
}
|
|
|
|
QString PathJsObject::getDirname() const {
|
|
return QString::fromStdString(this->virtualPath.parent_path().string());
|
|
}
|
|
|
|
QString PathJsObject::getFilename() const {
|
|
return QString::fromStdString(this->virtualPath.filename());
|
|
}
|
|
|
|
QString PathJsObject::getBasename() const {
|
|
return QString::fromStdString(this->virtualPath.stem());
|
|
}
|
|
|
|
QString PathJsObject::getSuffix() const {
|
|
return QString::fromStdString(this->virtualPath.extension());
|
|
}
|
|
|
|
std::filesystem::path PathJsObject::getRealPath() const {
|
|
return this->engine->getFileSystem().resolve(this->virtualPath);
|
|
}
|