OpenTTD
dbg_helpers.cpp
Go to the documentation of this file.
1 /* $Id: dbg_helpers.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
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  */
9 
12 #include "../stdafx.h"
13 #include "../rail_map.h"
14 #include "dbg_helpers.h"
15 
16 #include "../safeguards.h"
17 
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",
22 };
23 
26 {
27  CStrA out;
28  out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
29  return out.Transfer();
30 }
31 
34 {
35  CStrA out;
36  out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data());
37  return out.Transfer();
38 }
39 
40 
42 static const char * const diagdir_names[] = {
43  "NE", "SE", "SW", "NW",
44 };
45 
48 {
49  CStrA out;
50  out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
51  return out.Transfer();
52 }
53 
54 
56 static const char * const signal_type_names[] = {
57  "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
58 };
59 
62 {
63  CStrA out;
64  out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
65  return out.Transfer();
66 }
67 
68 
71 {
72  CStrA out;
73  out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile));
74  return out.Transfer();
75 }
76  size_t& DumpTarget::LastTypeId()
80 {
81  static size_t last_type_id = 0;
82  return last_type_id;
83 }
84 
87 {
88  CStrA out;
89  if (!m_cur_struct.empty()) {
90  /* we are inside some named struct, return its name */
91  out = m_cur_struct.top();
92  }
93  return out.Transfer();
94 }
95 
100 bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
101 {
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 */
105  name = (*it).second;
106  return true;
107  }
108  return false;
109 }
110 
113 {
114  int num_spaces = 2 * m_indent;
115  if (num_spaces > 0) {
116  memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
117  }
118 }
119 
121 void DumpTarget::WriteLine(const char *format, ...)
122 {
123  WriteIndent();
124  va_list args;
125  va_start(args, format);
126  m_out.AddFormatL(format, args);
127  va_end(args);
128  m_out.AppendStr("\n");
129 }
130 
132 void DumpTarget::WriteValue(const char *name, const char *value_str)
133 {
134  WriteIndent();
135  m_out.AddFormat("%s = %s\n", name, value_str);
136 }
137 
139 void DumpTarget::WriteTile(const char *name, TileIndex tile)
140 {
141  WriteIndent();
142  m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
143 }
144 
148 void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
149 {
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(".");
155  }
156  cur_name.AppendStr(name);
157 
158  /* put the name onto stack (as current struct name) */
159  m_cur_struct.push(cur_name);
160 
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));
163 
164  WriteIndent();
165  m_out.AddFormat("%s = {\n", name);
166  m_indent++;
167 }
168 
173 {
174  m_indent--;
175  WriteIndent();
176  m_out.AddFormat("}\n");
177 
178  /* remove current struct name from the stack */
179  m_cur_struct.pop();
180 }
181 
183 /* static */ ByteBlob::BlobHeader ByteBlob::hdrEmpty[] = {{0, 0}, {0, 0}};