# dbg_helpers.cpp

Go to the documentation of this file.

    00001 /* $Id: dbg_helpers.cpp 11684 2007-12-23 10:56:02Z rubidium $ */
    00002
    00004 #include "../stdafx.h"
    00005 #include "../openttd.h"
    00006 #include "../direction_type.h"
    00007 #include "../rail.h"
    00008 #include "../rail_map.h"
    00009 #include "dbg_helpers.h"
    00010
    00012 static const char* trackdir_names[] = {
    00013   "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
    00014   "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
    00015 };
    00016
    00018 CStrA ValueStr(Trackdir td)
    00019 {
    00020   CStrA out;
    00021   out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
    00022   return out.Transfer();
    00023 }
    00024
    00026 CStrA ValueStr(TrackdirBits td_bits)
    00027 {
    00028   CStrA out;
    00029   out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data());
    00030   return out.Transfer();
    00031 }
    00032
    00033
    00035 static const char* diagdir_names[] = {
    00036   "NE", "SE", "SW", "NW",
    00037 };
    00038
    00040 CStrA ValueStr(DiagDirection dd)
    00041 {
    00042   CStrA out;
    00043   out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
    00044   return out.Transfer();
    00045 }
    00046
    00047
    00049 static const char* signal_type_names[] = {
    00050   "NORMAL", "ENTRY", "EXIT", "COMBO",
    00051 };
    00052
    00054 CStrA ValueStr(SignalType t)
    00055 {
    00056   CStrA out;
    00057   out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
    00058   return out.Transfer();
    00059 }
    00060
    00061
    00063 CStrA TileStr(TileIndex tile)
    00064 {
    00065   CStrA out;
    00066   out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile));
    00067   return out.Transfer();
    00068 }
    00069
    00071 /*static*/ size_t& DumpTarget::LastTypeId()
    00072 {
    00073   static size_t last_type_id = 0;
    00074   return last_type_id;
    00075 }
    00076
    00078 CStrA DumpTarget::GetCurrentStructName()
    00079 {
    00080   CStrA out;
    00081   if (!m_cur_struct.empty()) {
    00082     // we are inside some named struct, return its name
    00083     out = m_cur_struct.top();
    00084   }
    00085   return out.Transfer();
    00086 }
    00087
    00092 bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
    00093 {
    00094   KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
    00095   if (it != m_known_names.end()) {
    00096     /* we have found it */
    00097     name = (*it).second;
    00098     return true;
    00099   }
    00100   return false;
    00101 }
    00102
    00104 void DumpTarget::WriteIndent()
    00105 {
    00106   int num_spaces = 2 * m_indent;
    00107   memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
    00108 }
    00109
    00111 void DumpTarget::WriteLine(const char *format, ...)
    00112 {
    00113   WriteIndent();
    00114   va_list args;
    00115   va_start(args, format);
    00116   m_out.AddFormatL(format, args);
    00117   va_end(args);
    00118   m_out.AppendStr("\n");
    00119 }
    00120
    00122 void DumpTarget::WriteValue(const char *name, const char *value_str)
    00123 {
    00124   WriteIndent();
    00125   m_out.AddFormat("%s = %s\n", name, value_str);
    00126 }
    00127
    00129 void DumpTarget::WriteTile(const char *name, TileIndex tile)
    00130 {
    00131   WriteIndent();
    00132   m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
    00133 }
    00134
    00138 void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
    00139 {
    00140   /* make composite name */
    00141   CStrA cur_name = GetCurrentStructName().Transfer();
    00142   if (cur_name.Size() > 0) {
    00143     /* add name delimiter (we use structured names) */
    00144     cur_name.AppendStr(".");
    00145   }
    00146   cur_name.AppendStr(name);
    00147
    00148   /* put the name onto stack (as current struct name) */
    00149   m_cur_struct.push(cur_name);
    00150
    00151   /* put it also to the map of known structures */
    00152   m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
    00153
    00154   WriteIndent();
    00155   m_out.AddFormat("%s = {\n", name);
    00156   m_indent++;
    00157 }
    00158
    00162 void DumpTarget::EndStruct()
    00163 {
    00164   m_indent--;
    00165   WriteIndent();
    00166   m_out.AddFormat("}\n");
    00167
    00168   /* remove current struct name from the stack */
    00169   m_cur_struct.pop();
    00170 }
    00171