1 /* $Id: dbg_helpers.cpp 20283 2010-08-01 19:22:34Z frosch $ */
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 "../rail_map.h"
14 #include "dbg_helpers.h"
16 #include "../safeguards.h"
18 /** Trackdir & TrackdirBits short names. */
19 static const char * const trackdir_names
[] = {
20 "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
21 "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
24 /** Return name of given Trackdir. */
25 CStrA
ValueStr(Trackdir td
)
28 out
.Format("%d (%s)", td
, ItemAtT(td
, trackdir_names
, "UNK", INVALID_TRACKDIR
, "INV"));
29 return out
.Transfer();
32 /** Return composed name of given TrackdirBits. */
33 CStrA
ValueStr(TrackdirBits td_bits
)
36 out
.Format("%d (%s)", td_bits
, ComposeNameT(td_bits
, trackdir_names
, "UNK", INVALID_TRACKDIR_BIT
, "INV").Data());
37 return out
.Transfer();
41 /** DiagDirection short names. */
42 static const char * const diagdir_names
[] = {
43 "NE", "SE", "SW", "NW",
46 /** Return name of given DiagDirection. */
47 CStrA
ValueStr(DiagDirection dd
)
50 out
.Format("%d (%s)", dd
, ItemAtT(dd
, diagdir_names
, "UNK", INVALID_DIAGDIR
, "INV"));
51 return out
.Transfer();
55 /** SignalType short names. */
56 static const char * const signal_type_names
[] = {
57 "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
60 /** Return name of given SignalType. */
61 CStrA
ValueStr(SignalType t
)
64 out
.Format("%d (%s)", t
, ItemAtT(t
, signal_type_names
, "UNK"));
65 return out
.Transfer();
69 /** Translate TileIndex into string. */
70 CStrA
TileStr(TileIndex tile
)
73 out
.Format("0x%04X (%d, %d)", tile
, TileX(tile
), TileY(tile
));
74 return out
.Transfer();
78 * Keep track of the last assigned type_id. Used for anti-recursion.
79 *static*/ size_t& DumpTarget::LastTypeId()
81 static size_t last_type_id
= 0;
85 /** Return structured name of the current class/structure. */
86 CStrA
DumpTarget::GetCurrentStructName()
89 if (!m_cur_struct
.empty()) {
90 /* we are inside some named struct, return its name */
91 out
= m_cur_struct
.top();
93 return out
.Transfer();
97 * Find the given instance in our anti-recursion repository.
98 * Return true and set name when object was found.
100 bool DumpTarget::FindKnownName(size_t type_id
, const void *ptr
, CStrA
&name
)
102 KNOWN_NAMES::const_iterator it
= m_known_names
.find(KnownStructKey(type_id
, ptr
));
103 if (it
!= m_known_names
.end()) {
104 /* we have found it */
111 /** Write some leading spaces into the output. */
112 void DumpTarget::WriteIndent()
114 int num_spaces
= 2 * m_indent
;
115 if (num_spaces
> 0) {
116 memset(m_out
.GrowSizeNC(num_spaces
), ' ', num_spaces
);
120 /** Write a line with indent at the beginning and <LF> at the end. */
121 void DumpTarget::WriteLine(const char *format
, ...)
125 va_start(args
, format
);
126 m_out
.AddFormatL(format
, args
);
128 m_out
.AppendStr("\n");
131 /** Write 'name = value' with indent and new-line. */
132 void DumpTarget::WriteValue(const char *name
, const char *value_str
)
135 m_out
.AddFormat("%s = %s\n", name
, value_str
);
138 /** Write name & TileIndex to the output. */
139 void DumpTarget::WriteTile(const char *name
, TileIndex tile
)
142 m_out
.AddFormat("%s = %s\n", name
, TileStr(tile
).Data());
146 * Open new structure (one level deeper than the current one) 'name = {<LF>'.
148 void DumpTarget::BeginStruct(size_t type_id
, const char *name
, const void *ptr
)
150 /* make composite name */
151 CStrA cur_name
= GetCurrentStructName().Transfer();
152 if (cur_name
.Size() > 0) {
153 /* add name delimiter (we use structured names) */
154 cur_name
.AppendStr(".");
156 cur_name
.AppendStr(name
);
158 /* put the name onto stack (as current struct name) */
159 m_cur_struct
.push(cur_name
);
161 /* put it also to the map of known structures */
162 m_known_names
.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id
, ptr
), cur_name
));
165 m_out
.AddFormat("%s = {\n", name
);
170 * Close structure '}<LF>'.
172 void DumpTarget::EndStruct()
176 m_out
.AddFormat("}\n");
178 /* remove current struct name from the stack */
182 /** Just to silence an unsilencable GCC 4.4+ warning */
183 /* static */ ByteBlob::BlobHeader
ByteBlob::hdrEmpty
[] = {{0, 0}, {0, 0}};