Skip to content

Commit

Permalink
Codechange: use fmt::format instead of vseprintf for midi command for…
Browse files Browse the repository at this point in the history
…matting
  • Loading branch information
rubidium42 authored and pull[bot] committed Jun 30, 2023
1 parent 4a64803 commit 572653e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 28 deletions.
12 changes: 4 additions & 8 deletions src/music/os2_m.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "os2_m.h"
#include "midifile.hpp"
#include "../base_media_base.h"
#include "../3rdparty/fmt/format.h"

#define INCL_DOS
#define INCL_OS2MM
Expand All @@ -36,14 +37,9 @@
* @param cmd The command to send.
* @return The result of sending it.
*/
static long CDECL MidiSendCommand(const char *cmd, ...)
static long CDECL MidiSendCommand(const std::string_view cmd)
{
va_list va;
char buf[512];
va_start(va, cmd);
vseprintf(buf, lastof(buf), cmd, va);
va_end(va);
return mciSendString(buf, nullptr, 0, nullptr, 0);
return mciSendString(cmd.data(), nullptr, 0, nullptr, 0);
}

/** OS/2's music player's factory. */
Expand All @@ -56,7 +52,7 @@ void MusicDriver_OS2::PlaySong(const MusicSongInfo &song)
MidiSendCommand("close all");
if (filename.empty()) return;

if (MidiSendCommand("open %s type sequencer alias song", filename.c_str()) != 0) {
if (MidiSendCommand(fmt::format("open {} type sequencer alias song", filename)) != 0) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/safeguards.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#define sprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define snprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD

/* Use vseprintf instead. */
/* Use fmt::format instead. */
#define vsprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define vsnprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD

Expand Down
23 changes: 5 additions & 18 deletions src/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,6 @@
#include "safeguards.h"
#undef vsnprintf

/**
* Safer implementation of vsnprintf; same as vsnprintf except:
* - last instead of size, i.e. replace sizeof with lastof.
* - return gives the amount of characters added, not what it would add.
* @param str buffer to write to up to last
* @param last last character we may write to
* @param format the formatting (see snprintf)
* @param ap the list of arguments for the format
* @return the number of added characters
*/
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
{
ptrdiff_t diff = last - str;
if (diff < 0) return 0;
return std::min(static_cast<int>(diff), vsnprintf(str, diff + 1, format, ap));
}

/**
* Appends characters from one string to another.
*
Expand Down Expand Up @@ -536,10 +519,14 @@ int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
*/
int CDECL seprintf(char *str, const char *last, const char *format, ...)
{
ptrdiff_t diff = last - str;
if (diff < 0) return 0;

va_list ap;

va_start(ap, format);
int ret = vseprintf(str, last, format, ap);
int ret = std::min(static_cast<int>(diff), vsnprintf(str, diff + 1, format, ap));

va_end(ap);
return ret;
}
Expand Down
1 change: 0 additions & 1 deletion src/string_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ char *strecpy(char *dst, const char *src, const char *last) NOACCESS(3);
char *stredup(const char *src, const char *last = nullptr) NOACCESS(2);

int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4) NOACCESS(2);
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap) WARN_FORMAT(3, 0) NOACCESS(2);

std::string FormatArrayAsHex(span<const byte> data);

Expand Down

0 comments on commit 572653e

Please sign in to comment.