1
0
mirror of synced 2024-11-24 15:50:16 +01:00

sys: Moved more functions to libwolv

This commit is contained in:
WerWolv 2023-03-12 18:43:05 +01:00
parent 0dafb3d230
commit fb2e668589
22 changed files with 56 additions and 97 deletions

@ -1 +1 @@
Subproject commit 6742cbb5c464ae4dcd7c1d5d1a573403258000bd
Subproject commit f98c18807a961dfcbf19dae256361e6b99a265d2

@ -1 +1 @@
Subproject commit 714fb62cf47b171dae58bf2c5a95e82d4d8310a7
Subproject commit 0858a0a1a0309bacaa34670488f7643d53d91849

View File

@ -48,7 +48,6 @@ if (APPLE)
endif ()
set(LIBIMHEX_SOURCES ${LIBIMHEX_SOURCES}
source/helpers/fs_macos.m
source/helpers/utils_macos.m)
endif ()

View File

@ -274,13 +274,6 @@ namespace hex {
return iter != a.end();
}
template<typename T> requires requires(T t) { t.u8string(); }
std::string toUTF8String(const T &value) {
auto result = value.u8string();
return { result.begin(), result.end() };
}
template<typename T, typename... VariantTypes>
T get_or(const std::variant<VariantTypes...> &variant, T alt) {
const T *value = std::get_if<T>(&variant);

View File

@ -3,6 +3,8 @@
#include <hex/helpers/logger.hpp>
#include <hex/helpers/utils.hpp>
#include <wolv/utils/string.hpp>
#include <filesystem>
#include <system_error>
@ -17,7 +19,7 @@ namespace hex {
return;
}
#else
this->m_handle = dlopen(hex::toUTF8String(path).c_str(), RTLD_LAZY);
this->m_handle = dlopen(wolv::util::toUTF8String(path).c_str(), RTLD_LAZY);
if (this->m_handle == nullptr) {
log::error("dlopen failed: {}!", dlerror());
@ -69,7 +71,7 @@ namespace hex {
bool Plugin::initializePlugin() const {
const auto requestedVersion = getCompatibleVersion();
if (requestedVersion != IMHEX_VERSION) {
log::error("Refused to load plugin '{}' which was built for a different version of ImHex: '{}'", hex::toUTF8String(this->m_path.filename()), requestedVersion);
log::error("Refused to load plugin '{}' which was built for a different version of ImHex: '{}'", wolv::util::toUTF8String(this->m_path.filename()), requestedVersion);
return false;
}

View File

@ -1,44 +0,0 @@
#if defined(OS_MACOS)
#include <string.h>
#include <stdlib.h>
#include <Foundation/Foundation.h>
char* getMacExecutableDirectoryPath(void) {
@autoreleasepool {
const char *pathString = [[[[[NSBundle mainBundle] executableURL] URLByDeletingLastPathComponent] path] UTF8String];
char *result = malloc(strlen(pathString) + 1);
strcpy(result, pathString);
return result;
}
}
char* getMacApplicationSupportDirectoryPath(void) {
@autoreleasepool {
NSError* error = nil;
NSURL* dirUrl = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:&error];
if (error != nil) {
return NULL;
}
const char *pathString = [[[dirUrl URLByAppendingPathComponent:(@"imhex")] path] UTF8String];
char *result = malloc(strlen(pathString) + 1);
strcpy(result, pathString);
return result;
}
}
void macFree(void *ptr) {
free(ptr);
}
#endif

View File

@ -4,6 +4,7 @@
#include <hex/helpers/fs.hpp>
#include <wolv/utils/guards.hpp>
#include <wolv/utils/string.hpp>
#include <hex/providers/provider.hpp>
@ -29,7 +30,7 @@ namespace hex::magic {
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Magic)) {
for (const auto &entry : std::fs::directory_iterator(dir, error)) {
if (entry.is_regular_file() && ((sourceFiles && entry.path().extension().empty()) || (!sourceFiles && entry.path().extension() == ".mgc"))) {
magicFiles += hex::toUTF8String(wolv::io::fs::toShortPath(entry.path())) + MAGIC_PATH_SEPARATOR;
magicFiles += wolv::util::toUTF8String(wolv::io::fs::toShortPath(entry.path())) + MAGIC_PATH_SEPARATOR;
}
}
}

View File

@ -211,7 +211,7 @@ namespace hex {
curl_mime *mime = curl_mime_init(this->m_ctx);
curl_mimepart *part = curl_mime_addpart(mime);
auto fileName = hex::toUTF8String(filePath.filename());
auto fileName = wolv::util::toUTF8String(filePath.filename());
curl_mime_data_cb(part, file.getSize(),
[](char *buffer, size_t size, size_t nitems, void *arg) -> size_t {
auto file = static_cast<FILE*>(arg);

View File

@ -98,7 +98,7 @@ namespace hex::init {
try {
wolv::io::fs::createDirectories(folder);
} catch (...) {
log::error("Failed to create folder {}!", hex::toUTF8String(folder));
log::error("Failed to create folder {}!", wolv::util::toUTF8String(folder));
result = false;
}
}
@ -159,7 +159,7 @@ namespace hex::init {
fonts->Clear();
fonts->AddFontDefault(&cfg);
} else {
fonts->AddFontFromFileTTF(hex::toUTF8String(fontFile).c_str(), 0, &cfg, ranges.Data);
fonts->AddFontFromFileTTF(wolv::util::toUTF8String(fontFile).c_str(), 0, &cfg, ranges.Data);
}
// Merge all fonts into one big font atlas
@ -333,7 +333,7 @@ namespace hex::init {
// Initialize the plugin
if (!plugin.initializePlugin()) {
log::error("Failed to initialize plugin {}", hex::toUTF8String(plugin.getPath().filename()));
log::error("Failed to initialize plugin {}", wolv::util::toUTF8String(plugin.getPath().filename()));
loadErrors++;
}
}
@ -349,7 +349,7 @@ namespace hex::init {
// Initialize the plugin
if (!plugin.initializePlugin()) {
log::error("Failed to initialize plugin {}", hex::toUTF8String(plugin.getPath().filename()));
log::error("Failed to initialize plugin {}", wolv::util::toUTF8String(plugin.getPath().filename()));
loadErrors++;
}
}

View File

@ -47,7 +47,7 @@ namespace hex {
if (data == nullptr) break;
std::fs::path path = data;
log::info("Opening file in existing instance: {}", hex::toUTF8String(path));
log::info("Opening file in existing instance: {}", wolv::util::toUTF8String(path));
EventManager::post<RequestOpenFile>(path);
break;
}

View File

@ -167,7 +167,7 @@ namespace hex {
// Save a backup project when the application crashes
EventManager::subscribe<EventAbnormalTermination>(this, [this](int) {
ImGui::SaveIniSettingsToDisk(hex::toUTF8String(this->m_imguiSettingsPath).c_str());
ImGui::SaveIniSettingsToDisk(wolv::util::toUTF8String(this->m_imguiSettingsPath).c_str());
if (!ImHexApi::Provider::isDirty())
return;
@ -441,7 +441,7 @@ namespace hex {
const auto filePath = path / "builtin.hexplug";
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted(hex::toUTF8String(filePath).c_str());
ImGui::TextUnformatted(wolv::util::toUTF8String(filePath).c_str());
ImGui::TableNextColumn();
ImGui::TextUnformatted(wolv::io::fs::exists(filePath) ? ICON_VS_CHECK : ICON_VS_CLOSE);
}
@ -868,7 +868,7 @@ namespace hex {
}
if (!this->m_imguiSettingsPath.empty() && wolv::io::fs::exists(this->m_imguiSettingsPath))
ImGui::LoadIniSettingsFromDisk(hex::toUTF8String(this->m_imguiSettingsPath).c_str());
ImGui::LoadIniSettingsFromDisk(wolv::util::toUTF8String(this->m_imguiSettingsPath).c_str());
}
ImGui_ImplGlfw_InitForOpenGL(this->m_window, true);
@ -893,7 +893,7 @@ namespace hex {
void Window::exitImGui() {
delete static_cast<ImGui::ImHexCustomData *>(ImGui::GetIO().UserData);
ImGui::SaveIniSettingsToDisk(hex::toUTF8String(this->m_imguiSettingsPath).c_str());
ImGui::SaveIniSettingsToDisk(wolv::util::toUTF8String(this->m_imguiSettingsPath).c_str());
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();

View File

@ -6,6 +6,8 @@
#include <hex/helpers/utils.hpp>
#include <hex/ui/imgui_imhex_extensions.h>
#include <wolv/utils/string.hpp>
#include <bitset>
#include <filesystem>
@ -259,12 +261,12 @@ namespace hex::plugin::builtin {
}
std::string DiskProvider::getName() const {
return hex::toUTF8String(this->m_path);
return wolv::util::toUTF8String(this->m_path);
}
std::vector<std::pair<std::string, std::string>> DiskProvider::getDataDescription() const {
return {
{ "hex.builtin.provider.disk.selected_disk"_lang, hex::toUTF8String(this->m_path) },
{ "hex.builtin.provider.disk.selected_disk"_lang, wolv::util::toUTF8String(this->m_path) },
{ "hex.builtin.provider.disk.disk_size"_lang, hex::toByteString(this->m_diskSize) },
{ "hex.builtin.provider.disk.sector_size"_lang, hex::toByteString(this->m_sectorSize) }
};
@ -341,7 +343,7 @@ namespace hex::plugin::builtin {
}
nlohmann::json DiskProvider::storeSettings(nlohmann::json settings) const {
settings["path"] = hex::toUTF8String(this->m_path);
settings["path"] = wolv::util::toUTF8String(this->m_path);
return Provider::storeSettings(settings);
}
@ -365,7 +367,7 @@ namespace hex::plugin::builtin {
std::variant<std::string, i128> DiskProvider::queryInformation(const std::string &category, const std::string &argument) {
if (category == "file_path")
return hex::toUTF8String(this->m_path);
return wolv::util::toUTF8String(this->m_path);
else if (category == "sector_size")
return this->m_sectorSize;
else

View File

@ -9,6 +9,8 @@
#include <hex/helpers/utils.hpp>
#include <hex/helpers/fmt.hpp>
#include <wolv/utils/string.hpp>
#include <nlohmann/json.hpp>
namespace hex::plugin::builtin {
@ -169,13 +171,13 @@ namespace hex::plugin::builtin {
}
std::string FileProvider::getName() const {
return hex::toUTF8String(this->m_path.filename());
return wolv::util::toUTF8String(this->m_path.filename());
}
std::vector<std::pair<std::string, std::string>> FileProvider::getDataDescription() const {
std::vector<std::pair<std::string, std::string>> result;
result.emplace_back("hex.builtin.provider.file.path"_lang, hex::toUTF8String(this->m_path));
result.emplace_back("hex.builtin.provider.file.path"_lang, wolv::util::toUTF8String(this->m_path));
result.emplace_back("hex.builtin.provider.file.size"_lang, hex::toByteString(this->getActualSize()));
if (this->m_fileStats.has_value()) {
@ -189,11 +191,11 @@ namespace hex::plugin::builtin {
std::variant<std::string, i128> FileProvider::queryInformation(const std::string &category, const std::string &argument) {
if (category == "file_path")
return hex::toUTF8String(this->m_path);
return wolv::util::toUTF8String(this->m_path);
else if (category == "file_name")
return hex::toUTF8String(this->m_path.filename());
return wolv::util::toUTF8String(this->m_path.filename());
else if (category == "file_extension")
return hex::toUTF8String(this->m_path.extension());
return wolv::util::toUTF8String(this->m_path.extension());
else if (category == "creation_time")
return this->m_fileStats->st_ctime;
else if (category == "access_time")
@ -253,9 +255,9 @@ namespace hex::plugin::builtin {
nlohmann::json FileProvider::storeSettings(nlohmann::json settings) const {
std::string path;
if (auto projectPath = ProjectFile::getPath(); !projectPath.empty())
path = hex::toUTF8String(std::fs::proximate(this->m_path, projectPath.parent_path()));
path = wolv::util::toUTF8String(std::fs::proximate(this->m_path, projectPath.parent_path()));
if (path.empty())
path = hex::toUTF8String(this->m_path);
path = wolv::util::toUTF8String(this->m_path);
settings["path"] = path;

View File

@ -10,6 +10,7 @@
#include <nlohmann/json.hpp>
#include <wolv/io/file.hpp>
#include <wolv/utils/string.hpp>
namespace hex::plugin::builtin {
@ -222,7 +223,7 @@ namespace hex::plugin::builtin {
}
[[nodiscard]] std::string IntelHexProvider::getName() const {
return hex::format("hex.builtin.provider.intel_hex.name"_lang, hex::toUTF8String(this->m_sourceFilePath.filename()));
return hex::format("hex.builtin.provider.intel_hex.name"_lang, wolv::util::toUTF8String(this->m_sourceFilePath.filename()));
}
bool IntelHexProvider::handleFilePicker() {
@ -274,7 +275,7 @@ namespace hex::plugin::builtin {
}
nlohmann::json IntelHexProvider::storeSettings(nlohmann::json settings) const {
settings["path"] = hex::toUTF8String(this->m_sourceFilePath);
settings["path"] = wolv::util::toUTF8String(this->m_sourceFilePath);
return Provider::storeSettings(settings);
}

View File

@ -7,6 +7,7 @@
#include <wolv/io/file.hpp>
#include <wolv/io/fs.hpp>
#include <wolv/utils/string.hpp>
namespace hex::plugin::builtin {
@ -199,7 +200,7 @@ namespace hex::plugin::builtin {
}
[[nodiscard]] std::string MotorolaSRECProvider::getName() const {
return hex::format("hex.builtin.provider.motorola_srec.name"_lang, hex::toUTF8String(this->m_sourceFilePath.filename()));
return hex::format("hex.builtin.provider.motorola_srec.name"_lang, wolv::util::toUTF8String(this->m_sourceFilePath.filename()));
}
bool MotorolaSRECProvider::handleFilePicker() {

View File

@ -402,7 +402,7 @@ namespace hex::plugin::builtin {
if (ImGui::IconButton(ICON_VS_FOLDER_OPENED, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
return fs::openFileBrowser(fs::DialogMode::Open, { { "TTF Font", "ttf" }, { "OTF Font", "otf" } },
[&](const std::fs::path &path) {
fontPath = hex::toUTF8String(path);
fontPath = wolv::util::toUTF8String(path);
setting = fontPath;
});
}
@ -461,7 +461,7 @@ namespace hex::plugin::builtin {
} else {
for (size_t n = 0; n < userFolders.size(); n++) {
const bool isSelected = (currentItemIndex == n);
if (ImGui::Selectable(hex::toUTF8String(userFolders.at(n)).c_str(), isSelected)) { currentItemIndex = n; }
if (ImGui::Selectable(wolv::util::toUTF8String(userFolders.at(n)).c_str(), isSelected)) { currentItemIndex = n; }
if (isSelected) { ImGui::SetItemDefaultFocus(); }
}
ImGui::EndListBox();
@ -576,7 +576,7 @@ namespace hex::plugin::builtin {
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Resources)) {
auto path = dir / "font.ttf";
if (wolv::io::fs::exists(path)) {
log::info("Loading custom front from {}", hex::toUTF8String(path));
log::info("Loading custom front from {}", wolv::util::toUTF8String(path));
fontFile = path;
break;

View File

@ -619,7 +619,7 @@ namespace hex::plugin::builtin {
try {
auto json = nlohmann::json::parse(response.body);
links.push_back({
hex::toUTF8String(currFile.filename()),
wolv::util::toUTF8String(currFile.filename()),
json["data"]["file"]["url"]["short"],
json["data"]["file"]["metadata"]["size"]["readable"]
});
@ -974,7 +974,7 @@ namespace hex::plugin::builtin {
if (ImGui::BeginListBox("##files", { -FLT_MIN, 10 * ImGui::GetTextLineHeightWithSpacing() })) {
u32 index = 0;
for (auto &file : files) {
if (ImGui::Selectable(hex::toUTF8String(file).c_str(), index == selectedIndex))
if (ImGui::Selectable(wolv::util::toUTF8String(file).c_str(), index == selectedIndex))
selectedIndex = index;
index++;
}
@ -1064,7 +1064,7 @@ namespace hex::plugin::builtin {
wolv::io::File input(file, wolv::io::File::Mode::Read);
if (!input.isValid()) {
View::showErrorPopup(hex::format("hex.builtin.tools.file_tools.combiner.open_input"_lang, hex::toUTF8String(file)));
View::showErrorPopup(hex::format("hex.builtin.tools.file_tools.combiner.open_input"_lang, wolv::util::toUTF8String(file)));
return;
}

View File

@ -150,7 +150,7 @@ namespace hex::plugin::builtin {
ImGui::PushID(index);
bool selected = s_selectableFileIndices.contains(index);
if (ImGui::Selectable(hex::toUTF8String(path.filename()).c_str(), selected)) {
if (ImGui::Selectable(wolv::util::toUTF8String(path.filename()).c_str(), selected)) {
if (!s_selectableFileMultiple) {
s_selectableFileIndices.clear();
s_selectableFileIndices.insert(index);

View File

@ -172,9 +172,9 @@ namespace hex::plugin::builtin {
ImGui::TableNextColumn();
for (auto &path : fs::getDefaultPaths(type, true)){
if(wolv::io::fs::isDirectory(path)){
ImGui::TextUnformatted(hex::toUTF8String(path).c_str());
ImGui::TextUnformatted(wolv::util::toUTF8String(path).c_str());
}else{
ImGui::TextFormattedColored(ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarRed), hex::toUTF8String(path).c_str());
ImGui::TextFormattedColored(ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarRed), wolv::util::toUTF8String(path).c_str());
}
}
}

View File

@ -4,6 +4,8 @@
#include <hex/helpers/logger.hpp>
#include <hex/helpers/utils.hpp>
#include <wolv/utils/string.hpp>
#include <fstream>
#include <filesystem>
#include <nlohmann/json.hpp>
@ -54,7 +56,7 @@ namespace hex::plugin::builtin {
this->m_constants.push_back(constant);
}
} catch (...) {
log::error("Failed to parse constants file {}", hex::toUTF8String(file.path()));
log::error("Failed to parse constants file {}", wolv::util::toUTF8String(file.path()));
continue;
}
}

View File

@ -506,13 +506,13 @@ namespace hex::plugin::builtin {
entries.resize(this->m_possiblePatternFiles.size());
for (u32 i = 0; i < entries.size(); i++) {
entries[i] = hex::toUTF8String(this->m_possiblePatternFiles[i].filename());
entries[i] = wolv::util::toUTF8String(this->m_possiblePatternFiles[i].filename());
}
if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 0))) {
u32 index = 0;
for (auto &path : this->m_possiblePatternFiles) {
if (ImGui::Selectable(hex::toUTF8String(path.filename()).c_str(), index == this->m_selectedPatternFile))
if (ImGui::Selectable(wolv::util::toUTF8String(path.filename()).c_str(), index == this->m_selectedPatternFile))
this->m_selectedPatternFile = index;
index++;
}

View File

@ -80,8 +80,8 @@ namespace hex::plugin::builtin {
auto &extraData = ProviderExtraData::get(provider).yara;
for (auto &[name, path] : extraData.rules) {
data["rules"].push_back({
{ "name", hex::toUTF8String(name) },
{ "path", hex::toUTF8String(path) }
{ "name", wolv::util::toUTF8String(name) },
{ "path", wolv::util::toUTF8String(path) }
});
}
@ -109,7 +109,7 @@ namespace hex::plugin::builtin {
if (ImGui::BeginListBox("##rules", ImVec2(-FLT_MIN, ImGui::GetTextLineHeightWithSpacing() * 5))) {
for (u32 i = 0; i < rules.size(); i++) {
const bool selected = (this->m_selectedRule == i);
if (ImGui::Selectable(hex::toUTF8String(rules[i].first).c_str(), selected)) {
if (ImGui::Selectable(wolv::util::toUTF8String(rules[i].first).c_str(), selected)) {
this->m_selectedRule = i;
}
}
@ -264,7 +264,7 @@ namespace hex::plugin::builtin {
yr_compiler_destroy(compiler);
};
auto currFilePath = hex::toUTF8String(wolv::io::fs::toShortPath(filePath));
auto currFilePath = wolv::util::toUTF8String(wolv::io::fs::toShortPath(filePath));
yr_compiler_set_include_callback(
compiler,