From a70ece7b9ca4aa9f643a14a49601d4bcf34f324e Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 16 Jan 2022 01:51:31 +0100 Subject: [PATCH] sys: Bunch of cleanup, use fs::path instead of std::string for paths --- main/include/helpers/plugin_manager.hpp | 2 +- main/source/helpers/plugin_manager.cpp | 4 +- main/source/window/window.cpp | 7 +- .../content/providers/disk_provider.hpp | 8 +- .../content/providers/file_provider.hpp | 6 +- .../content/providers/gdb_provider.hpp | 2 +- .../include/content/views/view_hexeditor.hpp | 6 +- .../content/views/view_pattern_editor.hpp | 2 +- .../content/providers/disk_provider.cpp | 20 +-- .../content/providers/file_provider.cpp | 142 +++++++----------- .../source/content/providers/gdb_provider.cpp | 2 +- .../builtin/source/content/tools_entries.cpp | 20 +-- .../content/views/view_data_processor.cpp | 2 +- .../source/content/views/view_hexeditor.cpp | 141 +++++++---------- .../content/views/view_pattern_editor.cpp | 32 ++-- plugins/libimhex/include/hex/api/event.hpp | 5 +- .../include/hex/helpers/encoding_file.hpp | 18 +-- plugins/libimhex/include/hex/helpers/file.hpp | 8 +- .../hex/helpers/loader_script_handler.hpp | 8 +- .../hex/helpers/project_file_handler.hpp | 16 +- .../include/hex/helpers/shared_data.hpp | 2 +- .../libimhex/include/hex/helpers/utils.hpp | 3 +- .../hex/pattern_language/pattern_language.hpp | 2 +- .../include/hex/providers/provider.hpp | 2 +- .../libimhex/source/helpers/encoding_file.cpp | 4 +- plugins/libimhex/source/helpers/file.cpp | 10 +- .../source/helpers/loader_script_handler.cpp | 6 +- .../source/helpers/project_file_handler.cpp | 10 +- .../libimhex/source/helpers/shared_data.cpp | 2 +- plugins/libimhex/source/helpers/utils.cpp | 2 +- .../pattern_language/pattern_language.cpp | 2 +- .../libimhex/source/providers/provider.cpp | 2 +- 32 files changed, 206 insertions(+), 292 deletions(-) diff --git a/main/include/helpers/plugin_manager.hpp b/main/include/helpers/plugin_manager.hpp index c69d78f4e..91e46416b 100644 --- a/main/include/helpers/plugin_manager.hpp +++ b/main/include/helpers/plugin_manager.hpp @@ -16,7 +16,7 @@ namespace hex { class Plugin { public: - Plugin(const std::string &path); + Plugin(const fs::path &path); Plugin(const Plugin&) = delete; Plugin(Plugin &&other) noexcept; ~Plugin(); diff --git a/main/source/helpers/plugin_manager.cpp b/main/source/helpers/plugin_manager.cpp index 201588335..aabb9e040 100644 --- a/main/source/helpers/plugin_manager.cpp +++ b/main/source/helpers/plugin_manager.cpp @@ -14,8 +14,8 @@ namespace hex { constexpr auto GetPluginDescriptionSymbol = "_ZN3hex6plugin{0}{1}8internal20getPluginDescriptionEv"; constexpr auto SetImGuiContextSymbol = "_ZN3hex6plugin{0}{1}8internal15setImGuiContextEP12ImGuiContext"; - Plugin::Plugin(const std::string &path) { - this->m_handle = dlopen(path.data(), RTLD_LAZY); + Plugin::Plugin(const fs::path &path) { + this->m_handle = dlopen(path.string().c_str(), RTLD_LAZY); if (this->m_handle == nullptr) { log::error("dlopen failed: {}", dlerror()); diff --git a/main/source/window/window.cpp b/main/source/window/window.cpp index 872efbd2a..63b732be7 100644 --- a/main/source/window/window.cpp +++ b/main/source/window/window.cpp @@ -174,11 +174,11 @@ namespace hex { } }); - EventManager::subscribe(this, [](const std::string &path){ + EventManager::subscribe(this, [](const auto &path){ SharedData::recentFilePaths.push_front(path); { - std::list uniques; + std::list uniques; for (auto &file : SharedData::recentFilePaths) { bool exists = false; @@ -198,7 +198,8 @@ namespace hex { { std::vector recentFilesVector; - std::copy(SharedData::recentFilePaths.begin(), SharedData::recentFilePaths.end(), std::back_inserter(recentFilesVector)); + for (const auto &recentPath : SharedData::recentFilePaths) + recentFilesVector.push_back(recentPath.string()); ContentRegistry::Settings::write("hex.builtin.setting.imhex", "hex.builtin.setting.imhex.recent_files", recentFilesVector); } diff --git a/plugins/builtin/include/content/providers/disk_provider.hpp b/plugins/builtin/include/content/providers/disk_provider.hpp index 5551b1f94..51537bb45 100644 --- a/plugins/builtin/include/content/providers/disk_provider.hpp +++ b/plugins/builtin/include/content/providers/disk_provider.hpp @@ -27,13 +27,13 @@ namespace hex::plugin::builtin::prv { void writeRaw(u64 offset, const void *buffer, size_t size) override; [[nodiscard]] size_t getActualSize() const override; - void setPath(const std::string &path); + void setPath(const fs::path &path); [[nodiscard]] bool open() override; void close() override; - [[nodiscard]] virtual std::string getName() const; - [[nodiscard]] virtual std::vector> getDataInformation() const; + [[nodiscard]] std::string getName() const override; + [[nodiscard]] std::vector> getDataInformation() const override; [[nodiscard]] bool hasLoadInterface() const override { return true; } void drawLoadInterface() override; @@ -42,7 +42,7 @@ namespace hex::plugin::builtin::prv { void reloadDrives(); std::set m_availableDrives; - std::string m_path; + fs::path m_path; #if defined(OS_WINDOWS) HANDLE m_diskHandle = INVALID_HANDLE_VALUE; diff --git a/plugins/builtin/include/content/providers/file_provider.hpp b/plugins/builtin/include/content/providers/file_provider.hpp index e38f6bb54..0c870b3ea 100644 --- a/plugins/builtin/include/content/providers/file_provider.hpp +++ b/plugins/builtin/include/content/providers/file_provider.hpp @@ -36,12 +36,12 @@ namespace hex::plugin::builtin::prv { [[nodiscard]] size_t getActualSize() const override; void save() override; - void saveAs(const std::string &path) override; + void saveAs(const fs::path &path) override; [[nodiscard]] std::string getName() const override; [[nodiscard]] std::vector> getDataInformation() const override; - void setPath(const std::string &path); + void setPath(const fs::path &path); [[nodiscard]] bool open() override; void close() override; @@ -54,7 +54,7 @@ namespace hex::plugin::builtin::prv { int m_file = -1; #endif - std::string m_path; + fs::path m_path; void *m_mappedFile = nullptr; size_t m_fileSize = 0; diff --git a/plugins/builtin/include/content/providers/gdb_provider.hpp b/plugins/builtin/include/content/providers/gdb_provider.hpp index 0940c02ea..79fe1ad0d 100644 --- a/plugins/builtin/include/content/providers/gdb_provider.hpp +++ b/plugins/builtin/include/content/providers/gdb_provider.hpp @@ -29,7 +29,7 @@ class GDBProvider : public hex::prv::Provider { [[nodiscard]] size_t getActualSize() const override; void save() override; - void saveAs(const std::string &path) override; + void saveAs(const fs::path &path) override; [[nodiscard]] std::string getName() const override; [[nodiscard]] std::vector> getDataInformation() const override; diff --git a/plugins/builtin/include/content/views/view_hexeditor.hpp b/plugins/builtin/include/content/views/view_hexeditor.hpp index ba0b056f9..4ac31d3ab 100644 --- a/plugins/builtin/include/content/views/view_hexeditor.hpp +++ b/plugins/builtin/include/content/views/view_hexeditor.hpp @@ -60,12 +60,8 @@ namespace hex::plugin::builtin { void drawGotoPopup(); void drawEditPopup(); - bool createFile(const std::string &path); - void openFile(const std::string &path); - bool saveToFile(const std::string &path, const std::vector& data); - bool loadFromFile(const std::string &path, std::vector& data); + void openFile(const fs::path &path); - enum class Language { C, Cpp, CSharp, Rust, Python, Java, JavaScript }; void copyBytes() const; void pasteBytes() const; void copyString() const; diff --git a/plugins/builtin/include/content/views/view_pattern_editor.hpp b/plugins/builtin/include/content/views/view_pattern_editor.hpp index 327681e8f..ea10217e8 100644 --- a/plugins/builtin/include/content/views/view_pattern_editor.hpp +++ b/plugins/builtin/include/content/views/view_pattern_editor.hpp @@ -77,7 +77,7 @@ namespace hex::plugin::builtin { void drawEnvVars(ImVec2 size); void drawVariableSettings(ImVec2 size); - void loadPatternFile(const std::string &path); + void loadPatternFile(const fs::path &path); void clearPatternData(); void parsePattern(const std::string &code); diff --git a/plugins/builtin/source/content/providers/disk_provider.cpp b/plugins/builtin/source/content/providers/disk_provider.cpp index 3fc776727..0c2e52b12 100644 --- a/plugins/builtin/source/content/providers/disk_provider.cpp +++ b/plugins/builtin/source/content/providers/disk_provider.cpp @@ -54,7 +54,7 @@ namespace hex::plugin::builtin::prv { } - void DiskProvider::setPath(const std::string &path) { + void DiskProvider::setPath(const fs::path &path) { this->m_path = path; } @@ -64,19 +64,11 @@ namespace hex::plugin::builtin::prv { #if defined (OS_WINDOWS) - std::wstring widePath; - { - auto length = this->m_path.length() + 1; - auto wideLength = MultiByteToWideChar(CP_UTF8, 0, this->m_path.data(), length, 0, 0); - auto buffer = new wchar_t[wideLength]; - MultiByteToWideChar(CP_UTF8, 0, this->m_path.data(), length, buffer, wideLength); - widePath = buffer; - delete[] buffer; - } + const auto &path = this->m_path.native(); - this->m_diskHandle = reinterpret_cast(CreateFileW(widePath.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); + this->m_diskHandle = reinterpret_cast(CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); if (this->m_diskHandle == INVALID_HANDLE_VALUE) { - this->m_diskHandle = reinterpret_cast(CreateFileW(widePath.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); + this->m_diskHandle = reinterpret_cast(CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); this->m_writable = false; if (this->m_diskHandle == INVALID_HANDLE_VALUE) @@ -245,12 +237,12 @@ namespace hex::plugin::builtin::prv { } std::string DiskProvider::getName() const { - return this->m_path; + return this->m_path.string(); } std::vector> DiskProvider::getDataInformation() const { return { - { "hex.builtin.provider.disk.selected_disk"_lang, this->m_path }, + { "hex.builtin.provider.disk.selected_disk"_lang, this->m_path.string() }, { "hex.builtin.provider.disk.disk_size"_lang, hex::toByteString(this->m_diskSize) }, { "hex.builtin.provider.disk.sector_size"_lang, hex::toByteString(this->m_sectorSize) } }; diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index 61fb782a4..38219915a 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -82,7 +82,7 @@ namespace hex::plugin::builtin::prv { this->applyPatches(); } - void FileProvider::saveAs(const std::string &path) { + void FileProvider::saveAs(const fs::path &path) { File file(path, File::Mode::Create); if (file.isValid()) { @@ -104,32 +104,10 @@ namespace hex::plugin::builtin::prv { void FileProvider::resize(ssize_t newSize) { this->close(); - #if defined(OS_WINDOWS) - std::wstring widePath; { - auto length = static_cast(this->m_path.length() + 1); - auto wideLength = MultiByteToWideChar(CP_UTF8, 0, this->m_path.data(), length, nullptr, 0); - auto buffer = new wchar_t[wideLength]; - MultiByteToWideChar(CP_UTF8, 0, this->m_path.data(), length, buffer, wideLength); - widePath = buffer; - delete[] buffer; + File(this->m_path, File::Mode::Write).setSize(newSize); } - auto handle = ::CreateFileW(widePath.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); - - if (handle != INVALID_HANDLE_VALUE) { - ::SetFilePointerEx(handle, LARGE_INTEGER { .QuadPart = newSize }, nullptr, FILE_BEGIN); - ::SetEndOfFile(handle); - ::CloseHandle(handle); - } - #else - auto handle = ::open(this->m_path.data(), 0644); - - ::ftruncate(handle, newSize - 1); - - ::close(handle); - #endif - this->open(); } @@ -144,7 +122,7 @@ namespace hex::plugin::builtin::prv { std::vector> FileProvider::getDataInformation() const { std::vector> result; - result.emplace_back("hex.builtin.provider.file.path"_lang, this->m_path); + result.emplace_back("hex.builtin.provider.file.path"_lang, this->m_path.string()); result.emplace_back("hex.builtin.provider.file.size"_lang, hex::toByteString(this->getActualSize())); if (this->m_fileStatsValid) { @@ -156,90 +134,82 @@ namespace hex::plugin::builtin::prv { return result; } - void FileProvider::setPath(const std::string &path) { + void FileProvider::setPath(const fs::path &path) { this->m_path = path; } bool FileProvider::open() { - this->m_fileStatsValid = stat(this->m_path.data(), &this->m_fileStats) == 0; + this->m_fileStatsValid = stat(this->m_path.string().data(), &this->m_fileStats) == 0; this->m_readable = true; this->m_writable = true; #if defined(OS_WINDOWS) - std::wstring widePath; - { - auto length = this->m_path.length() + 1; - auto wideLength = MultiByteToWideChar(CP_UTF8, 0, this->m_path.data(), length, 0, 0); - auto buffer = new wchar_t[wideLength]; - MultiByteToWideChar(CP_UTF8, 0, this->m_path.data(), length, buffer, wideLength); - widePath = buffer; - delete[] buffer; - } + const auto &path = this->m_path.native(); - LARGE_INTEGER fileSize = { 0 }; - this->m_file = reinterpret_cast(CreateFileW(widePath.data(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); + LARGE_INTEGER fileSize = { 0 }; + this->m_file = reinterpret_cast(CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); - GetFileSizeEx(this->m_file, &fileSize); - this->m_fileSize = fileSize.QuadPart; - CloseHandle(this->m_file); - - this->m_file = reinterpret_cast(CreateFileW(widePath.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); - if (this->m_file == nullptr || this->m_file == INVALID_HANDLE_VALUE) { - this->m_file = reinterpret_cast(CreateFileW(widePath.data(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); - this->m_writable = false; - } - - auto fileCleanup = SCOPE_GUARD { + GetFileSizeEx(this->m_file, &fileSize); + this->m_fileSize = fileSize.QuadPart; CloseHandle(this->m_file); - this->m_readable = false; - this->m_file = nullptr; - }; - - if (this->m_file == nullptr || this->m_file == INVALID_HANDLE_VALUE) { - return false; - } - - if (this->m_fileSize > 0) { - this->m_mapping = CreateFileMapping(this->m_file, nullptr, PAGE_READWRITE, 0, 0, nullptr); - if (this->m_mapping == nullptr || this->m_mapping == INVALID_HANDLE_VALUE) { - - this->m_mapping = CreateFileMapping(this->m_file, nullptr, PAGE_READONLY, 0, 0, nullptr); - - if (this->m_mapping == nullptr || this->m_mapping == INVALID_HANDLE_VALUE) - return false; + this->m_file = reinterpret_cast(CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); + if (this->m_file == nullptr || this->m_file == INVALID_HANDLE_VALUE) { + this->m_file = reinterpret_cast(CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); + this->m_writable = false; } - auto mappingCleanup = SCOPE_GUARD { - CloseHandle(this->m_mapping); + auto fileCleanup = SCOPE_GUARD { + CloseHandle(this->m_file); - this->m_mapping = nullptr; this->m_readable = false; + this->m_file = nullptr; }; - this->m_mappedFile = MapViewOfFile(this->m_mapping, FILE_MAP_ALL_ACCESS, 0, 0, this->m_fileSize); - if (this->m_mappedFile == nullptr) { - - this->m_mappedFile = MapViewOfFile(this->m_mapping, FILE_MAP_READ, 0, 0, this->m_fileSize); - if (this->m_mappedFile == nullptr) { - this->m_readable = false; - - return false; - } + if (this->m_file == nullptr || this->m_file == INVALID_HANDLE_VALUE) { + return false; } - mappingCleanup.release(); + if (this->m_fileSize > 0) { + this->m_mapping = CreateFileMapping(this->m_file, nullptr, PAGE_READWRITE, 0, 0, nullptr); + if (this->m_mapping == nullptr || this->m_mapping == INVALID_HANDLE_VALUE) { - ProjectFile::setFilePath(this->m_path); - } else if (!this->m_emptyFile) { - this->m_emptyFile = true; - this->resize(1); - } else { - return false; - } + this->m_mapping = CreateFileMapping(this->m_file, nullptr, PAGE_READONLY, 0, 0, nullptr); - fileCleanup.release(); + if (this->m_mapping == nullptr || this->m_mapping == INVALID_HANDLE_VALUE) + return false; + } + + auto mappingCleanup = SCOPE_GUARD { + CloseHandle(this->m_mapping); + + this->m_mapping = nullptr; + this->m_readable = false; + }; + + this->m_mappedFile = MapViewOfFile(this->m_mapping, FILE_MAP_ALL_ACCESS, 0, 0, this->m_fileSize); + if (this->m_mappedFile == nullptr) { + + this->m_mappedFile = MapViewOfFile(this->m_mapping, FILE_MAP_READ, 0, 0, this->m_fileSize); + if (this->m_mappedFile == nullptr) { + this->m_readable = false; + + return false; + } + } + + mappingCleanup.release(); + + ProjectFile::setFilePath(this->m_path); + } else if (!this->m_emptyFile) { + this->m_emptyFile = true; + this->resize(1); + } else { + return false; + } + + fileCleanup.release(); #else this->m_file = ::open(this->m_path.data(), O_RDWR); diff --git a/plugins/builtin/source/content/providers/gdb_provider.cpp b/plugins/builtin/source/content/providers/gdb_provider.cpp index 3cc5ddae1..54c31539a 100644 --- a/plugins/builtin/source/content/providers/gdb_provider.cpp +++ b/plugins/builtin/source/content/providers/gdb_provider.cpp @@ -214,7 +214,7 @@ namespace hex::plugin::builtin::prv { this->applyPatches(); } - void GDBProvider::saveAs(const std::string &path) { + void GDBProvider::saveAs(const fs::path &path) { } diff --git a/plugins/builtin/source/content/tools_entries.cpp b/plugins/builtin/source/content/tools_entries.cpp index a55f70da5..882b835dc 100644 --- a/plugins/builtin/source/content/tools_entries.cpp +++ b/plugins/builtin/source/content/tools_entries.cpp @@ -682,8 +682,8 @@ namespace hex::plugin::builtin { ImGui::InputText("##path", selectedFile.data(), selectedFile.capacity(), ImGuiInputTextFlags_CallbackEdit, ImGui::UpdateStringSizeCallback, &selectedFile); ImGui::SameLine(); if (ImGui::Button("...")) { - hex::openFileBrowser("hex.builtin.tools.file_tools.shredder.picker"_lang, DialogMode::Open, {}, [](const std::string &path) { - selectedFile = path; + hex::openFileBrowser("hex.builtin.tools.file_tools.shredder.picker"_lang, DialogMode::Open, {}, [](const auto &path) { + selectedFile = path.string(); }); } @@ -798,8 +798,8 @@ namespace hex::plugin::builtin { ImGui::InputText("##path", selectedFile.data(), selectedFile.capacity(), ImGuiInputTextFlags_CallbackEdit, ImGui::UpdateStringSizeCallback, &selectedFile); ImGui::SameLine(); if (ImGui::Button("...##input")) { - hex::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.input"_lang, DialogMode::Open, {}, [](const std::string &path) { - selectedFile = path; + hex::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.input"_lang, DialogMode::Open, {}, [](const auto &path) { + selectedFile = path.string(); }); } ImGui::SameLine(); @@ -808,8 +808,8 @@ namespace hex::plugin::builtin { ImGui::InputText("##base_path", baseOutputPath.data(), baseOutputPath.capacity(), ImGuiInputTextFlags_CallbackEdit, ImGui::UpdateStringSizeCallback, &baseOutputPath); ImGui::SameLine(); if (ImGui::Button("...##output")) { - hex::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.output"_lang, DialogMode::Save, {}, [](const std::string &path) { - baseOutputPath = path; + hex::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.output"_lang, DialogMode::Save, {}, [](const auto &path) { + baseOutputPath = path.string(); }); } ImGui::SameLine(); @@ -934,8 +934,8 @@ namespace hex::plugin::builtin { ImGui::BeginDisabled(combining); { if (ImGui::Button("hex.builtin.tools.file_tools.combiner.add"_lang)) { - hex::openFileBrowser("hex.builtin.tools.file_tools.combiner.add.picker"_lang, DialogMode::Open, {}, [](const std::string &path) { - files.push_back(path); + hex::openFileBrowser("hex.builtin.tools.file_tools.combiner.add.picker"_lang, DialogMode::Open, {}, [](const auto &path) { + files.push_back(path.string()); }); } ImGui::SameLine(); @@ -958,8 +958,8 @@ namespace hex::plugin::builtin { ImGui::InputText("##output_path", outputPath.data(), outputPath.capacity(), ImGuiInputTextFlags_CallbackEdit, ImGui::UpdateStringSizeCallback, &outputPath); ImGui::SameLine(); if (ImGui::Button("...")) { - hex::openFileBrowser("hex.builtin.tools.file_tools.combiner.output.picker"_lang, DialogMode::Save, {}, [](const std::string &path) { - outputPath = path; + hex::openFileBrowser("hex.builtin.tools.file_tools.combiner.output.picker"_lang, DialogMode::Save, {}, [](const auto &path) { + outputPath = path.string(); }); } ImGui::SameLine(); diff --git a/plugins/builtin/source/content/views/view_data_processor.cpp b/plugins/builtin/source/content/views/view_data_processor.cpp index 5e08d1ae8..15ed16cc3 100644 --- a/plugins/builtin/source/content/views/view_data_processor.cpp +++ b/plugins/builtin/source/content/views/view_data_processor.cpp @@ -38,7 +38,7 @@ namespace hex::plugin::builtin { } }); - EventManager::subscribe(this, [this](const std::string &path){ + EventManager::subscribe(this, [this](const fs::path &path){ for (auto &node : this->m_nodes) { node->setCurrentOverlay(nullptr); } diff --git a/plugins/builtin/source/content/views/view_hexeditor.cpp b/plugins/builtin/source/content/views/view_hexeditor.cpp index fadd32303..8c096fe5d 100644 --- a/plugins/builtin/source/content/views/view_hexeditor.cpp +++ b/plugins/builtin/source/content/views/view_hexeditor.cpp @@ -17,19 +17,9 @@ #include -#undef __STRICT_ANSI__ -#include - #include #include -#if defined(OS_WINDOWS) - #include -#else - #include - #include -#endif - namespace hex::plugin::builtin { ViewHexEditor::ViewHexEditor() : View("hex.builtin.view.hexeditor.name"_lang) { @@ -41,6 +31,12 @@ namespace hex::plugin::builtin { return ProjectFile::load(path.string()); }); + ContentRegistry::FileHandler::add({ ".tbl" }, [this](const auto &path) { + this->m_currEncodingFile = EncodingFile(EncodingFile::Type::Thingy, path); + + return true; + }); + this->m_memoryEditor.ReadFn = [](const ImU8 *data, size_t off) -> ImU8 { auto provider = ImHexApi::Provider::get(); if (!provider->isAvailable() || !provider->isReadable()) @@ -233,7 +229,7 @@ namespace hex::plugin::builtin { } static void saveAs() { - hex::openFileBrowser("hex.builtin.view.hexeditor.save_as"_lang, DialogMode::Save, { }, [](auto path) { + hex::openFileBrowser("hex.builtin.view.hexeditor.save_as"_lang, DialogMode::Save, { }, [](const auto &path) { ImHexApi::Provider::get()->saveAs(path); }); } @@ -267,15 +263,15 @@ namespace hex::plugin::builtin { ImGui::InputText("##nolabel", this->m_loaderScriptScriptPath.data(), this->m_loaderScriptScriptPath.length(), ImGuiInputTextFlags_ReadOnly); ImGui::SameLine(); if (ImGui::Button("hex.builtin.view.hexeditor.script.script"_lang)) { - hex::openFileBrowser("hex.builtin.view.hexeditor.script.script.title"_lang, DialogMode::Open, { { "Python Script", "py" } }, [this](auto path) { - this->m_loaderScriptScriptPath = path; + hex::openFileBrowser("hex.builtin.view.hexeditor.script.script.title"_lang, DialogMode::Open, { { "Python Script", "py" } }, [this](const auto &path) { + this->m_loaderScriptScriptPath = path.string(); }); } ImGui::InputText("##nolabel", this->m_loaderScriptFilePath.data(), this->m_loaderScriptFilePath.length(), ImGuiInputTextFlags_ReadOnly); ImGui::SameLine(); if (ImGui::Button("hex.builtin.view.hexeditor.script.file"_lang)) { - hex::openFileBrowser("hex.builtin.view.hexeditor.script.file.title"_lang, DialogMode::Open, { }, [this](auto path) { - this->m_loaderScriptFilePath = path; + hex::openFileBrowser("hex.builtin.view.hexeditor.script.file.title"_lang, DialogMode::Open, { }, [this](const auto &path) { + this->m_loaderScriptFilePath = path.string(); }); } if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) @@ -346,7 +342,7 @@ namespace hex::plugin::builtin { if (ImGui::BeginMenu("hex.menu.file"_lang)) { if (ImGui::MenuItem("hex.builtin.view.hexeditor.menu.file.open_file"_lang, "CTRL + O")) { - hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](auto path) { + hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [](const auto &path) { EventManager::post(path); }); } @@ -402,19 +398,19 @@ namespace hex::plugin::builtin { ImGui::Separator(); if (ImGui::MenuItem("hex.builtin.view.hexeditor.menu.file.open_project"_lang, "")) { - hex::openFileBrowser("hex.builtin.view.hexeditor.menu.file.open_project"_lang, DialogMode::Open, { { "Project File", "hexproj" } }, [this](auto path) { + hex::openFileBrowser("hex.builtin.view.hexeditor.menu.file.open_project"_lang, DialogMode::Open, { { "Project File", "hexproj" } }, [](const auto &path) { ProjectFile::load(path); }); } if (ImGui::MenuItem("hex.builtin.view.hexeditor.menu.file.save_project"_lang, "", false, providerValid && provider->isWritable())) { if (ProjectFile::getProjectFilePath() == "") { - hex::openFileBrowser("hex.builtin.view.hexeditor.save_project"_lang, DialogMode::Save, { { "Project File", "hexproj" } }, [](auto path) { - if (path.ends_with(".hexproj")) { + hex::openFileBrowser("hex.builtin.view.hexeditor.save_project"_lang, DialogMode::Save, { { "Project File", "hexproj" } }, [](const auto &path) { + if (path.extension() == ".hexproj") { ProjectFile::store(path); } else { - ProjectFile::store(path + ".hexproj"); + ProjectFile::store(path.string() + ".hexproj"); } }); } @@ -423,7 +419,7 @@ namespace hex::plugin::builtin { } if (ImGui::MenuItem("hex.builtin.view.hexeditor.menu.file.load_encoding_file"_lang)) { - hex::openFileBrowser("hex.builtin.view.hexeditor.load_enconding_file"_lang, DialogMode::Open, { { "Thingy Table File", "tbl" } }, [this](auto path) { + hex::openFileBrowser("hex.builtin.view.hexeditor.load_enconding_file"_lang, DialogMode::Open, { { "Thingy Table File", "tbl" } }, [this](const auto &path) { this->m_currEncodingFile = EncodingFile(EncodingFile::Type::Thingy, path); }); } @@ -433,9 +429,14 @@ namespace hex::plugin::builtin { if (ImGui::BeginMenu("hex.builtin.view.hexeditor.menu.file.import"_lang)) { if (ImGui::MenuItem("hex.builtin.view.hexeditor.menu.file.import.base64"_lang)) { - hex::openFileBrowser("hex.builtin.view.hexeditor.menu.file.import.base64"_lang, DialogMode::Open, { }, [this](auto path) { - std::vector base64; - this->loadFromFile(path, base64); + hex::openFileBrowser("hex.builtin.view.hexeditor.menu.file.import.base64"_lang, DialogMode::Open, { }, [this](const auto &path) { + File file(path, File::Mode::Read); + if (!file.isValid()) { + View::showErrorPopup("hex.builtin.view.hexeditor.error.open"_lang); + return; + } + + auto base64 = file.readBytes(); if (!base64.empty()) { this->m_dataToSave = crypt::decode64(base64); @@ -453,7 +454,7 @@ namespace hex::plugin::builtin { if (ImGui::MenuItem("hex.builtin.view.hexeditor.menu.file.import.ips"_lang, nullptr, false, !this->m_processingImportExport)) { - hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](auto path) { + hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](const auto &path) { this->m_processingImportExport = true; std::thread([this, path] { auto task = ImHexApi::Tasks::createTask("hex.builtin.view.hexeditor.processing", 0); @@ -483,7 +484,7 @@ namespace hex::plugin::builtin { } if (ImGui::MenuItem("hex.builtin.view.hexeditor.menu.file.import.ips32"_lang, nullptr, false, !this->m_processingImportExport)) { - hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](auto path) { + hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](const auto &path) { this->m_processingImportExport = true; std::thread([this, path] { auto task = ImHexApi::Tasks::createTask("hex.builtin.view.hexeditor.processing", 0); @@ -536,8 +537,14 @@ namespace hex::plugin::builtin { this->m_processingImportExport = false; View::doLater([this]{ - hex::openFileBrowser("hex.builtin.view.hexeditor.menu.file.export.title"_lang, DialogMode::Save, { }, [this](auto path) { - this->saveToFile(path, this->m_dataToSave); + hex::openFileBrowser("hex.builtin.view.hexeditor.menu.file.export.title"_lang, DialogMode::Save, { }, [this](const auto &path) { + auto file = File(path, File::Mode::Create); + if (!file.isValid()) { + View::showErrorPopup("hex.builtin.view.hexeditor.error.create"_lang); + return; + } + + file.write(this->m_dataToSave); }); }); }).detach(); @@ -558,8 +565,14 @@ namespace hex::plugin::builtin { this->m_processingImportExport = false; View::doLater([this]{ - hex::openFileBrowser("hex.builtin.view.hexeditor.menu.file.export.title"_lang, DialogMode::Save, { }, [this](auto path) { - this->saveToFile(path, this->m_dataToSave); + hex::openFileBrowser("hex.builtin.view.hexeditor.menu.file.export.title"_lang, DialogMode::Save, { }, [this](const auto &path) { + auto file = File(path, File::Mode::Create); + if (!file.isValid()) { + View::showErrorPopup("hex.builtin.view.hexeditor.error.create"_lang); + return; + } + + file.write(this->m_dataToSave); }); }); }).detach(); @@ -591,41 +604,7 @@ namespace hex::plugin::builtin { } } - bool ViewHexEditor::createFile(const std::string &path) { - #if defined(OS_WINDOWS) - std::wstring widePath; - { - auto length = path.length() + 1; - auto wideLength = MultiByteToWideChar(CP_UTF8, 0, path.data(), length, 0, 0); - auto buffer = new wchar_t[wideLength]; - MultiByteToWideChar(CP_UTF8, 0, path.data(), length, buffer, wideLength); - widePath = buffer; - delete[] buffer; - } - - auto handle = ::CreateFileW(widePath.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); - - if (handle == INVALID_HANDLE_VALUE) - return false; - - ::SetFilePointer(handle, 1, nullptr, FILE_BEGIN); - ::SetEndOfFile(handle); - ::CloseHandle(handle); - #else - auto handle = ::open(path.data(), O_RDWR | O_CREAT, 0644); - if (handle == -1) - return false; - - lseek(handle, 0, SEEK_SET); - write(handle, "", 1); - - close(handle); - #endif - - return true; - } - - void ViewHexEditor::openFile(const std::string &path) { + void ViewHexEditor::openFile(const fs::path &path) { hex::prv::Provider *provider = nullptr; EventManager::post("hex.builtin.provider.file", &provider); @@ -666,18 +645,6 @@ namespace hex::plugin::builtin { } } - bool ViewHexEditor::saveToFile(const std::string &path, const std::vector& data) { - File(path, File::Mode::Create).write(data); - - return true; - } - - bool ViewHexEditor::loadFromFile(const std::string &path, std::vector& data) { - data = File(path, File::Mode::Read).readBytes(); - - return true; - } - void ViewHexEditor::copyBytes() const { auto provider = ImHexApi::Provider::get(); @@ -1046,8 +1013,8 @@ namespace hex::plugin::builtin { } void ViewHexEditor::registerEvents() { - EventManager::subscribe(this, [this](const std::string &filePath) { - this->openFile(filePath); + EventManager::subscribe(this, [this](const auto &path) { + this->openFile(path); this->getWindowOpenState() = true; }); @@ -1080,22 +1047,26 @@ namespace hex::plugin::builtin { EventManager::subscribe(this, [this](std::string name) { if (name == "Create File") { - hex::openFileBrowser("hex.builtin.view.hexeditor.create_file"_lang, DialogMode::Save, { }, [this](auto path) { - if (!this->createFile(path)) { + hex::openFileBrowser("hex.builtin.view.hexeditor.create_file"_lang, DialogMode::Save, { }, [this](const auto &path) { + File file(path, File::Mode::Create); + + if (!file.isValid()) { View::showErrorPopup("hex.builtin.view.hexeditor.error.create"_lang); return; } + file.setSize(1); + EventManager::post(path); this->getWindowOpenState() = true; }); } else if (name == "Open File") { - hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](auto path) { + hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](const auto &path) { EventManager::post(path); this->getWindowOpenState() = true; }); } else if (name == "Open Project") { - hex::openFileBrowser("hex.builtin.view.hexeditor.open_project"_lang, DialogMode::Open, { { "Project File", "hexproj" } }, [this](auto path) { + hex::openFileBrowser("hex.builtin.view.hexeditor.open_project"_lang, DialogMode::Open, { { "Project File", "hexproj" } }, [this](const auto &path) { ProjectFile::load(path); this->getWindowOpenState() = true; }); @@ -1198,7 +1169,7 @@ namespace hex::plugin::builtin { }); ShortcutManager::addShortcut(this, CTRL + Keys::O, [] { - hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [](auto path) { + hex::openFileBrowser("hex.builtin.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [](const auto &path) { EventManager::post(path); }); }); @@ -1216,7 +1187,7 @@ namespace hex::plugin::builtin { this->pasteBytes(); }); - ShortcutManager::addShortcut(this, CTRL + Keys::A, [this] { + ShortcutManager::addShortcut(this, CTRL + Keys::A, [] { auto provider = ImHexApi::Provider::get(); EventManager::post(Region { provider->getBaseAddress(), provider->getActualSize() }); }); diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index e333aa0e7..03af39bb4 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -110,7 +110,7 @@ namespace hex::plugin::builtin { this->m_textEditor.InsertText(code); }); - EventManager::subscribe(this, [this](const std::string &path) { + EventManager::subscribe(this, [this](const fs::path &path) { if (!ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.auto_load_patterns", 1)) return; @@ -232,7 +232,7 @@ namespace hex::plugin::builtin { } if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang)) { - hex::openFileBrowser("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang, DialogMode::Save, { { "Pattern", "hexpat" }}, [this](const std::string &path) { + hex::openFileBrowser("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang, DialogMode::Save, { { "Pattern", "hexpat" }}, [this](const auto &path) { File file(path, File::Mode::Create); file.write(this->m_textEditor.GetText()); @@ -586,7 +586,7 @@ namespace hex::plugin::builtin { ImGui::SameLine(); if (ImGui::Button("hex.common.browse"_lang)) { - hex::openFileBrowser("hex.builtin.view.pattern_editor.open_pattern"_lang, DialogMode::Open, { { "Pattern File", "hexpat" } }, [this](auto path) { + hex::openFileBrowser("hex.builtin.view.pattern_editor.open_pattern"_lang, DialogMode::Open, { { "Pattern File", "hexpat" } }, [this](const auto &path) { this->loadPatternFile(path); ImGui::CloseCurrentPopup(); }); @@ -597,27 +597,13 @@ namespace hex::plugin::builtin { } - void ViewPatternEditor::loadPatternFile(const std::string &path) { - FILE *file = fopen(path.c_str(), "rb"); + void ViewPatternEditor::loadPatternFile(const fs::path &path) { + File file(path, File::Mode::Read); + if (file.isValid()) { + auto code = file.readString(); - if (file != nullptr) { - char *buffer; - fseek(file, 0, SEEK_END); - size_t size = ftell(file); - rewind(file); - - buffer = new char[size + 1]; - - fread(buffer, size, 1, file); - buffer[size] = 0x00; - - - fclose(file); - - this->evaluatePattern(buffer); - this->m_textEditor.SetText(buffer); - - delete[] buffer; + this->evaluatePattern(code); + this->m_textEditor.SetText(code); } } diff --git a/plugins/libimhex/include/hex/api/event.hpp b/plugins/libimhex/include/hex/api/event.hpp index 218f3a819..0b401b26f 100644 --- a/plugins/libimhex/include/hex/api/event.hpp +++ b/plugins/libimhex/include/hex/api/event.hpp @@ -8,6 +8,7 @@ #include #include +#include #define EVENT_DEF(event_name, ...) \ struct event_name final : public hex::Event<__VA_ARGS__> { \ @@ -98,7 +99,7 @@ namespace hex { namespace pl { class PatternData; } /* Default Events */ - EVENT_DEF(EventFileLoaded, std::string); + EVENT_DEF(EventFileLoaded, fs::path); EVENT_DEF(EventFileUnloaded); EVENT_DEF(EventDataChanged); EVENT_DEF(EventPatternChanged, std::vector&); @@ -117,7 +118,7 @@ namespace hex { EVENT_DEF(RequestSetPatternLanguageCode, std::string); EVENT_DEF(RequestChangeWindowTitle, std::string); EVENT_DEF(RequestCloseImHex, bool); - EVENT_DEF(RequestOpenFile, std::string); + EVENT_DEF(RequestOpenFile, fs::path); EVENT_DEF(RequestChangeTheme, u32); EVENT_DEF(RequestOpenPopup, std::string); EVENT_DEF(RequestCreateProvider, std::string, hex::prv::Provider **); diff --git a/plugins/libimhex/include/hex/helpers/encoding_file.hpp b/plugins/libimhex/include/hex/helpers/encoding_file.hpp index af008cf68..4d849f83d 100644 --- a/plugins/libimhex/include/hex/helpers/encoding_file.hpp +++ b/plugins/libimhex/include/hex/helpers/encoding_file.hpp @@ -6,31 +6,23 @@ #include #include -namespace hex { +#include - template - struct SizeSorter { - bool operator() (const T& lhs, const T& rhs) const { - return lhs.size() < rhs.size(); - } - }; +namespace hex { class EncodingFile { public: enum class Type { - Thingy, - CSV + Thingy }; EncodingFile() = default; - EncodingFile(Type type, const std::string &path); + EncodingFile(Type type, const fs::path &path); [[nodiscard]] std::pair getEncodingFor(const std::vector &buffer) const; [[nodiscard]] size_t getLongestSequence() const { return this->m_longestSequence; } - bool valid() const { - return this->m_valid; - } + [[nodiscard]] bool valid() const { return this->m_valid; } private: void parseThingyFile(std::ifstream &content); diff --git a/plugins/libimhex/include/hex/helpers/file.hpp b/plugins/libimhex/include/hex/helpers/file.hpp index c1952e355..122664324 100644 --- a/plugins/libimhex/include/hex/helpers/file.hpp +++ b/plugins/libimhex/include/hex/helpers/file.hpp @@ -6,6 +6,8 @@ #include #include +#include + #if defined(OS_MACOS) #define off64_t off_t #define fopen64 fopen @@ -24,7 +26,7 @@ namespace hex { Create }; - explicit File(const std::string &path, Mode mode); + explicit File(const fs::path &path, Mode mode); File(); File(const File&) = delete; File(File &&other) noexcept; @@ -51,11 +53,11 @@ namespace hex { void remove(); auto getHandle() { return this->m_file; } - const std::string& getPath() { return this->m_path; } + const fs::path& getPath() { return this->m_path; } private: FILE *m_file; - std::string m_path; + fs::path m_path; }; } \ No newline at end of file diff --git a/plugins/libimhex/include/hex/helpers/loader_script_handler.hpp b/plugins/libimhex/include/hex/helpers/loader_script_handler.hpp index 8633e9b7f..9b9992765 100644 --- a/plugins/libimhex/include/hex/helpers/loader_script_handler.hpp +++ b/plugins/libimhex/include/hex/helpers/loader_script_handler.hpp @@ -3,6 +3,8 @@ #include #include +#include + struct _object; typedef struct _object PyObject; @@ -14,12 +16,12 @@ namespace hex { public: LoaderScript() = delete; - static bool processFile(const std::string &scriptPath); + static bool processFile(const fs::path &scriptPath); - static void setFilePath(const std::string &filePath) { LoaderScript::s_filePath = filePath; } + static void setFilePath(const fs::path &filePath) { LoaderScript::s_filePath = filePath; } static void setDataProvider(prv::Provider* provider) { LoaderScript::s_dataProvider = provider; } private: - static inline std::string s_filePath; + static inline fs::path s_filePath; static inline prv::Provider* s_dataProvider; static PyObject* Py_getFilePath(PyObject *self, PyObject *args); diff --git a/plugins/libimhex/include/hex/helpers/project_file_handler.hpp b/plugins/libimhex/include/hex/helpers/project_file_handler.hpp index dc0b8dc65..ce8ca05ef 100644 --- a/plugins/libimhex/include/hex/helpers/project_file_handler.hpp +++ b/plugins/libimhex/include/hex/helpers/project_file_handler.hpp @@ -16,8 +16,8 @@ namespace hex { public: ProjectFile() = delete; - static bool load(const std::string &filePath); - static bool store(std::string filePath = ""); + static bool load(const fs::path &filePath); + static bool store(fs::path filePath = { }); [[nodiscard]] static bool hasUnsavedChanges() { return ProjectFile::s_hasUnsavedChanged; @@ -32,7 +32,7 @@ namespace hex { EventManager::post(fs::path(getFilePath()).filename().string()); } - [[nodiscard]] static const std::string& getProjectFilePath() { + [[nodiscard]] static const fs::path& getProjectFilePath() { return ProjectFile::s_currProjectFilePath; } @@ -41,14 +41,14 @@ namespace hex { } - [[nodiscard]] static const std::string& getFilePath() { + [[nodiscard]] static const fs::path& getFilePath() { return ProjectFile::s_filePath; } - static void setFilePath(const std::string &filePath) { + static void setFilePath(const fs::path &filePath) { ProjectFile::s_filePath = filePath; - EventManager::post(fs::path(filePath).filename().string()); + EventManager::post(filePath.filename().string()); } @@ -92,10 +92,10 @@ namespace hex { } private: - static std::string s_currProjectFilePath; + static fs::path s_currProjectFilePath; static bool s_hasUnsavedChanged; - static std::string s_filePath; + static fs::path s_filePath; static std::string s_pattern; static Patches s_patches; static std::list s_bookmarks; diff --git a/plugins/libimhex/include/hex/helpers/shared_data.hpp b/plugins/libimhex/include/hex/helpers/shared_data.hpp index 88be88ee2..d69497087 100644 --- a/plugins/libimhex/include/hex/helpers/shared_data.hpp +++ b/plugins/libimhex/include/hex/helpers/shared_data.hpp @@ -94,7 +94,7 @@ namespace hex { static std::vector dataFormatters; static std::vector fileHandlers; - static std::list recentFilePaths; + static std::list recentFilePaths; static int mainArgc; static char **mainArgv; diff --git a/plugins/libimhex/include/hex/helpers/utils.hpp b/plugins/libimhex/include/hex/helpers/utils.hpp index 95f5e1ba8..72dffe0f0 100644 --- a/plugins/libimhex/include/hex/helpers/utils.hpp +++ b/plugins/libimhex/include/hex/helpers/utils.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -221,7 +222,7 @@ namespace hex { Folder }; - void openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath = {}); + void openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath = {}); float float16ToFloat32(u16 float16); diff --git a/plugins/libimhex/include/hex/pattern_language/pattern_language.hpp b/plugins/libimhex/include/hex/pattern_language/pattern_language.hpp index 89bb80733..efc5a021b 100644 --- a/plugins/libimhex/include/hex/pattern_language/pattern_language.hpp +++ b/plugins/libimhex/include/hex/pattern_language/pattern_language.hpp @@ -35,7 +35,7 @@ namespace hex::pl { [[nodiscard]] std::optional> executeString(prv::Provider *provider, const std::string &string, const std::map &envVars = { }, const std::map &inVariables = { }); [[nodiscard]] - std::optional> executeFile(prv::Provider *provider, const std::string &path, const std::map &envVars = { }, const std::map &inVariables = { }); + std::optional> executeFile(prv::Provider *provider, const fs::path &path, const std::map &envVars = { }, const std::map &inVariables = { }); [[nodiscard]] const std::vector& getCurrentAST() const; diff --git a/plugins/libimhex/include/hex/providers/provider.hpp b/plugins/libimhex/include/hex/providers/provider.hpp index efd1af19d..83994f98d 100644 --- a/plugins/libimhex/include/hex/providers/provider.hpp +++ b/plugins/libimhex/include/hex/providers/provider.hpp @@ -31,7 +31,7 @@ namespace hex::prv { virtual void resize(ssize_t newSize); virtual void save(); - virtual void saveAs(const std::string &path); + virtual void saveAs(const fs::path &path); virtual void readRaw(u64 offset, void *buffer, size_t size) = 0; virtual void writeRaw(u64 offset, const void *buffer, size_t size) = 0; diff --git a/plugins/libimhex/source/helpers/encoding_file.cpp b/plugins/libimhex/source/helpers/encoding_file.cpp index c77eb0339..8eb4453db 100644 --- a/plugins/libimhex/source/helpers/encoding_file.cpp +++ b/plugins/libimhex/source/helpers/encoding_file.cpp @@ -6,12 +6,12 @@ namespace hex { - EncodingFile::EncodingFile(Type type, const std::string &path) { + EncodingFile::EncodingFile(Type type, const fs::path &path) { std::ifstream encodingFile(path.c_str()); switch (type) { case Type::Thingy: parseThingyFile(encodingFile); break; - default: throw std::runtime_error("Invalid encoding file type"); + default: return; } this->m_valid = true; diff --git a/plugins/libimhex/source/helpers/file.cpp b/plugins/libimhex/source/helpers/file.cpp index 85d18f279..6ca2f125d 100644 --- a/plugins/libimhex/source/helpers/file.cpp +++ b/plugins/libimhex/source/helpers/file.cpp @@ -3,14 +3,14 @@ namespace hex { - File::File(const std::string &path, Mode mode) : m_path(path) { + File::File(const fs::path &path, Mode mode) : m_path(path) { if (mode == File::Mode::Read) - this->m_file = fopen64(path.c_str(), "rb"); + this->m_file = fopen64(path.string().c_str(), "rb"); else if (mode == File::Mode::Write) - this->m_file = fopen64(path.c_str(), "r+b"); + this->m_file = fopen64(path.string().c_str(), "r+b"); if (mode == File::Mode::Create || (mode == File::Mode::Write && this->m_file == nullptr)) - this->m_file = fopen64(path.c_str(), "w+b"); + this->m_file = fopen64(path.string().c_str(), "w+b"); } File::File() { @@ -105,7 +105,7 @@ namespace hex { void File::remove() { this->close(); - std::remove(this->m_path.c_str()); + std::remove(this->m_path.string().c_str()); } } \ No newline at end of file diff --git a/plugins/libimhex/source/helpers/loader_script_handler.cpp b/plugins/libimhex/source/helpers/loader_script_handler.cpp index 37c46e7d1..41e524583 100644 --- a/plugins/libimhex/source/helpers/loader_script_handler.cpp +++ b/plugins/libimhex/source/helpers/loader_script_handler.cpp @@ -18,7 +18,7 @@ using namespace std::literals::string_literals; namespace hex { PyObject* LoaderScript::Py_getFilePath(PyObject *self, PyObject *args) { - return PyUnicode_FromString(LoaderScript::s_filePath.c_str()); + return PyUnicode_FromString(LoaderScript::s_filePath.string().c_str()); } PyObject* LoaderScript::Py_addPatch(PyObject *self, PyObject *args) { @@ -178,7 +178,7 @@ namespace hex { return createStructureType("union", args); } - bool LoaderScript::processFile(const std::string &scriptPath) { + bool LoaderScript::processFile(const fs::path &scriptPath) { Py_SetProgramName(Py_DecodeLocale((SharedData::mainArgv)[0], nullptr)); for (const auto &dir : hex::getPath(ImHexPath::Python)) { @@ -220,7 +220,7 @@ namespace hex { } File scriptFile(scriptPath, File::Mode::Read); - PyRun_SimpleFile(scriptFile.getHandle(), scriptPath.c_str()); + PyRun_SimpleFile(scriptFile.getHandle(), scriptFile.getPath().string().c_str()); Py_Finalize(); diff --git a/plugins/libimhex/source/helpers/project_file_handler.cpp b/plugins/libimhex/source/helpers/project_file_handler.cpp index 038d1ef4e..ac0ee135c 100644 --- a/plugins/libimhex/source/helpers/project_file_handler.cpp +++ b/plugins/libimhex/source/helpers/project_file_handler.cpp @@ -9,10 +9,10 @@ using json = nlohmann::json; namespace hex { - std::string ProjectFile::s_currProjectFilePath; + fs::path ProjectFile::s_currProjectFilePath; bool ProjectFile::s_hasUnsavedChanged = false; - std::string ProjectFile::s_filePath; + fs::path ProjectFile::s_filePath; std::string ProjectFile::s_pattern; Patches ProjectFile::s_patches; std::list ProjectFile::s_bookmarks; @@ -39,7 +39,7 @@ namespace hex { } - bool ProjectFile::load(const std::string &filePath) { + bool ProjectFile::load(const fs::path &filePath) { ProjectFile::s_hasUnsavedChanged = false; json projectFileData; @@ -48,7 +48,7 @@ namespace hex { std::ifstream projectFile(filePath.c_str()); projectFile >> projectFileData; - ProjectFile::s_filePath = projectFileData["filePath"]; + ProjectFile::s_filePath = fs::path(projectFileData["filePath"].get()); ProjectFile::s_pattern = projectFileData["pattern"]; ProjectFile::s_patches = projectFileData["patches"].get(); ProjectFile::s_dataProcessorContent = projectFileData["dataProcessor"]; @@ -73,7 +73,7 @@ namespace hex { return true; } - bool ProjectFile::store(std::string filePath) { + bool ProjectFile::store(fs::path filePath) { EventManager::post(); json projectFileData; diff --git a/plugins/libimhex/source/helpers/shared_data.cpp b/plugins/libimhex/source/helpers/shared_data.cpp index b1d072355..1a7cee499 100644 --- a/plugins/libimhex/source/helpers/shared_data.cpp +++ b/plugins/libimhex/source/helpers/shared_data.cpp @@ -44,7 +44,7 @@ namespace hex { std::vector SharedData::dataFormatters; std::vector SharedData::fileHandlers; - std::list SharedData::recentFilePaths; + std::list SharedData::recentFilePaths; int SharedData::mainArgc; char **SharedData::mainArgv; diff --git a/plugins/libimhex/source/helpers/utils.cpp b/plugins/libimhex/source/helpers/utils.cpp index df662ff0b..e028837a1 100644 --- a/plugins/libimhex/source/helpers/utils.cpp +++ b/plugins/libimhex/source/helpers/utils.cpp @@ -208,7 +208,7 @@ namespace hex { } - void openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath) { + void openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath) { NFD::Init(); nfdchar_t *outPath; diff --git a/plugins/libimhex/source/pattern_language/pattern_language.cpp b/plugins/libimhex/source/pattern_language/pattern_language.cpp index 2fb22fb35..6b4b294bf 100644 --- a/plugins/libimhex/source/pattern_language/pattern_language.cpp +++ b/plugins/libimhex/source/pattern_language/pattern_language.cpp @@ -149,7 +149,7 @@ namespace hex::pl { return patterns; } - std::optional> PatternLanguage::executeFile(prv::Provider *provider, const std::string &path, const std::map &envVars, const std::map &inVariables) { + std::optional> PatternLanguage::executeFile(prv::Provider *provider, const fs::path &path, const std::map &envVars, const std::map &inVariables) { File file(path, File::Mode::Read); return this->executeString(provider, file.readString(), envVars, inVariables); diff --git a/plugins/libimhex/source/providers/provider.cpp b/plugins/libimhex/source/providers/provider.cpp index 7f12e9e29..f3b98dd02 100644 --- a/plugins/libimhex/source/providers/provider.cpp +++ b/plugins/libimhex/source/providers/provider.cpp @@ -32,7 +32,7 @@ namespace hex::prv { } void Provider::save() { } - void Provider::saveAs(const std::string &path) { } + void Provider::saveAs(const fs::path &path) { } void Provider::resize(ssize_t newSize) { }