# dbg_helpers.cpp

Go to the documentation of this file.

    00001 /* $Id: dbg_helpers.cpp 17596 2009-09-20 23:11:01Z smatz $ */
    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()
    00077 {
    00078   static size_t last_type_id = 0;
    00079   return last_type_id;
    00080 }
    00081
    00083 CStrA DumpTarget::GetCurrentStructName()
    00084 {
    00085   CStrA out;
    00086   if (!m_cur_struct.empty()) {
    00087     /* we are inside some named struct, return its name */
    00088     out = m_cur_struct.top();
    00089   }
    00090   return out.Transfer();
    00091 }
    00092
    00097 bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
    00098 {
    00099   KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
    00100   if (it != m_known_names.end()) {
    00101     /* we have found it */
    00102     name = (*it).second;
    00103     return true;
    00104   }
    00105   return false;
    00106 }
    00107
    00109 void DumpTarget::WriteIndent()
    00110 {
    00111   int num_spaces = 2 * m_indent;
    00112   memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
    00113 }
    00114
    00116 void DumpTarget::WriteLine(const char *format, ...)
    00117 {
    00118   WriteIndent();
    00119   va_list args;
    00120   va_start(args, format);
    00121   m_out.AddFormatL(format, args);
    00122   va_end(args);
    00123   m_out.AppendStr("\n");
    00124 }
    00125
    00127 void DumpTarget::WriteValue(const char *name, const char *value_str)
    00128 {
    00129   WriteIndent();
    00130   m_out.AddFormat("%s = %s\n", name, value_str);
    00131 }
    00132
    00134 void DumpTarget::WriteTile(const char *name, TileIndex tile)
    00135 {
    00136   WriteIndent();
    00137   m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
    00138 }
    00139
    00143 void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
    00144 {
    00145   /* make composite name */
    00146   CStrA cur_name = GetCurrentStructName().Transfer();
    00147   if (cur_name.Size() > 0) {
    00148     /* add name delimiter (we use structured names) */
    00149     cur_name.AppendStr(".");
    00150   }
    00151   cur_name.AppendStr(name);
    00152
    00153   /* put the name onto stack (as current struct name) */
    00154   m_cur_struct.push(cur_name);
    00155
    00156   /* put it also to the map of known structures */
    00157   m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
    00158
    00159   WriteIndent();
    00160   m_out.AddFormat("%s = {\n", name);
    00161   m_indent++;
    00162 }
    00163
    00167 void DumpTarget::EndStruct()
    00168 {
    00169   m_indent--;
    00170   WriteIndent();
    00171   m_out.AddFormat("}\n");
    00172
    00173   /* remove current struct name from the stack */
    00174   m_cur_struct.pop();
    00175 }
    00176
    00178 /* static */ const CBlobBaseSimple::CHdr CBlobBaseSimple::hdrEmpty[] = {{0, 0}, {0, 0}};