From c45a436fc594101f676cfabe90225d825d935fec Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 30 Oct 2024 20:52:29 +0100 Subject: clean up duplicated code --- backend/print.cpp | 17 ----------------- frontend/Exception.cpp | 18 +++++------------- frontend/Exception.h | 4 +--- 3 files changed, 6 insertions(+), 33 deletions(-) diff --git a/backend/print.cpp b/backend/print.cpp index fc8ad9b..1cb6a75 100644 --- a/backend/print.cpp +++ b/backend/print.cpp @@ -6,23 +6,6 @@ #include "print.h" #include "util.h" -static String va_stringf(va_list args, const char * fmt) { - va_list args_copy; - va_copy(args_copy, args); - - size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1; - char * msg = (char *) malloc(sz); - va_end(args_copy); - - vsnprintf(msg, sz, fmt, args); - - String out = msg; - free(msg); - - va_end(args); - return out; -} - void lprtf(const char * fmt, ...) { va_list args; va_start(args, fmt); diff --git a/frontend/Exception.cpp b/frontend/Exception.cpp index 423f4e9..c852f0a 100644 --- a/frontend/Exception.cpp +++ b/frontend/Exception.cpp @@ -1,30 +1,22 @@ #include #include #include +#include + +#include "backend/String.h" #include "Exception.h" using namespace std; const char * Exception::what() { - return error.get(); -} - -void Exception::va_format(va_list args, const char * fmt) { - va_list args_copy; - va_copy(args_copy, args); - size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1; - va_end(args_copy); - - this->error = unique_ptr(static_cast(malloc(sz))); - - vsnprintf(this->error.get(), sz, fmt, args); + return this->error.get(); } Exception::Exception(const char * fmt, ...) { va_list args; va_start(args, fmt); - va_format(args, fmt); + this->error = unique_ptr(strdup(String::va_fmt(args, fmt).c_str())); va_end(args); } diff --git a/frontend/Exception.h b/frontend/Exception.h index 633cb4f..3bbced2 100644 --- a/frontend/Exception.h +++ b/frontend/Exception.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include @@ -11,7 +10,6 @@ public: protected: Exception() = default; - void va_format(va_list args, const char * fmt); - std::unique_ptr error = NULL; + std::unique_ptr error; }; -- cgit v1.2.3