Inline TileStr in dbg_helpers.cpp
[openttd/fttd.git] / src / misc / dbg_helpers.cpp
blob852eb138200e4f0884b477ce2844a988a6d38031
1 /* $Id$ */
3 /*
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/>.
8 */
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)
24 CStrA out;
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)
32 CStrA out;
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)
46 CStrA out;
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)
60 CStrA out;
61 out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
62 return out.Transfer();
66 /**
67 * Keep track of the last assigned type_id. Used for anti-recursion.
68 *static*/ size_t& DumpTarget::LastTypeId()
70 static size_t last_type_id = 0;
71 return last_type_id;
74 /** Return structured name of the current class/structure. */
75 CStrA DumpTarget::GetCurrentStructName()
77 CStrA out;
78 if (!m_cur_struct.empty()) {
79 /* we are inside some named struct, return its name */
80 out = m_cur_struct.top();
82 return out.Transfer();
85 /**
86 * Find the given instance in our anti-recursion repository.
87 * Return true and set name when object was found.
89 bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
91 KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
92 if (it != m_known_names.end()) {
93 /* we have found it */
94 name = (*it).second;
95 return true;
97 return false;
100 /** Write some leading spaces into the output. */
101 void DumpTarget::WriteIndent()
103 int num_spaces = 2 * m_indent;
104 while (num_spaces > 0) {
105 putc (' ', f);
106 num_spaces--;
110 /** Write a line with indent at the beginning and <LF> at the end. */
111 void DumpTarget::WriteLine(const char *format, ...)
113 WriteIndent();
114 va_list args;
115 va_start(args, format);
116 vfprintf (f, format, args);
117 va_end(args);
118 putc ('\n', f);
121 /** Write 'name = value' with indent and new-line. */
122 void DumpTarget::WriteValue(const char *name, const char *value_str)
124 WriteIndent();
125 fprintf (f, "%s = %s\n", name, value_str);
128 /** Write name & TileIndex to the output. */
129 void DumpTarget::WriteTile(const char *name, TileIndex tile)
131 WriteIndent();
132 fprintf (f, "%s = 0x%04X (%d, %d)\n", name, tile, TileX(tile), TileY(tile));
136 * Open new structure (one level deeper than the current one) 'name = {<LF>'.
138 void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
140 /* make composite name */
141 CStrA cur_name = GetCurrentStructName().Transfer();
142 if (cur_name.Size() > 0) {
143 /* add name delimiter (we use structured names) */
144 cur_name.AppendStr(".");
146 cur_name.AppendStr(name);
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));
154 WriteIndent();
155 fprintf (f, "%s = {\n", name);
156 m_indent++;
160 * Close structure '}<LF>'.
162 void DumpTarget::EndStruct()
164 m_indent--;
165 WriteIndent();
166 fputs ("}\n", f);
168 /* remove current struct name from the stack */
169 m_cur_struct.pop();
172 /** Just to silence an unsilencable GCC 4.4+ warning */
173 /* static */ ByteBlob::BlobHeader ByteBlob::hdrEmpty[] = {{0, 0}, {0, 0}};