KUDr@7122: /* $Id$ */ KUDr@7118: KUDr@7118: /** @file dbg_helpers.cpp */ KUDr@7118: #include "../stdafx.h" KUDr@7118: #include "../direction.h" KUDr@7118: #include "../rail.h" KUDr@7118: #include "../rail_map.h" KUDr@7118: #include "dbg_helpers.h" KUDr@7118: KUDr@7118: /** Trackdir & TrackdirBits short names. */ KUDr@7118: static const char* trackdir_names[] = { KUDr@7118: "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse", KUDr@7118: "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw", KUDr@7118: }; KUDr@7118: KUDr@7118: /** Return name of given Trackdir. */ KUDr@7118: CStrA ValueStr(Trackdir td) KUDr@7118: { KUDr@7118: CStrA out; KUDr@7118: out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV")); KUDr@7118: return out.Transfer(); KUDr@7118: } KUDr@7118: KUDr@7118: /** Return composed name of given TrackdirBits. */ KUDr@7118: CStrA ValueStr(TrackdirBits td_bits) KUDr@7118: { KUDr@7118: CStrA out; KUDr@7118: out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data()); KUDr@7118: return out.Transfer(); KUDr@7118: } KUDr@7118: KUDr@7118: KUDr@7118: /** DiagDirection short names. */ KUDr@7118: static const char* diagdir_names[] = { KUDr@7118: "NE", "SE", "SW", "NW", KUDr@7118: }; KUDr@7118: KUDr@7118: /** Return name of given DiagDirection. */ KUDr@7118: CStrA ValueStr(DiagDirection dd) KUDr@7118: { KUDr@7118: CStrA out; KUDr@7118: out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV")); KUDr@7118: return out.Transfer(); KUDr@7118: } KUDr@7118: KUDr@7118: KUDr@7118: /** SignalType short names. */ KUDr@7118: static const char* signal_type_names[] = { KUDr@7118: "NORMAL", "ENTRY", "EXIT", "COMBO", KUDr@7118: }; KUDr@7118: KUDr@7118: /** Return name of given SignalType. */ KUDr@7118: CStrA ValueStr(SignalType t) KUDr@7118: { KUDr@7118: CStrA out; KUDr@7118: out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK")); KUDr@7118: return out.Transfer(); KUDr@7118: } KUDr@7118: KUDr@7118: KUDr@7118: /** Translate TileIndex into string. */ KUDr@7118: CStrA TileStr(TileIndex tile) KUDr@7118: { KUDr@7118: CStrA out; KUDr@7118: out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile)); KUDr@7118: return out.Transfer(); KUDr@7118: } KUDr@7118: KUDr@7118: /** Keep track of the last assigned type_id. Used for anti-recursion. */ KUDr@7118: /*static*/ size_t& DumpTarget::LastTypeId() KUDr@7118: { KUDr@7118: static size_t last_type_id = 0; KUDr@7118: return last_type_id; KUDr@7118: } KUDr@7118: KUDr@7118: /** Return structured name of the current class/structure. */ KUDr@7118: CStrA DumpTarget::GetCurrentStructName() KUDr@7118: { KUDr@7118: CStrA out; KUDr@7118: if (!m_cur_struct.empty()) { KUDr@7118: // we are inside some named struct, return its name KUDr@7118: out = m_cur_struct.top(); KUDr@7118: } KUDr@7118: return out.Transfer(); KUDr@7118: } KUDr@7118: KUDr@7118: /** KUDr@7118: * Find the given instance in our anti-recursion repository. KUDr@7118: * Return true and set name when object was found. KUDr@7118: */ KUDr@7118: bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name) KUDr@7118: { KUDr@7118: KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr)); KUDr@7118: if (it != m_known_names.end()) { KUDr@7118: /* we have found it */ KUDr@7118: name = (*it).second; KUDr@7118: return true; KUDr@7118: } KUDr@7118: return false; KUDr@7118: } KUDr@7118: KUDr@7118: /** Write some leading spaces into the output. */ KUDr@7118: void DumpTarget::WriteIndent() KUDr@7118: { KUDr@7118: int num_spaces = 2 * m_indent; KUDr@7118: memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces); KUDr@7118: } KUDr@7118: KUDr@7118: /** Write a line with indent at the beginning and at the end. */ KUDr@7118: void DumpTarget::WriteLine(const char *format, ...) KUDr@7118: { KUDr@7118: WriteIndent(); KUDr@7118: va_list args; KUDr@7118: va_start(args, format); KUDr@7118: m_out.AddFormatL(format, args); KUDr@7118: va_end(args); KUDr@7118: m_out.AppendStr("\n"); KUDr@7118: } KUDr@7118: KUDr@7118: /** Write 'name = value' with indent and new-line. */ KUDr@7118: void DumpTarget::WriteValue(const char *name, const char *value_str) KUDr@7118: { KUDr@7118: WriteIndent(); KUDr@7118: m_out.AddFormat("%s = %s\n", name, value_str); KUDr@7118: } KUDr@7118: KUDr@7118: /** Write name & TileIndex to the output. */ KUDr@7118: void DumpTarget::WriteTile(const char *name, TileIndex tile) KUDr@7118: { KUDr@7118: WriteIndent(); KUDr@7118: m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data()); KUDr@7118: } KUDr@7118: KUDr@7118: /** KUDr@7118: * Open new structure (one level deeper than the current one) 'name = {'. KUDr@7118: */ KUDr@7118: void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr) KUDr@7118: { KUDr@7118: /* make composite name */ KUDr@7118: CStrA cur_name = GetCurrentStructName().Transfer(); KUDr@7118: if (cur_name.Size() > 0) { KUDr@7118: /* add name delimiter (we use structured names) */ KUDr@7118: cur_name.AppendStr("."); KUDr@7118: } KUDr@7118: cur_name.AppendStr(name); KUDr@7118: KUDr@7118: /* put the name onto stack (as current struct name) */ KUDr@7118: m_cur_struct.push(cur_name); KUDr@7118: KUDr@7118: /* put it also to the map of known structures */ KUDr@7118: m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name)); KUDr@7118: KUDr@7118: WriteIndent(); KUDr@7118: m_out.AddFormat("%s = {\n", name); KUDr@7118: m_indent++; KUDr@7118: } KUDr@7118: KUDr@7118: /** KUDr@7118: * Close structure '}'. KUDr@7118: */ KUDr@7118: void DumpTarget::EndStruct() KUDr@7118: { KUDr@7118: m_indent--; KUDr@7118: WriteIndent(); KUDr@7118: m_out.AddFormat("}\n"); KUDr@7118: KUDr@7118: /* remove current struct name from the stack */ KUDr@7118: m_cur_struct.pop(); KUDr@7118: } KUDr@7118: