Fix sprintf() warnings using snprintf() or fmt::format()

This commit is contained in:
David Capello 2023-09-22 19:14:36 -03:00
parent 5e34ae0ce2
commit 63fb39e0e8
21 changed files with 150 additions and 160 deletions

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2018-2019 Igara Studio S.A. // Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -21,6 +21,7 @@
#include "doc/mask.h" #include "doc/mask.h"
#include "doc/palette.h" #include "doc/palette.h"
#include "doc/sprite.h" #include "doc/sprite.h"
#include "fmt/format.h"
#include <cstdio> #include <cstdio>
@ -80,11 +81,13 @@ void NewSpriteFromSelectionCommand::onExecute(Context* context)
std::unique_ptr<Doc> dstDoc(new Doc(dstSprite.get())); std::unique_ptr<Doc> dstDoc(new Doc(dstSprite.get()));
dstSprite.release(); dstSprite.release();
char buf[1024];
std::sprintf(buf, "%s-%dx%d-%dx%d", const std::string buf =
base::get_file_title(doc->filename()).c_str(), fmt::format("{}-{}x{}-{}x{}",
mask->bounds().x, mask->bounds().y, base::get_file_title(doc->filename()),
mask->bounds().w, mask->bounds().h); mask->bounds().x, mask->bounds().y,
mask->bounds().w, mask->bounds().h);
dstDoc->setFilename(buf); dstDoc->setFilename(buf);
dstDoc->setContext(context); dstDoc->setContext(context);
dstDoc.release(); dstDoc.release();

View File

@ -16,6 +16,7 @@
#include "app/modules/gui.h" #include "app/modules/gui.h"
#include "app/ui/skin/skin_theme.h" #include "app/ui/skin/skin_theme.h"
#include "doc/image.h" #include "doc/image.h"
#include "fmt/format.h"
#include "ui/box.h" #include "ui/box.h"
#include "ui/button.h" #include "ui/button.h"
#include "ui/theme.h" #include "ui/theme.h"
@ -115,10 +116,10 @@ void FilterTargetButtons::updateFromCelsTarget()
void FilterTargetButtons::updateComponentTooltip(Item* item, const char* channelName, int align) void FilterTargetButtons::updateComponentTooltip(Item* item, const char* channelName, int align)
{ {
if (item) { if (item) {
char buf[256]; std::string buf =
std::sprintf(buf, "%s %s Component", fmt::format("{} {} Component",
(item->isSelected() ? "Modify": "Ignore"), (item->isSelected() ? "Modify": "Ignore"),
channelName); channelName);
m_tooltips.addTooltipFor(item, buf, align); m_tooltips.addTooltipFor(item, buf, align);
} }
} }

View File

@ -19,6 +19,7 @@
#include "base/fs.h" #include "base/fs.h"
#include "base/thread.h" #include "base/thread.h"
#include "base/time.h" #include "base/time.h"
#include "fmt/format.h"
#include "ui/system.h" #include "ui/system.h"
#include <algorithm> #include <algorithm>
@ -55,10 +56,10 @@ DataRecovery::DataRecovery(Context* ctx)
do { do {
base::Time time = base::current_time(); base::Time time = base::current_time();
char buf[1024]; std::string buf =
sprintf(buf, "%04d%02d%02d-%02d%02d%02d-%d", fmt::format("{:04}{:02}{:02}-{:02}{:02}{:02}-{}",
time.year, time.month, time.day, time.year, time.month, time.day,
time.hour, time.minute, time.second, pid); time.hour, time.minute, time.second, pid);
newSessionDir = base::join_path(m_sessionsDir, buf); newSessionDir = base::join_path(m_sessionsDir, buf);

View File

@ -401,7 +401,7 @@ FileOp* FileOp::createLoadDocumentOperation(Context* context,
if (!(flags & FILE_LOAD_SEQUENCE_NONE)) { if (!(flags & FILE_LOAD_SEQUENCE_NONE)) {
std::string left, right; std::string left, right;
int c, width, start_from; int c, width, start_from;
char buf[512]; std::string buf;
// First of all, we must generate the list of files to load in the // First of all, we must generate the list of files to load in the
// sequence... // sequence...
@ -412,7 +412,7 @@ FileOp* FileOp::createLoadDocumentOperation(Context* context,
// Try to get more file names // Try to get more file names
for (c=start_from+1; ; c++) { for (c=start_from+1; ; c++) {
// Get the next file name // Get the next file name
sprintf(buf, "%s%0*d%s", left.c_str(), width, c, right.c_str()); buf = fmt::format("{0}{1:0{2}d}{3}", left, c, width, right);
// If the file doesn't exist, we doesn't need more files to load // If the file doesn't exist, we doesn't need more files to load
if (!base::is_file(buf)) if (!base::is_file(buf))

View File

@ -18,6 +18,7 @@
#include "doc/color.h" #include "doc/color.h"
#include "doc/document.h" #include "doc/document.h"
#include "doc/slice.h" #include "doc/slice.h"
#include "fmt/format.h"
#include "gfx/color.h" #include "gfx/color.h"
#include <cstdlib> #include <cstdlib>
@ -33,19 +34,19 @@ namespace {
std::string color_to_hex(doc::color_t color) std::string color_to_hex(doc::color_t color)
{ {
char buf[256]; std::string buf;
if (doc::rgba_geta(color) == 255) { if (doc::rgba_geta(color) == 255) {
std::sprintf(buf, "#%02x%02x%02x", buf = fmt::format("#{:02x}{:02x}{:02x}",
doc::rgba_getr(color), doc::rgba_getr(color),
doc::rgba_getg(color), doc::rgba_getg(color),
doc::rgba_getb(color)); doc::rgba_getb(color));
} }
else { else {
std::sprintf(buf, "#%02x%02x%02x%02x", buf = fmt::format("#{:02x}{:02x}{:02x}{:02x}",
doc::rgba_getr(color), doc::rgba_getr(color),
doc::rgba_getg(color), doc::rgba_getg(color),
doc::rgba_getb(color), doc::rgba_getb(color),
doc::rgba_geta(color)); doc::rgba_geta(color));
} }
return buf; return buf;
} }

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2018-2019 Igara Studio S.A. // Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -15,6 +15,7 @@
#include "base/base64.h" #include "base/base64.h"
#include "doc/doc.h" #include "doc/doc.h"
#include "doc/user_data.h" #include "doc/user_data.h"
#include "fmt/format.h"
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -27,18 +28,18 @@ using namespace app;
TEST(File, SeveralSizes) TEST(File, SeveralSizes)
{ {
// Register all possible image formats. // Register all possible image formats.
std::vector<char> fn(256); std::string fn;
app::Context ctx; app::Context ctx;
for (int w=10; w<=10+503*2; w+=503) { for (int w=10; w<=10+503*2; w+=503) {
for (int h=10; h<=10+503*2; h+=503) { for (int h=10; h<=10+503*2; h+=503) {
//std::sprintf(&fn[0], "test_%dx%d.ase", w, h); //fn = fmt::format("test_{}x{}.ase", w, h);
std::sprintf(&fn[0], "test.ase"); fn = "test.ase";
{ {
std::unique_ptr<Doc> doc( std::unique_ptr<Doc> doc(
ctx.documents().add(w, h, doc::ColorMode::INDEXED, 256)); ctx.documents().add(w, h, doc::ColorMode::INDEXED, 256));
doc->setFilename(&fn[0]); doc->setFilename(fn);
// Random pixels // Random pixels
Layer* layer = doc->sprite()->root()->firstLayer(); Layer* layer = doc->sprite()->root()->firstLayer();
@ -59,7 +60,7 @@ TEST(File, SeveralSizes)
} }
{ {
std::unique_ptr<Doc> doc(load_document(&ctx, &fn[0])); std::unique_ptr<Doc> doc(load_document(&ctx, fn));
ASSERT_EQ(w, doc->sprite()->width()); ASSERT_EQ(w, doc->sprite()->width());
ASSERT_EQ(h, doc->sprite()->height()); ASSERT_EQ(h, doc->sprite()->height());

View File

@ -16,6 +16,7 @@
#include "base/convert_to.h" #include "base/convert_to.h"
#include "base/fs.h" #include "base/fs.h"
#include "base/replace_string.h" #include "base/replace_string.h"
#include "fmt/format.h"
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
@ -35,14 +36,17 @@ static bool replace_frame(const char* frameKey, // E.g. = "{frame"
if (j != std::string::npos) { if (j != std::string::npos) {
std::string from = str.substr(i, j - i + 1); std::string from = str.substr(i, j - i + 1);
if (frameBase >= 0) { if (frameBase >= 0) {
std::vector<char> to(32);
int offset = std::strtol(from.c_str()+keyLen, NULL, 10); int offset = std::strtol(from.c_str()+keyLen, NULL, 10);
std::sprintf(&to[0], "%0*d", (int(j)-int(i+keyLen)), frameBase + offset); const std::string to =
base::replace_string(str, from, &to[0]); fmt::format("{0:0{1}d}",
frameBase + offset,
(int(j)-int(i+keyLen)));
base::replace_string(str, from, to);
} }
else else
base::replace_string(str, from, ""); base::replace_string(str, from, std::string());
} }
return true; return true;
} }
@ -59,9 +63,7 @@ static bool autodetect_frame_format(const std::string& filename,
int frameWidth = 0; int frameWidth = 0;
frameBase = split_filename(filename, left, right, frameWidth); frameBase = split_filename(filename, left, right, frameWidth);
if (frameBase >= 0) { if (frameBase >= 0) {
std::vector<char> buf(32); frameFormat = fmt::format("{{frame{0:0{1}d}}}", frameBase, frameWidth);
std::sprintf(&buf[0], "{frame%0*d}", frameWidth, frameBase);
frameFormat = std::string(&buf[0]);
return true; return true;
} }
else else

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2018-2021 Igara Studio S.A. // Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2001-2016 David Capello // Copyright (C) 2001-2016 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -16,6 +16,7 @@
#include "base/split_string.h" #include "base/split_string.h"
#include "base/string.h" #include "base/string.h"
#include "cfg/cfg.h" #include "cfg/cfg.h"
#include "fmt/format.h"
#ifdef __APPLE__ #ifdef __APPLE__
#include "os/logger.h" #include "os/logger.h"
@ -208,9 +209,8 @@ Point get_config_point(const char* section, const char* name, const Point& point
void set_config_point(const char* section, const char* name, const Point& point) void set_config_point(const char* section, const char* name, const Point& point)
{ {
char buf[128]; std::string buf = fmt::format("{} {}", point.x, point.y);
sprintf(buf, "%d %d", point.x, point.y); set_config_string(section, name, buf.c_str());
set_config_string(section, name, buf);
} }
Size get_config_size(const char* section, const char* name, const Size& size) Size get_config_size(const char* section, const char* name, const Size& size)
@ -230,9 +230,8 @@ Size get_config_size(const char* section, const char* name, const Size& size)
void set_config_size(const char* section, const char* name, const Size& size) void set_config_size(const char* section, const char* name, const Size& size)
{ {
char buf[128]; std::string buf = fmt::format("{} {}", size.w, size.h);
sprintf(buf, "%d %d", size.w, size.h); set_config_string(section, name, buf.c_str());
set_config_string(section, name, buf);
} }
Rect get_config_rect(const char* section, const char* name, const Rect& rect) Rect get_config_rect(const char* section, const char* name, const Rect& rect)
@ -254,9 +253,8 @@ Rect get_config_rect(const char* section, const char* name, const Rect& rect)
void set_config_rect(const char* section, const char* name, const Rect& rect) void set_config_rect(const char* section, const char* name, const Rect& rect)
{ {
char buf[128]; std::string buf = fmt::format("{} {} {} {}", rect.x, rect.y, rect.w, rect.h);
sprintf(buf, "%d %d %d %d", rect.x, rect.y, rect.w, rect.h); set_config_string(section, name, buf.c_str());
set_config_string(section, name, buf);
} }
app::Color get_config_color(const char* section, const char* name, const app::Color& value) app::Color get_config_color(const char* section, const char* name, const app::Color& value)

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2019-2021 Igara Studio S.A. // Copyright (C) 2019-2023 Igara Studio S.A.
// Copyright (C) 2001-2016 David Capello // Copyright (C) 2001-2016 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -89,31 +89,31 @@ void ResourceFinder::includeDataDir(const char* filename)
#ifdef _WIN32 #ifdef _WIN32
sprintf(buf, "data/%s", filename); std::snprintf(buf, sizeof(buf), "data/%s", filename);
includeHomeDir(buf); // %AppData%/Aseprite/data/filename includeHomeDir(buf); // %AppData%/Aseprite/data/filename
includeBinDir(buf); // $BINDIR/data/filename includeBinDir(buf); // $BINDIR/data/filename
#elif __APPLE__ #elif __APPLE__
sprintf(buf, "data/%s", filename); std::snprintf(buf, sizeof(buf), "data/%s", filename);
includeUserDir(buf); // $HOME/Library/Application Support/Aseprite/data/filename includeUserDir(buf); // $HOME/Library/Application Support/Aseprite/data/filename
includeBinDir(buf); // $BINDIR/data/filename (outside the bundle) includeBinDir(buf); // $BINDIR/data/filename (outside the bundle)
sprintf(buf, "../Resources/data/%s", filename); std::snprintf(buf, sizeof(buf), "../Resources/data/%s", filename);
includeBinDir(buf); // $BINDIR/../Resources/data/filename (inside a bundle) includeBinDir(buf); // $BINDIR/../Resources/data/filename (inside a bundle)
#else #else
// $HOME/.config/aseprite/filename // $HOME/.config/aseprite/filename
sprintf(buf, "aseprite/data/%s", filename); std::snprintf(buf, sizeof(buf), "aseprite/data/%s", filename);
includeHomeConfigDir(buf); includeHomeConfigDir(buf);
// $BINDIR/data/filename // $BINDIR/data/filename
sprintf(buf, "data/%s", filename); std::snprintf(buf, sizeof(buf), "data/%s", filename);
includeBinDir(buf); includeBinDir(buf);
// $BINDIR/../share/aseprite/data/filename (installed in /usr/ or /usr/local/) // $BINDIR/../share/aseprite/data/filename (installed in /usr/ or /usr/local/)
sprintf(buf, "../share/aseprite/data/%s", filename); std::snprintf(buf, sizeof(buf), "../share/aseprite/data/%s", filename);
includeBinDir(buf); includeBinDir(buf);
#endif #endif
@ -139,7 +139,7 @@ void ResourceFinder::includeHomeDir(const char* filename)
if ((env) && (*env)) { if ((env) && (*env)) {
// $HOME/filename // $HOME/filename
sprintf(buf, "%s/%s", env, filename); std::snprintf(buf, sizeof(buf), "%s/%s", env, filename);
addPath(buf); addPath(buf);
} }
else { else {

View File

@ -1,4 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2023 Igara Studio S.A.
// Copyright (C) 2001-2015 David Capello // Copyright (C) 2001-2015 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -31,8 +32,9 @@ std::string shade_to_string(const Shade& shade)
{ {
std::string res; std::string res;
for (const auto& s : shade) { for (const auto& s : shade) {
if (!res.empty())
res += " ";
res += s.toString(); res += s.toString();
res += " ";
} }
return res; return res;
} }

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2019-2022 Igara Studio S.A. // Copyright (C) 2019-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -8,6 +8,7 @@
#include "app/snap_to_grid.h" #include "app/snap_to_grid.h"
#include "base/gcd.h" #include "base/gcd.h"
#include "base/pi.h" #include "base/pi.h"
#include "fmt/format.h"
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
@ -93,13 +94,12 @@ public:
return; return;
gfx::Point offset = loop->statusBarPositionOffset(); gfx::Point offset = loop->statusBarPositionOffset();
char buf[1024]; text = fmt::format(
sprintf(buf, ":start: %d %d :end: %d %d", ":start: {} {} :end: {} {}",
stroke.firstPoint().x+offset.x, stroke.firstPoint().x+offset.x,
stroke.firstPoint().y+offset.y, stroke.firstPoint().y+offset.y,
stroke.lastPoint().x+offset.x, stroke.lastPoint().x+offset.x,
stroke.lastPoint().y+offset.y); stroke.lastPoint().y+offset.y);
text = buf;
} }
private: private:
@ -253,16 +253,16 @@ public:
if (stroke.size() < 2) if (stroke.size() < 2)
return; return;
int w = ABS(stroke[1].x-stroke[0].x)+1; const int w = ABS(stroke[1].x-stroke[0].x)+1;
int h = ABS(stroke[1].y-stroke[0].y)+1; const int h = ABS(stroke[1].y-stroke[0].y)+1;
gfx::Point offset = loop->statusBarPositionOffset(); const gfx::Point offset = loop->statusBarPositionOffset();
char buf[1024]; const int gcd = base::gcd(w, h);
int gcd = base::gcd(w, h);
sprintf(buf, ":start: %d %d :end: %d %d :size: %d %d :distance: %.1f", text = fmt::format(":start: {} {} :end: {} {} :size: {} {} :distance: {:.1f}",
stroke[0].x+offset.x, stroke[0].y+offset.y, stroke[0].x+offset.x, stroke[0].y+offset.y,
stroke[1].x+offset.x, stroke[1].y+offset.y, stroke[1].x+offset.x, stroke[1].y+offset.y,
w, h, std::sqrt(w*w + h*h)); w, h, std::sqrt(w*w + h*h));
if (hasAngle() || if (hasAngle() ||
loop->getIntertwine()->snapByAngle()) { loop->getIntertwine()->snapByAngle()) {
@ -272,14 +272,12 @@ public:
else else
angle = std::atan2(static_cast<double>(stroke[0].y-stroke[1].y), angle = std::atan2(static_cast<double>(stroke[0].y-stroke[1].y),
static_cast<double>(stroke[1].x-stroke[0].x)); static_cast<double>(stroke[1].x-stroke[0].x));
sprintf(buf+strlen(buf), " :angle: %.1f", 180.0 * angle / PI); text += fmt::format(" :angle: {:.1f}", 180.0 * angle / PI);
} }
// Aspect ratio at the end // Aspect ratio at the end
sprintf(buf+strlen(buf), " :aspect_ratio: %d:%d", text += fmt::format(" :aspect_ratio: {}:{}",
w/gcd, h/gcd); w/gcd, h/gcd);
text = buf;
} }
double getShapeAngle() const override { double getShapeAngle() const override {
@ -361,13 +359,11 @@ public:
return; return;
gfx::Point offset = loop->statusBarPositionOffset(); gfx::Point offset = loop->statusBarPositionOffset();
char buf[1024]; text = fmt::format(":start: {} {} :end: {} {}",
sprintf(buf, ":start: %d %d :end: %d %d", stroke.firstPoint().x+offset.x,
stroke.firstPoint().x+offset.x, stroke.firstPoint().y+offset.y,
stroke.firstPoint().y+offset.y, stroke.lastPoint().x+offset.x,
stroke.lastPoint().x+offset.x, stroke.lastPoint().y+offset.y);
stroke.lastPoint().y+offset.y);
text = buf;
} }
}; };
@ -401,11 +397,9 @@ public:
return; return;
gfx::Point offset = loop->statusBarPositionOffset(); gfx::Point offset = loop->statusBarPositionOffset();
char buf[1024]; text = fmt::format(":pos: {} {}",
sprintf(buf, ":pos: %d %d", stroke[0].x+offset.x,
stroke[0].x+offset.x, stroke[0].y+offset.y);
stroke[0].y+offset.y);
text = buf;
} }
}; };
@ -459,14 +453,12 @@ public:
return; return;
gfx::Point offset = loop->statusBarPositionOffset(); gfx::Point offset = loop->statusBarPositionOffset();
char buf[1024];
sprintf(buf, ":start: %d %d :end: %d %d (%d %d - %d %d)",
stroke[0].x+offset.x, stroke[0].y+offset.y,
stroke[3].x+offset.x, stroke[3].y+offset.y,
stroke[1].x+offset.x, stroke[1].y+offset.y,
stroke[2].x+offset.x, stroke[2].y+offset.y);
text = buf; text = fmt::format(":start: {} {} :end: {} {} ({} {} - {} {})",
stroke[0].x+offset.x, stroke[0].y+offset.y,
stroke[3].x+offset.x, stroke[3].y+offset.y,
stroke[1].x+offset.x, stroke[1].y+offset.y,
stroke[2].x+offset.x, stroke[2].y+offset.y);
} }
private: private:

View File

@ -654,12 +654,13 @@ private:
m_loaded = true; m_loaded = true;
char buf[32]; std::string buf;
int n = get_config_int("shades", "count", 0); int n = get_config_int("shades", "count", 0);
n = std::clamp(n, 0, 256); n = std::clamp(n, 0, 256);
for (int i=0; i<n; ++i) { for (int i=0; i<n; ++i) {
sprintf(buf, "shade%d", i); buf = fmt::format("shade{}", i);
Shade shade = shade_from_string(get_config_string("shades", buf, "")); Shade shade = shade_from_string(
get_config_string("shades", buf.c_str(), ""));
if (shade.size() >= 2) if (shade.size() >= 2)
m_shades.push_back(shade); m_shades.push_back(shade);
} }
@ -669,12 +670,13 @@ private:
if (!m_loaded) if (!m_loaded)
return; return;
char buf[32]; std::string buf;
int n = int(m_shades.size()); int n = int(m_shades.size());
set_config_int("shades", "count", n); set_config_int("shades", "count", n);
for (int i=0; i<n; ++i) { for (int i=0; i<n; ++i) {
sprintf(buf, "shade%d", i); buf = fmt::format("shade{}", i);
set_config_string("shades", buf, shade_to_string(m_shades[i]).c_str()); set_config_string("shades", buf.c_str(),
shade_to_string(m_shades[i]).c_str());
} }
} }

View File

@ -2243,10 +2243,10 @@ void Editor::onPaint(ui::PaintEvent& ev)
if (Preferences::instance().perf.showRenderTime()) { if (Preferences::instance().perf.showRenderTime()) {
View* view = View::getView(this); View* view = View::getView(this);
gfx::Rect vp = view->viewportBounds(); gfx::Rect vp = view->viewportBounds();
char buf[128]; std::string buf =
sprintf(buf, "%c %.4gs", fmt::format("{:c} {:.4g}s",
Preferences::instance().experimental.newRenderEngine() ? 'N': 'O', Preferences::instance().experimental.newRenderEngine() ? 'N': 'O',
renderElapsed); renderElapsed);
g->drawText( g->drawText(
buf, buf,
gfx::rgba(255, 255, 255, 255), gfx::rgba(255, 255, 255, 255),

View File

@ -3746,7 +3746,7 @@ void Timeline::updateStatusBarForFrame(const frame_t frame,
if (!m_sprite) if (!m_sprite)
return; return;
char buf[256] = { 0 }; std::string buf;
frame_t base = docPref().timeline.firstFrame(); frame_t base = docPref().timeline.firstFrame();
frame_t firstFrame = frame; frame_t firstFrame = frame;
frame_t lastFrame = frame; frame_t lastFrame = frame;
@ -3761,41 +3761,34 @@ void Timeline::updateStatusBarForFrame(const frame_t frame,
lastFrame = m_range.lastFrame(); lastFrame = m_range.lastFrame();
} }
std::sprintf( buf += fmt::format(":frame: {}",
buf+std::strlen(buf), ":frame: %d", int(base+frame));
base+frame);
if (firstFrame != lastFrame) { if (firstFrame != lastFrame) {
std::sprintf( buf += fmt::format(" [{}...{}]",
buf+std::strlen(buf), " [%d...%d]", int(base+firstFrame),
int(base+firstFrame), int(base+lastFrame));
int(base+lastFrame));
} }
std::sprintf( buf += fmt::format(" :clock: {}",
buf+std::strlen(buf), " :clock: %s", human_readable_time(m_sprite->frameDuration(frame)));
human_readable_time(m_sprite->frameDuration(frame)).c_str());
if (firstFrame != lastFrame) { if (firstFrame != lastFrame) {
std::sprintf( buf += fmt::format(" [{}]",
buf+std::strlen(buf), " [%s]", tag ?
tag ? human_readable_time(tagFramesDuration(tag)):
human_readable_time(tagFramesDuration(tag)).c_str(): human_readable_time(selectedFramesDuration()));
human_readable_time(selectedFramesDuration()).c_str());
} }
if (m_sprite->totalFrames() > 1) if (m_sprite->totalFrames() > 1)
std::sprintf( buf += fmt::format("/{}",
buf+std::strlen(buf), "/%s", human_readable_time(m_sprite->totalAnimationDuration()));
human_readable_time(m_sprite->totalAnimationDuration()).c_str());
if (cel) { if (cel) {
std::sprintf( buf += fmt::format(" Cel :pos: {} {} :size: {} {}",
buf+std::strlen(buf), " Cel :pos: %d %d :size: %d %d", cel->bounds().x, cel->bounds().y,
cel->bounds().x, cel->bounds().y, cel->bounds().w, cel->bounds().h);
cel->bounds().w, cel->bounds().h);
if (cel->links() > 0) { if (cel->links() > 0) {
std::sprintf( buf += fmt::format(" Links {}",
buf+std::strlen(buf), " Links %d", int(cel->links()));
int(cel->links()));
} }
} }

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2021 Igara Studio S.A. // Copyright (C) 2021-2023 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello // Copyright (C) 2001-2017 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -13,6 +13,7 @@
#include "app/modules/gui.h" #include "app/modules/gui.h"
#include "base/scoped_value.h" #include "base/scoped_value.h"
#include "fmt/format.h"
#include "gfx/rect.h" #include "gfx/rect.h"
#include "gfx/region.h" #include "gfx/region.h"
#include "ui/manager.h" #include "ui/manager.h"
@ -65,10 +66,7 @@ void ZoomEntry::onValueChange()
std::string ZoomEntry::onGetTextFromValue(int value) std::string ZoomEntry::onGetTextFromValue(int value)
{ {
render::Zoom zoom = render::Zoom::fromLinearScale(value); render::Zoom zoom = render::Zoom::fromLinearScale(value);
return fmt::format("{:.1f}", zoom.scale() * 100.0);
char buf[256];
std::sprintf(buf, "%.1f", zoom.scale() * 100.0);
return buf;
} }
int ZoomEntry::onGetValueFromText(const std::string& text) int ZoomEntry::onGetValueFromText(const std::string& text)

View File

@ -1,4 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2023 Igara Studio S.A.
// Copyright (C) 2017 David Capello // Copyright (C) 2017 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -10,22 +11,20 @@
#include "app/util/readable_time.h" #include "app/util/readable_time.h"
#include <cstdio> #include "fmt/format.h"
namespace app { namespace app {
std::string human_readable_time(const int t) std::string human_readable_time(const int t)
{ {
char buf[32];
if (t < 900) if (t < 900)
std::sprintf(buf, "%dms", t); return fmt::format("{:d}ms", t);
else if (t < 1000*59) else if (t < 1000*59)
std::sprintf(buf, "%0.2fs", double(t) / 1000.0); return fmt::format("{:0.2f}s", double(t) / 1000.0);
else if (t < 1000*60*59) else if (t < 1000*60*59)
std::sprintf(buf, "%0.2fm", double(t) / 1000.0 / 60.0); return fmt::format("{:0.2f}m", double(t) / 1000.0 / 60.0);
else else
std::sprintf(buf, "%0.2fh", double(t) / 1000.0 / 60.0 / 60.0); return fmt::format("{:0.2f}h", double(t) / 1000.0 / 60.0 / 60.0);
return buf;
} }
} // namespace app } // namespace app

View File

@ -1,4 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2023 Igara Studio S.A.
// Copyright (C) 2001-2015 David Capello // Copyright (C) 2001-2015 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -10,22 +11,18 @@
#include "app/xml_exception.h" #include "app/xml_exception.h"
#include "fmt/format.h"
#include "tinyxml.h" #include "tinyxml.h"
#include <cstdio>
namespace app { namespace app {
XmlException::XmlException(const TiXmlDocument* doc) throw() XmlException::XmlException(const TiXmlDocument* doc) throw()
{ {
try { try {
char buf[4096]; // TODO Overflow setMessage(
fmt::format("Error in XML file '{}' (line {}, column {})\nError {}: {}",
sprintf(buf, "Error in XML file '%s' (line %d, column %d)\nError %d: %s", doc->Value(), doc->ErrorRow(), doc->ErrorCol(),
doc->Value(), doc->ErrorRow(), doc->ErrorCol(), doc->ErrorId(), doc->ErrorDesc()).c_str());
doc->ErrorId(), doc->ErrorDesc());
setMessage(buf);
} }
catch (...) { catch (...) {
// No throw // No throw

View File

@ -121,7 +121,7 @@ void Alert::addButton(const std::string& text)
m_buttons.push_back(button); m_buttons.push_back(button);
char id[256]; char id[256];
sprintf(id, "button-%lu", m_buttons.size()); std::snprintf(id, sizeof(id), "button-%lu", m_buttons.size());
button->setId(id); button->setId(id);
button->Click.connect([this, button]{ closeWindow(button); }); button->Click.connect([this, button]{ closeWindow(button); });

View File

@ -60,7 +60,7 @@ Entry::Entry(const int maxsize, const char* format, ...)
if (format) { if (format) {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vsprintf(buf, format, ap); std::vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap); va_end(ap);
} }
// empty string // empty string

View File

@ -74,7 +74,7 @@ std::string Slider::convertValueToText(int value) const
return m_delegate->onGetTextFromValue(value); return m_delegate->onGetTextFromValue(value);
else { else {
char buf[128]; char buf[128];
std::sprintf(buf, "%d", value); std::snprintf(buf, sizeof(buf), "%d", value);
return buf; return buf;
} }
} }

View File

@ -144,7 +144,7 @@ void Widget::setTextf(const char *format, ...)
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
char buf[4096]; char buf[4096];
vsprintf(buf, format, ap); std::vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap); va_end(ap);
setText(buf); setText(buf);