Changeset - r27175:3b7d2aa1c902
[Not reviewed]
master
0 4 0
Rubidium - 18 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
 
@@ -9,12 +9,13 @@
 

	
 
#include "../stdafx.h"
 
#include "../openttd.h"
 
#include "os2_m.h"
 
#include "midifile.hpp"
 
#include "../base_media_base.h"
 
#include "../3rdparty/fmt/format.h"
 

	
 
#define INCL_DOS
 
#define INCL_OS2MM
 
#define INCL_WIN
 

	
 
#include <stdarg.h>
 
@@ -33,33 +34,28 @@
 

	
 
/**
 
 * Send a midi command.
 
 * @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. */
 
static FMusicDriver_OS2 iFMusicDriver_OS2;
 

	
 
void MusicDriver_OS2::PlaySong(const MusicSongInfo &song)
 
{
 
	std::string filename = MidiFile::GetSMFFile(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;
 
	}
 

	
 
	MidiSendCommand("play song from 0");
 
}
 

	
src/safeguards.h
Show inline comments
 
@@ -41,13 +41,13 @@
 
#define strncat   SAFEGUARD_DO_NOT_USE_THIS_METHOD
 

	
 
/* Use seprintf instead. */
 
#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
 

	
 
/* Use fgets instead. */
 
#define gets      SAFEGUARD_DO_NOT_USE_THIS_METHOD
 

	
src/string.cpp
Show inline comments
 
@@ -48,29 +48,12 @@
 
/* The function vsnprintf is used internally to perform the required formatting
 
 * tasks. As such this one must be allowed, and makes sure it's terminated. */
 
#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.
 
 *
 
 * Appends the source string to the destination string with respect of the
 
 * terminating null-character and and the last pointer to the last element
 
 * in the destination buffer. If the last pointer is set to nullptr no
 
 * boundary check is performed.
 
@@ -533,16 +516,20 @@ int CDECL vsnprintf(char *str, size_t si
 
 * @param last   last character we may write to
 
 * @param format the formatting (see snprintf)
 
 * @return the number of added characters
 
 */
 
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
 
@@ -33,13 +33,12 @@
 

	
 
char *strecat(char *dst, const char *src, const char *last) NOACCESS(3);
 
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);
 

	
 
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
 
[[nodiscard]] std::string StrMakeValid(const std::string &str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
 
void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
0 comments (0 inline, 0 general)