# dbg_helpers.cpp

Go to the documentation of this file.

    00001 /* $Id: dbg_helpers.cpp 19134 2010-02-14 18:33:57Z 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()
    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   if (num_spaces > 0) {
    00113     memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
    00114   }
    00115 }
    00116
    00118 void DumpTarget::WriteLine(const char *format, ...)
    00119 {
    00120   WriteIndent();
    00121   va_list args;
    00122   va_start(args, format);
    00123   m_out.AddFormatL(format, args);
    00124   va_end(args);
    00125   m_out.AppendStr("\n");
    00126 }
    00127
    00129 void DumpTarget::WriteValue(const char *name, const char *value_str)
    00130 {
    00131   WriteIndent();
    00132   m_out.AddFormat("%s = %s\n", name, value_str);
    00133 }
    00134
    00136 void DumpTarget::WriteTile(const char *name, TileIndex tile)
    00137 {
    00138   WriteIndent();
    00139   m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
    00140 }
    00141
    00145 void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
    00146 {
    00147   /* make composite name */
    00148   CStrA cur_name = GetCurrentStructName().Transfer();
    00149   if (cur_name.Size() > 0) {
    00150     /* add name delimiter (we use structured names) */
    00151     cur_name.AppendStr(".");
    00152   }
    00153   cur_name.AppendStr(name);
    00154
    00155   /* put the name onto stack (as current struct name) */
    00156   m_cur_struct.push(cur_name);
    00157
    00158   /* put it also to the map of known structures */
    00159   m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
    00160
    00161   WriteIndent();
    00162   m_out.AddFormat("%s = {\n", name);
    00163   m_indent++;
    00164 }
    00165
    00169 void DumpTarget::EndStruct()
    00170 {
    00171   m_indent--;
    00172   WriteIndent();
    00173   m_out.AddFormat("}\n");
    00174
    00175   /* remove current struct name from the stack */
    00176   m_cur_struct.pop();
    00177 }
    00178
    00180 /* static */ CBlobBaseSimple::CHdr CBlobBaseSimple::hdrEmpty[] = {{0, 0}, {0, 0}};