dynastr.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 4k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "priv.h"
  2. #include "dynastr.h"
  3. #define DM_DYNASTR  DM_TRACE
  4. LPWSTR DynaStrW::_Realloc(UINT cch)
  5. {
  6.     ASSERT(cch >= CCH_DYNASTR_MIN);
  7.     UINT cb = cch*SIZEOF(_ach[0]);
  8.     if (_psz != _ach) {
  9.         if ( LocalSize(_psz) < cb ) {
  10.             LPWSTR pszNew = (LPWSTR)LocalReAlloc(_psz, cb, LMEM_MOVEABLE);
  11.             TraceMsg(DM_DYNASTR, "DynaStrW::_Realloc reallocated memory %x->%x", _psz, pszNew);
  12.             if (!pszNew) {
  13.                 return NULL;
  14.             }
  15.             _psz = pszNew;
  16.         }
  17.     } else {
  18.         LPWSTR pszNew = (LPWSTR)LocalAlloc(LPTR, cb);
  19.         TraceMsg(DM_DYNASTR, "DynaStrW::_Realloc allocated memory %x", pszNew);
  20.         if (!pszNew) {
  21.             return NULL;
  22.         }
  23.         _psz = pszNew;
  24.     }
  25.     return _psz;
  26. }
  27. BOOL DynaStrW::lstrcpy(LPCWSTR pwszSrc)
  28. {
  29.     UINT cch = lstrlenW(pwszSrc);
  30.     if (cch < CCH_DYNASTR_MIN || _Realloc(cch+1)) {
  31.         ASSERT(_psz==_ach || LocalSize(_psz) >= cch+1*SIZEOF(_psz[0]));
  32.         memcpy(_psz, pwszSrc, (cch+1)*SIZEOF(_psz[0]));
  33.         return TRUE;
  34.     }
  35.     return FALSE;
  36. }
  37. BOOL DynaStrW::lstrcpy(LPCSTR pszSrc)
  38. {
  39.     UINT cch = MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, NULL, 0);
  40.     if (cch < CCH_DYNASTR_MIN || _Realloc(cch+1)) {
  41.         ASSERT(_psz==_ach || LocalSize(_psz) >= cch+1*SIZEOF(_psz[0]));
  42.         MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, _psz, cch+1);
  43.         return TRUE;
  44.     }
  45.     return FALSE;
  46. }
  47. DynaStrW::~DynaStrW()
  48. {
  49.     if (_psz != _ach) {
  50.         TraceMsg(DM_DYNASTR, "dtr ~DynaStrW freeing allocated memory %x", _psz);
  51.         LocalFree(_psz);
  52.     }
  53. }
  54. LPSTR DynaStrA::_Realloc(UINT cch)
  55. {
  56.     ASSERT(cch >= CCH_DYNASTR_MIN);
  57.     UINT cb = cch*SIZEOF(_ach[0]);
  58.     if (_psz != _ach) {
  59.         if ( LocalSize(_psz) < cb ) {
  60.             LPSTR pszNew = (LPSTR)LocalReAlloc(_psz, cb, LMEM_MOVEABLE);
  61.             TraceMsg(DM_DYNASTR, "DynaStrA::_Realloc reallocated memory %x->%x", _psz, pszNew);
  62.             if (!pszNew) {
  63.                 return NULL;
  64.             }
  65.             _psz = pszNew;
  66.         }
  67.     } else {
  68.         LPSTR pszNew = (LPSTR)LocalAlloc(LPTR, cb);
  69.         TraceMsg(DM_DYNASTR, "DynaStrA::_Realloc allocated memory %x", pszNew);
  70.         if (!pszNew) {
  71.             return NULL;
  72.         }
  73.         _psz = pszNew;
  74.     }
  75.     return _psz;
  76. }
  77. BOOL DynaStrA::lstrcpy(LPCSTR pszSrc)
  78. {
  79.     UINT cch = lstrlen(pszSrc);
  80.     if (cch < CCH_DYNASTR_MIN || _Realloc(cch+1)) {
  81.         ASSERT(_psz==_ach || LocalSize(_psz) >= cch+1*SIZEOF(_psz[0]));
  82.         memcpy(_psz, pszSrc, (cch+1)*SIZEOF(_psz[0]));
  83.         return TRUE;
  84.     }
  85.     return FALSE;
  86. }
  87. BOOL DynaStrA::lstrcpy(LPCWSTR pwszSrc)
  88. {
  89.     UINT cch = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
  90.     if (cch < CCH_DYNASTR_MIN || _Realloc(cch+1)) {
  91.         ASSERT(_psz==_ach || LocalSize(_psz) >= cch+1*SIZEOF(_psz[0]));
  92.         WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, _psz, cch+1, NULL, NULL);
  93.         return TRUE;
  94.     }
  95.     return FALSE;
  96. }
  97. DynaStrA::~DynaStrA()
  98. {
  99.     if (_psz != _ach) {
  100.         TraceMsg(DM_DYNASTR, "dtr ~DynaStrA freeing allocated memory %x", _psz);
  101.         LocalFree(_psz);
  102.     }
  103. }
  104. // Some test cases
  105. #if 0
  106. extern HRESULT test0(LPCWSTR pwsz);
  107. //
  108. // Code size:   58 bytes
  109. // Stack frame: 4096+
  110. //
  111. HRESULT test1(LPSTR pszIn)
  112. {
  113.     WCHAR wsz[MAX_URL_STRING*2];
  114.     MultiByteToWideChar(CP_ACP, 0, pszIn, -1, wsz, ARRAYSIZE(wsz));
  115.     return test0(wsz);
  116. }
  117. //
  118. // Code size:   70 bytes
  119. // Stack frame: 0+
  120. //
  121. HRESULT test2(LPSTR pszIn)
  122. {
  123.     LPWSTR pwsz = (LPWSTR)LocalAlloc(LPTR, 2*MAX_URL_STRING*SIZEOF(WCHAR));
  124.     HRESULT hres;
  125.     if (pwsz) {
  126.         MultiByteToWideChar(CP_ACP, 0, pszIn, -1, pwsz, MAX_URL_STRING);
  127.         hres = test0(pwsz);
  128.         LocalFree(pwsz);
  129.     } else {
  130.         hres = E_OUTOFMEMORY;
  131.     }
  132.     return hres;
  133. }
  134. //
  135. // Code size:   68 bytes
  136. // Stack frame: 0+
  137. //
  138. HRESULT test3(LPSTR pszIn)
  139. {
  140.     DynaStrW wsz;
  141.     HRESULT hres;
  142.     if (wsz.lstrcpy(pszIn)) {
  143.         hres = test0(wsz);
  144.     } else {
  145.         hres = E_OUTOFMEMORY;
  146.     }
  147.     return hres;
  148. }
  149. #endif