[nostalgia/studio] Add ability to index and list files of different types in Project

This commit is contained in:
Gary Talent 2022-03-24 20:53:29 -05:00
parent 27b7b11146
commit ef3a28846c
2 changed files with 44 additions and 7 deletions

View File

@ -20,6 +20,7 @@ ox::String filePathToName(const ox::String &path, const ox::String &prefix, cons
Project::Project(ox::FileSystem *fs, const ox::String &path) noexcept: m_fs(fs) {
oxTracef("nostalgia::studio", "Project: {}", path);
m_path = path;
buildFileIndex();
}
ox::Error Project::create() noexcept {
@ -46,6 +47,28 @@ bool Project::exists(const ox::String &path) const noexcept {
return m_fs->stat(path.c_str()).error == 0;
}
const ox::Vector<ox::String> &Project::fileList(const char *ng) noexcept {
return m_fileExtFileMap[ng];
}
void Project::buildFileIndex() noexcept {
auto [files, err] = listFiles();
if (err) {
oxLogError(err);
return;
}
std::sort(files.begin(), files.end());
for (const auto &file : files) {
if (!file.beginsWith("/.nostalgia/")) {
const auto [ext, err] = fileExt(file);
if (err) {
continue;
}
m_fileExtFileMap[ext].emplace_back(file);
}
}
}
ox::Error Project::writeBuff(const ox::String &path, const ox::Buffer &buff) const noexcept {
oxReturnError(m_fs->write(path.c_str(), buff.data(), buff.size()));
fileUpdated.emit(path);

View File

@ -6,14 +6,13 @@
#include <memory>
#include <ox/claw/claw.hpp>
#include <ox/claw/read.hpp>
#include <ox/claw/write.hpp>
#include <ox/event/signal.hpp>
#include <ox/fs/fs.hpp>
#include <ox/mc/mc.hpp>
#include <ox/model/descwrite.hpp>
#include <ox/event/signal.hpp>
#include <ox/std/std.hpp>
#include <ox/std/hashmap.hpp>
#include "nostalgiastudio_export.h"
@ -29,6 +28,15 @@ enum class ProjectEvent {
FileUpdated,
};
[[nodiscard]]
constexpr ox::Result<ox::String> fileExt(auto path) noexcept {
const auto extStart = std::find(path.crbegin(), path.crend(), '.').offset();
if (!extStart) {
return OxError(1, "Cannot open a file without valid extension.");
}
return path.substr(extStart + 1);
}
[[nodiscard]]
ox::String filePathToName(const ox::String &path, const ox::String &prefix, const ox::String &suffix) noexcept;
@ -36,6 +44,7 @@ class NOSTALGIASTUDIO_EXPORT Project {
private:
ox::String m_path = "";
mutable ox::FileSystem *m_fs = nullptr;
ox::HashMap<ox::String, ox::Vector<ox::String>> m_fileExtFileMap;
public:
explicit Project(ox::FileSystem *fs, const ox::String &path) noexcept;
@ -63,7 +72,12 @@ class NOSTALGIASTUDIO_EXPORT Project {
template<typename Functor>
ox::Error subscribe(ProjectEvent e, ox::SignalHandler *tgt, Functor &&slot) const noexcept;
[[nodiscard]]
const ox::Vector<ox::String> &fileList(const char *ng) noexcept;
private:
void buildFileIndex() noexcept;
ox::Error writeBuff(const ox::String &path, const ox::Buffer &buff) const noexcept;
ox::Result<ox::Buffer> loadBuff(const ox::String &path) const noexcept;
@ -96,8 +110,8 @@ ox::Error Project::writeObj(const ox::String &path, auto *obj) const noexcept {
// replace garbage last character with new line
typeOut.back().value = '\n';
// write to FS
ox::String descPath = "/.nostalgia/type_descriptors/";
const auto typePath = descPath + type->typeName;
static constexpr auto descPath = "/.nostalgia/type_descriptors/";
const auto typePath = ox::sfmt("{}{}", descPath, type->typeName);
oxReturnError(mkdir(descPath));
oxReturnError(writeBuff(typePath, typeOut));
fileUpdated.emit(path);
@ -121,8 +135,8 @@ ox::Error Project::subscribe(ProjectEvent e, ox::SignalHandler *tgt, Functor &&s
break;
case ProjectEvent::FileRecognized:
{
oxRequire(fileList, listFiles());
for (auto f : fileList) {
oxRequire(files, listFiles());
for (auto f : files) {
slot(f);
}
connect(this, &Project::fileRecognized, tgt, slot);