Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/PluginManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ PluginManagement::loadPlugins(const std::vector<PluginLoadWrapper> &pluginDataLi
plugins.emplace_back(std::move(*metaInfo), PluginLinkInformation::CreateStub(), pluginDataWrapper.getPluginData());
}
} else {
auto errMsg = string_format("Failed to load plugin: %s", *pluginDataWrapper.getPluginData()->getSource().c_str());
auto errMsg = string_format("Failed to load plugin: %s", pluginDataWrapper.getPluginData()->getSource().c_str());
if (error == PLUGIN_PARSE_ERROR_INCOMPATIBLE_VERSION) {
errMsg += ". Incompatible version.";
}
Expand Down
33 changes: 33 additions & 0 deletions source/utils/StringTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,41 @@

#include <string>

#include <memory>

#include <cstdarg>
#include <cstdio>
#include <cstring>

std::string string_format(const char *format, ...) {
char stack_buf[128];
va_list args;
va_start(args, format);
int len = std::vsnprintf(stack_buf, sizeof(stack_buf), format, args);
va_end(args);

if (len >= 0 && static_cast<size_t>(len) < sizeof(stack_buf)) {
return std::string(stack_buf, len);
}

// on error return the unformatted string
if (len < 0) {
return std::string(format);
}

const auto buf = make_unique_nothrow<char[]>(static_cast<size_t>(len));
if (!buf) {
DEBUG_FUNCTION_LINE_ERR("string_format failed, not enough memory");
OSFatal("WiiUPluginBackend: string_format failed, not enough memory");
return std::string("");
}

va_start(args, format);
std::vsnprintf(buf.get(), len, format, args);
va_end(args);
return std::string(buf.get(), len);
}

std::string StringTools::truncate(const std::string &str, const size_t width, const bool show_ellipsis) {
if (str.length() > width - 3) {
if (show_ellipsis) {
Expand Down
18 changes: 2 additions & 16 deletions source/utils/StringTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,10 @@
#include "logger.h"
#include "utils.h"

#include <coreinit/debug.h>

#include <memory>
#include <string>

template<typename... Args>
std::string string_format(const std::string_view format, Args... args) {
const int size_s = std::snprintf(nullptr, 0, format.data(), args...) + 1; // Extra space for '\0'
const auto size = static_cast<size_t>(size_s);
const auto buf = make_unique_nothrow<char[]>(size);
if (!buf) {
DEBUG_FUNCTION_LINE_ERR("string_format failed, not enough memory");
OSFatal("string_format failed, not enough memory");
return std::string("");
}
std::snprintf(buf.get(), size, format.data(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
std::string string_format(const char *format, ...)
WUT_FORMAT_PRINTF(1, 2);

class StringTools {
public:
Expand Down