# dbg_helpers.cpp

Go to the documentation of this file.

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