Changeset - r27175:3b7d2aa1c902
[Not reviewed]
master
0 4 0
Rubidium - 14 months ago 2023-04-25 16:03:11
rubidium@openttd.org
Codechange: use fmt::format instead of vseprintf for midi command formatting
4 files changed with 10 insertions and 28 deletions:
0 comments (0 inline, 0 general)
src/music/os2_m.cpp
Show inline comments
 
@@ -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
 
@@ -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. */
 
@@ -56,7 +52,7 @@ void MusicDriver_OS2::PlaySong(const Mus
 
	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;
 
	}
 

	
src/safeguards.h
Show inline comments
 
@@ -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
 

	
src/string.cpp
Show inline comments
 
@@ -51,23 +51,6 @@
 
#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.
 
 *
 
 * Appends the source string to the destination string with respect of the
 
@@ -536,10 +519,14 @@ int CDECL vsnprintf(char *str, size_t si
 
 */
 
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;
 
}
src/string_func.h
Show inline comments
 
@@ -36,7 +36,6 @@ char *strecpy(char *dst, const char *src
 
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);
 

	
0 comments (0 inline, 0 general)