4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file dbg_helpers.cpp Helpers for outputting debug information. */
12 #include "../stdafx.h"
13 #include "dbg_helpers.h"
15 /** Trackdir & TrackdirBits short names. */
16 static const char * const trackdir_names
[] = {
17 "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
18 "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
21 /** Return name of given Trackdir. */
22 CStrA
ValueStr(Trackdir td
)
25 out
.Format("%d (%s)", td
, ItemAtT(td
, trackdir_names
, "UNK", INVALID_TRACKDIR
, "INV"));
26 return out
.Transfer();
29 /** Return composed name of given TrackdirBits. */
30 CStrA
ValueStr(TrackdirBits td_bits
)
33 out
.Format("%d (%s)", td_bits
, ComposeNameT(td_bits
, trackdir_names
, "UNK", INVALID_TRACKDIR_BIT
, "INV").Data());
34 return out
.Transfer();
38 /** DiagDirection short names. */
39 static const char * const diagdir_names
[] = {
40 "NE", "SE", "SW", "NW",
43 /** Return name of given DiagDirection. */
44 CStrA
ValueStr(DiagDirection dd
)
47 out
.Format("%d (%s)", dd
, ItemAtT(dd
, diagdir_names
, "UNK", INVALID_DIAGDIR
, "INV"));
48 return out
.Transfer();
52 /** SignalType short names. */
53 static const char * const signal_type_names
[] = {
54 "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
57 /** Return name of given SignalType. */
58 CStrA
ValueStr(SignalType t
)
61 out
.Format("%d (%s)", t
, ItemAtT(t
, signal_type_names
, "UNK"));
62 return out
.Transfer();
66 /** Translate TileIndex into string. */
67 CStrA
TileStr(TileIndex tile
)
70 out
.Format("0x%04X (%d, %d)", tile
, TileX(tile
), TileY(tile
));
71 return out
.Transfer();
75 * Keep track of the last assigned type_id. Used for anti-recursion.
76 *static*/ size_t& DumpTarget::LastTypeId()
78 static size_t last_type_id
= 0;
82 /** Return structured name of the current class/structure. */
83 CStrA
DumpTarget::GetCurrentStructName()
86 if (!m_cur_struct
.empty()) {
87 /* we are inside some named struct, return its name */
88 out
= m_cur_struct
.top();
90 return out
.Transfer();
94 * Find the given instance in our anti-recursion repository.
95 * Return true and set name when object was found.
97 bool DumpTarget::FindKnownName(size_t type_id
, const void *ptr
, CStrA
&name
)
99 KNOWN_NAMES::const_iterator it
= m_known_names
.find(KnownStructKey(type_id
, ptr
));
100 if (it
!= m_known_names
.end()) {
101 /* we have found it */
108 /** Write some leading spaces into the output. */
109 void DumpTarget::WriteIndent()
111 int num_spaces
= 2 * m_indent
;
112 if (num_spaces
> 0) {
113 memset(m_out
.GrowSizeNC(num_spaces
), ' ', num_spaces
);
117 /** Write a line with indent at the beginning and <LF> at the end. */
118 void DumpTarget::WriteLine(const char *format
, ...)
122 va_start(args
, format
);
123 m_out
.AddFormatL(format
, args
);
125 m_out
.AppendStr("\n");
128 /** Write 'name = value' with indent and new-line. */
129 void DumpTarget::WriteValue(const char *name
, const char *value_str
)
132 m_out
.AddFormat("%s = %s\n", name
, value_str
);
135 /** Write name & TileIndex to the output. */
136 void DumpTarget::WriteTile(const char *name
, TileIndex tile
)
139 m_out
.AddFormat("%s = %s\n", name
, TileStr(tile
).Data());
143 * Open new structure (one level deeper than the current one) 'name = {<LF>'.
145 void DumpTarget::BeginStruct(size_t type_id
, const char *name
, const void *ptr
)
147 /* make composite name */
148 CStrA cur_name
= GetCurrentStructName().Transfer();
149 if (cur_name
.Size() > 0) {
150 /* add name delimiter (we use structured names) */
151 cur_name
.AppendStr(".");
153 cur_name
.AppendStr(name
);
155 /* put the name onto stack (as current struct name) */
156 m_cur_struct
.push(cur_name
);
158 /* put it also to the map of known structures */
159 m_known_names
.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id
, ptr
), cur_name
));
162 m_out
.AddFormat("%s = {\n", name
);
167 * Close structure '}<LF>'.
169 void DumpTarget::EndStruct()
173 m_out
.AddFormat("}\n");
175 /* remove current struct name from the stack */
179 /** Just to silence an unsilencable GCC 4.4+ warning */
180 /* static */ ByteBlob::BlobHeader
ByteBlob::hdrEmpty
[] = {{0, 0}, {0, 0}};