Skip to content

Commit 7fa6b1c

Browse files
committed
cleanup
1 parent 9da15dd commit 7fa6b1c

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed

Emulator/VAmiga/Components/Memory/MemoryDebugger.cpp

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -381,49 +381,72 @@ MemoryDebugger::writeCs(Reg reg, u16 value)
381381
void
382382
MemoryDebugger::convertNumeric(std::ostream& os, u8 value) const
383383
{
384-
using namespace util;
385-
386-
os << std::setw(10) << std::right << std::setfill(' ') << dec(value) << " | ";
387-
os << hex(value) << " | ";
388-
os << bin(value) << " | ";
389-
os << str(value);
384+
convertNumeric<u16>(os, value, "%3d | %h | %b | %s");
390385
}
391386

392387
void
393388
MemoryDebugger::convertNumeric(std::ostream& os, u16 value) const
394389
{
395-
using namespace util;
396-
397-
os << std::setw(10) << std::right << std::setfill(' ') << dec(value) << " | ";
398-
os << hex(value) << " | ";
399-
os << bin(value) << " | ";
400-
os << str(value);
390+
convertNumeric<u16>(os, value, "%5d | %h | %b | %s");
401391
}
402392

403393
void
404394
MemoryDebugger::convertNumeric(std::ostream& os, u32 value) const
405395
{
406-
using namespace util;
407-
408-
os << std::setw(10) << std::right << std::setfill(' ') << dec(value) << " | ";
409-
os << hex(value) << " | ";
410-
os << bin(value) << " | ";
411-
os << str(value);
396+
convertNumeric<u32>(os, value, "%10d | %h | %b | %s");
412397
}
413398

414399
void
415400
MemoryDebugger::convertNumeric(std::ostream& os, string s) const
416401
{
417-
u8 bytes[4];
402+
auto len = s.length();
418403

419-
bytes[0] = s.length() >= 4 ? (u8)s[s.length() - 4] : 0;
420-
bytes[1] = s.length() >= 3 ? (u8)s[s.length() - 3] : 0;
421-
bytes[2] = s.length() >= 2 ? (u8)s[s.length() - 2] : 0;
422-
bytes[3] = s.length() >= 1 ? (u8)s[s.length() - 1] : 0;
404+
u8 bytes[4];
405+
bytes[0] = len >= 4 ? (u8)s[len - 4] : 0;
406+
bytes[1] = len >= 3 ? (u8)s[len - 3] : 0;
407+
bytes[2] = len >= 2 ? (u8)s[len - 2] : 0;
408+
bytes[3] = len >= 1 ? (u8)s[len - 1] : 0;
423409

424410
convertNumeric(os, u32(HI_HI_LO_LO(bytes[0], bytes[1], bytes[2], bytes[3])));
425411
}
426412

413+
template <typename T> void
414+
MemoryDebugger::convertNumeric(std::ostream& os, T value, const char *fmt) const
415+
{
416+
bool ctrl = false;
417+
isize tab = 0;
418+
419+
for (char c = fmt[0]; c != 0; c = (++fmt)[0]) {
420+
421+
if (!ctrl) {
422+
423+
if (c == '%') { ctrl = true; tab = 0; } else { os << c; }
424+
continue;
425+
}
426+
427+
if (c >= '0' && c <= '9') {
428+
429+
tab = 10 * tab + (c - '0');
430+
continue;
431+
}
432+
433+
if (tab) { os << std::setw(int(tab)) << std::right << std::setfill(' '); }
434+
435+
switch (c) {
436+
437+
case 'd': os << util::dec(value); break;
438+
case 'h': os << util::hex(value); break;
439+
case 'b': os << util::bin(value); break;
440+
case 's': os << util::str(value); break;
441+
442+
default:
443+
fatalError;
444+
}
445+
446+
ctrl = false;
447+
}
448+
}
449+
427450
template const char *MemoryDebugger::ascDump <Accessor::CPU> (u32, isize) const;
428451
template const char *MemoryDebugger::ascDump <Accessor::AGNUS> (u32, isize) const;
429452
template const char *MemoryDebugger::hexDump <Accessor::CPU> (u32, isize, isize) const;

Emulator/VAmiga/Components/Memory/MemoryDebugger.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,14 @@ class MemoryDebugger final : public SubComponent
132132
public:
133133

134134
// Displays a value in different number formats (hex, dec, bin, alpha)
135-
// TODO: REPLACE BY GENERIC FORMAT STRING FORMATTER
136135
void convertNumeric(std::ostream& os, u8 value) const;
137136
void convertNumeric(std::ostream& os, u16 value) const;
138137
void convertNumeric(std::ostream& os, u32 value) const;
139138
void convertNumeric(std::ostream& os, string value) const;
139+
140+
private:
141+
142+
template <typename T> void convertNumeric(std::ostream& os, T value, const char *fmt) const;
140143
};
141144

142145
}

Emulator/VAmiga/Misc/RetroShell/DebugConsole.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ DebugConsole::initCommands(RetroShellCmd &root)
16271627
std::stringstream ss;
16281628

16291629
if (isNum(argv[0])) {
1630-
mem.debugger.convertNumeric(ss, u32(parseNum(argv[0])));
1630+
mem.debugger.convertNumeric(ss, (u32)parseNum(argv[0]));
16311631
} else {
16321632
mem.debugger.convertNumeric(ss, argv.front());
16331633
}

0 commit comments

Comments
 (0)