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

View File

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

View File

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

View File

@ -16,40 +16,33 @@
namespace nostalgia::studio { namespace nostalgia::studio {
using namespace ox; OxFSFile::OxFSFile(ox::FileSystem *fs, QString path, OxFSFile *parentItem) {
OxFSFile::OxFSFile(PassThroughFS *fs, QString path, OxFSFile *parentItem) {
m_path = path; m_path = path;
m_parentItem = parentItem; m_parentItem = parentItem;
// find children // find children
if (fs) { oxRequireT(stat, fs->stat(static_cast<const char*>(m_path.toUtf8())));
QVector<QString> ls; QVector<QString> ls;
auto stat = fs->stat(static_cast<const char*>(m_path.toUtf8())); if (stat.fileType == ox::FileType_Directory) {
if (!stat.error) { oxRequireT(names, fs->ls(m_path.toUtf8()));
if (stat.value.fileType == FileType_Directory) { for (const auto &name : names) {
oxThrowError(fs->ls(m_path.toUtf8(), [&ls](const char *name, ox::InodeId_t) { if (name[0] != '.') {
if (name[0] != '.') { ls.push_back(name.c_str());
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);
}
} }
} }
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::OxFSModel(PassThroughFS *fs, QObject*) { OxFSModel::OxFSModel(ox::FileSystem *fs, QObject*) {
m_rootItem = new OxFSFile(fs, ""); m_rootItem = new OxFSFile(fs, "");
} }

View File

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