From 58b62eb3a8732be1740e8017cdba16a6db05b1d3 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sat, 23 Mar 2024 02:20:07 +0500 Subject: [PATCH] format() --- dependencies/kerep | 2 +- src/UsefulException.cpp | 4 --- src/UsefulException.hpp | 4 --- src/format.cpp | 66 +++++++++++++++++++++++++++++++++++++++++ src/format.hpp | 3 ++ src/gui/fonts.cpp | 2 +- src/gui/gui.hpp | 1 + 7 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 src/format.cpp create mode 100644 src/format.hpp diff --git a/dependencies/kerep b/dependencies/kerep index dd5788f..0e370b3 160000 --- a/dependencies/kerep +++ b/dependencies/kerep @@ -1 +1 @@ -Subproject commit dd5788f72a909b602592da7861ba717d529be9f3 +Subproject commit 0e370b31ba98ec85720a4a5295b995348c247d3c diff --git a/src/UsefulException.cpp b/src/UsefulException.cpp index 2f66805..ac39ff9 100644 --- a/src/UsefulException.cpp +++ b/src/UsefulException.cpp @@ -1,8 +1,6 @@ #include "UsefulException.hpp" #include -namespace GraphC { - UsefulException_::UsefulException_(std::string _message, std::string _file, std::string _func, int _line_n) : message(_message), file(_file), function(_func), line_n(_line_n) { @@ -15,5 +13,3 @@ UsefulException_::UsefulException_(std::string _message, std::string _file, std: char const* UsefulException_::what() const noexcept { return complete_text.c_str(); } - -} diff --git a/src/UsefulException.hpp b/src/UsefulException.hpp index 76be2e2..25b4217 100644 --- a/src/UsefulException.hpp +++ b/src/UsefulException.hpp @@ -3,8 +3,6 @@ #include #include -namespace GraphC { - #define UsefulException(MSG) UsefulException_(MSG, __FILE__, __func__, __LINE__) class UsefulException_ : public std::exception { @@ -19,5 +17,3 @@ public: virtual char const* what() const noexcept; }; - -} diff --git a/src/format.cpp b/src/format.cpp new file mode 100644 index 0000000..4e0f47d --- /dev/null +++ b/src/format.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include "UsefulException.hpp" +#include "../dependencies/kerep/src/base/base.h" + +std::string format(const std::string format_str, size_t args_count, ...){ + va_list vl; + va_start(vl, args_count); + std::stringstream ss; + + for(size_t i = 0; i < format_str.length(); i++){ + char c = format_str[i]; + + // format specifier + if(c == '%'){ + c = format_str[++i]; + bool l = false; + while(c == 'l'){ + l = true; + c = format_str[++i]; + } + + switch(c){ + case 'u': + if(l) ss<<(u64)va_arg(vl, u64); + else ss<<(u32)va_arg(vl, u32); + break; + case 'i': + case 'd': + if(l) ss<<(i64)va_arg(vl, i64); + else ss<<(i32)va_arg(vl, i32); + break; + case 'f': + // f32 is promoted to f64 when passed through '...' + ss<<(f64)va_arg(vl, f64); + break; + case 'p': + ss<<(void*)va_arg(vl, void*); + break; + case 'x': + if(l) ss<"; + break; + } + case 'c': + ss<<(char)va_arg(vl,int); + break; + default: + throw UsefulException("invalid format cpecifier"); + } + } + + // regular character + else ss< + +std::string format(const std::string format_str, size_t args_count, ...); diff --git a/src/gui/fonts.cpp b/src/gui/fonts.cpp index 671fb00..862c9d4 100644 --- a/src/gui/fonts.cpp +++ b/src/gui/fonts.cpp @@ -12,7 +12,7 @@ ImFont* _ImFont_LoadEmbedded(const void* data, int data_size, const char* font_n ImGuiIO& io = ImGui::GetIO(); font_size *= dpi; ImFontConfig font_cfg = ImFontConfig(); - sprintf_s(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", font_name, font_size); + ksprintf(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", font_name, font_size); return io.Fonts->AddFontFromMemoryCompressedTTF(data, data_size, font_size, &font_cfg); } diff --git a/src/gui/gui.hpp b/src/gui/gui.hpp index 686a66f..9e1c4a2 100644 --- a/src/gui/gui.hpp +++ b/src/gui/gui.hpp @@ -4,6 +4,7 @@ #include "../../dependencies/SDL2/include/SDL.h" #include "../../dependencies/SDL2/include/SDL_opengl.h" #include "../../dependencies/imgui/imgui.h" +#include "../format.hpp" #include "NodeEditor.hpp" #include "imgui_extensions.hpp" #include "fonts.hpp"