apphelp.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_CORE3_SEG
  12. #pragma code_seg(AFX_CORE3_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Help and other support
  20. // Strings in format ".....%1 .... %2 ...." etc.
  21. void AFXAPI AfxFormatStrings(CString& rString, UINT nIDS,
  22. LPCTSTR const* rglpsz, int nString)
  23. {
  24. TCHAR szFormat[256];
  25. if (!AfxLoadString(nIDS, szFormat) != 0)
  26. {
  27. TRACE1("Error: failed to load AfxFormatString string 0x%04x.\n", nIDS);
  28. ASSERT(FALSE);
  29. return;
  30. }
  31. AfxFormatStrings(rString, szFormat, rglpsz, nString);
  32. }
  33. void AFXAPI AfxFormatStrings(CString& rString, LPCTSTR lpszFormat,
  34. LPCTSTR const* rglpsz, int nString)
  35. {
  36. // determine length of destination string
  37. int nTotalLen = 0;
  38. LPCTSTR pchSrc = lpszFormat;
  39. while (*pchSrc != '\0')
  40. {
  41. if (pchSrc[0] == '%' &&
  42. ( (pchSrc[1] >= '0' && pchSrc[1] <= '9') ||
  43. (pchSrc[1] >= 'A' && pchSrc[1] <= 'Z')) )
  44. {
  45. // %A comes after %9 -- we'll need it someday
  46. int i;
  47. if (pchSrc[1] > '9')
  48. i = 9 + (pchSrc[1] - 'A');
  49. else
  50. i = pchSrc[1] - '1';
  51. pchSrc += 2;
  52. if (i >= nString)
  53. ++nTotalLen;
  54. else if (rglpsz[i] != NULL)
  55. nTotalLen += lstrlen(rglpsz[i]);
  56. }
  57. else
  58. {
  59. if (_istlead(*pchSrc))
  60. ++nTotalLen, ++pchSrc;
  61. ++pchSrc;
  62. ++nTotalLen;
  63. }
  64. }
  65. pchSrc = lpszFormat;
  66. LPTSTR pchDest = rString.GetBuffer(nTotalLen);
  67. while (*pchSrc != '\0')
  68. {
  69. if (pchSrc[0] == '%' &&
  70. ( (pchSrc[1] >= '0' && pchSrc[1] <= '9') ||
  71. (pchSrc[1] >= 'A' && pchSrc[1] <= 'Z')) )
  72. {
  73. // %A comes after %9 -- we'll need it someday
  74. int i;
  75. if (pchSrc[1] > '9')
  76. i = 9 + (pchSrc[1] - 'A');
  77. else
  78. i = pchSrc[1] - '1';
  79. pchSrc += 2;
  80. if (i >= nString)
  81. {
  82. TRACE1("Error: illegal string index requested %d.\n", i);
  83. *pchDest++ = '?';
  84. }
  85. else if (rglpsz[i] != NULL)
  86. {
  87. lstrcpy(pchDest, rglpsz[i]);
  88. pchDest += lstrlen(pchDest);
  89. }
  90. }
  91. else
  92. {
  93. if (_istlead(*pchSrc))
  94. *pchDest++ = *pchSrc++; // copy first of 2 bytes
  95. *pchDest++ = *pchSrc++;
  96. }
  97. }
  98. rString.ReleaseBuffer((int)((LPCTSTR)pchDest - (LPCTSTR)rString));
  99. // ReleaseBuffer will assert if we went too far
  100. }
  101. void AFXAPI AfxFormatString1(CString& rString, UINT nIDS, LPCTSTR lpsz1)
  102. {
  103. AfxFormatStrings(rString, nIDS, &lpsz1, 1);
  104. }
  105. void AFXAPI AfxFormatString2(CString& rString, UINT nIDS, LPCTSTR lpsz1,
  106. LPCTSTR lpsz2)
  107. {
  108. LPCTSTR rglpsz[2];
  109. rglpsz[0] = lpsz1;
  110. rglpsz[1] = lpsz2;
  111. AfxFormatStrings(rString, nIDS, rglpsz, 2);
  112. }
  113. /////////////////////////////////////////////////////////////////////////////