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.

151 lines
3.6 KiB

  1. /*****************************************************************/
  2. /** Microsoft Windows for Workgroups **/
  3. /** Copyright (C) Microsoft Corp., 1991-1992 **/
  4. /*****************************************************************/
  5. /*
  6. strinsrt.cxx
  7. NLS/DBCS-aware string class: InsertParams method
  8. This file contains the implementation of the InsertParams method
  9. for the STRING class. It is separate so that clients of STRING which
  10. do not use this operator need not link to it.
  11. FILE HISTORY:
  12. Johnl 01/31/91 Created
  13. beng 02/07/91 Uses lmui.hxx
  14. */
  15. #include "npcommon.h"
  16. extern "C"
  17. {
  18. #include <netlib.h>
  19. }
  20. #if defined(DEBUG)
  21. static const CHAR szFileName[] = __FILE__;
  22. #define _FILENAME_DEFINED_ONCE szFileName
  23. #endif
  24. #include <npassert.h>
  25. #include <npstring.h>
  26. #define MAX_INSERT_PARAMS 9
  27. /*******************************************************************
  28. NAME: NLS_STR::InsertParams
  29. SYNOPSIS: Fill in a message string from the resource file replacing
  30. the number parameters with the real text.
  31. ENTRY: pchMessage is a pointer to the message text
  32. apnlsParamStrings is an array of pointers to NLS_STRs
  33. Example:
  34. *this = "Error %1 occurred, do %2, or %1 will happen again"
  35. apnlsParamStrings[0] = "696969"
  36. apnlsParamStrings[1] = "Something else"
  37. Return string = "Error 696969 occurred, do Something else or
  38. 696969 will happen again"
  39. EXIT: 0 if successful, error code otherwise, one of:
  40. WN_OUT_OF_MEMORY
  41. NOTES: The minimum parameter is 1, the maximum parameter is 9.
  42. The array of param strings must have a NULL to mark
  43. the end of the array.
  44. HISTORY:
  45. JohnL 01/30/91 Created
  46. beng 04/26/91 Uses WCHAR
  47. beng 07/23/91 Allow on erroneous string
  48. ********************************************************************/
  49. #define PARAM_ESC '%'
  50. USHORT NLS_STR::InsertParams( const NLS_STR * apnlsParamStrings[] )
  51. {
  52. if (QueryError())
  53. return (USHORT) QueryError();
  54. INT iNumParams = 0; // Number of param strings in the array
  55. // Max string length of expanded message (include \0)
  56. INT iMaxMessLen = strlen() + 1;
  57. /* How many parameters were we passed?
  58. */
  59. for ( ; apnlsParamStrings[iNumParams] != NULL ; iNumParams++ )
  60. ;
  61. UIASSERT(iNumParams <= MAX_INSERT_PARAMS);
  62. if ( iNumParams > MAX_INSERT_PARAMS )
  63. return WN_OUT_OF_MEMORY;
  64. /* Determine total string length required for the expanded string
  65. * and get out if we can't fulfill the request
  66. */
  67. ISTR istrCurPos( *this );
  68. while ( 1 )
  69. {
  70. if ( !strchr( &istrCurPos, PARAM_ESC, istrCurPos ) )
  71. break;
  72. WCHAR wchParam = QueryChar( ++istrCurPos );
  73. if ( wchParam >= '1' && wchParam <= '9' )
  74. {
  75. INT iParamIndex = wchParam - '1';
  76. if ( iNumParams < iParamIndex )
  77. return WN_OUT_OF_MEMORY;
  78. iMaxMessLen += apnlsParamStrings[iParamIndex]->strlen() - 2;
  79. }
  80. }
  81. if ( iMaxMessLen > QueryAllocSize() )
  82. {
  83. if ( IsOwnerAlloc() )
  84. return WN_OUT_OF_MEMORY;
  85. else
  86. if ( !realloc( iMaxMessLen ) )
  87. return WN_OUT_OF_MEMORY;
  88. }
  89. /* Now do the parameter substitution
  90. */
  91. istrCurPos.Reset();
  92. for (;;)
  93. {
  94. if ( !strchr( &istrCurPos, PARAM_ESC, istrCurPos ) )
  95. break;
  96. ISTR istrParamEsc( istrCurPos );
  97. WCHAR wchParam = QueryChar( ++istrCurPos );
  98. if ( wchParam >= '1' && wchParam <= '9' )
  99. {
  100. INT iParamIndex = wchParam - '1';
  101. if (iParamIndex < iNumParams) {
  102. ReplSubStr( *apnlsParamStrings[iParamIndex],
  103. istrParamEsc,
  104. ++istrCurPos ) ; // Replace #
  105. // Skip past entire substituted string
  106. istrCurPos.SetIB(istrParamEsc.QueryIB() +
  107. apnlsParamStrings[iParamIndex]->strlen());
  108. }
  109. // else istrCurPos has been advanced past the out-of-range digit
  110. }
  111. }
  112. return 0;
  113. }