From 86f4dd6e5c0f94878b4190565409877ab20d9059 Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Sun, 30 Jan 2022 10:19:29 -0800 Subject: [PATCH] use ImFormatString instead of sprintf/snprintf --- implot.cpp | 78 +++++++++++++++++++++++------------------------- implot_items.cpp | 8 ++--- 2 files changed, 39 insertions(+), 47 deletions(-) diff --git a/implot.cpp b/implot.cpp index 903d8cf..3a447eb 100644 --- a/implot.cpp +++ b/implot.cpp @@ -113,10 +113,6 @@ You can read releases logs https://github.com/epezent/implot/releases for more d #include -#ifdef _MSC_VER -#define sprintf sprintf_s -#endif - // Support for pre-1.82 versions. Users on 1.82+ can use 0 (default) flags to mean "all corners" but in order to support older versions we are more explicit. #if (IMGUI_VERSION_NUM < 18102) && !defined(ImDrawFlags_RoundCornersAll) #define ImDrawFlags_RoundCornersAll ImDrawCornerFlags_All @@ -124,7 +120,7 @@ You can read releases logs https://github.com/epezent/implot/releases for more d // Visual Studio warnings #ifdef _MSC_VER -#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vImFormatString, sscanf, fopen #endif // Clang/GCC warnings with -Weverything @@ -998,14 +994,14 @@ int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt, b if (use_24_hr_clk) { const int hr = Tm.tm_hour; switch(fmt) { - case ImPlotTimeFmt_Us: return snprintf(buffer, size, ".%03d %03d", ms, us); - case ImPlotTimeFmt_SUs: return snprintf(buffer, size, ":%02d.%03d %03d", sec, ms, us); - case ImPlotTimeFmt_SMs: return snprintf(buffer, size, ":%02d.%03d", sec, ms); - case ImPlotTimeFmt_S: return snprintf(buffer, size, ":%02d", sec); - case ImPlotTimeFmt_HrMinSMs: return snprintf(buffer, size, "%02d:%02d:%02d.%03d", hr, min, sec, ms); - case ImPlotTimeFmt_HrMinS: return snprintf(buffer, size, "%02d:%02d:%02d", hr, min, sec); - case ImPlotTimeFmt_HrMin: return snprintf(buffer, size, "%02d:%02d", hr, min); - case ImPlotTimeFmt_Hr: return snprintf(buffer, size, "%02d:00", hr); + case ImPlotTimeFmt_Us: return ImFormatString(buffer, size, ".%03d %03d", ms, us); + case ImPlotTimeFmt_SUs: return ImFormatString(buffer, size, ":%02d.%03d %03d", sec, ms, us); + case ImPlotTimeFmt_SMs: return ImFormatString(buffer, size, ":%02d.%03d", sec, ms); + case ImPlotTimeFmt_S: return ImFormatString(buffer, size, ":%02d", sec); + case ImPlotTimeFmt_HrMinSMs: return ImFormatString(buffer, size, "%02d:%02d:%02d.%03d", hr, min, sec, ms); + case ImPlotTimeFmt_HrMinS: return ImFormatString(buffer, size, "%02d:%02d:%02d", hr, min, sec); + case ImPlotTimeFmt_HrMin: return ImFormatString(buffer, size, "%02d:%02d", hr, min); + case ImPlotTimeFmt_Hr: return ImFormatString(buffer, size, "%02d:00", hr); default: return 0; } } @@ -1013,14 +1009,14 @@ int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt, b const char* ap = Tm.tm_hour < 12 ? "am" : "pm"; const int hr = (Tm.tm_hour == 0 || Tm.tm_hour == 12) ? 12 : Tm.tm_hour % 12; switch(fmt) { - case ImPlotTimeFmt_Us: return snprintf(buffer, size, ".%03d %03d", ms, us); - case ImPlotTimeFmt_SUs: return snprintf(buffer, size, ":%02d.%03d %03d", sec, ms, us); - case ImPlotTimeFmt_SMs: return snprintf(buffer, size, ":%02d.%03d", sec, ms); - case ImPlotTimeFmt_S: return snprintf(buffer, size, ":%02d", sec); - case ImPlotTimeFmt_HrMinSMs: return snprintf(buffer, size, "%d:%02d:%02d.%03d%s", hr, min, sec, ms, ap); - case ImPlotTimeFmt_HrMinS: return snprintf(buffer, size, "%d:%02d:%02d%s", hr, min, sec, ap); - case ImPlotTimeFmt_HrMin: return snprintf(buffer, size, "%d:%02d%s", hr, min, ap); - case ImPlotTimeFmt_Hr: return snprintf(buffer, size, "%d%s", hr, ap); + case ImPlotTimeFmt_Us: return ImFormatString(buffer, size, ".%03d %03d", ms, us); + case ImPlotTimeFmt_SUs: return ImFormatString(buffer, size, ":%02d.%03d %03d", sec, ms, us); + case ImPlotTimeFmt_SMs: return ImFormatString(buffer, size, ":%02d.%03d", sec, ms); + case ImPlotTimeFmt_S: return ImFormatString(buffer, size, ":%02d", sec); + case ImPlotTimeFmt_HrMinSMs: return ImFormatString(buffer, size, "%d:%02d:%02d.%03d%s", hr, min, sec, ms, ap); + case ImPlotTimeFmt_HrMinS: return ImFormatString(buffer, size, "%d:%02d:%02d%s", hr, min, sec, ap); + case ImPlotTimeFmt_HrMin: return ImFormatString(buffer, size, "%d:%02d%s", hr, min, ap); + case ImPlotTimeFmt_Hr: return ImFormatString(buffer, size, "%d%s", hr, ap); default: return 0; } } @@ -1035,21 +1031,21 @@ int FormatDate(const ImPlotTime& t, char* buffer, int size, ImPlotDateFmt fmt, b const int yr = year % 100; if (use_iso_8601) { switch (fmt) { - case ImPlotDateFmt_DayMo: return snprintf(buffer, size, "--%02d-%02d", mon, day); - case ImPlotDateFmt_DayMoYr: return snprintf(buffer, size, "%d-%02d-%02d", year, mon, day); - case ImPlotDateFmt_MoYr: return snprintf(buffer, size, "%d-%02d", year, mon); - case ImPlotDateFmt_Mo: return snprintf(buffer, size, "--%02d", mon); - case ImPlotDateFmt_Yr: return snprintf(buffer, size, "%d", year); + case ImPlotDateFmt_DayMo: return ImFormatString(buffer, size, "--%02d-%02d", mon, day); + case ImPlotDateFmt_DayMoYr: return ImFormatString(buffer, size, "%d-%02d-%02d", year, mon, day); + case ImPlotDateFmt_MoYr: return ImFormatString(buffer, size, "%d-%02d", year, mon); + case ImPlotDateFmt_Mo: return ImFormatString(buffer, size, "--%02d", mon); + case ImPlotDateFmt_Yr: return ImFormatString(buffer, size, "%d", year); default: return 0; } } else { switch (fmt) { - case ImPlotDateFmt_DayMo: return snprintf(buffer, size, "%d/%d", mon, day); - case ImPlotDateFmt_DayMoYr: return snprintf(buffer, size, "%d/%d/%02d", mon, day, yr); - case ImPlotDateFmt_MoYr: return snprintf(buffer, size, "%s %d", MONTH_ABRVS[Tm.tm_mon], year); - case ImPlotDateFmt_Mo: return snprintf(buffer, size, "%s", MONTH_ABRVS[Tm.tm_mon]); - case ImPlotDateFmt_Yr: return snprintf(buffer, size, "%d", year); + case ImPlotDateFmt_DayMo: return ImFormatString(buffer, size, "%d/%d", mon, day); + case ImPlotDateFmt_DayMoYr: return ImFormatString(buffer, size, "%d/%d/%02d", mon, day, yr); + case ImPlotDateFmt_MoYr: return ImFormatString(buffer, size, "%s %d", MONTH_ABRVS[Tm.tm_mon], year); + case ImPlotDateFmt_Mo: return ImFormatString(buffer, size, "%s", MONTH_ABRVS[Tm.tm_mon]); + case ImPlotDateFmt_Yr: return ImFormatString(buffer, size, "%d", year); default: return 0; } } @@ -1450,7 +1446,7 @@ void ShowPlotContextMenu(ImPlotPlot& plot) { if (!x_axis.Enabled || !x_axis.HasMenus()) continue; ImGui::PushID(i); - snprintf(buf, sizeof(buf) - 1, i == 0 ? "X-Axis" : "X-Axis %d", i + 1); + ImFormatString(buf, sizeof(buf) - 1, i == 0 ? "X-Axis" : "X-Axis %d", i + 1); if (ImGui::BeginMenu(x_axis.HasLabel() ? plot.GetAxisLabel(x_axis) : buf)) { ShowAxisContextMenu(x_axis, equal ? x_axis.OrthoAxis : NULL, false); ImGui::EndMenu(); @@ -1463,7 +1459,7 @@ void ShowPlotContextMenu(ImPlotPlot& plot) { if (!y_axis.Enabled || !y_axis.HasMenus()) continue; ImGui::PushID(i); - snprintf(buf, sizeof(buf) - 1, i == 0 ? "Y-Axis" : "Y-Axis %d", i + 1); + ImFormatString(buf, sizeof(buf) - 1, i == 0 ? "Y-Axis" : "Y-Axis %d", i + 1); if (ImGui::BeginMenu(y_axis.HasLabel() ? plot.GetAxisLabel(y_axis) : buf)) { ShowAxisContextMenu(y_axis, equal ? y_axis.OrthoAxis : NULL, false); ImGui::EndMenu(); @@ -1517,7 +1513,7 @@ void ShowPlotContextMenu(ImPlotPlot& plot) { static inline void DefaultFormatter(double value, char* buff, int size, void* data) { char* fmt = (char*)data; - snprintf(buff, size, fmt, value); + ImFormatString(buff, size, fmt, value); } static inline int AxisPrecision(const ImPlotAxis& axis) { @@ -5035,14 +5031,14 @@ void ShowMetricsWindow(bool* p_popen) { } char buff[16]; for (int i = 0; i < IMPLOT_NUM_X_AXES; ++i) { - snprintf(buff,16,"X-Axis %d", i+1); + ImFormatString(buff,16,"X-Axis %d", i+1); if (plot.XAxis(i).Enabled && ImGui::TreeNode(buff, "X-Axis %d [0x%08X]", i+1, plot.XAxis(i).ID)) { ShowAxisMetrics(plot, plot.XAxis(i)); ImGui::TreePop(); } } for (int i = 0; i < IMPLOT_NUM_Y_AXES; ++i) { - snprintf(buff,16,"Y-Axis %d", i+1); + ImFormatString(buff,16,"Y-Axis %d", i+1); if (plot.YAxis(i).Enabled && ImGui::TreeNode(buff, "Y-Axis %d [0x%08X]", i+1, plot.YAxis(i).ID)) { ShowAxisMetrics(plot, plot.YAxis(i)); ImGui::TreePop(); @@ -5199,7 +5195,7 @@ bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime* GetTime(t_first_mo,&Tm); const int first_wd = Tm.tm_wday; // month year - snprintf(buff, 32, "%s %d", MONTH_NAMES[this_mon], this_year); + ImFormatString(buff, 32, "%s %d", MONTH_NAMES[this_mon], this_year); if (ImGui::Button(buff)) *level = 1; ImGui::SameLine(5*cell_size.x); @@ -5247,7 +5243,7 @@ bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime* ImGui::PushStyleColor(ImGuiCol_Text, col_txt); } ImGui::PushID(i*7+j); - snprintf(buff,32,"%d",day); + ImFormatString(buff,32,"%d",day); if (now_yr == min_yr-1 || now_yr == max_yr+1) { ImGui::Dummy(cell_size); } @@ -5271,7 +5267,7 @@ bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime* *t = FloorTime(*t, ImPlotTimeUnit_Mo); GetTime(*t, &Tm); int this_yr = Tm.tm_year + 1900; - snprintf(buff, 32, "%d", this_yr); + ImFormatString(buff, 32, "%d", this_yr); if (ImGui::Button(buff)) *level = 2; BeginDisabledControls(this_yr <= min_yr); @@ -5311,7 +5307,7 @@ bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime* int this_yr = GetYear(*t); int yr = this_yr - this_yr % 20; ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true); - snprintf(buff,32,"%d-%d",yr,yr+19); + ImFormatString(buff,32,"%d-%d",yr,yr+19); ImGui::Button(buff); ImGui::PopItemFlag(); ImGui::SameLine(5*cell_size.x); @@ -5332,7 +5328,7 @@ bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime* const bool t1_or_t2 = (t1 != NULL && t1_yr == yr) || (t2 != NULL && t2_yr == yr); if (t1_or_t2) ImGui::PushStyleColor(ImGuiCol_Button, col_btn); - snprintf(buff,32,"%d",yr); + ImFormatString(buff,32,"%d",yr); if (yr<1970||yr>3000) { ImGui::Dummy(cell_size); } diff --git a/implot_items.cpp b/implot_items.cpp index 6177a4a..ae750c9 100644 --- a/implot_items.cpp +++ b/implot_items.cpp @@ -25,10 +25,6 @@ #include "implot.h" #include "implot_internal.h" -#ifdef _MSC_VER -#define sprintf sprintf_s -#endif - #define SQRT_1_2 0.70710678118f #define SQRT_3_2 0.86602540378f @@ -1906,7 +1902,7 @@ void PlotPieChart(const char* const label_ids[], const T* values, int count, dou double percent = normalize ? (double)values[i] / sum : (double)values[i]; a1 = a0 + 2 * IM_PI * percent; if (item->Show) { - snprintf(buffer, 32, fmt, (double)values[i]); + ImFormatString(buffer, 32, fmt, (double)values[i]); ImVec2 size = ImGui::CalcTextSize(buffer); double angle = a0 + (a1 - a0) * 0.5; ImVec2 pos = PlotToPixels(center.x + 0.5 * radius * cos(angle), center.y + 0.5 * radius * sin(angle),IMPLOT_AUTO,IMPLOT_AUTO); @@ -2060,7 +2056,7 @@ void RenderHeatmap(Transformer transformer, ImDrawList& DrawList, const T* value p.y = yref + ydir * (0.5*h + r*h); ImVec2 px = transformer(p); char buff[32]; - sprintf(buff, 32, fmt, values[i]); + ImFormatString(buff, 32, fmt, values[i]); ImVec2 size = ImGui::CalcTextSize(buff); double t = ImClamp(ImRemap01((double)values[i], scale_min, scale_max),0.0,1.0); ImVec4 color = SampleColormap((float)t);