From ef3a28846c76184307f57973f0e325574b3160cd Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 24 Mar 2022 20:53:29 -0500 Subject: [PATCH] [nostalgia/studio] Add ability to index and list files of different types in Project --- src/nostalgia/studio/lib/project.cpp | 23 +++++++++++++++++++++++ src/nostalgia/studio/lib/project.hpp | 28 +++++++++++++++++++++------- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/nostalgia/studio/lib/project.cpp b/src/nostalgia/studio/lib/project.cpp index 222e1c32..bc334939 100644 --- a/src/nostalgia/studio/lib/project.cpp +++ b/src/nostalgia/studio/lib/project.cpp @@ -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 &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); diff --git a/src/nostalgia/studio/lib/project.hpp b/src/nostalgia/studio/lib/project.hpp index cb94df60..faf3b90f 100644 --- a/src/nostalgia/studio/lib/project.hpp +++ b/src/nostalgia/studio/lib/project.hpp @@ -6,14 +6,13 @@ #include -#include #include #include +#include #include #include #include -#include -#include +#include #include "nostalgiastudio_export.h" @@ -29,6 +28,15 @@ enum class ProjectEvent { FileUpdated, }; +[[nodiscard]] +constexpr ox::Result 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> m_fileExtFileMap; public: explicit Project(ox::FileSystem *fs, const ox::String &path) noexcept; @@ -63,7 +72,12 @@ class NOSTALGIASTUDIO_EXPORT Project { template ox::Error subscribe(ProjectEvent e, ox::SignalHandler *tgt, Functor &&slot) const noexcept; + [[nodiscard]] + const ox::Vector &fileList(const char *ng) noexcept; + private: + void buildFileIndex() noexcept; + ox::Error writeBuff(const ox::String &path, const ox::Buffer &buff) const noexcept; ox::Result 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);