# dbg_helpers.cpp

Go to the documentation of this file.

    00001 /* $Id: dbg_helpers.cpp 20283 2010-08-01 19:22:34Z frosch $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "../rail_map.h"
    00014 #include "dbg_helpers.h"
    00015
    00017 static const char * const trackdir_names[] = {
    00018   "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
    00019   "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
    00020 };
    00021
    00023 CStrA ValueStr(Trackdir td)
    00024 {
    00025   CStrA out;
    00026   out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
    00027   return out.Transfer();
    00028 }
    00029
    00031 CStrA ValueStr(TrackdirBits td_bits)
    00032 {
    00033   CStrA out;
    00034   out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data());
    00035   return out.Transfer();
    00036 }
    00037
    00038
    00040 static const char * const diagdir_names[] = {
    00041   "NE", "SE", "SW", "NW",
    00042 };
    00043
    00045 CStrA ValueStr(DiagDirection dd)
    00046 {
    00047   CStrA out;
    00048   out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
    00049   return out.Transfer();
    00050 }
    00051
    00052
    00054 static const char * const signal_type_names[] = {
    00055   "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
    00056 };
    00057
    00059 CStrA ValueStr(SignalType t)
    00060 {
    00061   CStrA out;
    00062   out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
    00063   return out.Transfer();
    00064 }
    00065
    00066
    00068 CStrA TileStr(TileIndex tile)
    00069 {
    00070   CStrA out;
    00071   out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile));
    00072   return out.Transfer();
    00073 }
    00074  size_t& DumpTarget::LastTypeId()
    00078 {
    00079   static size_t last_type_id = 0;
    00080   return last_type_id;
    00081 }
    00082
    00084 CStrA DumpTarget::GetCurrentStructName()
    00085 {
    00086   CStrA out;
    00087   if (!m_cur_struct.empty()) {
    00088     /* we are inside some named struct, return its name */
    00089     out = m_cur_struct.top();
    00090   }
    00091   return out.Transfer();
    00092 }
    00093
    00098 bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
    00099 {
    00100   KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
    00101   if (it != m_known_names.end()) {
    00102     /* we have found it */
    00103     name = (*it).second;
    00104     return true;
    00105   }
    00106   return false;
    00107 }
    00108
    00110 void DumpTarget::WriteIndent()
    00111 {
    00112   int num_spaces = 2 * m_indent;
    00113   if (num_spaces > 0) {
    00114     memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
    00115   }
    00116 }
    00117
    00119 void DumpTarget::WriteLine(const char *format, ...)
    00120 {
    00121   WriteIndent();
    00122   va_list args;
    00123   va_start(args, format);
    00124   m_out.AddFormatL(format, args);
    00125   va_end(args);
    00126   m_out.AppendStr("\n");
    00127 }
    00128
    00130 void DumpTarget::WriteValue(const char *name, const char *value_str)
    00131 {
    00132   WriteIndent();
    00133   m_out.AddFormat("%s = %s\n", name, value_str);
    00134 }
    00135
    00137 void DumpTarget::WriteTile(const char *name, TileIndex tile)
    00138 {
    00139   WriteIndent();
    00140   m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
    00141 }
    00142
    00146 void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
    00147 {
    00148   /* make composite name */
    00149   CStrA cur_name = GetCurrentStructName().Transfer();
    00150   if (cur_name.Size() > 0) {
    00151     /* add name delimiter (we use structured names) */
    00152     cur_name.AppendStr(".");
    00153   }
    00154   cur_name.AppendStr(name);
    00155
    00156   /* put the name onto stack (as current struct name) */
    00157   m_cur_struct.push(cur_name);
    00158
    00159   /* put it also to the map of known structures */
    00160   m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
    00161
    00162   WriteIndent();
    00163   m_out.AddFormat("%s = {\n", name);
    00164   m_indent++;
    00165 }
    00166
    00170 void DumpTarget::EndStruct()
    00171 {
    00172   m_indent--;
    00173   WriteIndent();
    00174   m_out.AddFormat("}\n");
    00175
    00176   /* remove current struct name from the stack */
    00177   m_cur_struct.pop();
    00178 }
    00179
    00181 /* static */ ByteBlob::BlobHeader ByteBlob::hdrEmpty[] = {{0, 0}, {0, 0}};