summaryrefslogtreecommitdiff
path: root/src/misc/dbg_helpers.cpp
blob: 561de50f33bae35dd193b2806f3177e1d6712d4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* $Id$ */

/** @file dbg_helpers.cpp */
#include "../stdafx.h"
#include "../direction.h"
#include "../rail.h"
#include "../rail_map.h"
#include "dbg_helpers.h"

/** Trackdir & TrackdirBits short names. */
static const char* trackdir_names[] = {
	"NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
	"SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
};

/** Return name of given Trackdir. */
CStrA ValueStr(Trackdir td)
{
	CStrA out;
	out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
	return out.Transfer();
}

/** Return composed name of given TrackdirBits. */
CStrA ValueStr(TrackdirBits td_bits)
{
	CStrA out;
	out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data());
	return out.Transfer();
}


/** DiagDirection short names. */
static const char* diagdir_names[] = {
	"NE", "SE", "SW", "NW",
};

/** Return name of given DiagDirection. */
CStrA ValueStr(DiagDirection dd)
{
	CStrA out;
	out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
	return out.Transfer();
}


/** SignalType short names. */
static const char* signal_type_names[] = {
	"NORMAL", "ENTRY", "EXIT", "COMBO",
};

/** Return name of given SignalType. */
CStrA ValueStr(SignalType t)
{
	CStrA out;
	out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
	return out.Transfer();
}


/** Translate TileIndex into string. */
CStrA TileStr(TileIndex tile)
{
	CStrA out;
	out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile));
	return out.Transfer();
}

/** Keep track of the last assigned type_id. Used for anti-recursion. */
/*static*/ size_t& DumpTarget::LastTypeId()
{
	static size_t last_type_id = 0;
	return last_type_id;
}

/** Return structured name of the current class/structure. */
CStrA DumpTarget::GetCurrentStructName()
{
	CStrA out;
	if (!m_cur_struct.empty()) {
		// we are inside some named struct, return its name
		out = m_cur_struct.top();
	}
	return out.Transfer();
}

/**
 * Find the given instance in our anti-recursion repository.
 * Return true and set name when object was found.
 */
bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
{
	KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
	if (it != m_known_names.end()) {
		/* we have found it */
		name = (*it).second;
		return true;
	}
	return false;
}

/** Write some leading spaces into the output. */
void DumpTarget::WriteIndent()
{
	int num_spaces = 2 * m_indent;
	memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
}

/** Write a line with indent at the beginning and <LF> at the end. */
void DumpTarget::WriteLine(const char *format, ...)
{
	WriteIndent();
	va_list args;
	va_start(args, format);
	m_out.AddFormatL(format, args);
	va_end(args);
	m_out.AppendStr("\n");
}

/** Write 'name = value' with indent and new-line. */
void DumpTarget::WriteValue(const char *name, const char *value_str)
{
	WriteIndent();
	m_out.AddFormat("%s = %s\n", name, value_str);
}

/** Write name & TileIndex to the output. */
void DumpTarget::WriteTile(const char *name, TileIndex tile)
{
	WriteIndent();
	m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
}

/**
 * Open new structure (one level deeper than the current one) 'name = {<LF>'.
 */
void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
{
	/* make composite name */
	CStrA cur_name = GetCurrentStructName().Transfer();
	if (cur_name.Size() > 0) {
		/* add name delimiter (we use structured names) */
		cur_name.AppendStr(".");
	}
	cur_name.AppendStr(name);

	/* put the name onto stack (as current struct name) */
	m_cur_struct.push(cur_name);

	/* put it also to the map of known structures */
	m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));

	WriteIndent();
	m_out.AddFormat("%s = {\n", name);
	m_indent++;
}

/**
 * Close structure '}<LF>'.
 */
void DumpTarget::EndStruct()
{
	m_indent--;
	WriteIndent();
	m_out.AddFormat("}\n");

	/* remove current struct name from the stack */
	m_cur_struct.pop();
}