2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file dbg_helpers.cpp Helpers for outputting debug information. */
10 #include "../stdafx.h"
11 #include "../rail_map.h"
12 #include "dbg_helpers.h"
17 #include "../safeguards.h"
19 /** Trackdir & TrackdirBits short names. */
20 static const char * const trackdir_names
[] = {
21 "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
22 "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
25 /** Return name of given Trackdir. */
26 std::string
ValueStr(Trackdir td
)
28 return std::to_string(td
) + " (" + ItemAtT(td
, trackdir_names
, "UNK", INVALID_TRACKDIR
, "INV") + ")";
31 /** Return composed name of given TrackdirBits. */
32 std::string
ValueStr(TrackdirBits td_bits
)
34 return std::to_string(td_bits
) + " (" + ComposeNameT(td_bits
, trackdir_names
, "UNK", INVALID_TRACKDIR_BIT
, "INV") + ")";
38 /** DiagDirection short names. */
39 static const char * const diagdir_names
[] = {
40 "NE", "SE", "SW", "NW",
43 /** Return name of given DiagDirection. */
44 std::string
ValueStr(DiagDirection dd
)
46 return std::to_string(dd
) + " (" + ItemAtT(dd
, diagdir_names
, "UNK", INVALID_DIAGDIR
, "INV") + ")";
50 /** SignalType short names. */
51 static const char * const signal_type_names
[] = {
52 "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
55 /** Return name of given SignalType. */
56 std::string
ValueStr(SignalType t
)
58 return std::to_string(t
) + " (" + ItemAtT(t
, signal_type_names
, "UNK") + ")";
62 /** Translate TileIndex into string. */
63 std::string
TileStr(TileIndex tile
)
66 ss
<< "0x" << std::setfill('0') << std::setw(4) << std::hex
<< tile
.base(); // 0x%04X
67 ss
<< " (" << TileX(tile
) << ", " << TileY(tile
) << ")";
72 * Keep track of the last assigned type_id. Used for anti-recursion.
73 *static*/ size_t& DumpTarget::LastTypeId()
75 static size_t last_type_id
= 0;
79 /** Return structured name of the current class/structure. */
80 std::string
DumpTarget::GetCurrentStructName()
83 if (!m_cur_struct
.empty()) {
84 /* we are inside some named struct, return its name */
85 out
= m_cur_struct
.top();
91 * Find the given instance in our anti-recursion repository.
92 * Return true and set name when object was found.
94 bool DumpTarget::FindKnownName(size_t type_id
, const void *ptr
, std::string
&name
)
96 KNOWN_NAMES::const_iterator it
= m_known_names
.find(KnownStructKey(type_id
, ptr
));
97 if (it
!= m_known_names
.end()) {
98 /* we have found it */
105 /** Write some leading spaces into the output. */
106 void DumpTarget::WriteIndent()
108 int num_spaces
= 2 * m_indent
;
109 if (num_spaces
> 0) {
110 m_out
+= std::string(num_spaces
, ' ');
114 /** Write 'name = value' with indent and new-line. */
115 void DumpTarget::WriteValue(const std::string
&name
, int value
)
118 m_out
+= name
+ " = " + std::to_string(value
) + "\n";
121 /** Write 'name = value' with indent and new-line. */
122 void DumpTarget::WriteValue(const std::string
&name
, const std::string
&value_str
)
125 m_out
+= name
+ " = " + value_str
+ "\n";
128 /** Write name & TileIndex to the output. */
129 void DumpTarget::WriteTile(const std::string
&name
, TileIndex tile
)
132 m_out
+= name
+ " = " + TileStr(tile
) + "\n";
136 * Open new structure (one level deeper than the current one) 'name = {\<LF\>'.
138 void DumpTarget::BeginStruct(size_t type_id
, const std::string
&name
, const void *ptr
)
140 /* make composite name */
141 std::string cur_name
= GetCurrentStructName();
142 if (!cur_name
.empty()) {
143 /* add name delimiter (we use structured names) */
148 /* put the name onto stack (as current struct name) */
149 m_cur_struct
.push(cur_name
);
151 /* put it also to the map of known structures */
152 m_known_names
.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id
, ptr
), cur_name
));
155 m_out
+= name
+ " = {\n";
160 * Close structure '}\<LF\>'.
162 void DumpTarget::EndStruct()
168 /* remove current struct name from the stack */