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.

306 lines
9.1 KiB

  1. /****************************************************************************\
  2. STRAPI.C / OPK Wizard (OPKWIZ.EXE)
  3. Microsoft Confidential
  4. Copyright (c) Microsoft Corporation 1999
  5. All rights reserved
  6. String API source file for generic APIs used in the OPK Wizard.
  7. 4/99 - Jason Cohen (JCOHEN)
  8. Added this new source file for the OPK Wizard as part of the
  9. Millennium rewrite.
  10. 7/00 - Brian Ku (BRIANK)
  11. Added to Whistler
  12. \****************************************************************************/
  13. //
  14. // Include file(s)
  15. //
  16. #include <pch.h>
  17. #include <tchar.h>
  18. //
  19. // Internal Defined Value(s):
  20. //
  21. #define NULLCHR _T('\0')
  22. //
  23. // External Function(s):
  24. //
  25. /****************************************************************************\
  26. LPTSTR // Returns a pointer to the first occurance of a
  27. // character in a string, or NULL if the
  28. // character isn't found.
  29. StrChr( // Searches a string for a particular character.
  30. LPCTSTR lpString, // Points to a string buffer to search.
  31. TCHAR cSearch // Character to search for.
  32. );
  33. \****************************************************************************/
  34. #ifndef _INC_SHLWAPI
  35. LPTSTR StrChr(LPCTSTR lpString, TCHAR cSearch)
  36. {
  37. // Validate the parameters passed in.
  38. //
  39. if ( ( lpString == NULL ) || ( *lpString == NULLCHR ) )
  40. return NULL;
  41. // Go through the string until the character is found,
  42. // or we hit the null terminator.
  43. //
  44. while ( ( *lpString != cSearch ) && ( *(lpString = CharNext(lpString)) ) );
  45. // If we didn't find it, null the pointer out.
  46. //
  47. if ( *lpString != cSearch )
  48. lpString = NULL;
  49. // Return either null or a pointer to the found character.
  50. //
  51. return (LPTSTR) lpString;
  52. }
  53. /****************************************************************************\
  54. LPTSTR // Returns a pointer to the last occurance of a
  55. // character in a string, or NULL if the
  56. // character isn't found.
  57. StrRChr( // Searches a string for a particular character.
  58. LPCTSTR lpString, // Points to a string buffer to search.
  59. TCHAR cSearch // Character to search for.
  60. );
  61. \****************************************************************************/
  62. LPTSTR StrRChr(LPCTSTR lpString, TCHAR cSearch)
  63. {
  64. LPTSTR lpSearch;
  65. // Validate the parameters passed in.
  66. //
  67. if ( ( lpString == NULL ) || ( *lpString == NULLCHR ) )
  68. return NULL;
  69. // Go back through the string until the character is found,
  70. // or we hit the begining of the string.
  71. //
  72. for ( lpSearch = (LPTSTR) lpString + lstrlen(lpString);
  73. ( lpSearch > lpString ) && ( *lpSearch != cSearch );
  74. lpSearch = CharPrev(lpString, lpSearch));
  75. // If we didn't find it, null the pointer out.
  76. //
  77. if ( *lpSearch != cSearch )
  78. lpSearch = NULL;
  79. // Return either null or a pointer to the found character.
  80. //
  81. return (LPTSTR) lpSearch;
  82. }
  83. #endif // _INC_SHLWAPI
  84. /****************************************************************************\
  85. LPTSTR // Returns a pointer to the string buffer passed
  86. // in.
  87. StrRem( // Searches a string for a particular character
  88. // and removes that character from the string in
  89. // place.
  90. LPCTSTR lpString, // Points to a string buffer to search and remove
  91. // the characters from.
  92. TCHAR cRemove // Character to search for and remove.
  93. );
  94. \****************************************************************************/
  95. LPTSTR StrRem(LPTSTR lpString, TCHAR cRemove)
  96. {
  97. LPTSTR lpSearch;
  98. // Validate the parameters passed in.
  99. //
  100. if ( ( lpString == NULL ) || ( *lpString == NULLCHR ) || ( cRemove == NULLCHR ) )
  101. return lpString;
  102. // Search the string for the character we want to remove.
  103. // Everytime we find it, shift the string over a character
  104. // to remove it.
  105. //
  106. for ( lpSearch = StrChr(lpString, cRemove); lpSearch; lpSearch = StrChr(lpSearch, cRemove) )
  107. lstrcpy(lpSearch, lpSearch + 1);
  108. // Return the pointer to the string passed in.
  109. //
  110. return lpString;
  111. }
  112. /****************************************************************************\
  113. LPTSTR // Returns a pointer to the string buffer passed
  114. // in.
  115. StrRTrm( // Searches a string for a particular ending
  116. // character, and removes all of them from the
  117. // ending of the string.
  118. LPCTSTR lpString, // Points to a string buffer to search and remove
  119. // the characters from.
  120. TCHAR cTrim // Character to search for and remove.
  121. );
  122. \****************************************************************************/
  123. LPTSTR StrRTrm(LPTSTR lpString, TCHAR cTrim)
  124. {
  125. LPTSTR lpEnd;
  126. // Validate the parameters passed in.
  127. //
  128. if ( ( lpString == NULL ) || ( *lpString == NULLCHR ) || ( cTrim == NULLCHR ) )
  129. return lpString;
  130. // Null out the end of the string minus
  131. // the chacters we are trimming.
  132. //
  133. for ( lpEnd = lpString + lstrlen(lpString); (lpEnd > lpString) && (*CharPrev(lpString, lpEnd) == cTrim); lpEnd = CharPrev(lpString, lpEnd) );
  134. *lpEnd = NULLCHR;
  135. return lpString;
  136. }
  137. /****************************************************************************\
  138. LPTSTR // Returns a pointer to the string buffer passed
  139. // in.
  140. StrTrm( // Searches a string for a particular proceeding
  141. // and ending character, and removes all of them
  142. // from the beginning and ending of the string.
  143. LPCTSTR lpString, // Points to a string buffer to search and remove
  144. // the characters from.
  145. TCHAR cTrim // Character to search for and remove.
  146. );
  147. \****************************************************************************/
  148. LPTSTR StrTrm(LPTSTR lpString, TCHAR cTrim)
  149. {
  150. LPTSTR lpBegin;
  151. // Validate the parameters passed in.
  152. //
  153. if ( ( lpString == NULL ) || ( *lpString == NULLCHR ) || ( cTrim == NULLCHR ) )
  154. return lpString;
  155. // Get a pointer to the begining of the string
  156. // minus the characters we are trimming.
  157. //
  158. for ( lpBegin = lpString; *lpBegin == cTrim; lpBegin = CharNext(lpBegin) );
  159. // Make sure we didn't hit the null terminator.
  160. //
  161. if ( *lpBegin == NULLCHR )
  162. {
  163. *lpString = NULLCHR;
  164. return lpString;
  165. }
  166. // Null out the end of the string minus
  167. // the chacters we are trimming.
  168. //
  169. StrRTrm(lpBegin, cTrim);
  170. // Now we may need to move the string to the beginning
  171. // of the buffer if we trimmed of proceeding characters.
  172. //
  173. if ( lpBegin > lpString )
  174. lstrcpy(lpString, lpBegin);
  175. return lpString;
  176. }
  177. /****************************************************************************\
  178. LPTSTR // Returns a pointer to the string a character in
  179. // the string passed in.
  180. StrMov( // Moves a pointer forward or backward the number
  181. // of characters passed in.
  182. LPCTSTR lpStart, // Pointer to the begining of the string buffer.
  183. // This may only be NULL if nCount is positive.
  184. LPCTSTR lpCurrent, // Pointer to a character in the null-terminated
  185. // string.
  186. INT nCount // Number of characters to move the pointer,
  187. // forward if it is a positive value, backward
  188. // if it is negative.
  189. );
  190. \****************************************************************************/
  191. LPTSTR StrMov(LPTSTR lpStart, LPTSTR lpCurrent, INT nCount)
  192. {
  193. // Validate the parameters.
  194. //
  195. if ( ( lpCurrent == NULL ) ||
  196. ( ( lpStart == NULL ) && ( nCount < 0 ) ) )
  197. {
  198. return lpCurrent;
  199. }
  200. // Loop throuh until we don't need to move the pointer anymore.
  201. //
  202. while ( nCount != 0 )
  203. {
  204. // Check to see if we are moving forward or backward.
  205. //
  206. if ( nCount > 0 )
  207. {
  208. // Move the pointer forward one character.
  209. //
  210. lpCurrent = CharNext(lpCurrent);
  211. nCount--;
  212. }
  213. else
  214. {
  215. // Move the pointer backward one character.
  216. //
  217. lpCurrent = CharPrev(lpStart, lpCurrent);
  218. nCount++;
  219. }
  220. }
  221. // Return the pointer to the new position.
  222. //
  223. return lpCurrent;
  224. }