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.

476 lines
11 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. // All rights reserved.
  5. //
  6. // File Name:
  7. // string.c
  8. //
  9. // Description:
  10. //
  11. //----------------------------------------------------------------------------
  12. #include "pch.h"
  13. #define _SMGR_MAX_STRING_LEN_ 1024
  14. //----------------------------------------------------------------------------
  15. //
  16. // Function: MyLoadString
  17. //
  18. // Purpose: Loads a string resource given it's IDS_* and returns
  19. // a malloc'ed buffer with its contents.
  20. //
  21. // The malloc()'ed buffer can be freed with free()
  22. //
  23. // Arguments:
  24. // UINT StringId
  25. //
  26. // Returns:
  27. // Pointer to buffer. An empty string is returned if the StringId
  28. // does not exist. Null is returned if out of memory.
  29. //
  30. //----------------------------------------------------------------------------
  31. LPTSTR
  32. MyLoadString(IN UINT StringId)
  33. {
  34. TCHAR Buffer[_SMGR_MAX_STRING_LEN_];
  35. TCHAR *lpszRetVal;
  36. UINT Length;
  37. Length = LoadString(FixedGlobals.hInstance,
  38. StringId,
  39. Buffer,
  40. sizeof(Buffer)/sizeof(TCHAR));
  41. if( ! Length ) {
  42. Buffer[0] = _T('\0');
  43. }
  44. lpszRetVal = lstrdup(Buffer);
  45. if (lpszRetVal == NULL)
  46. TerminateTheWizard(IDS_ERROR_OUTOFMEMORY);
  47. return lpszRetVal;
  48. }
  49. //----------------------------------------------------------------------------
  50. //
  51. // Function: CleanSpaceAndQuotes
  52. //
  53. // Purpose: Cleans white-space and double quotes from the string and
  54. // returns a pointer to the start of the non-white-space data.
  55. //
  56. // Arguments:
  57. // LPTSTR - input string
  58. //
  59. // Returns:
  60. // LPTSTR
  61. //
  62. // Notes:
  63. // Uses crt iswspace and iswcntrl. Unicode only.
  64. //
  65. //----------------------------------------------------------------------------
  66. LPTSTR
  67. CleanSpaceAndQuotes(LPTSTR Buffer)
  68. {
  69. TCHAR *p;
  70. TCHAR *pEnd;
  71. p = CleanLeadSpace( Buffer );
  72. CleanTrailingSpace( p );
  73. pEnd = p + lstrlen( p ) - 1;
  74. //
  75. // Only remove quotes if there is a matching quote at the beginning and
  76. // end of the string
  77. //
  78. if( *p == _T('"') && *pEnd == _T('"'))
  79. {
  80. *pEnd = _T('\0');
  81. pEnd--;
  82. p++;
  83. }
  84. p = CleanLeadSpace( p );
  85. CleanTrailingSpace( p );
  86. return( p );
  87. }
  88. //----------------------------------------------------------------------------
  89. //
  90. // Function: CleanLeadSpace
  91. //
  92. // Purpose: Cleans leading white-space. Returns a pointer to the start
  93. // of the non-white-space data.
  94. //
  95. // Arguments:
  96. // LPTSTR - input string
  97. //
  98. // Returns:
  99. // LPTSTR
  100. //
  101. //----------------------------------------------------------------------------
  102. LPTSTR
  103. CleanLeadSpace(LPTSTR Buffer)
  104. {
  105. TCHAR *p = Buffer;
  106. while ( *p && ( _istspace(*p) || _istcntrl(*p) ) )
  107. p++;
  108. return p;
  109. }
  110. //----------------------------------------------------------------------------
  111. //
  112. // Function: CleanTrailingSpace
  113. //
  114. // Purpose: Cleans any trailing spaces on a string.
  115. //
  116. // Arguments:
  117. // TCHAR *pszBuffer - the string to remove the trailing spaces from
  118. //
  119. // Returns:
  120. // VOID
  121. //
  122. //----------------------------------------------------------------------------
  123. VOID
  124. CleanTrailingSpace(TCHAR *pszBuffer)
  125. {
  126. TCHAR *p = pszBuffer;
  127. p = p + lstrlen( pszBuffer );
  128. while ( p >= pszBuffer && ( _istspace(*p) || _istcntrl(*p) ) )
  129. {
  130. *p = _T('\0');
  131. p--;
  132. }
  133. }
  134. //----------------------------------------------------------------------------
  135. //
  136. // Function: ConvertQuestionsToNull
  137. //
  138. // Purpose: Scan a string and replace all ? with the null char (\0).
  139. //
  140. // Arguments: IN OUT TCHAR *pszString -
  141. //
  142. // Returns: VOID
  143. //
  144. //----------------------------------------------------------------------------
  145. VOID
  146. ConvertQuestionsToNull( IN OUT TCHAR *pszString )
  147. {
  148. while( *pszString != _T('\0') )
  149. {
  150. if( *pszString == _T('?') )
  151. {
  152. *pszString = _T('\0');
  153. }
  154. pszString++;
  155. }
  156. }
  157. //----------------------------------------------------------------------------
  158. //
  159. // Function: lstrcatn
  160. //
  161. // Purpose: The standard libraries do not include this function so here it
  162. // is. It does exactly what you would expect it to. It concatenates
  163. // one string on to another with a char max limit. No matter the value of
  164. // iMaxLength, the first string will never be truncated. The terminating
  165. // null(\0) is always appended.
  166. //
  167. // Arguments:
  168. // IN TCHAR *pszString1 - pointer to target buffer
  169. // IN const TCHAR *pszString2 - pointer to source string
  170. // IN INT iMaxLength - max number of characters to appear in the
  171. // returned string (the combination of the two strings)
  172. //
  173. // Returns:
  174. // LPTSTR
  175. //
  176. //----------------------------------------------------------------------------
  177. TCHAR*
  178. lstrcatn( IN TCHAR *pszString1, IN const TCHAR *pszString2, IN INT iMaxLength )
  179. {
  180. INT i;
  181. INT iCharCount = 0;
  182. if( lstrlen( pszString1 ) >= iMaxLength ) {
  183. return( pszString1 );
  184. }
  185. //
  186. // Advance to the end of the first string
  187. //
  188. while( *pszString1 != _T('\0') && iCharCount < iMaxLength )
  189. {
  190. pszString1++;
  191. iCharCount++;
  192. }
  193. //
  194. // Append on to the string character by character
  195. //
  196. for( ; iCharCount < (iMaxLength - 1) && *pszString2 != _T('\0'); iCharCount++ )
  197. {
  198. *pszString1 = *pszString2;
  199. pszString1++;
  200. pszString2++;
  201. }
  202. *pszString1 = _T('\0');
  203. return( pszString1 );
  204. }
  205. //----------------------------------------------------------------------------
  206. //
  207. // Function: DoubleNullStringToNameList
  208. //
  209. // Purpose: Takes a pointer to a list of strings (each terminated by a null)
  210. // with a double null terminating the last string and adds each one to the
  211. // given namelist. If there are any double quotes(") in the string, they
  212. // are removed.
  213. //
  214. // Arguments:
  215. // TCHAR *szDoubleNullString - string with embedded strings
  216. // NAMELIST *pNameList - namelist to add the strings to
  217. //
  218. // Returns: VOID
  219. //
  220. // Example:
  221. // If the function is called with the string:
  222. // one\0two\0\three\0\0
  223. // then the following strings are added to the namelist:
  224. // one
  225. // two
  226. // three
  227. //
  228. //----------------------------------------------------------------------------
  229. VOID
  230. DoubleNullStringToNameList( IN TCHAR *szDoubleNullString,
  231. IN OUT NAMELIST *pNameList )
  232. {
  233. TCHAR szTempString[MAX_INILINE_LEN];
  234. TCHAR *pStr;
  235. TCHAR *pShiftStr;
  236. do
  237. {
  238. lstrcpyn( szTempString, szDoubleNullString, AS(szTempString) );
  239. pStr = szTempString;
  240. //
  241. // Remove quotes(") from the string
  242. //
  243. while( *pStr != _T('\0') )
  244. {
  245. if( *pStr == _T('"') )
  246. {
  247. //
  248. // Found a quote so slide the string down one to overwrite the "
  249. //
  250. pShiftStr = pStr;
  251. while( *pShiftStr != _T('\0') )
  252. {
  253. *pShiftStr = *(pShiftStr+1);
  254. pShiftStr++;
  255. }
  256. }
  257. pStr++;
  258. }
  259. AddNameToNameList( pNameList, szTempString );
  260. //
  261. // Advance to 1 character passed the \0
  262. //
  263. szDoubleNullString = szDoubleNullString + lstrlen( szDoubleNullString ) + 1;
  264. } while( *szDoubleNullString != _T('\0') );
  265. }
  266. //----------------------------------------------------------------------------
  267. //
  268. // Function: GetCommaDelimitedEntry
  269. //
  270. // Purpose: Used to extract comma separated items out of a buffer
  271. //
  272. // pBuffer is passed by reference so it always points to the next
  273. // char that has not been extracted yet
  274. //
  275. // Arguments: TCHAR szIPString[] - used to put the new IP into
  276. // TCHAR **Buffer - pointer to the IP addresses
  277. //
  278. // Returns: BOOL TRUE if an IP was placed in szIPString
  279. // FALSE if an IP was NOT placed in szIPString
  280. //
  281. //----------------------------------------------------------------------------
  282. BOOL
  283. GetCommaDelimitedEntry( OUT TCHAR szIPString[], IN OUT TCHAR **pBuffer )
  284. {
  285. INT i;
  286. if( **pBuffer == _T('\0') )
  287. {
  288. return( FALSE );
  289. }
  290. else
  291. {
  292. if( **pBuffer == _T(',') )
  293. {
  294. (*pBuffer)++;
  295. }
  296. //
  297. // Copy an IP string into szIPString char by char
  298. //
  299. for(i = 0;
  300. **pBuffer != _T(',') && **pBuffer != _T('\0');
  301. (*pBuffer)++, i++)
  302. {
  303. szIPString[i] = **pBuffer;
  304. }
  305. szIPString[i] = _T('\0'); // append the null character
  306. return( TRUE );
  307. }
  308. }
  309. //----------------------------------------------------------------------------
  310. //
  311. // Function: StripQuotes
  312. //
  313. // Purpose: If a string is quoted(") it removes the quotes and returns the
  314. // string without quotes.
  315. //
  316. // This function has no effect on strings that are not quoted. It
  317. // will only removed quotes at the beginning at end of the string,
  318. // regardless if they are paired or not
  319. //
  320. // Arguments:
  321. // TCHAR *String - the string to have its quotes removed.
  322. //
  323. // Returns: via the output parameter String, the string with no quotes
  324. //
  325. // Example: Some example calls and their return values
  326. //
  327. // Called With: Returns:
  328. // ----------- -------
  329. //
  330. // "Quoted" Quoted
  331. // Not Quoted Not Quoted
  332. // "Single Quote Single Quote
  333. // Another Quote" Another Quote
  334. //
  335. //----------------------------------------------------------------------------
  336. VOID
  337. StripQuotes( IN OUT TCHAR *String )
  338. {
  339. TCHAR *pLastChar = String + lstrlen(String) - 1;
  340. //
  341. // If the last char is quoted, replace it with \0
  342. //
  343. if( *pLastChar == _T('"') )
  344. {
  345. *pLastChar = _T('\0');
  346. }
  347. if( String[0] == _T('"') )
  348. {
  349. TCHAR *pString = String;
  350. //
  351. // Slide the entire string back one
  352. //
  353. while( *pString != _T('\0') )
  354. {
  355. *pString = *(pString+1);
  356. pString++;
  357. }
  358. }
  359. }
  360. //----------------------------------------------------------------------------
  361. //
  362. // Function: DoesContainWhiteSpace
  363. //
  364. // Purpose: Determines if a given string contains white space chars.
  365. //
  366. // Arguments:
  367. // LPCTSTR p - the strig to scan for white space
  368. //
  369. // Returns: BOOL - TRUE if the string contains white space, FALSE if not
  370. //
  371. //----------------------------------------------------------------------------
  372. BOOL
  373. DoesContainWhiteSpace( LPCTSTR p )
  374. {
  375. for( ; *p; p++ )
  376. {
  377. if( iswspace( *p ) )
  378. {
  379. return( TRUE );
  380. }
  381. }
  382. return( FALSE );
  383. }