[nostalgia/studio] Cleanup

This commit is contained in:
Gary Talent 2021-07-17 12:20:57 -05:00
parent 2be31cca5c
commit 2f9accf5ba
3 changed files with 26 additions and 29 deletions

View File

@ -13,20 +13,18 @@
namespace nostalgia::studio { namespace nostalgia::studio {
QString filePathToName(QString path, QString prefix, QString suffix) { QString filePathToName(const QString &path, const QString &prefix, const QString &suffix) {
const auto begin = prefix.size(); const auto begin = prefix.size();
const auto end = path.size() - (suffix.size() + prefix.size()); const auto end = path.size() - (suffix.size() + prefix.size());
return path.mid(begin, end); return path.mid(begin, end);
} }
Project::Project(QString path): m_fs(std::make_unique<ox::PassThroughFS>(path.toUtf8())) { Project::Project(const QString &path): m_fs(std::make_unique<ox::PassThroughFS>(path.toUtf8())) {
qDebug() << "Project:" << path; qDebug() << "Project:" << path;
m_path = path; m_path = path;
} }
Project::~Project() = default;
void Project::create() { void Project::create() {
QDir().mkpath(m_path); QDir().mkpath(m_path);
} }
@ -35,32 +33,32 @@ ox::FileSystem *Project::romFs() {
return m_fs.get(); return m_fs.get();
} }
void Project::mkdir(QString path) const { void Project::mkdir(const 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(const 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(const 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(const 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);
} }
ox::Buffer Project::loadBuff(QString path) const { ox::Buffer Project::loadBuff(const QString &path) const {
const auto csPath = path.toUtf8(); const auto csPath = path.toUtf8();
oxRequireMT(buff, m_fs->read(csPath.data())); oxRequireMT(buff, m_fs->read(csPath.data()));
return std::move(buff); return std::move(buff);
} }
void Project::lsProcDir(QStringList *paths, QString path) const { void Project::lsProcDir(QStringList *paths, const QString &path) const {
oxRequireT(files, m_fs->ls(path.toUtf8())); oxRequireT(files, m_fs->ls(path.toUtf8()));
for (const auto &name : files) { for (const auto &name : files) {
const auto fullPath = path + "/" + name.c_str(); const auto fullPath = path + "/" + name.c_str();
@ -78,10 +76,10 @@ void Project::lsProcDir(QStringList *paths, QString path) const {
} }
} }
QStringList Project::listFiles(QString path) const { QStringList Project::listFiles(const QString &path) const {
QStringList paths; QStringList paths;
lsProcDir(&paths, path); lsProcDir(&paths, path);
return paths; return ox::move(paths);
} }
} }

View File

@ -32,7 +32,8 @@ enum class ProjectEvent {
FileUpdated, FileUpdated,
}; };
[[nodiscard]] QString filePathToName(QString path, QString prefix, QString suffix); [[nodiscard]]
QString filePathToName(const QString &path, const QString &prefix, const QString &suffix);
class NOSTALGIASTUDIO_EXPORT Project: public QObject { class NOSTALGIASTUDIO_EXPORT Project: public QObject {
Q_OBJECT Q_OBJECT
@ -42,42 +43,40 @@ class NOSTALGIASTUDIO_EXPORT Project: public QObject {
mutable std::unique_ptr<ox::FileSystem> m_fs; mutable std::unique_ptr<ox::FileSystem> m_fs;
public: public:
explicit Project(QString path); explicit Project(const QString &path);
~Project() override; ~Project() override = default;
void create(); void create();
ox::FileSystem *romFs(); ox::FileSystem *romFs();
void mkdir(QString path) const; void mkdir(const QString &path) const;
/** /**
* Writes a MetalClaw object to the project at the given path. * Writes a MetalClaw object to the project at the given path.
*/ */
template<typename T> template<typename T>
void writeObj(QString path, T *obj) const; void writeObj(const QString &path, T *obj) const;
template<typename T> template<typename T>
std::unique_ptr<T> loadObj(QString path) const; std::unique_ptr<T> loadObj(const QString &path) const;
ox::FileStat stat(QString path) const; ox::FileStat stat(const QString &path) const;
bool exists(QString path) const; bool exists(const QString& path) const;
void subscribe(ProjectEvent e, QObject *tgt, const char *slot) const;
template<typename Functor> template<typename Functor>
void subscribe(ProjectEvent e, QObject *tgt, Functor &&slot) const; void subscribe(ProjectEvent e, QObject *tgt, Functor &&slot) const;
private: private:
void writeBuff(QString path, uint8_t *buff, size_t buffLen) const; void writeBuff(const QString &path, uint8_t *buff, size_t buffLen) const;
ox::Buffer loadBuff(QString path) const; ox::Buffer loadBuff(const QString &path) const;
void lsProcDir(QStringList *paths, QString path) const; void lsProcDir(QStringList *paths, const QString &path) const;
QStringList listFiles(QString path = "") const; QStringList listFiles(const QString &path = "") const;
signals: signals:
void fileEvent(ProjectEvent, QString path) const; void fileEvent(ProjectEvent, QString path) const;
@ -92,7 +91,7 @@ class NOSTALGIASTUDIO_EXPORT Project: public QObject {
}; };
template<typename T> template<typename T>
void Project::writeObj(QString path, T *obj) const { void Project::writeObj(const QString &path, T *obj) const {
// write MetalClaw // write MetalClaw
oxRequireMT(buff, ox::writeClaw(obj, ox::ClawFormat::Metal)); oxRequireMT(buff, ox::writeClaw(obj, ox::ClawFormat::Metal));
// write to FS // write to FS
@ -112,7 +111,7 @@ void Project::writeObj(QString path, T *obj) const {
} }
template<typename T> template<typename T>
std::unique_ptr<T> Project::loadObj(QString path) const { std::unique_ptr<T> Project::loadObj(const 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()));