mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 05:27:41 -05:00
sys: Replace printf formatting with libfmt
This commit is contained in:
@@ -22,9 +22,9 @@ namespace hex::plugin::builtin {
|
||||
|
||||
|
||||
if (result.has_value())
|
||||
return hex::format("#%s = %Lf", input.data(), result.value());
|
||||
return hex::format("#{0} = %{1}", input.data(), result.value());
|
||||
else
|
||||
return hex::format("#%s = ???", input.data());
|
||||
return hex::format("#{0} = ???", input.data());
|
||||
});
|
||||
|
||||
hex::ContentRegistry::CommandPaletteCommands::add(
|
||||
|
||||
@@ -32,71 +32,71 @@ namespace hex::plugin::builtin {
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.u8", sizeof(u8), [](auto buffer, auto endian, auto style) {
|
||||
auto format = (style == Style::Decimal) ? "%u" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
|
||||
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
|
||||
auto value = hex::format(format, *reinterpret_cast<u8*>(buffer.data()));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.s8", sizeof(s8), [](auto buffer, auto endian, auto style) {
|
||||
auto format = (style == Style::Decimal) ? "%d" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
|
||||
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
|
||||
auto value = hex::format(format, *reinterpret_cast<s8*>(buffer.data()));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.u16", sizeof(u16), [](auto buffer, auto endian, auto style) {
|
||||
auto format = (style == Style::Decimal) ? "%u" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
|
||||
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
|
||||
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<u16*>(buffer.data()), endian));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.s16", sizeof(s16), [](auto buffer, auto endian, auto style) {
|
||||
auto format = (style == Style::Decimal) ? "%d" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
|
||||
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
|
||||
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<s16*>(buffer.data()), endian));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.u32", sizeof(u32), [](auto buffer, auto endian, auto style) {
|
||||
auto format = (style == Style::Decimal) ? "%u" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
|
||||
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
|
||||
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<u32*>(buffer.data()), endian));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.s32", sizeof(s32), [](auto buffer, auto endian, auto style) {
|
||||
auto format = (style == Style::Decimal) ? "%d" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
|
||||
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
|
||||
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<s32*>(buffer.data()), endian));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.u64", sizeof(u64), [](auto buffer, auto endian, auto style) {
|
||||
auto format = (style == Style::Decimal) ? "%llu" : ((style == Style::Hexadecimal) ? "0x%llX" : "0o%llo");
|
||||
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
|
||||
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<u64*>(buffer.data()), endian));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.s64", sizeof(s64), [](auto buffer, auto endian, auto style) {
|
||||
auto format = (style == Style::Decimal) ? "%lld" : ((style == Style::Hexadecimal) ? "0x%llX" : "0o%llo");
|
||||
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
|
||||
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<s64*>(buffer.data()), endian));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.float", sizeof(float), [](auto buffer, auto endian, auto style) {
|
||||
auto value = hex::format("%e", hex::changeEndianess(*reinterpret_cast<float*>(buffer.data()), endian));
|
||||
auto value = hex::format("{0:E}", hex::changeEndianess(*reinterpret_cast<float*>(buffer.data()), endian));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.double", sizeof(double), [](auto buffer, auto endian, auto style) {
|
||||
auto value = hex::format("%e", hex::changeEndianess(*reinterpret_cast<double*>(buffer.data()), endian));
|
||||
auto value = hex::format("{0:E}", hex::changeEndianess(*reinterpret_cast<float*>(buffer.data()), endian));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.ascii", sizeof(char8_t), [](auto buffer, auto endian, auto style) {
|
||||
auto value = hex::format("'%s'", makePrintable(*reinterpret_cast<char8_t*>(buffer.data())).c_str());
|
||||
auto value = hex::format("'{0}'", makePrintable(*reinterpret_cast<char8_t*>(buffer.data())).c_str());
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.wide", sizeof(char16_t), [](auto buffer, auto endian, auto style) {
|
||||
auto c = *reinterpret_cast<char16_t*>(buffer.data());
|
||||
auto value = hex::format("'%lc'", c == 0 ? '\x01' : hex::changeEndianess(c, endian));
|
||||
auto value = hex::format("'{0}'", c == 0 ? '\x01' : char16_t(hex::changeEndianess(c, endian)));
|
||||
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
|
||||
});
|
||||
|
||||
@@ -109,7 +109,7 @@ namespace hex::plugin::builtin {
|
||||
u8 codepointSize = ImTextCharFromUtf8(&codepoint, utf8Buffer, utf8Buffer + 4);
|
||||
|
||||
std::memcpy(codepointString, &codepoint, std::min(codepointSize, u8(4)));
|
||||
auto value = hex::format("'%s' (U+%04lx)", codepoint == 0xFFFD ? "Invalid" :
|
||||
auto value = hex::format("'{0}' (U+0x{1:04X})", codepoint == 0xFFFD ? "Invalid" :
|
||||
codepoint < 0xFF ? makePrintable(codepoint).c_str() :
|
||||
codepointString,
|
||||
codepoint);
|
||||
@@ -121,11 +121,10 @@ namespace hex::plugin::builtin {
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.time32", sizeof(__time32_t), [](auto buffer, auto endian, auto style) {
|
||||
auto endianAdjustedTime = hex::changeEndianess(*reinterpret_cast<__time32_t*>(buffer.data()), endian);
|
||||
std::tm * ptm = _localtime32(&endianAdjustedTime);
|
||||
char timeBuffer[32];
|
||||
struct tm *ptm = _localtime32(&endianAdjustedTime);
|
||||
std::string value;
|
||||
if (ptm != nullptr && std::strftime(timeBuffer, 32, "%a, %d.%m.%Y %H:%M:%S", ptm))
|
||||
value = timeBuffer;
|
||||
if (ptm != nullptr)
|
||||
value = hex::format("{0:%a, %d.%m.%Y %H:%M:%S}", *ptm);
|
||||
else
|
||||
value = "Invalid";
|
||||
|
||||
@@ -134,11 +133,10 @@ namespace hex::plugin::builtin {
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.time64", sizeof(__time64_t), [](auto buffer, auto endian, auto style) {
|
||||
auto endianAdjustedTime = hex::changeEndianess(*reinterpret_cast<__time64_t*>(buffer.data()), endian);
|
||||
std::tm * ptm = _localtime64(&endianAdjustedTime);
|
||||
char timeBuffer[64];
|
||||
struct tm *ptm = _localtime64(&endianAdjustedTime);
|
||||
std::string value;
|
||||
if (ptm != nullptr && std::strftime(timeBuffer, 64, "%a, %d.%m.%Y %H:%M:%S", ptm))
|
||||
value = timeBuffer;
|
||||
if (ptm != nullptr)
|
||||
value = hex::format("{0:%a, %d.%m.%Y %H:%M:%S}", *ptm);
|
||||
else
|
||||
value = "Invalid";
|
||||
|
||||
@@ -149,11 +147,10 @@ namespace hex::plugin::builtin {
|
||||
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.time", sizeof(time_t), [](auto buffer, auto endian, auto style) {
|
||||
auto endianAdjustedTime = hex::changeEndianess(*reinterpret_cast<time_t*>(buffer.data()), endian);
|
||||
std::tm * ptm = localtime(&endianAdjustedTime);
|
||||
char timeBuffer[64];
|
||||
struct tm *ptm = localtime(&endianAdjustedTime);
|
||||
std::string value;
|
||||
if (ptm != nullptr && std::strftime(timeBuffer, 64, "%a, %d.%m.%Y %H:%M:%S", ptm))
|
||||
value = timeBuffer;
|
||||
if (ptm != nullptr)
|
||||
value = hex::format("{0:%a, %d.%m.%Y %H:%M:%S}", *ptm);
|
||||
else
|
||||
value = "Invalid";
|
||||
|
||||
@@ -165,7 +162,7 @@ namespace hex::plugin::builtin {
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.guid", sizeof(GUID), [](auto buffer, auto endian, auto style) {
|
||||
GUID guid;
|
||||
std::memcpy(&guid, buffer.data(), sizeof(GUID));
|
||||
auto value = hex::format("%s{%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}",
|
||||
auto value = hex::format("{}{{{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}}}",
|
||||
(hex::changeEndianess(guid.data3, endian) >> 12) <= 5 && ((guid.data4[0] >> 4) >= 8 || (guid.data4[0] >> 4) == 0) ? "" : "Invalid ",
|
||||
hex::changeEndianess(guid.data1, endian),
|
||||
hex::changeEndianess(guid.data2, endian),
|
||||
@@ -179,7 +176,7 @@ namespace hex::plugin::builtin {
|
||||
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.rgba8", sizeof(u32), [](auto buffer, auto endian, auto style) {
|
||||
ImColor value(hex::changeEndianess(*reinterpret_cast<u32*>(buffer.data()), endian));
|
||||
|
||||
auto stringValue = hex::format("(0x%02X, 0x%02X, 0x%02X, 0x%02X)", value.Value.x, value.Value.y, value.Value.z, value.Value.w);
|
||||
auto stringValue = hex::format("(0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X})", u8(0xFF * (value.Value.x)), u8(0xFF * (value.Value.y)), u8(0xFF * (value.Value.z)), u8(0xFF * (value.Value.w)));
|
||||
|
||||
return [value, stringValue] {
|
||||
ImGui::ColorButton("##inspectorColor", value,
|
||||
|
||||
@@ -105,7 +105,7 @@ namespace hex::plugin::builtin {
|
||||
auto message = AS_TYPE(ASTNodeStringLiteral, params[1])->getString();
|
||||
|
||||
if (LITERAL_COMPARE(condition, condition == 0))
|
||||
ctx.getConsole().abortEvaluation(hex::format("assert failed \"%s\"", message.data()));
|
||||
ctx.getConsole().abortEvaluation(hex::format("assert failed \"{0}\"", message.data()));
|
||||
|
||||
return nullptr;
|
||||
});
|
||||
@@ -116,7 +116,7 @@ namespace hex::plugin::builtin {
|
||||
auto message = AS_TYPE(ASTNodeStringLiteral, params[1])->getString();
|
||||
|
||||
if (LITERAL_COMPARE(condition, condition == 0))
|
||||
ctx.getConsole().log(LogConsole::Level::Warning, hex::format("assert failed \"%s\"", message.data()));
|
||||
ctx.getConsole().log(LogConsole::Level::Warning, hex::format("assert failed \"{0}\"", message.data()));
|
||||
|
||||
return nullptr;
|
||||
});
|
||||
|
||||
@@ -311,8 +311,8 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
auto base10String = std::to_string(number);
|
||||
auto base16String = hex::format("%X", number);
|
||||
auto base8String = hex::format("%o", number);
|
||||
auto base16String = hex::format("0x{0:X}", number);
|
||||
auto base8String = hex::format("{0:#o}", number);
|
||||
auto base2String = hex::toBinaryString(number);
|
||||
|
||||
std::strncpy(buffer[0], base10String.c_str(), sizeof(buffer[0]));
|
||||
|
||||
@@ -60,10 +60,10 @@ namespace hex::plugin::builtin {
|
||||
{ "hex.common.set", "Set" },
|
||||
|
||||
{ "hex.view.bookmarks.name", "Bookmarks" },
|
||||
{ "hex.view.bookmarks.default_title", "Bookmark [0x%lX - 0x%lX]" },
|
||||
{ "hex.view.bookmarks.default_title", "Bookmark [0x{0:X} - 0x{1:X}]" },
|
||||
{ "hex.view.bookmarks.no_bookmarks", "No bookmarks created yet. Add one with Edit -> Add Bookmark" },
|
||||
{ "hex.view.bookmarks.title.info", "Information" },
|
||||
{ "hex.view.bookmarks.address", "0x%08lx : 0x%08lx (%lu bytes)" },
|
||||
{ "hex.view.bookmarks.address", "0x{0:X} : 0x{1:X} ({2} bytes)" },
|
||||
{ "hex.view.bookmarks.button.jump", "Jump to" },
|
||||
{ "hex.view.bookmarks.button.remove", "Remove" },
|
||||
{ "hex.view.bookmarks.header.name", "Name" },
|
||||
@@ -139,7 +139,7 @@ namespace hex::plugin::builtin {
|
||||
{ "hex.view.hexeditor.save_data", "Save Data" },
|
||||
{ "hex.view.hexeditor.open_base64", "Open Base64 File" },
|
||||
{ "hex.view.hexeditor.load_enconding_file", "Load custom encoding File" },
|
||||
{ "hex.view.hexeditor.page", "Page %d / %d" },
|
||||
{ "hex.view.hexeditor.page", "Page {0} / {1}" },
|
||||
{ "hex.view.hexeditor.save_as", "Save As" },
|
||||
{ "hex.view.hexeditor.save_changes.title", "Save Changes" },
|
||||
{ "hex.view.hexeditor.save_changes.desc", "You have unsaved changes made to your Project.\nAre you sure you want to exit?" },
|
||||
@@ -207,7 +207,7 @@ namespace hex::plugin::builtin {
|
||||
{ "hex.view.information.distribution", "Byte distribution" },
|
||||
{ "hex.view.information.entropy", "Entropy" },
|
||||
{ "hex.view.information.block_size", "Block size" },
|
||||
{ "hex.view.information.block_size.desc", "%lu blocks of %lu bytes" },
|
||||
{ "hex.view.information.block_size.desc", "{0} blocks of {1} bytes" },
|
||||
{ "hex.view.information.file_entropy", "File entropy" },
|
||||
{ "hex.view.information.highest_entropy", "Highest entropy block" },
|
||||
{ "hex.view.information.encrypted", "This data is most likely encrypted or compressed!" },
|
||||
@@ -267,7 +267,7 @@ namespace hex::plugin::builtin {
|
||||
|
||||
{ "hex.builtin.command.calc.desc", "Calculator" },
|
||||
{ "hex.builtin.command.web.desc", "Website lookup" },
|
||||
{ "hex.builtin.command.web.result", "Navigate to '%s'"},
|
||||
{ "hex.builtin.command.web.result", "Navigate to '{0}'"},
|
||||
|
||||
{ "hex.builtin.inspector.binary", "Binary (8 bit)" },
|
||||
{ "hex.builtin.inspector.u8", "uint8_t" },
|
||||
@@ -442,7 +442,7 @@ namespace hex::plugin::builtin {
|
||||
{ "hex.builtin.tools.format.scientific", "Scientific" },
|
||||
{ "hex.builtin.tools.format.engineering", "Engineering" },
|
||||
{ "hex.builtin.tools.format.programmer", "Programmer" },
|
||||
{ "hex.builtin.tools.error", "Last error: '%s'" },
|
||||
{ "hex.builtin.tools.error", "Last error: '{0}'" },
|
||||
{ "hex.builtin.tools.history", "History" },
|
||||
{ "hex.builtin.tools.name", "Name" },
|
||||
{ "hex.builtin.tools.value", "Value" },
|
||||
|
||||
@@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 20)
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/ImGui ${CMAKE_CURRENT_BINARY_DIR}/external/ImGui)
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/nlohmann_json ${CMAKE_CURRENT_BINARY_DIR}/external/nlohmann_json)
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/nativefiledialog ${CMAKE_CURRENT_BINARY_DIR}/external/nativefiledialog)
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/fmt ${CMAKE_CURRENT_BINARY_DIR}/external/fmt)
|
||||
set(XDGPP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../external/xdgpp")
|
||||
|
||||
if (WIN32)
|
||||
@@ -59,10 +60,10 @@ target_include_directories(libimhex PUBLIC include ${MBEDTLS_INCLUDE_DIR} ${XDGP
|
||||
target_link_directories(libimhex PUBLIC ${MBEDTLS_LIBRARY_DIR})
|
||||
|
||||
if (WIN32)
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json libmbedcrypto.a nfd)
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json libmbedcrypto.a nfd fmt)
|
||||
elseif (APPLE)
|
||||
find_library(FOUNDATION NAMES Foundation)
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto ${FOUNDATION} nfd)
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto ${FOUNDATION} nfd fmt)
|
||||
else()
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto nfd)
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto nfd fmt)
|
||||
endif ()
|
||||
@@ -59,7 +59,7 @@ namespace hex::dp {
|
||||
auto attribute = this->getConnectedInputAttribute(index);
|
||||
|
||||
if (attribute == nullptr)
|
||||
throwNodeError(hex::format("Nothing connected to input '%s'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
|
||||
throwNodeError(hex::format("Nothing connected to input '{0}'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
|
||||
|
||||
if (attribute->getType() != Attribute::Type::Buffer)
|
||||
throwNodeError("Tried to read buffer from non-buffer attribute");
|
||||
@@ -78,7 +78,7 @@ namespace hex::dp {
|
||||
auto attribute = this->getConnectedInputAttribute(index);
|
||||
|
||||
if (attribute == nullptr)
|
||||
throwNodeError(hex::format("Nothing connected to input '%s'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
|
||||
throwNodeError(hex::format("Nothing connected to input '{0}'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
|
||||
|
||||
if (attribute->getType() != Attribute::Type::Integer)
|
||||
throwNodeError("Tried to read integer from non-integer attribute");
|
||||
@@ -100,7 +100,7 @@ namespace hex::dp {
|
||||
auto attribute = this->getConnectedInputAttribute(index);
|
||||
|
||||
if (attribute == nullptr)
|
||||
throwNodeError(hex::format("Nothing connected to input '%s'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
|
||||
throwNodeError(hex::format("Nothing connected to input '{0}'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
|
||||
|
||||
if (attribute->getType() != Attribute::Type::Float)
|
||||
throwNodeError("Tried to read float from non-float attribute");
|
||||
|
||||
@@ -11,6 +11,9 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <fmt/chrono.h>
|
||||
|
||||
#ifdef __MINGW32__
|
||||
#include <winsock.h>
|
||||
#else
|
||||
@@ -85,18 +88,13 @@ namespace hex {
|
||||
void openWebpage(std::string_view url);
|
||||
|
||||
template<typename ... Args>
|
||||
inline std::string format(const char *format, Args ... args) {
|
||||
ssize_t size = snprintf( nullptr, 0, format, args ... );
|
||||
inline std::string format(std::string_view format, Args ... args) {
|
||||
return fmt::format(format, args...);
|
||||
}
|
||||
|
||||
if (size <= 0)
|
||||
return "";
|
||||
|
||||
std::vector<char> buffer(size + 1, 0x00);
|
||||
if (snprintf(buffer.data(), size + 1, format, args ...) <= 0)
|
||||
return "";
|
||||
|
||||
|
||||
return std::string(buffer.data(), buffer.data() + size);
|
||||
template<typename ... Args>
|
||||
inline void print(std::string_view format, Args ... args) {
|
||||
fmt::print(format, args...);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const hex::unsigned_integral auto &value) {
|
||||
|
||||
@@ -298,7 +298,7 @@ namespace hex::lang {
|
||||
provider->read(this->getOffset(), &data, this->getSize());
|
||||
data = hex::changeEndianess(data, this->getSize(), this->getEndian());
|
||||
|
||||
this->createDefaultEntry(hex::format("%llu (0x%0*llX)", data, this->getSize() * 2, data));
|
||||
this->createDefaultEntry(hex::format("{:d} (0x{:0{}X})", data, data, this->getSize() * 2));
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string getFormattedName() const override {
|
||||
@@ -329,7 +329,7 @@ namespace hex::lang {
|
||||
|
||||
s64 signedData = hex::signExtend(data, this->getSize(), 64);
|
||||
|
||||
this->createDefaultEntry(hex::format("%lld (0x%0*llX)", signedData, this->getSize() * 2, data));
|
||||
this->createDefaultEntry(hex::format("{:d} (0x{:0{}X})", signedData, data, this->getSize() * 2));
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string getFormattedName() const override {
|
||||
@@ -359,13 +359,13 @@ namespace hex::lang {
|
||||
provider->read(this->getOffset(), &data, 4);
|
||||
data = hex::changeEndianess(data, 4, this->getEndian());
|
||||
|
||||
this->createDefaultEntry(hex::format("%e (0x%0*lX)", *reinterpret_cast<float*>(&data), this->getSize() * 2, data));
|
||||
this->createDefaultEntry(hex::format("{:e} (0x{:0{}X})", *reinterpret_cast<float*>(&data), data, this->getSize() * 2));
|
||||
} else if (this->getSize() == 8) {
|
||||
u64 data = 0;
|
||||
provider->read(this->getOffset(), &data, 8);
|
||||
data = hex::changeEndianess(data, 8, this->getEndian());
|
||||
|
||||
this->createDefaultEntry(hex::format("%e (0x%0*llX)", *reinterpret_cast<double*>(&data), this->getSize() * 2, data));
|
||||
this->createDefaultEntry(hex::format("{:e} (0x{:0{}X})", *reinterpret_cast<double*>(&data), data, this->getSize() * 2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -417,7 +417,7 @@ namespace hex::lang {
|
||||
char character;
|
||||
provider->read(this->getOffset(), &character, 1);
|
||||
|
||||
this->createDefaultEntry(hex::format("'%c'", character));
|
||||
this->createDefaultEntry(hex::format("'{0}'", character));
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string getFormattedName() const override {
|
||||
@@ -439,7 +439,7 @@ namespace hex::lang {
|
||||
provider->read(this->getOffset(), buffer.data(), this->getSize());
|
||||
buffer[this->getSize()] = '\0';
|
||||
|
||||
this->createDefaultEntry(hex::format("\"%s\"", makeDisplayable(buffer.data(), this->getSize()).c_str()));
|
||||
this->createDefaultEntry(hex::format("\"{0}\"", makeDisplayable(buffer.data(), this->getSize()).c_str()));
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string getFormattedName() const override {
|
||||
@@ -757,7 +757,7 @@ namespace hex::lang {
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextColored(ImColor(0xFFD69C56), "enum"); ImGui::SameLine(); ImGui::Text("%s", PatternData::getTypeName().c_str());
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%s", hex::format("%s (0x%0*llX)", valueString.c_str(), this->getSize() * 2, value).c_str());
|
||||
ImGui::Text("%s", hex::format("{} (0x{:0{}X})", valueString.c_str(), value, this->getSize() * 2).c_str());
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string getFormattedName() const override {
|
||||
@@ -803,7 +803,7 @@ namespace hex::lang {
|
||||
|
||||
std::string valueString = "{ ";
|
||||
for (u64 i = 0; i < value.size(); i++)
|
||||
valueString += hex::format("%02x ", value[i]);
|
||||
valueString += hex::format("{0:02X} ", value[i]);
|
||||
valueString += "}";
|
||||
|
||||
ImGui::TextUnformatted(valueString.c_str());
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace hex {
|
||||
break;
|
||||
}
|
||||
|
||||
std::string result = hex::format("%.2f", value);
|
||||
std::string result = hex::format("{0:.2f}", value);
|
||||
|
||||
switch (unitIndex) {
|
||||
case 0: result += " Bytes"; break;
|
||||
@@ -168,11 +168,11 @@ namespace hex {
|
||||
void openWebpage(std::string_view url) {
|
||||
|
||||
#if defined(OS_WINDOWS)
|
||||
system(hex::format("start %s", url.data()).c_str());
|
||||
system(hex::format("start {0}", url.data()).c_str());
|
||||
#elif defined(OS_MACOS)
|
||||
system(hex::format("open %s", url.data()).c_str());
|
||||
system(hex::format("open {0}", url.data()).c_str());
|
||||
#elif defined(OS_LINUX)
|
||||
system(hex::format("xdg-open %s", url.data()).c_str());
|
||||
system(hex::format("xdg-open {0}", url.data()).c_str());
|
||||
#else
|
||||
#warning "Unknown OS, can't open webpages"
|
||||
#endif
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace hex::lang {
|
||||
if (candidate != currMembers.end())
|
||||
currPattern = *candidate;
|
||||
else
|
||||
this->getConsole().abortEvaluation(hex::format("could not find identifier '%s'", identifier.c_str()));
|
||||
this->getConsole().abortEvaluation(hex::format("could not find identifier '{0}'", identifier.c_str()));
|
||||
}
|
||||
|
||||
if (auto pointerPattern = dynamic_cast<PatternDataPointer*>(currPattern); pointerPattern != nullptr)
|
||||
@@ -134,7 +134,7 @@ namespace hex::lang {
|
||||
}
|
||||
|
||||
if (!ContentRegistry::PatternLanguageFunctions::getEntries().contains(node->getFunctionName().data()))
|
||||
this->getConsole().abortEvaluation(hex::format("no function named '%s' found", node->getFunctionName().data()));
|
||||
this->getConsole().abortEvaluation(hex::format("no function named '{0}' found", node->getFunctionName().data()));
|
||||
|
||||
auto &function = ContentRegistry::PatternLanguageFunctions::getEntries()[node->getFunctionName().data()];
|
||||
|
||||
@@ -143,12 +143,12 @@ namespace hex::lang {
|
||||
}
|
||||
else if (function.parameterCount & ContentRegistry::PatternLanguageFunctions::LessParametersThan) {
|
||||
if (evaluatedParams.size() >= (function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::LessParametersThan))
|
||||
this->getConsole().abortEvaluation(hex::format("too many parameters for function '%s'. Expected %d", node->getFunctionName().data(), function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::LessParametersThan));
|
||||
this->getConsole().abortEvaluation(hex::format("too many parameters for function '{0}'. Expected {1}", node->getFunctionName().data(), function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::LessParametersThan));
|
||||
} else if (function.parameterCount & ContentRegistry::PatternLanguageFunctions::MoreParametersThan) {
|
||||
if (evaluatedParams.size() <= (function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::MoreParametersThan))
|
||||
this->getConsole().abortEvaluation(hex::format("too few parameters for function '%s'. Expected %d", node->getFunctionName().data(), function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::MoreParametersThan));
|
||||
this->getConsole().abortEvaluation(hex::format("too few parameters for function '{0}'. Expected {1}", node->getFunctionName().data(), function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::MoreParametersThan));
|
||||
} else if (function.parameterCount != evaluatedParams.size()) {
|
||||
this->getConsole().abortEvaluation(hex::format("invalid number of parameters for function '%s'. Expected %d", node->getFunctionName().data(), function.parameterCount));
|
||||
this->getConsole().abortEvaluation(hex::format("invalid number of parameters for function '{0}'. Expected {1}", node->getFunctionName().data(), function.parameterCount));
|
||||
}
|
||||
|
||||
return function.func(*this, evaluatedParams);
|
||||
@@ -654,7 +654,7 @@ namespace hex::lang {
|
||||
else
|
||||
this->getConsole().abortEvaluation("ASTNodeVariableDecl had an invalid type. This is a bug!");
|
||||
|
||||
entry->setVariableName(hex::format("[%llu]", (u64)i));
|
||||
entry->setVariableName(hex::format("[{0}]", (u64)i));
|
||||
entry->setEndian(this->getCurrentEndian());
|
||||
|
||||
if (!color.has_value())
|
||||
|
||||
@@ -45,14 +45,14 @@ namespace hex::lang {
|
||||
offset += 1;
|
||||
|
||||
if (offset >= code.length())
|
||||
throwPreprocessorError(hex::format("missing terminating '%c' character", endChar), lineNumber);
|
||||
throwPreprocessorError(hex::format("missing terminating '{0}' character", endChar), lineNumber);
|
||||
}
|
||||
offset += 1;
|
||||
|
||||
if (includeFile[0] != '/') {
|
||||
std::string tempPath = includeFile;
|
||||
for (const auto &dir : hex::getPath(ImHexPath::PatternsInclude)) {
|
||||
tempPath = hex::format("%s/%s", dir.c_str(), includeFile.c_str());
|
||||
tempPath = hex::format("{0}/{1}", dir.c_str(), includeFile.c_str());
|
||||
if (std::filesystem::exists(includeFile))
|
||||
break;
|
||||
}
|
||||
@@ -61,7 +61,7 @@ namespace hex::lang {
|
||||
|
||||
FILE *file = fopen(includeFile.c_str(), "r");
|
||||
if (file == nullptr)
|
||||
throwPreprocessorError(hex::format("%s: No such file or directory", includeFile.c_str()), lineNumber);
|
||||
throwPreprocessorError(hex::format("{0}: No such file or directory", includeFile.c_str()), lineNumber);
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
size_t size = ftell(file);
|
||||
@@ -199,9 +199,9 @@ namespace hex::lang {
|
||||
for (const auto &[type, value] : this->m_pragmas) {
|
||||
if (this->m_pragmaHandlers.contains(type)) {
|
||||
if (!this->m_pragmaHandlers[type](value))
|
||||
throwPreprocessorError(hex::format("invalid value provided to '%s' #pragma directive", type.c_str()), lineNumber);
|
||||
throwPreprocessorError(hex::format("invalid value provided to '{0}' #pragma directive", type.c_str()), lineNumber);
|
||||
} else
|
||||
throwPreprocessorError(hex::format("no #pragma handler registered for type %s", type.c_str()), lineNumber);
|
||||
throwPreprocessorError(hex::format("no #pragma handler registered for type {0}", type.c_str()), lineNumber);
|
||||
}
|
||||
}
|
||||
} catch (PreprocessorError &e) {
|
||||
|
||||
@@ -22,12 +22,12 @@ namespace hex::lang {
|
||||
|
||||
if (auto variableDeclNode = dynamic_cast<ASTNodeVariableDecl*>(node); variableDeclNode != nullptr) {
|
||||
if (!identifiers.insert(variableDeclNode->getName().data()).second)
|
||||
throwValidateError(hex::format("redefinition of identifier '%s'", variableDeclNode->getName().data()), variableDeclNode->getLineNumber());
|
||||
throwValidateError(hex::format("redefinition of identifier '{0}'", variableDeclNode->getName().data()), variableDeclNode->getLineNumber());
|
||||
|
||||
this->validate({ variableDeclNode->getType() });
|
||||
} else if (auto typeDeclNode = dynamic_cast<ASTNodeTypeDecl*>(node); typeDeclNode != nullptr) {
|
||||
if (!identifiers.insert(typeDeclNode->getName().data()).second)
|
||||
throwValidateError(hex::format("redefinition of identifier '%s'", typeDeclNode->getName().data()), typeDeclNode->getLineNumber());
|
||||
throwValidateError(hex::format("redefinition of identifier '{0}'", typeDeclNode->getName().data()), typeDeclNode->getLineNumber());
|
||||
|
||||
this->validate({ typeDeclNode->getType() });
|
||||
} else if (auto structNode = dynamic_cast<ASTNodeStruct*>(node); structNode != nullptr) {
|
||||
@@ -38,7 +38,7 @@ namespace hex::lang {
|
||||
std::unordered_set<std::string> enumIdentifiers;
|
||||
for (auto &[name, value] : enumNode->getEntries()) {
|
||||
if (!enumIdentifiers.insert(name).second)
|
||||
throwValidateError(hex::format("redefinition of enum constant '%s'", name.c_str()), value->getLineNumber());
|
||||
throwValidateError(hex::format("redefinition of enum constant '{0}'", name.c_str()), value->getLineNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user