winstr.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_CORE1_SEG
  12. #pragma code_seg(AFX_CORE1_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Windows extensions to strings
  20. #ifdef _UNICODE
  21. #define CHAR_FUDGE 1 // one TCHAR unused is good enough
  22. #else
  23. #define CHAR_FUDGE 2 // two BYTES unused for case of DBC last char
  24. #endif
  25. BOOL CString::LoadString(UINT nID)
  26. {
  27. // try fixed buffer first (to avoid wasting space in the heap)
  28. TCHAR szTemp[256];
  29. int nLen = AfxLoadString(nID, szTemp, _countof(szTemp));
  30. if (_countof(szTemp) - nLen > CHAR_FUDGE)
  31. {
  32. *this = szTemp;
  33. return nLen > 0;
  34. }
  35. // try buffer size of 512, then larger size until entire string is retrieved
  36. int nSize = 256;
  37. do
  38. {
  39. nSize += 256;
  40. nLen = AfxLoadString(nID, GetBuffer(nSize-1), nSize);
  41. } while (nSize - nLen <= CHAR_FUDGE);
  42. ReleaseBuffer();
  43. return nLen > 0;
  44. }
  45. #ifndef _AFXDLL
  46. int AFXAPI AfxLoadString(UINT nID, LPTSTR lpszBuf, UINT nMaxBuf)
  47. {
  48. ASSERT(AfxIsValidAddress(lpszBuf, nMaxBuf*sizeof(TCHAR)));
  49. #ifdef _DEBUG
  50. // LoadString without annoying warning from the Debug kernel if the
  51. // segment containing the string is not present
  52. if (::FindResource(AfxGetResourceHandle(),
  53. MAKEINTRESOURCE((nID>>4)+1), RT_STRING) == NULL)
  54. {
  55. lpszBuf[0] = '\0';
  56. return 0; // not found
  57. }
  58. #endif //_DEBUG
  59. int nLen = ::LoadString(AfxGetResourceHandle(), nID, lpszBuf, nMaxBuf);
  60. if (nLen == 0)
  61. lpszBuf[0] = '\0';
  62. return nLen;
  63. }
  64. #endif
  65. /////////////////////////////////////////////////////////////////////////////
  66. BOOL AFXAPI AfxExtractSubString(CString& rString, LPCTSTR lpszFullString,
  67. int iSubString, TCHAR chSep)
  68. {
  69. if (lpszFullString == NULL)
  70. return FALSE;
  71. while (iSubString--)
  72. {
  73. lpszFullString = _tcschr(lpszFullString, chSep);
  74. if (lpszFullString == NULL)
  75. {
  76. rString.Empty(); // return empty string as well
  77. return FALSE;
  78. }
  79. lpszFullString++; // point past the separator
  80. }
  81. LPCTSTR lpchEnd = _tcschr(lpszFullString, chSep);
  82. int nLen = (lpchEnd == NULL) ?
  83. lstrlen(lpszFullString) : (int)(lpchEnd - lpszFullString);
  84. ASSERT(nLen >= 0);
  85. memcpy(rString.GetBufferSetLength(nLen), lpszFullString, nLen*sizeof(TCHAR));
  86. return TRUE;
  87. }
  88. /////////////////////////////////////////////////////////////////////////////