Make DumpTarget::FindKnownName return a pointer to a string
[openttd/fttd.git] / src / misc / dbg_helpers.cpp
blobd66ae0dcdc820652383586e366ee0fa918f88efa
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 /** Write name of given Trackdir. */
22 void WriteValueStr(Trackdir td, FILE *f)
24 fprintf (f, "%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
27 /** Write composed name of given TrackdirBits. */
28 void WriteValueStr(TrackdirBits td_bits, FILE *f)
30 fprintf (f, "%d (", td_bits);
31 ComposeNameT (f, td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV");
32 putc (')', f);
36 /** DiagDirection short names. */
37 static const char * const diagdir_names[] = {
38 "NE", "SE", "SW", "NW",
41 /** Write name of given DiagDirection. */
42 void WriteValueStr(DiagDirection dd, FILE *f)
44 fprintf (f, "%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
48 /** SignalType short names. */
49 static const char * const signal_type_names[] = {
50 "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
53 /** Write name of given SignalType. */
54 void WriteValueStr(SignalType t, FILE *f)
56 fprintf (f, "%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
60 /**
61 * Keep track of the last assigned type_id. Used for anti-recursion.
62 *static*/ size_t& DumpTarget::LastTypeId()
64 static size_t last_type_id = 0;
65 return last_type_id;
68 /** Return structured name of the current class/structure. */
69 std::string DumpTarget::GetCurrentStructName()
71 std::string out;
72 if (!m_cur_struct.empty()) {
73 /* we are inside some named struct, return its name */
74 out = m_cur_struct.top();
76 return out;
79 /**
80 * Find the given instance in our anti-recursion repository.
81 * Return a pointer to the name if found, else NULL.
83 const std::string *DumpTarget::FindKnownName(size_t type_id, const void *ptr)
85 KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
86 return it != m_known_names.end() ? &it->second : NULL;
89 /** Write some leading spaces into the output. */
90 void DumpTarget::WriteIndent()
92 int num_spaces = 2 * m_indent;
93 while (num_spaces > 0) {
94 putc (' ', f);
95 num_spaces--;
99 /** Write a line with indent at the beginning and <LF> at the end. */
100 void DumpTarget::WriteLine(const char *format, ...)
102 WriteIndent();
103 va_list args;
104 va_start(args, format);
105 vfprintf (f, format, args);
106 va_end(args);
107 putc ('\n', f);
110 /** Write 'name = ' with indent. */
111 void DumpTarget::WriteValue(const char *name)
113 WriteIndent();
114 fprintf (f, "%s = ", name);
117 /** Write name & TileIndex to the output. */
118 void DumpTarget::WriteTile(const char *name, TileIndex tile)
120 WriteIndent();
121 fputs (name, f);
122 if (tile == INVALID_TILE) {
123 fputs (" = INVALID_TILE\n", f);
124 } else {
125 fprintf (f, " = 0x%04X (%d, %d)\n", tile, TileX(tile), TileY(tile));
130 * Open new structure (one level deeper than the current one) 'name = {<LF>'.
132 void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
134 /* make composite name */
135 std::string cur_name = GetCurrentStructName();
136 if (cur_name.size() > 0) {
137 /* add name delimiter (we use structured names) */
138 cur_name.append(".");
140 cur_name.append(name);
142 /* put the name onto stack (as current struct name) */
143 m_cur_struct.push(cur_name);
145 /* put it also to the map of known structures */
146 m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
148 WriteIndent();
149 fprintf (f, "%s = {\n", name);
150 m_indent++;
154 * Close structure '}<LF>'.
156 void DumpTarget::EndStruct()
158 m_indent--;
159 WriteIndent();
160 fputs ("}\n", f);
162 /* remove current struct name from the stack */
163 m_cur_struct.pop();
166 /** Just to silence an unsilencable GCC 4.4+ warning */
167 /* static */ ByteBlob::BlobHeader ByteBlob::hdrEmpty[] = {{0, 0}, {0, 0}};