home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectInput / DIConfig / cyclestr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  3.2 KB  |  174 lines

  1. //-----------------------------------------------------------------------------
  2. // File: cyclestr.cpp
  3. //
  4. // Desc: Implements a circular queue that provides space to hold a string
  5. //       without repeatedly allocating and deallocating memory.  This is
  6. //       only for short-term use such as outputting debug message to
  7. //       ensure that the same buffer is not used at more than one place
  8. //       simultaneously.
  9. //
  10. // Copyright (C) 1999-2001 Microsoft Corporation. All Rights Reserved.
  11. //-----------------------------------------------------------------------------
  12.  
  13. #include "common.hpp"
  14.  
  15.  
  16. static const int g_cycles = 16;
  17. static const int g_cyclelen = 256;
  18.  
  19.  
  20. LPTSTR getcyclestr()
  21. {
  22.     static int on = g_cycles;
  23.     static TCHAR cycle[g_cycles][g_cyclelen];
  24.  
  25.     on++;
  26.     if (on >= g_cycles)
  27.         on = 0;
  28.  
  29.     return cycle[on];
  30. }
  31.  
  32. inline static void cap(LPTSTR c)
  33. {
  34.     c[g_cyclelen - 1] = 0;
  35. }
  36.  
  37. #define SAFESTR_noconv \
  38.     if (str) \
  39.         return str; \
  40.     else \
  41.         return _T("NULL");
  42.  
  43. #define SAFESTR_conv \
  44.     LPTSTR c = getcyclestr(); \
  45.     CopyStr(c, str, g_cyclelen); \
  46.     cap(c); \
  47.     return SAFESTR(c);
  48.  
  49. LPCTSTR SAFESTR(LPCWSTR str)
  50. {
  51. #ifdef UNICODE
  52.     SAFESTR_noconv
  53. #else
  54.     SAFESTR_conv    
  55. #endif
  56. }
  57.  
  58. LPCTSTR SAFESTR(LPCSTR str)
  59. {
  60. #ifdef UNICODE
  61.     SAFESTR_conv
  62. #else
  63.     SAFESTR_noconv
  64. #endif
  65. }
  66.  
  67. #define QSAFESTR_doit(tf) \
  68.     if (!str) \
  69.         return _T("NULL"); \
  70. \
  71.     LPTSTR c = getcyclestr(); \
  72.     _sntprintf(c, g_cyclelen, _T("\"") tf _T("\""), str); \
  73.     cap(c); \
  74.     return c;
  75.  
  76. LPCTSTR QSAFESTR(LPCWSTR str)
  77. {
  78.     QSAFESTR_doit(_tfWSTR)
  79. }
  80.  
  81. LPCTSTR QSAFESTR(LPCSTR str)
  82. {
  83.     QSAFESTR_doit(_tfSTR)
  84. }
  85.  
  86. LPCTSTR BOOLSTR(BOOL b)
  87. {
  88.     return b ? _T("TRUE"): _T("FALSE");
  89. }
  90.  
  91. LPCTSTR RECTSTR(RECT &rect)
  92. {
  93.     LPTSTR c = getcyclestr();
  94.     _sntprintf(c, g_cyclelen, _T("{%d,%d,%d,%d}"),
  95.         rect.left,
  96.         rect.top,
  97.         rect.right,
  98.         rect.bottom);
  99.     cap(c);
  100.     return c;
  101. }
  102.  
  103. LPCTSTR RECTDIMSTR(RECT &rect)
  104. {
  105.     LPTSTR c = getcyclestr();
  106.     _sntprintf(c, g_cyclelen, _T("{%dx%d}"),
  107.         rect.right - rect.left,
  108.         rect.bottom - rect.top);
  109.     cap(c);
  110.     return c;
  111. }
  112.  
  113. LPCTSTR POINTSTR(POINT &point)
  114. {
  115.     LPTSTR c = getcyclestr();
  116.     _sntprintf(c, g_cyclelen, _T("{%d,%d}"),
  117.         point.x,
  118.         point.y);
  119.     cap(c);
  120.     return c;
  121. }
  122.  
  123. LPCTSTR GUIDSTR(const GUID &guid)
  124. {
  125.     LPTSTR c = getcyclestr();
  126.  
  127.     wsprintf(c, _T("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
  128.              guid.Data1, guid.Data2, guid.Data3,
  129.              guid.Data4[0], guid.Data4[1],
  130.              guid.Data4[2], guid.Data4[3],
  131.              guid.Data4[4], guid.Data4[5],
  132.              guid.Data4[6], guid.Data4[7]);
  133.  
  134.     cap(c);
  135.  
  136.     return c;
  137. }
  138.  
  139. template<class T>
  140. static LPCTSTR _superstr(const T *str)
  141. {
  142.     static LPCTSTR
  143.         prefix = _T("(\""),
  144.         midfix = _T("\",\""),
  145.         suffix = _T("\")");
  146.  
  147.     LPTSTR c = getcyclestr(), c2 = getcyclestr();
  148.     c[0] =     0;
  149.     
  150.     for (int i = 0, n = CountSubStrings(str); i < n; i++)
  151.     {
  152.         if (!i)
  153.             _tcsncat(c, prefix, g_cyclelen);
  154.         else
  155.             _tcsncat(c, midfix, g_cyclelen);
  156.  
  157.         CopyStr(c2, GetSubString(str, i), g_cyclelen);
  158.         cap(c2);
  159.  
  160.         _tcsncat(c, c2, g_cyclelen);
  161.         cap(c);
  162.  
  163.         if (i == n - 1)
  164.             _tcsncat(c, suffix, g_cyclelen);
  165.     }
  166.  
  167.     cap(c);
  168.  
  169.     return c;
  170. }
  171.  
  172. LPCTSTR SUPERSTR(LPCWSTR str) {return _superstr(str);}
  173. LPCTSTR SUPERSTR(LPCSTR str) {return _superstr(str);}
  174.