9631
|
1 |
/* $Id$ */
|
|
2 |
|
|
3 |
/** @file dbg_helpers.cpp */
|
|
4 |
#include "../stdafx.h"
|
|
5 |
#include "../direction.h"
|
|
6 |
#include "../rail.h"
|
|
7 |
#include "../rail_map.h"
|
|
8 |
#include "dbg_helpers.h"
|
|
9 |
|
|
10 |
/** Trackdir & TrackdirBits short names. */
|
|
11 |
static const char* trackdir_names[] = {
|
|
12 |
"NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
|
|
13 |
"SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
|
|
14 |
};
|
|
15 |
|
|
16 |
/** Return name of given Trackdir. */
|
|
17 |
CStrA ValueStr(Trackdir td)
|
|
18 |
{
|
|
19 |
CStrA out;
|
|
20 |
out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
|
|
21 |
return out.Transfer();
|
|
22 |
}
|
|
23 |
|
|
24 |
/** Return composed name of given TrackdirBits. */
|
|
25 |
CStrA ValueStr(TrackdirBits td_bits)
|
|
26 |
{
|
|
27 |
CStrA out;
|
|
28 |
out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data());
|
|
29 |
return out.Transfer();
|
|
30 |
}
|
|
31 |
|
|
32 |
|
|
33 |
/** DiagDirection short names. */
|
|
34 |
static const char* diagdir_names[] = {
|
|
35 |
"NE", "SE", "SW", "NW",
|
|
36 |
};
|
|
37 |
|
|
38 |
/** Return name of given DiagDirection. */
|
|
39 |
CStrA ValueStr(DiagDirection dd)
|
|
40 |
{
|
|
41 |
CStrA out;
|
|
42 |
out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
|
|
43 |
return out.Transfer();
|
|
44 |
}
|
|
45 |
|
|
46 |
|
|
47 |
/** SignalType short names. */
|
|
48 |
static const char* signal_type_names[] = {
|
|
49 |
"NORMAL", "ENTRY", "EXIT", "COMBO",
|
|
50 |
};
|
|
51 |
|
|
52 |
/** Return name of given SignalType. */
|
|
53 |
CStrA ValueStr(SignalType t)
|
|
54 |
{
|
|
55 |
CStrA out;
|
|
56 |
out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
|
|
57 |
return out.Transfer();
|
|
58 |
}
|
|
59 |
|
|
60 |
|
|
61 |
/** Translate TileIndex into string. */
|
|
62 |
CStrA TileStr(TileIndex tile)
|
|
63 |
{
|
|
64 |
CStrA out;
|
|
65 |
out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile));
|
|
66 |
return out.Transfer();
|
|
67 |
}
|
|
68 |
|
|
69 |
/** Keep track of the last assigned type_id. Used for anti-recursion. */
|
|
70 |
/*static*/ size_t& DumpTarget::LastTypeId()
|
|
71 |
{
|
|
72 |
static size_t last_type_id = 0;
|
|
73 |
return last_type_id;
|
|
74 |
}
|
|
75 |
|
|
76 |
/** Return structured name of the current class/structure. */
|
|
77 |
CStrA DumpTarget::GetCurrentStructName()
|
|
78 |
{
|
|
79 |
CStrA out;
|
|
80 |
if (!m_cur_struct.empty()) {
|
|
81 |
// we are inside some named struct, return its name
|
|
82 |
out = m_cur_struct.top();
|
|
83 |
}
|
|
84 |
return out.Transfer();
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Find the given instance in our anti-recursion repository.
|
|
89 |
* Return true and set name when object was found.
|
|
90 |
*/
|
|
91 |
bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
|
|
92 |
{
|
|
93 |
KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
|
|
94 |
if (it != m_known_names.end()) {
|
|
95 |
/* we have found it */
|
|
96 |
name = (*it).second;
|
|
97 |
return true;
|
|
98 |
}
|
|
99 |
return false;
|
|
100 |
}
|
|
101 |
|
|
102 |
/** Write some leading spaces into the output. */
|
|
103 |
void DumpTarget::WriteIndent()
|
|
104 |
{
|
|
105 |
int num_spaces = 2 * m_indent;
|
|
106 |
memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
|
|
107 |
}
|
|
108 |
|
|
109 |
/** Write a line with indent at the beginning and <LF> at the end. */
|
|
110 |
void DumpTarget::WriteLine(const char *format, ...)
|
|
111 |
{
|
|
112 |
WriteIndent();
|
|
113 |
va_list args;
|
|
114 |
va_start(args, format);
|
|
115 |
m_out.AddFormatL(format, args);
|
|
116 |
va_end(args);
|
|
117 |
m_out.AppendStr("\n");
|
|
118 |
}
|
|
119 |
|
|
120 |
/** Write 'name = value' with indent and new-line. */
|
|
121 |
void DumpTarget::WriteValue(const char *name, const char *value_str)
|
|
122 |
{
|
|
123 |
WriteIndent();
|
|
124 |
m_out.AddFormat("%s = %s\n", name, value_str);
|
|
125 |
}
|
|
126 |
|
|
127 |
/** Write name & TileIndex to the output. */
|
|
128 |
void DumpTarget::WriteTile(const char *name, TileIndex tile)
|
|
129 |
{
|
|
130 |
WriteIndent();
|
|
131 |
m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Open new structure (one level deeper than the current one) 'name = {<LF>'.
|
|
136 |
*/
|
|
137 |
void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
|
|
138 |
{
|
|
139 |
/* make composite name */
|
|
140 |
CStrA cur_name = GetCurrentStructName().Transfer();
|
|
141 |
if (cur_name.Size() > 0) {
|
|
142 |
/* add name delimiter (we use structured names) */
|
|
143 |
cur_name.AppendStr(".");
|
|
144 |
}
|
|
145 |
cur_name.AppendStr(name);
|
|
146 |
|
|
147 |
/* put the name onto stack (as current struct name) */
|
|
148 |
m_cur_struct.push(cur_name);
|
|
149 |
|
|
150 |
/* put it also to the map of known structures */
|
|
151 |
m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
|
|
152 |
|
|
153 |
WriteIndent();
|
|
154 |
m_out.AddFormat("%s = {\n", name);
|
|
155 |
m_indent++;
|
|
156 |
}
|
|
157 |
|
|
158 |
/**
|
|
159 |
* Close structure '}<LF>'.
|
|
160 |
*/
|
|
161 |
void DumpTarget::EndStruct()
|
|
162 |
{
|
|
163 |
m_indent--;
|
|
164 |
WriteIndent();
|
|
165 |
m_out.AddFormat("}\n");
|
|
166 |
|
|
167 |
/* remove current struct name from the stack */
|
|
168 |
m_cur_struct.pop();
|
|
169 |
}
|
|
170 |
|