[nostalgia] Replace C strings with StringViews
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <ox/std/string.hpp>
|
||||
|
||||
#include "editor.hpp"
|
||||
@@ -93,6 +95,11 @@ ox::Error BaseEditor::saveItem() noexcept {
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
ox::StringView BaseEditor::pathToItemName(ox::CRStringView path) noexcept {
|
||||
const auto lastSlash = std::find(path.rbegin(), path.rend(), '/').offset();
|
||||
return path.substr(lastSlash + 1);
|
||||
}
|
||||
|
||||
Editor::Editor() noexcept {
|
||||
m_undoStack.changeTriggered.connect(this, &Editor::markUnsavedChanges);
|
||||
}
|
||||
|
@@ -100,10 +100,7 @@ class NOSTALGIASTUDIO_EXPORT BaseEditor: public Widget {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static constexpr auto pathToItemName(ox::CRStringView path) noexcept {
|
||||
const auto lastSlash = std::find(path.rbegin(), path.rend(), '/').offset();
|
||||
return path.substr(lastSlash + 1);
|
||||
}
|
||||
static ox::StringView pathToItemName(ox::CRStringView path) noexcept;
|
||||
|
||||
// signals
|
||||
public:
|
||||
|
@@ -26,21 +26,21 @@ ox::FileSystem *Project::romFs() noexcept {
|
||||
return m_fs;
|
||||
}
|
||||
|
||||
ox::Error Project::mkdir(const ox::String &path) const noexcept {
|
||||
oxReturnError(m_fs->mkdir(path.c_str(), true));
|
||||
ox::Error Project::mkdir(ox::CRStringView path) const noexcept {
|
||||
oxReturnError(m_fs->mkdir(path, true));
|
||||
fileUpdated.emit(path);
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
ox::Result<ox::FileStat> Project::stat(const ox::String &path) const noexcept {
|
||||
return m_fs->stat(path.c_str());
|
||||
ox::Result<ox::FileStat> Project::stat(ox::CRStringView path) const noexcept {
|
||||
return m_fs->stat(path);
|
||||
}
|
||||
|
||||
bool Project::exists(const ox::String &path) const noexcept {
|
||||
return m_fs->stat(path.c_str()).error == 0;
|
||||
bool Project::exists(ox::CRStringView path) const noexcept {
|
||||
return m_fs->stat(path).error == 0;
|
||||
}
|
||||
|
||||
const ox::Vector<ox::String> &Project::fileList(const char *ext) noexcept {
|
||||
const ox::Vector<ox::String> &Project::fileList(ox::CRStringView ext) noexcept {
|
||||
return m_fileExtFileMap[ext];
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ void Project::buildFileIndex() noexcept {
|
||||
}
|
||||
}
|
||||
|
||||
void Project::indexFile(const ox::String &path) noexcept {
|
||||
void Project::indexFile(ox::CRStringView path) noexcept {
|
||||
const auto [ext, err] = fileExt(path);
|
||||
if (err) {
|
||||
return;
|
||||
@@ -67,9 +67,9 @@ void Project::indexFile(const ox::String &path) noexcept {
|
||||
m_fileExtFileMap[ext].emplace_back(path);
|
||||
}
|
||||
|
||||
ox::Error Project::writeBuff(const ox::String &path, const ox::Buffer &buff) noexcept {
|
||||
const auto newFile = m_fs->stat(path.c_str()).error != 0;
|
||||
oxReturnError(m_fs->write(path.c_str(), buff.data(), buff.size()));
|
||||
ox::Error Project::writeBuff(const ox::StringView &path, const ox::Buffer &buff) noexcept {
|
||||
const auto newFile = m_fs->stat(path).error != 0;
|
||||
oxReturnError(m_fs->write(path, buff.data(), buff.size(), ox::FileType::NormalFile));
|
||||
if (newFile) {
|
||||
fileAdded.emit(path);
|
||||
indexFile(path);
|
||||
@@ -80,17 +80,17 @@ ox::Error Project::writeBuff(const ox::String &path, const ox::Buffer &buff) noe
|
||||
}
|
||||
|
||||
ox::Result<ox::Buffer> Project::loadBuff(const ox::String &path) const noexcept {
|
||||
return m_fs->read(path.c_str());
|
||||
return m_fs->read(path);
|
||||
}
|
||||
|
||||
ox::Error Project::lsProcDir(ox::Vector<ox::String> *paths, const ox::String &path) const noexcept {
|
||||
oxRequire(files, m_fs->ls(path.c_str()));
|
||||
ox::Error Project::lsProcDir(ox::Vector<ox::String> *paths, ox::CRStringView path) const noexcept {
|
||||
oxRequire(files, m_fs->ls(path));
|
||||
for (const auto &name : files) {
|
||||
const auto fullPath = path + "/" + name.c_str();
|
||||
oxRequire(stat, m_fs->stat(fullPath.c_str()));
|
||||
auto fullPath = ox::sfmt("{}/{}", path, name);
|
||||
oxRequire(stat, m_fs->stat(ox::StringView(fullPath)));
|
||||
switch (stat.fileType) {
|
||||
case ox::FileType::NormalFile:
|
||||
paths->push_back(fullPath);
|
||||
paths->emplace_back(std::move(fullPath));
|
||||
break;
|
||||
case ox::FileType::Directory:
|
||||
oxReturnError(lsProcDir(paths, fullPath));
|
||||
@@ -102,7 +102,7 @@ ox::Error Project::lsProcDir(ox::Vector<ox::String> *paths, const ox::String &pa
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
ox::Result<ox::Vector<ox::String>> Project::listFiles(const ox::String &path) const noexcept {
|
||||
ox::Result<ox::Vector<ox::String>> Project::listFiles(ox::CRStringView path) const noexcept {
|
||||
ox::Vector<ox::String> paths;
|
||||
oxReturnError(lsProcDir(&paths, path));
|
||||
return paths;
|
||||
|
@@ -29,7 +29,7 @@ enum class ProjectEvent {
|
||||
};
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr ox::Result<ox::String> fileExt(const ox::String &path) noexcept {
|
||||
constexpr ox::Result<ox::StringView> fileExt(ox::CRStringView path) noexcept {
|
||||
const auto extStart = ox::find(path.crbegin(), path.crend(), '.').offset();
|
||||
if (!extStart) {
|
||||
return OxError(1, "Cannot open a file without valid extension.");
|
||||
@@ -52,7 +52,7 @@ class NOSTALGIASTUDIO_EXPORT Project {
|
||||
[[nodiscard]]
|
||||
ox::FileSystem *romFs() noexcept;
|
||||
|
||||
ox::Error mkdir(const ox::String &path) const noexcept;
|
||||
ox::Error mkdir(ox::CRStringView path) const noexcept;
|
||||
|
||||
/**
|
||||
* Writes a MetalClaw object to the project at the given path.
|
||||
@@ -63,40 +63,40 @@ class NOSTALGIASTUDIO_EXPORT Project {
|
||||
template<typename T>
|
||||
ox::Result<T> loadObj(const ox::String &path) const noexcept;
|
||||
|
||||
ox::Result<ox::FileStat> stat(const ox::String &path) const noexcept;
|
||||
ox::Result<ox::FileStat> stat(ox::CRStringView path) const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
bool exists(const ox::String& path) const noexcept;
|
||||
bool exists(ox::CRStringView path) const noexcept;
|
||||
|
||||
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;
|
||||
const ox::Vector<ox::String> &fileList(ox::CRStringView ext) noexcept;
|
||||
|
||||
private:
|
||||
void buildFileIndex() noexcept;
|
||||
|
||||
void indexFile(const ox::String &path) noexcept;
|
||||
void indexFile(ox::CRStringView path) noexcept;
|
||||
|
||||
ox::Error writeBuff(const ox::String &path, const ox::Buffer &buff) noexcept;
|
||||
ox::Error writeBuff(const ox::StringView &path, const ox::Buffer &buff) noexcept;
|
||||
|
||||
ox::Result<ox::Buffer> loadBuff(const ox::String &path) const noexcept;
|
||||
|
||||
ox::Error lsProcDir(ox::Vector<ox::String> *paths, const ox::String &path) const noexcept;
|
||||
ox::Error lsProcDir(ox::Vector<ox::String> *paths, ox::CRStringView path) const noexcept;
|
||||
|
||||
ox::Result<ox::Vector<ox::String>> listFiles(const ox::String &path = "") const noexcept;
|
||||
ox::Result<ox::Vector<ox::String>> listFiles(ox::CRStringView path = "") const noexcept;
|
||||
|
||||
// signals
|
||||
public:
|
||||
ox::Signal<ox::Error(ProjectEvent, const ox::String&)> fileEvent;
|
||||
ox::Signal<ox::Error(const ox::String&)> fileAdded;
|
||||
ox::Signal<ox::Error(ox::CRStringView)> fileAdded;
|
||||
// FileRecognized is triggered for all matching files upon a new
|
||||
// subscription to a section of the project and upon the addition of a
|
||||
// file.
|
||||
ox::Signal<ox::Error(const ox::String&)> fileRecognized;
|
||||
ox::Signal<ox::Error(const ox::String&)> fileDeleted;
|
||||
ox::Signal<ox::Error(const ox::String&)> fileUpdated;
|
||||
ox::Signal<ox::Error(ox::StringView)> fileRecognized;
|
||||
ox::Signal<ox::Error(ox::StringView)> fileDeleted;
|
||||
ox::Signal<ox::Error(ox::StringView)> fileUpdated;
|
||||
|
||||
};
|
||||
|
||||
|
@@ -13,9 +13,9 @@
|
||||
namespace nostalgia {
|
||||
|
||||
static ox::Result<ox::UniquePtr<ProjectTreeModel>>
|
||||
buildProjectTreeModel(ProjectExplorer *explorer, const ox::String &name, const ox::String &path, ProjectTreeModel *parent) noexcept {
|
||||
buildProjectTreeModel(ProjectExplorer *explorer, ox::CRStringView name, ox::CRStringView path, ProjectTreeModel *parent) noexcept {
|
||||
const auto fs = explorer->romFs();
|
||||
oxRequire(stat, fs->stat(path.c_str()));
|
||||
oxRequire(stat, fs->stat(path));
|
||||
auto out = ox::make_unique<ProjectTreeModel>(explorer, name, parent);
|
||||
if (stat.fileType == ox::FileType::Directory) {
|
||||
oxRequireM(children, fs->ls(path));
|
||||
@@ -58,7 +58,7 @@ void ProjectExplorer::setModel(ox::UniquePtr<ProjectTreeModel> model) noexcept {
|
||||
m_treeModel = std::move(model);
|
||||
}
|
||||
|
||||
ox::Error ProjectExplorer::refreshProjectTreeModel(const ox::String&) noexcept {
|
||||
ox::Error ProjectExplorer::refreshProjectTreeModel(ox::CRStringView) noexcept {
|
||||
oxRequireM(model, buildProjectTreeModel(this, "Project", "/", nullptr));
|
||||
setModel(std::move(model));
|
||||
return OxError(0);
|
||||
|
@@ -23,7 +23,7 @@ class ProjectExplorer: public studio::Widget {
|
||||
|
||||
void setModel(ox::UniquePtr<ProjectTreeModel> model) noexcept;
|
||||
|
||||
ox::Error refreshProjectTreeModel(const ox::String& = {}) noexcept;
|
||||
ox::Error refreshProjectTreeModel(ox::CRStringView = {}) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr ox::FileSystem *romFs() noexcept {
|
||||
|
@@ -9,10 +9,10 @@
|
||||
|
||||
namespace nostalgia {
|
||||
|
||||
ProjectTreeModel::ProjectTreeModel(ProjectExplorer *explorer, const ox::String &name,
|
||||
ProjectTreeModel::ProjectTreeModel(ProjectExplorer *explorer, ox::String name,
|
||||
ProjectTreeModel *parent) noexcept {
|
||||
m_explorer = explorer;
|
||||
m_name = name;
|
||||
m_name = std::move(name);
|
||||
m_parent = parent;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ ProjectTreeModel::ProjectTreeModel(ProjectTreeModel &&other) noexcept {
|
||||
|
||||
void ProjectTreeModel::draw(core::Context *ctx) noexcept {
|
||||
constexpr auto dirFlags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
|
||||
if (m_children.size()) {
|
||||
if (!m_children.empty()) {
|
||||
if (ImGui::TreeNodeEx(m_name.c_str(), dirFlags)) {
|
||||
for (auto &child : m_children) {
|
||||
child->draw(ctx);
|
||||
|
@@ -18,7 +18,7 @@ class ProjectTreeModel {
|
||||
ox::String m_name;
|
||||
ox::Vector<ox::UniquePtr<ProjectTreeModel>> m_children;
|
||||
public:
|
||||
explicit ProjectTreeModel(class ProjectExplorer *explorer, const ox::String &name,
|
||||
explicit ProjectTreeModel(class ProjectExplorer *explorer, ox::String name,
|
||||
ProjectTreeModel *parent = nullptr) noexcept;
|
||||
|
||||
ProjectTreeModel(ProjectTreeModel &&other) noexcept;
|
||||
|
Reference in New Issue
Block a user