Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Switched some bare sprintfs to mh::format_string/printf_string.
Browse files Browse the repository at this point in the history
  • Loading branch information
PazerOP committed Aug 7, 2020
1 parent b9e3d28 commit d1b9194
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 39 deletions.
2 changes: 1 addition & 1 deletion submodules/mh_stuff
10 changes: 5 additions & 5 deletions tf2_bot_detector/Config/ChatWrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <vdf_parser.hpp>
#include <cppcoro/generator.hpp>
#include <mh/text/fmtstr.hpp>
#include <mh/text/string_insertion.hpp>
#include <nlohmann/json.hpp>

Expand All @@ -19,6 +20,9 @@
#include <regex>
#include <set>

#undef min
#undef max

using namespace std::string_literals;
using namespace std::string_view_literals;
using namespace tf2_bot_detector;
Expand Down Expand Up @@ -411,12 +415,8 @@ static void PrintChatWrappers(const ChatWrappers& wrappers)
mh::strwrapperstream os(str);
os << std::quoted(wrapper.m_Narrow) << " (";

char buf[16];
for (auto c : wrapper.m_Wide)
{
sprintf_s(buf, "%04X", +c);
os << "\\x" << buf;
}
os << mh::pfstr<64>("\\x%04X", +c);

os << ')';
};
Expand Down
6 changes: 3 additions & 3 deletions tf2_bot_detector/Config/DRPInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "RegexHelpers.h"

#include <mh/text/format.hpp>
#include <mh/text/fmtstr.hpp>
#include <mh/text/stringops.hpp>
#include <mh/text/string_insertion.hpp>
#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -126,9 +127,8 @@ std::string DRPInfo::Map::GetFriendlyName() const

bool DRPInfo::Map::Matches(const std::string_view& mapName) const
{
char buf[512];
sprintf_s(buf, "%s%s", m_MapNames.at(0).c_str(), R"regex(?(?:_(?!.*_)(?:(?:rc)|(?:final)|(?:[abv]))\d*[a-zA-Z]?)?)regex");
const std::regex s_MainRegex(buf, std::regex::icase);
mh::fmtstr<512> buf("{}{}", m_MapNames.at(0).c_str(), R"regex(?(?:_(?!.*_)(?:(?:rc)|(?:final)|(?:[abv]))\d*[a-zA-Z]?)?)regex");
const std::regex s_MainRegex(buf.c_str(), std::regex::icase);

if (std::regex_match(mapName.begin(), mapName.end(), s_MainRegex))
return true;
Expand Down
6 changes: 3 additions & 3 deletions tf2_bot_detector/ConsoleLog/ConsoleLines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "WorldState.h"

#include <mh/text/charconv_helper.hpp>
#include <mh/text/fmtstr.hpp>
#include <mh/text/format.hpp>
#include <mh/text/string_insertion.hpp>
#include <imgui_desktop/ScopeGuards.h>
Expand Down Expand Up @@ -142,9 +143,8 @@ static void ProcessChatMessage(const ChatConsoleLine& msgLine, const IConsoleLin

if (newlineEnd > nonNewlineEnd)
{
char buf[64];
sprintf_s(buf, "(\\n x %zu)", (newlineEnd - nonNewlineEnd));
textFunc(ImVec4(1, 0.5f, 0.5f, 1.0f), buf);
textFunc(ImVec4(1, 0.5f, 0.5f, 1.0f),
mh::fmtstr<64>("(\\n x {})", (newlineEnd - nonNewlineEnd)).c_str());
sameLineFunc();
}

Expand Down
20 changes: 8 additions & 12 deletions tf2_bot_detector/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <misc/cpp/imgui_stdlib.h>
#include <mh/math/interpolation.hpp>
#include <mh/text/case_insensitive_string.hpp>
#include <mh/text/fmtstr.hpp>
#include <mh/text/stringops.hpp>

#include <cassert>
Expand Down Expand Up @@ -918,19 +919,17 @@ void MainWindow::OnDrawServerStats()
ImGui::SameLine(0, 4);

auto& lastSample = m_EdictUsageSamples.back();
char buf[32];
const float percent = float(lastSample.m_UsedEdicts) / lastSample.m_MaxEdicts;
sprintf_s(buf, "%i (%1.0f%%)", lastSample.m_UsedEdicts, percent * 100);
ImGui::ProgressBar(percent, { -1, 0 }, buf);
ImGui::ProgressBar(percent, { -1, 0 },
mh::pfstr<64>("%i (%1.0f%%)", lastSample.m_UsedEdicts, percent * 100).c_str());

ImGui::SetHoverTooltip("%i of %i (%1.1f%%)", lastSample.m_UsedEdicts, lastSample.m_MaxEdicts, percent * 100);
}

if (!m_ServerPingSamples.empty())
{
char buf[64];
sprintf_s(buf, "Average ping: %u", m_ServerPingSamples.back().m_Ping);
ImGui::PlotLines(buf, [&](int idx)
ImGui::PlotLines(mh::fmtstr<64>("Average ping: {}", m_ServerPingSamples.back().m_Ping).c_str(),
[&](int idx)
{
return m_ServerPingSamples[idx].m_Ping;
}, (int)m_ServerPingSamples.size(), 0, nullptr, 0);
Expand Down Expand Up @@ -1015,9 +1014,7 @@ void MainWindow::OnDraw()

if (parseProgress < 0.95f)
{
char overlayStr[64];
sprintf_s(overlayStr, "%1.2f %%", parseProgress * 100);
ImGui::ProgressBar(parseProgress, { 0, 0 }, overlayStr);
ImGui::ProgressBar(parseProgress, { 0, 0 }, mh::pfstr<64>("%1.2f %%", parseProgress * 100).c_str());
ImGui::SameLine(0, 4);
}

Expand Down Expand Up @@ -1112,9 +1109,8 @@ void MainWindow::OnDrawMenuBar()

ImGui::Separator();

char buf[128];
sprintf_s(buf, "Version: %s", VERSION_STRING);
ImGui::MenuItem(buf, nullptr, false, false);
static const mh::fmtstr<128> VERSION_STRING_LABEL("Version: {}", VERSION_STRING);
ImGui::MenuItem(VERSION_STRING_LABEL.c_str(), nullptr, false, false);

if (m_Settings.m_AllowInternetUsage.value_or(false))
{
Expand Down
19 changes: 9 additions & 10 deletions tf2_bot_detector/ModeratorLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "WorldState.h"

#include <mh/text/case_insensitive_string.hpp>
#include <mh/text/fmtstr.hpp>
#include <mh/text/string_insertion.hpp>

#include <codecvt>
Expand Down Expand Up @@ -248,20 +249,19 @@ void ModeratorLogic::HandleConnectedEnemyCheaters(const std::vector<Cheater>& en

if (chatMsgCheaterNames.size() > 0)
{
constexpr char FMT_ONE_CHEATER[] = "Attention! There is a cheater on the other team named %s. Please kick them!";
constexpr char FMT_MULTIPLE_CHEATERS[] = "Attention! There are %u cheaters on the other team named %s. Please kick them!";
constexpr char FMT_ONE_CHEATER[] = "Attention! There is a cheater on the other team named \"{}\". Please kick them!";
constexpr char FMT_MULTIPLE_CHEATERS[] = "Attention! There are {} cheaters on the other team named {}. Please kick them!";
//constexpr char FMT_MANY_CHEATERS[] = "Attention! There are %u cheaters on the other team including %s. Please kick them!";
constexpr size_t MAX_CHATMSG_LENGTH = 127;
constexpr size_t MAX_NAMES_LENGTH_ONE = MAX_CHATMSG_LENGTH - std::size(FMT_ONE_CHEATER) - 1 - 2;
constexpr size_t MAX_NAMES_LENGTH_MULTIPLE = MAX_CHATMSG_LENGTH - std::size(FMT_MULTIPLE_CHEATERS) - 1 - 1 - 2;
//constexpr size_t MAX_NAMES_LENGTH_MANY = MAX_CHATMSG_LENGTH - std::size(FMT_MANY_CHEATERS) - 1 - 1 - 2;

static_assert(MAX_NAMES_LENGTH_ONE >= 32);
std::string chatMsg;
mh::fmtstr<MAX_CHATMSG_LENGTH + 1> chatMsg;
if (chatMsgCheaterNames.size() == 1)
{
chatMsg.resize(256);
chatMsg.resize(sprintf_s(chatMsg.data(), 256, FMT_ONE_CHEATER, chatMsgCheaterNames.front().c_str()));
chatMsg.fmt(FMT_ONE_CHEATER, chatMsgCheaterNames.front());
}
else
{
Expand All @@ -276,8 +276,7 @@ void ModeratorLogic::HandleConnectedEnemyCheaters(const std::vector<Cheater>& en
break;
}

chatMsg.resize(256);
chatMsg.resize(sprintf_s(chatMsg.data(), 256, FMT_MULTIPLE_CHEATERS, chatMsgCheaterNames.size(), cheaters.c_str()));
chatMsg.fmt(FMT_MULTIPLE_CHEATERS, chatMsgCheaterNames.size(), cheaters);
}

assert(chatMsg.size() <= 127);
Expand Down Expand Up @@ -352,14 +351,14 @@ void ModeratorLogic::HandleConnectingEnemyCheaters(const std::vector<Cheater>& c
if (!needsWarning || !m_Settings->m_AutoChatWarnings || !m_Settings->m_AutoChatWarningsConnecting)
return;

char chatMsg[128];
mh::fmtstr<128> chatMsg;
if (connectingEnemyCheaters.size() == 1)
{
strcpy_s(chatMsg, "Heads up! There is a known cheater joining the other team! Name unknown until they fully join.");
chatMsg.puts("Heads up! There is a known cheater joining the other team! Name unknown until they fully join.");
}
else
{
sprintf_s(chatMsg, "Heads up! There are %zu known cheaters joining the other team! Names unknown until they fully join.",
chatMsg.fmt("Heads up! There are {} known cheaters joining the other team! Names unknown until they fully join.",
connectingEnemyCheaters.size());
}

Expand Down
9 changes: 4 additions & 5 deletions tf2_bot_detector/TextUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "TextUtils.h"
#include "Log.h"

#include <mh/text/fmtstr.hpp>
#include <mh/text/string_insertion.hpp>

#include <codecvt>
Expand Down Expand Up @@ -131,12 +132,10 @@ std::string tf2_bot_detector::CollapseNewlines(const std::string_view& input)

const auto smallGroupMsgLength = newlineCount * (std::size("\\n") - 1);

char buf[64];
const auto largeGroupMsgLength = sprintf_s(buf, "(\\n x %zu)", newlineCount);

if (smallGroupMsgLength >= largeGroupMsgLength)
const mh::fmtstr<64> newlineGroupStr("(\\n x %zu)", newlineCount);
if (smallGroupMsgLength >= newlineGroupStr.size())
{
retVal.append(buf);
retVal.append(newlineGroupStr);
}
else
{
Expand Down

0 comments on commit d1b9194

Please sign in to comment.