[nostalgia/studio] Cleanup

This commit is contained in:
Gary Talent 2021-05-08 22:33:03 -05:00
parent 15d8e9de5b
commit b3fb724ae7
5 changed files with 54 additions and 65 deletions

View File

@ -20,7 +20,7 @@ QString filePathToName(QString path, QString prefix, QString suffix) {
}
Project::Project(QString path): m_fs(path.toUtf8()) {
Project::Project(QString path): m_fs(std::make_unique<ox::PassThroughFS>(path.toUtf8())) {
qDebug() << "Project:" << path;
m_path = path;
}
@ -31,56 +31,54 @@ void Project::create() {
QDir().mkpath(m_path);
}
ox::PassThroughFS *Project::romFs() {
return &m_fs;
ox::FileSystem *Project::romFs() {
return m_fs.get();
}
void Project::mkdir(QString path) const {
oxThrowError(m_fs.mkdir(path.toUtf8().data(), true));
oxThrowError(m_fs->mkdir(path.toUtf8().data(), true));
emit fileUpdated(path);
}
ox::FileStat Project::stat(QString path) const {
oxRequireT(s, m_fs.stat(path.toUtf8().data()));
oxRequireT(s, m_fs->stat(path.toUtf8().data()));
return s;
}
bool Project::exists(QString path) const {
return m_fs.stat(path.toUtf8().data()).error == 0;
return m_fs->stat(path.toUtf8().data()).error == 0;
}
void Project::writeBuff(QString path, uint8_t *buff, size_t buffLen) const {
oxThrowError(m_fs.write(path.toUtf8().data(), buff, buffLen));
oxThrowError(m_fs->write(path.toUtf8().data(), buff, buffLen));
emit fileUpdated(path);
}
std::vector<char> Project::loadBuff(QString path) const {
std::vector<char> buff(stat(path).size);
ox::Buffer Project::loadBuff(QString path) const {
const auto csPath = path.toUtf8();
oxThrowError(m_fs.read(csPath.data(), buff.data(), buff.size()));
return buff;
oxRequireMT(buff, m_fs->read(csPath.data()));
return std::move(buff);
}
void Project::procDir(QStringList &paths, QString path) const {
oxThrowError(m_fs.ls(path.toUtf8(), [&](QString name, ox::InodeId_t) {
auto fullPath = path + "/" + name;
auto [stat, err] = m_fs.stat(fullPath.toUtf8().data());
oxReturnError(err);
void Project::lsProcDir(QStringList *paths, QString path) const {
oxRequireT(files, m_fs->ls(path.toUtf8()));
for (const auto &name : files) {
const auto fullPath = path + "/" + name.c_str();
oxRequireT(stat, m_fs->stat(fullPath.toUtf8().data()));
switch (stat.fileType) {
case ox::FileType_NormalFile:
paths.push_back(fullPath);
paths->push_back(fullPath);
break;
case ox::FileType_Directory:
procDir(paths, fullPath);
lsProcDir(paths, fullPath);
break;
}
return OxError(0);
}));
}
}
QStringList Project::listFiles(QString path) const {
QStringList paths;
procDir(paths, path);
lsProcDir(&paths, path);
return paths;
}

View File

@ -11,14 +11,13 @@
#include <memory>
#include <QSharedPointer>
#include <ox/fs/fs.hpp>
#include <ox/mc/mc.hpp>
#include <qnamespace.h>
#include <ox/claw/claw.hpp>
#include <ox/fs/filesystem/passthroughfs.hpp>
#include <ox/fs/fs.hpp>
#include <ox/mc/mc.hpp>
#include <ox/model/descwrite.hpp>
#include <ox/std/std.hpp>
#include "nostalgiastudio_export.h"
@ -40,7 +39,7 @@ class NOSTALGIASTUDIO_EXPORT Project: public QObject {
private:
QString m_path = "";
mutable ox::PassThroughFS m_fs;
mutable std::unique_ptr<ox::FileSystem> m_fs;
public:
explicit Project(QString path);
@ -49,7 +48,7 @@ class NOSTALGIASTUDIO_EXPORT Project: public QObject {
void create();
ox::PassThroughFS *romFs();
ox::FileSystem *romFs();
void mkdir(QString path) const;
@ -74,9 +73,9 @@ class NOSTALGIASTUDIO_EXPORT Project: public QObject {
private:
void writeBuff(QString path, uint8_t *buff, size_t buffLen) const;
std::vector<char> loadBuff(QString path) const;
ox::Buffer loadBuff(QString path) const;
void procDir(QStringList &paths, QString path) const;
void lsProcDir(QStringList *paths, QString path) const;
QStringList listFiles(QString path = "") const;
@ -117,7 +116,7 @@ std::unique_ptr<T> Project::loadObj(QString path) const {
auto obj = std::make_unique<T>();
auto buff = loadBuff(path);
oxThrowError(ox::readClaw<T>(buff.data(), buff.size(), obj.get()));
return obj;
return std::move(obj);
}
template<typename Functor>

View File

@ -360,11 +360,10 @@ void MainWindow::openProject(QString projectPath) {
openFile(t, true);
} catch (const ox::Error &err) {
qInfo().nospace() << "Error opening tab: " << t << ", " << static_cast<int>(err) << ", " << err.file << ":" << err.line;
oxTrace("nostalgia::studio::MainWindow::openProject") << "Error opening tab:" << static_cast<int>(err)
<< ", " << static_cast<int>(err) << ", " << err.file << ":" << err.line;
oxTracef("nostalgia::studio::MainWindow::openProject", "Error opening tab: {}, {}, {}:{}", static_cast<int>(err), static_cast<int>(err), err.file, err.line);
} catch (...) {
qInfo() << "Error opening tab: " << t;
oxTrace("nostalgia::studio::MainWindow::openProject") << "Error opening tab";
oxTracef("nostalgia::studio::MainWindow::openProject", "Error opening tab: {}", t);
}
}
qInfo() << "Open project:" << projectPath;

View File

@ -16,40 +16,33 @@
namespace nostalgia::studio {
using namespace ox;
OxFSFile::OxFSFile(PassThroughFS *fs, QString path, OxFSFile *parentItem) {
OxFSFile::OxFSFile(ox::FileSystem *fs, QString path, OxFSFile *parentItem) {
m_path = path;
m_parentItem = parentItem;
// find children
if (fs) {
QVector<QString> ls;
auto stat = fs->stat(static_cast<const char*>(m_path.toUtf8()));
if (!stat.error) {
if (stat.value.fileType == FileType_Directory) {
oxThrowError(fs->ls(m_path.toUtf8(), [&ls](const char *name, ox::InodeId_t) {
if (name[0] != '.') {
ls.push_back(name);
}
return OxError(0);
}));
std::sort(ls.begin(), ls.end());
}
auto p = m_path;
// make sure ends with path separator
if (fs->stat(p.toUtf8().data()).value.fileType == FileType_Directory &&
p.size() && p.back() != QDir::separator()) {
p += QDir::separator();
}
for (auto name : ls) {
if (name != "." && name != "..") {
const auto path = m_path.size() ? m_path + '/' + name : name;
auto ch = new OxFSFile(fs, path, this);
m_childItems.push_back(ch);
}
oxRequireT(stat, fs->stat(static_cast<const char*>(m_path.toUtf8())));
QVector<QString> ls;
if (stat.fileType == ox::FileType_Directory) {
oxRequireT(names, fs->ls(m_path.toUtf8()));
for (const auto &name : names) {
if (name[0] != '.') {
ls.push_back(name.c_str());
}
}
std::sort(ls.begin(), ls.end());
}
auto p = m_path;
// make sure ends with path separator
if (fs->stat(p.toUtf8().data()).value.fileType == ox::FileType_Directory &&
p.size() && p.back() != QDir::separator()) {
p += QDir::separator();
}
for (const auto &name : ls) {
if (name != "." && name != "..") {
const auto path = m_path.size() ? m_path + '/' + name : name;
const auto ch = new OxFSFile(fs, path, this);
m_childItems.push_back(ch);
}
}
}
@ -123,7 +116,7 @@ QString OxFSFile::path() const {
// OxFSModel
OxFSModel::OxFSModel(PassThroughFS *fs, QObject*) {
OxFSModel::OxFSModel(ox::FileSystem *fs, QObject*) {
m_rootItem = new OxFSFile(fs, "");
}

View File

@ -23,7 +23,7 @@ class OxFSFile {
QVector<OxFSFile*> m_childItems;
public:
OxFSFile(ox::PassThroughFS *fs, QString path, OxFSFile *parentItem = nullptr);
OxFSFile(ox::FileSystem *fs, QString path, OxFSFile *parentItem = nullptr);
~OxFSFile();
@ -55,7 +55,7 @@ class OxFSModel: public QAbstractItemModel {
OxFSFile *m_rootItem = nullptr;
public:
explicit OxFSModel(ox::PassThroughFS *fs, QObject *parent = nullptr);
explicit OxFSModel(ox::FileSystem *fs, QObject *parent = nullptr);
~OxFSModel() override;