1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-22 18:28:53 -05:00

use ImFormatString instead of sprintf/snprintf

This commit is contained in:
Evan Pezent 2022-01-30 10:19:29 -08:00
parent adfc96810e
commit 86f4dd6e5c
2 changed files with 39 additions and 47 deletions

View File

@ -113,10 +113,6 @@ You can read releases logs https://github.com/epezent/implot/releases for more d
#include <stdlib.h> #include <stdlib.h>
#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. // 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) #if (IMGUI_VERSION_NUM < 18102) && !defined(ImDrawFlags_RoundCornersAll)
#define ImDrawFlags_RoundCornersAll ImDrawCornerFlags_All #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 // Visual Studio warnings
#ifdef _MSC_VER #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 #endif
// Clang/GCC warnings with -Weverything // 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) { if (use_24_hr_clk) {
const int hr = Tm.tm_hour; const int hr = Tm.tm_hour;
switch(fmt) { switch(fmt) {
case ImPlotTimeFmt_Us: return snprintf(buffer, size, ".%03d %03d", ms, us); case ImPlotTimeFmt_Us: return ImFormatString(buffer, size, ".%03d %03d", ms, us);
case ImPlotTimeFmt_SUs: return snprintf(buffer, size, ":%02d.%03d %03d", sec, ms, us); case ImPlotTimeFmt_SUs: return ImFormatString(buffer, size, ":%02d.%03d %03d", sec, ms, us);
case ImPlotTimeFmt_SMs: return snprintf(buffer, size, ":%02d.%03d", sec, ms); case ImPlotTimeFmt_SMs: return ImFormatString(buffer, size, ":%02d.%03d", sec, ms);
case ImPlotTimeFmt_S: return snprintf(buffer, size, ":%02d", sec); case ImPlotTimeFmt_S: return ImFormatString(buffer, size, ":%02d", sec);
case ImPlotTimeFmt_HrMinSMs: return snprintf(buffer, size, "%02d:%02d:%02d.%03d", hr, min, sec, ms); case ImPlotTimeFmt_HrMinSMs: return ImFormatString(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_HrMinS: return ImFormatString(buffer, size, "%02d:%02d:%02d", hr, min, sec);
case ImPlotTimeFmt_HrMin: return snprintf(buffer, size, "%02d:%02d", hr, min); case ImPlotTimeFmt_HrMin: return ImFormatString(buffer, size, "%02d:%02d", hr, min);
case ImPlotTimeFmt_Hr: return snprintf(buffer, size, "%02d:00", hr); case ImPlotTimeFmt_Hr: return ImFormatString(buffer, size, "%02d:00", hr);
default: return 0; 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 char* ap = Tm.tm_hour < 12 ? "am" : "pm";
const int hr = (Tm.tm_hour == 0 || Tm.tm_hour == 12) ? 12 : Tm.tm_hour % 12; const int hr = (Tm.tm_hour == 0 || Tm.tm_hour == 12) ? 12 : Tm.tm_hour % 12;
switch(fmt) { switch(fmt) {
case ImPlotTimeFmt_Us: return snprintf(buffer, size, ".%03d %03d", ms, us); case ImPlotTimeFmt_Us: return ImFormatString(buffer, size, ".%03d %03d", ms, us);
case ImPlotTimeFmt_SUs: return snprintf(buffer, size, ":%02d.%03d %03d", sec, ms, us); case ImPlotTimeFmt_SUs: return ImFormatString(buffer, size, ":%02d.%03d %03d", sec, ms, us);
case ImPlotTimeFmt_SMs: return snprintf(buffer, size, ":%02d.%03d", sec, ms); case ImPlotTimeFmt_SMs: return ImFormatString(buffer, size, ":%02d.%03d", sec, ms);
case ImPlotTimeFmt_S: return snprintf(buffer, size, ":%02d", sec); case ImPlotTimeFmt_S: return ImFormatString(buffer, size, ":%02d", sec);
case ImPlotTimeFmt_HrMinSMs: return snprintf(buffer, size, "%d:%02d:%02d.%03d%s", hr, min, sec, ms, ap); case ImPlotTimeFmt_HrMinSMs: return ImFormatString(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_HrMinS: return ImFormatString(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_HrMin: return ImFormatString(buffer, size, "%d:%02d%s", hr, min, ap);
case ImPlotTimeFmt_Hr: return snprintf(buffer, size, "%d%s", hr, ap); case ImPlotTimeFmt_Hr: return ImFormatString(buffer, size, "%d%s", hr, ap);
default: return 0; default: return 0;
} }
} }
@ -1035,21 +1031,21 @@ int FormatDate(const ImPlotTime& t, char* buffer, int size, ImPlotDateFmt fmt, b
const int yr = year % 100; const int yr = year % 100;
if (use_iso_8601) { if (use_iso_8601) {
switch (fmt) { switch (fmt) {
case ImPlotDateFmt_DayMo: return snprintf(buffer, size, "--%02d-%02d", mon, day); case ImPlotDateFmt_DayMo: return ImFormatString(buffer, size, "--%02d-%02d", mon, day);
case ImPlotDateFmt_DayMoYr: return snprintf(buffer, size, "%d-%02d-%02d", year, mon, day); case ImPlotDateFmt_DayMoYr: return ImFormatString(buffer, size, "%d-%02d-%02d", year, mon, day);
case ImPlotDateFmt_MoYr: return snprintf(buffer, size, "%d-%02d", year, mon); case ImPlotDateFmt_MoYr: return ImFormatString(buffer, size, "%d-%02d", year, mon);
case ImPlotDateFmt_Mo: return snprintf(buffer, size, "--%02d", mon); case ImPlotDateFmt_Mo: return ImFormatString(buffer, size, "--%02d", mon);
case ImPlotDateFmt_Yr: return snprintf(buffer, size, "%d", year); case ImPlotDateFmt_Yr: return ImFormatString(buffer, size, "%d", year);
default: return 0; default: return 0;
} }
} }
else { else {
switch (fmt) { switch (fmt) {
case ImPlotDateFmt_DayMo: return snprintf(buffer, size, "%d/%d", mon, day); case ImPlotDateFmt_DayMo: return ImFormatString(buffer, size, "%d/%d", mon, day);
case ImPlotDateFmt_DayMoYr: return snprintf(buffer, size, "%d/%d/%02d", mon, day, yr); case ImPlotDateFmt_DayMoYr: return ImFormatString(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_MoYr: return ImFormatString(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_Mo: return ImFormatString(buffer, size, "%s", MONTH_ABRVS[Tm.tm_mon]);
case ImPlotDateFmt_Yr: return snprintf(buffer, size, "%d", year); case ImPlotDateFmt_Yr: return ImFormatString(buffer, size, "%d", year);
default: return 0; default: return 0;
} }
} }
@ -1450,7 +1446,7 @@ void ShowPlotContextMenu(ImPlotPlot& plot) {
if (!x_axis.Enabled || !x_axis.HasMenus()) if (!x_axis.Enabled || !x_axis.HasMenus())
continue; continue;
ImGui::PushID(i); 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)) { if (ImGui::BeginMenu(x_axis.HasLabel() ? plot.GetAxisLabel(x_axis) : buf)) {
ShowAxisContextMenu(x_axis, equal ? x_axis.OrthoAxis : NULL, false); ShowAxisContextMenu(x_axis, equal ? x_axis.OrthoAxis : NULL, false);
ImGui::EndMenu(); ImGui::EndMenu();
@ -1463,7 +1459,7 @@ void ShowPlotContextMenu(ImPlotPlot& plot) {
if (!y_axis.Enabled || !y_axis.HasMenus()) if (!y_axis.Enabled || !y_axis.HasMenus())
continue; continue;
ImGui::PushID(i); 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)) { if (ImGui::BeginMenu(y_axis.HasLabel() ? plot.GetAxisLabel(y_axis) : buf)) {
ShowAxisContextMenu(y_axis, equal ? y_axis.OrthoAxis : NULL, false); ShowAxisContextMenu(y_axis, equal ? y_axis.OrthoAxis : NULL, false);
ImGui::EndMenu(); ImGui::EndMenu();
@ -1517,7 +1513,7 @@ void ShowPlotContextMenu(ImPlotPlot& plot) {
static inline void DefaultFormatter(double value, char* buff, int size, void* data) { static inline void DefaultFormatter(double value, char* buff, int size, void* data) {
char* fmt = (char*)data; char* fmt = (char*)data;
snprintf(buff, size, fmt, value); ImFormatString(buff, size, fmt, value);
} }
static inline int AxisPrecision(const ImPlotAxis& axis) { static inline int AxisPrecision(const ImPlotAxis& axis) {
@ -5035,14 +5031,14 @@ void ShowMetricsWindow(bool* p_popen) {
} }
char buff[16]; char buff[16];
for (int i = 0; i < IMPLOT_NUM_X_AXES; ++i) { 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)) { if (plot.XAxis(i).Enabled && ImGui::TreeNode(buff, "X-Axis %d [0x%08X]", i+1, plot.XAxis(i).ID)) {
ShowAxisMetrics(plot, plot.XAxis(i)); ShowAxisMetrics(plot, plot.XAxis(i));
ImGui::TreePop(); ImGui::TreePop();
} }
} }
for (int i = 0; i < IMPLOT_NUM_Y_AXES; ++i) { 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)) { if (plot.YAxis(i).Enabled && ImGui::TreeNode(buff, "Y-Axis %d [0x%08X]", i+1, plot.YAxis(i).ID)) {
ShowAxisMetrics(plot, plot.YAxis(i)); ShowAxisMetrics(plot, plot.YAxis(i));
ImGui::TreePop(); ImGui::TreePop();
@ -5199,7 +5195,7 @@ bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime*
GetTime(t_first_mo,&Tm); GetTime(t_first_mo,&Tm);
const int first_wd = Tm.tm_wday; const int first_wd = Tm.tm_wday;
// month year // 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)) if (ImGui::Button(buff))
*level = 1; *level = 1;
ImGui::SameLine(5*cell_size.x); 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::PushStyleColor(ImGuiCol_Text, col_txt);
} }
ImGui::PushID(i*7+j); 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) { if (now_yr == min_yr-1 || now_yr == max_yr+1) {
ImGui::Dummy(cell_size); ImGui::Dummy(cell_size);
} }
@ -5271,7 +5267,7 @@ bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime*
*t = FloorTime(*t, ImPlotTimeUnit_Mo); *t = FloorTime(*t, ImPlotTimeUnit_Mo);
GetTime(*t, &Tm); GetTime(*t, &Tm);
int this_yr = Tm.tm_year + 1900; int this_yr = Tm.tm_year + 1900;
snprintf(buff, 32, "%d", this_yr); ImFormatString(buff, 32, "%d", this_yr);
if (ImGui::Button(buff)) if (ImGui::Button(buff))
*level = 2; *level = 2;
BeginDisabledControls(this_yr <= min_yr); 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 this_yr = GetYear(*t);
int yr = this_yr - this_yr % 20; int yr = this_yr - this_yr % 20;
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true); 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::Button(buff);
ImGui::PopItemFlag(); ImGui::PopItemFlag();
ImGui::SameLine(5*cell_size.x); 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); const bool t1_or_t2 = (t1 != NULL && t1_yr == yr) || (t2 != NULL && t2_yr == yr);
if (t1_or_t2) if (t1_or_t2)
ImGui::PushStyleColor(ImGuiCol_Button, col_btn); ImGui::PushStyleColor(ImGuiCol_Button, col_btn);
snprintf(buff,32,"%d",yr); ImFormatString(buff,32,"%d",yr);
if (yr<1970||yr>3000) { if (yr<1970||yr>3000) {
ImGui::Dummy(cell_size); ImGui::Dummy(cell_size);
} }

View File

@ -25,10 +25,6 @@
#include "implot.h" #include "implot.h"
#include "implot_internal.h" #include "implot_internal.h"
#ifdef _MSC_VER
#define sprintf sprintf_s
#endif
#define SQRT_1_2 0.70710678118f #define SQRT_1_2 0.70710678118f
#define SQRT_3_2 0.86602540378f #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]; double percent = normalize ? (double)values[i] / sum : (double)values[i];
a1 = a0 + 2 * IM_PI * percent; a1 = a0 + 2 * IM_PI * percent;
if (item->Show) { if (item->Show) {
snprintf(buffer, 32, fmt, (double)values[i]); ImFormatString(buffer, 32, fmt, (double)values[i]);
ImVec2 size = ImGui::CalcTextSize(buffer); ImVec2 size = ImGui::CalcTextSize(buffer);
double angle = a0 + (a1 - a0) * 0.5; 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); 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); p.y = yref + ydir * (0.5*h + r*h);
ImVec2 px = transformer(p); ImVec2 px = transformer(p);
char buff[32]; char buff[32];
sprintf(buff, 32, fmt, values[i]); ImFormatString(buff, 32, fmt, values[i]);
ImVec2 size = ImGui::CalcTextSize(buff); ImVec2 size = ImGui::CalcTextSize(buff);
double t = ImClamp(ImRemap01((double)values[i], scale_min, scale_max),0.0,1.0); double t = ImClamp(ImRemap01((double)values[i], scale_min, scale_max),0.0,1.0);
ImVec4 color = SampleColormap((float)t); ImVec4 color = SampleColormap((float)t);