Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1999 - 1999
  5. //
  6. // File: string.cpp
  7. //
  8. // Contents: Utility functions for the CString class
  9. //
  10. // History: 10-Aug-99 VivekJ Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #include <stdafx.h>
  14. /*+-------------------------------------------------------------------------*
  15. *
  16. * LoadString
  17. *
  18. * PURPOSE: A function to load strings from the string module, not the AfxModule
  19. *
  20. * PARAMETERS:
  21. * CString & str :
  22. * UINT nID :
  23. *
  24. * RETURNS:
  25. * BOOL
  26. *
  27. *+-------------------------------------------------------------------------*/
  28. BOOL LoadString(CString &str, UINT nID)
  29. {
  30. const size_t STRING_LEN_INCREMENT = 256;
  31. str.Empty();
  32. // try fixed buffer first (to avoid wasting space in the heap)
  33. static TCHAR szTemp[STRING_LEN_INCREMENT];
  34. int nLen = ::LoadString(GetStringModule(), nID, szTemp, countof(szTemp));
  35. if (countof(szTemp) - nLen > 1)
  36. {
  37. szTemp[nLen] = 0;
  38. str = szTemp;
  39. return nLen > 0;
  40. }
  41. // try buffer size of 2*STRING_LEN_INCREMENT, then larger size until entire string is retrieved
  42. int nSize = STRING_LEN_INCREMENT;
  43. do
  44. {
  45. nSize += STRING_LEN_INCREMENT;
  46. nLen = ::LoadString(GetStringModule(), nID, str.GetBuffer(nSize-1), nSize);
  47. } while (nSize - nLen <= 1);
  48. str.ReleaseBuffer();
  49. return (nLen > 0);
  50. }
  51. /*+-------------------------------------------------------------------------*
  52. *
  53. * FormatStrings
  54. *
  55. * PURPOSE: Similar to AfxFormatStrings, but uses GetStringModule() instead of
  56. * AfxGetModuleInstance.
  57. *
  58. * PARAMETERS:
  59. * CString& rString :
  60. * UINT nIDS :
  61. * LPCTSTR const :
  62. * int nString :
  63. *
  64. * RETURNS:
  65. * void
  66. *
  67. *+-------------------------------------------------------------------------*/
  68. void FormatStrings(CString& rString, UINT nIDS, LPCTSTR const* rglpsz, int nString)
  69. {
  70. // empty the result (in case we fail)
  71. rString.Empty();
  72. // get the format string.
  73. CString strFormat;
  74. if (!LoadString(strFormat, nIDS))
  75. {
  76. TraceError(_T("FormatStrings"), SC(E_INVALIDARG));
  77. return; // failed...
  78. }
  79. AfxFormatStrings(rString, strFormat, rglpsz, nString);
  80. }
  81. /*+-------------------------------------------------------------------------*
  82. *
  83. * FormatString1
  84. *
  85. * PURPOSE: Similar to AfxFormatString1, but uses GetStringModule() instead
  86. * of AfxGetModuleInstance()
  87. *
  88. * PARAMETERS:
  89. * CString& rString :
  90. * UINT nIDS :
  91. * LPCTSTR lpsz1 :
  92. *
  93. * RETURNS:
  94. * void
  95. *
  96. *+-------------------------------------------------------------------------*/
  97. void FormatString1(CString& rString, UINT nIDS, LPCTSTR lpsz1)
  98. {
  99. FormatStrings(rString, nIDS, &lpsz1, 1);
  100. }
  101. /*+-------------------------------------------------------------------------*
  102. *
  103. * FormatString2
  104. *
  105. * PURPOSE: Similar to AfxFormatString2, but uses GetStringModule() instead
  106. * of AfxGetModuleInstance()
  107. *
  108. * PARAMETERS:
  109. * CString& rString :
  110. * UINT nIDS :
  111. * LPCTSTR lpsz1 :
  112. * LPCTSTR lpsz2 :
  113. *
  114. * RETURNS:
  115. * void
  116. *
  117. *+-------------------------------------------------------------------------*/
  118. void FormatString2(CString& rString, UINT nIDS, LPCTSTR lpsz1, LPCTSTR lpsz2)
  119. {
  120. LPCTSTR rglpsz[2];
  121. rglpsz[0] = lpsz1;
  122. rglpsz[1] = lpsz2;
  123. FormatStrings(rString, nIDS, rglpsz, 2);
  124. }