mirror of
https://github.com/holub/mame
synced 2025-07-01 16:19:38 +03:00
debugcmd.cpp: Modernize implementation details of mini_printf
This commit is contained in:
parent
74eb8b8537
commit
19397f4903
@ -32,6 +32,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -944,53 +945,51 @@ void debugger_commands::execute_print(const std::vector<std::string> ¶ms)
|
|||||||
mini_printf - safe printf to a buffer
|
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;
|
auto f = format.begin();
|
||||||
char *p = buffer;
|
|
||||||
|
|
||||||
/* parse the string looking for % signs */
|
// parse the string looking for % signs
|
||||||
for (;;)
|
while (f != format.end())
|
||||||
{
|
{
|
||||||
char c = *f++;
|
char c = *f++;
|
||||||
if (!c) break;
|
|
||||||
|
|
||||||
/* escape sequences */
|
// escape sequences
|
||||||
if (c == '\\')
|
if (c == '\\')
|
||||||
{
|
{
|
||||||
|
if (f == format.end()) break;
|
||||||
c = *f++;
|
c = *f++;
|
||||||
if (!c) break;
|
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case '\\': *p++ = c; break;
|
case '\\': stream << c; break;
|
||||||
case 'n': *p++ = '\n'; break;
|
case 'n': stream << '\n'; break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* formatting */
|
// formatting
|
||||||
else if (c == '%')
|
else if (c == '%')
|
||||||
{
|
{
|
||||||
int width = 0;
|
int width = 0;
|
||||||
int zerofill = 0;
|
int zerofill = 0;
|
||||||
|
|
||||||
/* parse out the width */
|
// parse out the width
|
||||||
for (;;)
|
while (f != format.end() && *f >= '0' && *f <= '9')
|
||||||
{
|
{
|
||||||
c = *f++;
|
c = *f++;
|
||||||
if (!c || c < '0' || c > '9') break;
|
|
||||||
if (c == '0' && width == 0)
|
if (c == '0' && width == 0)
|
||||||
zerofill = 1;
|
zerofill = 1;
|
||||||
width = width * 10 + (c - '0');
|
width = width * 10 + (c - '0');
|
||||||
}
|
}
|
||||||
if (!c) break;
|
if (f == format.end()) break;
|
||||||
|
|
||||||
/* get the format */
|
// get the format
|
||||||
|
c = *f++;
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case '%':
|
case '%':
|
||||||
*p++ = c;
|
stream << c;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'X':
|
case 'X':
|
||||||
@ -998,13 +997,13 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
|
|||||||
if (params == 0)
|
if (params == 0)
|
||||||
{
|
{
|
||||||
m_console.printf("Not enough parameters for format!\n");
|
m_console.printf("Not enough parameters for format!\n");
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
if (u32(*param >> 32) != 0)
|
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)
|
else if (width > 8)
|
||||||
p += sprintf(p, zerofill ? "%0*X" : "%*X", width - 8, 0);
|
util::stream_format(stream, 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) ? width : 8, u32(*param));
|
||||||
param++;
|
param++;
|
||||||
params--;
|
params--;
|
||||||
break;
|
break;
|
||||||
@ -1014,23 +1013,23 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
|
|||||||
if (params == 0)
|
if (params == 0)
|
||||||
{
|
{
|
||||||
m_console.printf("Not enough parameters for format!\n");
|
m_console.printf("Not enough parameters for format!\n");
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
if (u32(*param >> 60) != 0)
|
if (u32(*param >> 60) != 0)
|
||||||
{
|
{
|
||||||
p += sprintf(p, zerofill ? "%0*o" : "%*o", (width <= 20) ? 1 : width - 20, u32(*param >> 60));
|
util::stream_format(stream, 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, "%0*o", 10, u32(BIT(*param, 30, 30)));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (width > 20)
|
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)
|
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)
|
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++;
|
param++;
|
||||||
params--;
|
params--;
|
||||||
break;
|
break;
|
||||||
@ -1040,9 +1039,9 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
|
|||||||
if (params == 0)
|
if (params == 0)
|
||||||
{
|
{
|
||||||
m_console.printf("Not enough parameters for format!\n");
|
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++;
|
param++;
|
||||||
params--;
|
params--;
|
||||||
break;
|
break;
|
||||||
@ -1051,9 +1050,9 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
|
|||||||
if (params == 0)
|
if (params == 0)
|
||||||
{
|
{
|
||||||
m_console.printf("Not enough parameters for format!\n");
|
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++;
|
param++;
|
||||||
params--;
|
params--;
|
||||||
break;
|
break;
|
||||||
@ -1061,14 +1060,12 @@ int debugger_commands::mini_printf(char *buffer, const char *format, int params,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* normal stuff */
|
// normal stuff
|
||||||
else
|
else
|
||||||
*p++ = c;
|
stream << c;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* NULL-terminate and exit */
|
return true;
|
||||||
*p = 0;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1116,9 +1113,9 @@ void debugger_commands::execute_printf(const std::vector<std::string> ¶ms)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* then do a printf */
|
/* then do a printf */
|
||||||
char buffer[1024];
|
std::ostringstream buffer;
|
||||||
if (mini_printf(buffer, params[0].c_str(), params.size() - 1, &values[1]))
|
if (mini_printf(buffer, params[0], params.size() - 1, &values[1]))
|
||||||
m_console.printf("%s\n", buffer);
|
m_console.printf("%s\n", buffer.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1135,9 +1132,9 @@ void debugger_commands::execute_logerror(const std::vector<std::string> ¶ms)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* then do a printf */
|
/* then do a printf */
|
||||||
char buffer[1024];
|
std::ostringstream buffer;
|
||||||
if (mini_printf(buffer, params[0].c_str(), params.size() - 1, &values[1]))
|
if (mini_printf(buffer, params[0], params.size() - 1, &values[1]))
|
||||||
m_machine.logerror("%s", buffer);
|
m_machine.logerror("%s", buffer.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1154,9 +1151,9 @@ void debugger_commands::execute_tracelog(const std::vector<std::string> ¶ms)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* then do a printf */
|
/* then do a printf */
|
||||||
char buffer[1024];
|
std::ostringstream buffer;
|
||||||
if (mini_printf(buffer, params[0].c_str(), params.size() - 1, &values[1]))
|
if (mini_printf(buffer, params[0], params.size() - 1, &values[1]))
|
||||||
m_console.get_visible_cpu()->debug()->trace_printf("%s", buffer);
|
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> ¶ms)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// then do a printf
|
// then do a printf
|
||||||
char buffer[1024];
|
std::ostringstream buffer;
|
||||||
if (mini_printf(buffer, format.str().c_str(), params.size(), values))
|
if (mini_printf(buffer, format.str(), params.size(), values))
|
||||||
m_console.get_visible_cpu()->debug()->trace_printf("%s", buffer);
|
m_console.get_visible_cpu()->debug()->trace_printf("%s", buffer.str().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ private:
|
|||||||
u64 global_get(global_entry *global);
|
u64 global_get(global_entry *global);
|
||||||
void global_set(global_entry *global, u64 value);
|
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>
|
template <typename T>
|
||||||
void execute_index_command(std::vector<std::string> const ¶ms, T &&apply, char const *unused_message);
|
void execute_index_command(std::vector<std::string> const ¶ms, T &&apply, char const *unused_message);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user