summaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
author AJR <ajrhacker@users.noreply.github.com>2022-02-28 18:22:07 -0500
committer AJR <ajrhacker@users.noreply.github.com>2022-02-28 18:22:07 -0500
commit19397f4903fda22c7f64306cdab6865017825bce (patch)
treecb85b8a2ad5a16e1121da877229040ac8fb2e8b6 /src
parent74eb8b853791e07a7c8505a93f8fc8adf14984a2 (diff)
debugcmd.cpp: Modernize implementation details of mini_printf
Diffstat (limited to 'src')
-rw-r--r--src/emu/debug/debugcmd.cpp95
-rw-r--r--src/emu/debug/debugcmd.h2
2 files changed, 47 insertions, 50 deletions
diff --git a/src/emu/debug/debugcmd.cpp b/src/emu/debug/debugcmd.cpp
index 9b68c234d2f..2915ef342db 100644
--- a/src/emu/debug/debugcmd.cpp
+++ b/src/emu/debug/debugcmd.cpp
@@ -32,6 +32,7 @@
#include <algorithm>
#include <cctype>
#include <fstream>
+#include <sstream>
@@ -944,53 +945,51 @@ void debugger_commands::execute_print(const std::vector<std::string> &params)
mini_printf - safe printf to a buffer
-------------------------------------------------*/
-int debugger_commands::mini_printf(char *buffer, const char *format, int params, u64 *param)
+bool debugger_commands::mini_printf(std::ostream &stream, std::string_view format, int params, u64 *param)
{
- const char *f = format;
- char *p = buffer;
+ auto f = format.begin();
- /* parse the string looking for % signs */
- for (;;)
+ // parse the string looking for % signs
+ while (f != format.end())
{
char c = *f++;
- if (!c) break;
- /* escape sequences */
+ // escape sequences
if (c == '\\')
{
+ if (f == format.end()) break;
c = *f++;
- if (!c) break;
switch (c)
{
- case '\\': *p++ = c; break;
- case 'n': *p++ = '\n'; break;
+ case '\\': stream << c; break;
+ case 'n': stream << '\n'; break;
default: break;
}
continue;
}
- /* formatting */
+ // formatting
else if (c == '%')
{
int width = 0;
int zerofill = 0;
- /* parse out the width */
- for (;;)
+ // parse out the width
+ while (f != format.end() && *f >= '0' && *f <= '9')
{
c = *f++;
- if (!c || c < '0' || c > '9') break;
if (c == '0' && width == 0)
zerofill = 1;
width = width * 10 + (c - '0');
}
- if (!c) break;
+ if (f == format.end()) break;
- /* get the format */
+ // get the format
+ c = *f++;
switch (c)
{
case '%':
- *p++ = c;
+ stream << c;
break;
case 'X':
@@ -998,13 +997,13 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
if (params == 0)
{
m_console.printf("Not enough parameters for format!\n");
- return 0;
+ return false;
}
if (u32(*param >> 32) != 0)
- p += sprintf(p, zerofill ? "%0*X" : "%*X", (width <= 8) ? 1 : width - 8, u32(*param >> 32));
+ util::stream_format(stream, zerofill ? "%0*X" : "%*X", (width <= 8) ? 1 : width - 8, u32(*param >> 32));
else if (width > 8)
- p += sprintf(p, zerofill ? "%0*X" : "%*X", width - 8, 0);
- p += sprintf(p, zerofill ? "%0*X" : "%*X", (width < 8) ? width : 8, u32(*param));
+ util::stream_format(stream, zerofill ? "%0*X" : "%*X", width - 8, 0);
+ util::stream_format(stream, zerofill ? "%0*X" : "%*X", (width < 8) ? width : 8, u32(*param));
param++;
params--;
break;
@@ -1014,23 +1013,23 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
if (params == 0)
{
m_console.printf("Not enough parameters for format!\n");
- return 0;
+ return false;
}
if (u32(*param >> 60) != 0)
{
- p += sprintf(p, zerofill ? "%0*o" : "%*o", (width <= 20) ? 1 : width - 20, u32(*param >> 60));
- p += sprintf(p, "%0*o", 10, u32(BIT(*param, 30, 30)));
+ util::stream_format(stream, zerofill ? "%0*o" : "%*o", (width <= 20) ? 1 : width - 20, u32(*param >> 60));
+ util::stream_format(stream, "%0*o", 10, u32(BIT(*param, 30, 30)));
}
else
{
if (width > 20)
- p += sprintf(p, zerofill ? "%0*o" : "%*o", width - 20, 0);
+ util::stream_format(stream, zerofill ? "%0*o" : "%*o", width - 20, 0);
if (u32(BIT(*param, 30, 30)) != 0)
- p += sprintf(p, zerofill ? "%0*o" : "%*o", (width <= 10) ? 1 : width - 10, u32(BIT(*param, 30, 30)));
+ util::stream_format(stream, zerofill ? "%0*o" : "%*o", (width <= 10) ? 1 : width - 10, u32(BIT(*param, 30, 30)));
else if (width > 10)
- p += sprintf(p, zerofill ? "%0*o" : "%*o", width - 10, 0);
+ util::stream_format(stream, zerofill ? "%0*o" : "%*o", width - 10, 0);
}
- p += sprintf(p, zerofill ? "%0*o" : "%*o", (width < 10) ? width : 10, u32(BIT(*param, 0, 30)));
+ util::stream_format(stream, zerofill ? "%0*o" : "%*o", (width < 10) ? width : 10, u32(BIT(*param, 0, 30)));
param++;
params--;
break;
@@ -1040,9 +1039,9 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
if (params == 0)
{
m_console.printf("Not enough parameters for format!\n");
- return 0;
+ return false;
}
- p += sprintf(p, zerofill ? "%0*d" : "%*d", width, u32(*param));
+ util::stream_format(stream, zerofill ? "%0*d" : "%*d", width, u32(*param));
param++;
params--;
break;
@@ -1051,9 +1050,9 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
if (params == 0)
{
m_console.printf("Not enough parameters for format!\n");
- return 0;
+ return false;
}
- p += sprintf(p, "%c", char(*param));
+ util::stream_format(stream, "%c", char(*param));
param++;
params--;
break;
@@ -1061,14 +1060,12 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
}
}
- /* normal stuff */
+ // normal stuff
else
- *p++ = c;
+ stream << c;
}
- /* NULL-terminate and exit */
- *p = 0;
- return 1;
+ return true;
}
@@ -1116,9 +1113,9 @@ void debugger_commands::execute_printf(const std::vector<std::string> &params)
return;
/* then do a printf */
- char buffer[1024];
- if (mini_printf(buffer, params[0].c_str(), params.size() - 1, &values[1]))
- m_console.printf("%s\n", buffer);
+ std::ostringstream buffer;
+ if (mini_printf(buffer, params[0], params.size() - 1, &values[1]))
+ m_console.printf("%s\n", buffer.str());
}
@@ -1135,9 +1132,9 @@ void debugger_commands::execute_logerror(const std::vector<std::string> &params)
return;
/* then do a printf */
- char buffer[1024];
- if (mini_printf(buffer, params[0].c_str(), params.size() - 1, &values[1]))
- m_machine.logerror("%s", buffer);
+ std::ostringstream buffer;
+ if (mini_printf(buffer, params[0], params.size() - 1, &values[1]))
+ m_machine.logerror("%s", buffer.str());
}
@@ -1154,9 +1151,9 @@ void debugger_commands::execute_tracelog(const std::vector<std::string> &params)
return;
/* then do a printf */
- char buffer[1024];
- if (mini_printf(buffer, params[0].c_str(), params.size() - 1, &values[1]))
- m_console.get_visible_cpu()->debug()->trace_printf("%s", buffer);
+ std::ostringstream buffer;
+ if (mini_printf(buffer, params[0], params.size() - 1, &values[1]))
+ m_console.get_visible_cpu()->debug()->trace_printf("%s", buffer.str().c_str());
}
@@ -1190,9 +1187,9 @@ void debugger_commands::execute_tracesym(const std::vector<std::string> &params)
}
// then do a printf
- char buffer[1024];
- if (mini_printf(buffer, format.str().c_str(), params.size(), values))
- m_console.get_visible_cpu()->debug()->trace_printf("%s", buffer);
+ std::ostringstream buffer;
+ if (mini_printf(buffer, format.str(), params.size(), values))
+ m_console.get_visible_cpu()->debug()->trace_printf("%s", buffer.str().c_str());
}
diff --git a/src/emu/debug/debugcmd.h b/src/emu/debug/debugcmd.h
index fd0bcea673a..b50c07bd634 100644
--- a/src/emu/debug/debugcmd.h
+++ b/src/emu/debug/debugcmd.h
@@ -99,7 +99,7 @@ private:
u64 global_get(global_entry *global);
void global_set(global_entry *global, u64 value);
- int mini_printf(char *buffer, const char *format, int params, u64 *param);
+ bool mini_printf(std::ostream &stream, std::string_view format, int params, u64 *param);
template <typename T>
void execute_index_command(std::vector<std::string> const &params, T &&apply, char const *unused_message);