Leaked source code of windows server 2003
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.

181 lines
6.6 KiB

  1. /*****************************************************************************
  2. * (C) COPYRIGHT MICROSOFT CORPORATION, 2002
  3. *
  4. * AUTHOR: ByronC
  5. *
  6. * DATE: 4/22/2002
  7. *
  8. * @doc INTERNAL
  9. *
  10. * @module SimpleTokenReplacement.cpp - Implementation for <c SimpleTokenReplacement> |
  11. *
  12. * This file contains the implmentation for the <c SimpleTokenReplacement> class.
  13. *
  14. *****************************************************************************/
  15. #include "precomp.h"
  16. /*****************************************************************************
  17. * @doc INTERNAL
  18. *
  19. * @mfunc | SimpleTokenReplacement | SimpleTokenReplacement |
  20. *
  21. * We initialize all member variables. Here, we initialize our resulting string
  22. * to be the input string.
  23. *
  24. *****************************************************************************/
  25. SimpleTokenReplacement::SimpleTokenReplacement(
  26. const CSimpleString &csOriginalString) :
  27. m_csResult(csOriginalString)
  28. {
  29. }
  30. /*****************************************************************************
  31. * @doc INTERNAL
  32. *
  33. * @mfunc | SimpleTokenReplacement | ~SimpleTokenReplacement |
  34. *
  35. * Do any cleanup that is not already done.
  36. *
  37. *****************************************************************************/
  38. SimpleTokenReplacement::~SimpleTokenReplacement()
  39. {
  40. }
  41. /*****************************************************************************
  42. * @doc INTERNAL
  43. *
  44. * @mfunc BOOL | SimpleTokenReplacement | ExpandTokenIntoString |
  45. *
  46. * This method inserts a value string in place of a token, similar to how
  47. * printf expands:
  48. * <nl>CHAR *szMyString = "TokenValue";
  49. * <nl>printf("left %s right", szMyString);
  50. * <nl>into the string "left TokenValue right".
  51. *
  52. * This method will only substitute the first matching token.
  53. *
  54. * @parm const CSimpleString& | csToken |
  55. * The token we're looking for
  56. * @parm const CSimpleString& | csTokenValue |
  57. * The value we want to substitute for the token. It does not have to
  58. * be the same size as the token.
  59. * @parm const DWORD | dwStartIndex |
  60. * The character index to start the search from
  61. *
  62. * @rvalue <gt> 0 |
  63. * The token was found and replaced. The value returned is the
  64. * character position following the token value substitution.
  65. * This is useful in subsequent searches for the token,
  66. * since we can start the next search from this index.
  67. *
  68. * @rvalue -1 |
  69. * The token was not found, therefore no replacement occured.
  70. * The resulting string is unchanged.
  71. *****************************************************************************/
  72. int SimpleTokenReplacement::ExpandTokenIntoString(
  73. const CSimpleString &csToken,
  74. const CSimpleString &csTokenValue,
  75. const DWORD dwStartIndex)
  76. {
  77. CSimpleString csExpandedString;
  78. int iRet = -1;
  79. if (csToken.Length() > 0)
  80. {
  81. //
  82. // Look for the token start
  83. //
  84. int iTokenStart = m_csResult.Find(csToken, dwStartIndex);
  85. if (iTokenStart != -1)
  86. {
  87. //
  88. // We found the token, so let's make the substitution.
  89. // The original string looks like this:
  90. // lllllllTokenrrrrrrr
  91. // |
  92. // |
  93. // iTokenStart
  94. // We want the string to look like this:
  95. // lllllllTokenValuerrrrrrr
  96. // Therefore, take everything before the Token, add the token value, then
  97. // everything following the token i.e.
  98. // lllllll + TokenValue + rrrrrrr
  99. // | |
  100. // iTokenStart -1 |
  101. // iTokenStart + Token.length()
  102. //
  103. csExpandedString = m_csResult.SubStr(0, iTokenStart);
  104. csExpandedString += csTokenValue;
  105. csExpandedString += m_csResult.SubStr(iTokenStart + csToken.Length(), -1);
  106. m_csResult = csExpandedString;
  107. iRet = iTokenStart + csToken.Length();
  108. }
  109. else
  110. {
  111. iRet = -1;
  112. }
  113. }
  114. return iRet;
  115. }
  116. /*****************************************************************************
  117. * @doc INTERNAL
  118. *
  119. * @mfunc VOID | SimpleTokenReplacement | ExpandArrayOfTokensIntoString |
  120. *
  121. * This method will replace all instances of the input tokens with their
  122. * corresponding values. It basically calls <mf SimpleTokenReplacement::ExpandTokenIntoString>
  123. * for each token/value pair in the input list, until -1 is returned (i.e.
  124. * no more instances of that token were found).
  125. *
  126. * @parm TokenValueList& | ListOfTokenValuePairs |
  127. * A list class containing the tokens and values to substitute.
  128. *
  129. *****************************************************************************/
  130. VOID SimpleTokenReplacement::ExpandArrayOfTokensIntoString(TokenValueList &ListOfTokenValuePairs)
  131. {
  132. SimpleTokenReplacement::TokenValuePair *pTokenValuePair;
  133. //
  134. // Loop through the list of Token/Value pairs, and for each element,
  135. // replace the token with the value
  136. //
  137. for (pTokenValuePair = ListOfTokenValuePairs.getFirst();
  138. pTokenValuePair != NULL;
  139. pTokenValuePair = ListOfTokenValuePairs.getNext())
  140. {
  141. //
  142. // We need to replace the token element number dwIndex with the value
  143. // element number dwIndex. Since ExpandTokenIntoString only replaces the
  144. // first occurence, we need to loop through until we have replaced all
  145. // occurrences of this token.
  146. //
  147. int iSearchIndex = 0;
  148. while (iSearchIndex != -1)
  149. {
  150. iSearchIndex = ExpandTokenIntoString(pTokenValuePair->getToken().String(), pTokenValuePair->getValue(), iSearchIndex);
  151. }
  152. }
  153. }
  154. /*****************************************************************************
  155. * @doc INTERNAL
  156. *
  157. * @mfunc CSimpleString | SimpleTokenReplacement | getString |
  158. *
  159. * This method returns the resulting string, after any calls to
  160. * <mf SimpleTokenReplacement::ExpandTokenIntoString> or
  161. * <mf SimpleTokenReplacement::ExpandArrayOfTokensIntoString> have been
  162. * made.
  163. *
  164. * @rvalue CSimpleString |
  165. * The resulting string.
  166. *****************************************************************************/
  167. CSimpleString SimpleTokenReplacement::getString()
  168. {
  169. return m_csResult;
  170. }