author | rubidium |
Wed, 07 May 2008 21:09:51 +0000 | |
branch | noai |
changeset 10455 | 22c441f5adf9 |
parent 9723 | eee46cb39750 |
child 10513 | 33cb70ff2f5d |
permissions | -rw-r--r-- |
9631 | 1 |
/* $Id$ */ |
2 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
9723
diff
changeset
|
3 |
/** @file dbg_helpers.cpp Helpers for outputting debug information. */ |
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
9723
diff
changeset
|
4 |
|
9631 | 5 |
#include "../stdafx.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9631
diff
changeset
|
6 |
#include "../openttd.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9631
diff
changeset
|
7 |
#include "../direction_type.h" |
9631 | 8 |
#include "../rail.h" |
9 |
#include "../rail_map.h" |
|
10 |
#include "dbg_helpers.h" |
|
11 |
||
12 |
/** Trackdir & TrackdirBits short names. */ |
|
13 |
static const char* trackdir_names[] = { |
|
14 |
"NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse", |
|
15 |
"SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw", |
|
16 |
}; |
|
17 |
||
18 |
/** Return name of given Trackdir. */ |
|
19 |
CStrA ValueStr(Trackdir td) |
|
20 |
{ |
|
21 |
CStrA out; |
|
22 |
out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV")); |
|
23 |
return out.Transfer(); |
|
24 |
} |
|
25 |
||
26 |
/** Return composed name of given TrackdirBits. */ |
|
27 |
CStrA ValueStr(TrackdirBits td_bits) |
|
28 |
{ |
|
29 |
CStrA out; |
|
30 |
out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data()); |
|
31 |
return out.Transfer(); |
|
32 |
} |
|
33 |
||
34 |
||
35 |
/** DiagDirection short names. */ |
|
36 |
static const char* diagdir_names[] = { |
|
37 |
"NE", "SE", "SW", "NW", |
|
38 |
}; |
|
39 |
||
40 |
/** Return name of given DiagDirection. */ |
|
41 |
CStrA ValueStr(DiagDirection dd) |
|
42 |
{ |
|
43 |
CStrA out; |
|
44 |
out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV")); |
|
45 |
return out.Transfer(); |
|
46 |
} |
|
47 |
||
48 |
||
49 |
/** SignalType short names. */ |
|
50 |
static const char* signal_type_names[] = { |
|
51 |
"NORMAL", "ENTRY", "EXIT", "COMBO", |
|
52 |
}; |
|
53 |
||
54 |
/** Return name of given SignalType. */ |
|
55 |
CStrA ValueStr(SignalType t) |
|
56 |
{ |
|
57 |
CStrA out; |
|
58 |
out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK")); |
|
59 |
return out.Transfer(); |
|
60 |
} |
|
61 |
||
62 |
||
63 |
/** Translate TileIndex into string. */ |
|
64 |
CStrA TileStr(TileIndex tile) |
|
65 |
{ |
|
66 |
CStrA out; |
|
67 |
out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile)); |
|
68 |
return out.Transfer(); |
|
69 |
} |
|
70 |
||
71 |
/** Keep track of the last assigned type_id. Used for anti-recursion. */ |
|
72 |
/*static*/ size_t& DumpTarget::LastTypeId() |
|
73 |
{ |
|
74 |
static size_t last_type_id = 0; |
|
75 |
return last_type_id; |
|
76 |
} |
|
77 |
||
78 |
/** Return structured name of the current class/structure. */ |
|
79 |
CStrA DumpTarget::GetCurrentStructName() |
|
80 |
{ |
|
81 |
CStrA out; |
|
82 |
if (!m_cur_struct.empty()) { |
|
83 |
// we are inside some named struct, return its name |
|
84 |
out = m_cur_struct.top(); |
|
85 |
} |
|
86 |
return out.Transfer(); |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Find the given instance in our anti-recursion repository. |
|
91 |
* Return true and set name when object was found. |
|
92 |
*/ |
|
93 |
bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name) |
|
94 |
{ |
|
95 |
KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr)); |
|
96 |
if (it != m_known_names.end()) { |
|
97 |
/* we have found it */ |
|
98 |
name = (*it).second; |
|
99 |
return true; |
|
100 |
} |
|
101 |
return false; |
|
102 |
} |
|
103 |
||
104 |
/** Write some leading spaces into the output. */ |
|
105 |
void DumpTarget::WriteIndent() |
|
106 |
{ |
|
107 |
int num_spaces = 2 * m_indent; |
|
108 |
memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces); |
|
109 |
} |
|
110 |
||
111 |
/** Write a line with indent at the beginning and <LF> at the end. */ |
|
112 |
void DumpTarget::WriteLine(const char *format, ...) |
|
113 |
{ |
|
114 |
WriteIndent(); |
|
115 |
va_list args; |
|
116 |
va_start(args, format); |
|
117 |
m_out.AddFormatL(format, args); |
|
118 |
va_end(args); |
|
119 |
m_out.AppendStr("\n"); |
|
120 |
} |
|
121 |
||
122 |
/** Write 'name = value' with indent and new-line. */ |
|
123 |
void DumpTarget::WriteValue(const char *name, const char *value_str) |
|
124 |
{ |
|
125 |
WriteIndent(); |
|
126 |
m_out.AddFormat("%s = %s\n", name, value_str); |
|
127 |
} |
|
128 |
||
129 |
/** Write name & TileIndex to the output. */ |
|
130 |
void DumpTarget::WriteTile(const char *name, TileIndex tile) |
|
131 |
{ |
|
132 |
WriteIndent(); |
|
133 |
m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data()); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Open new structure (one level deeper than the current one) 'name = {<LF>'. |
|
138 |
*/ |
|
139 |
void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr) |
|
140 |
{ |
|
141 |
/* make composite name */ |
|
142 |
CStrA cur_name = GetCurrentStructName().Transfer(); |
|
143 |
if (cur_name.Size() > 0) { |
|
144 |
/* add name delimiter (we use structured names) */ |
|
145 |
cur_name.AppendStr("."); |
|
146 |
} |
|
147 |
cur_name.AppendStr(name); |
|
148 |
||
149 |
/* put the name onto stack (as current struct name) */ |
|
150 |
m_cur_struct.push(cur_name); |
|
151 |
||
152 |
/* put it also to the map of known structures */ |
|
153 |
m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name)); |
|
154 |
||
155 |
WriteIndent(); |
|
156 |
m_out.AddFormat("%s = {\n", name); |
|
157 |
m_indent++; |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Close structure '}<LF>'. |
|
162 |
*/ |
|
163 |
void DumpTarget::EndStruct() |
|
164 |
{ |
|
165 |
m_indent--; |
|
166 |
WriteIndent(); |
|
167 |
m_out.AddFormat("}\n"); |
|
168 |
||
169 |
/* remove current struct name from the stack */ |
|
170 |
m_cur_struct.pop(); |
|
171 |
} |
|
172 |