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.

592 lines
13 KiB

  1. // STRUTIL.CPP
  2. //
  3. // Assorted string utility functions for use in NetMeeting components.
  4. // Derived from STRCORE.CPP.
  5. #include "precomp.h"
  6. #include <oprahcom.h>
  7. #include <cstring.hpp>
  8. // global helper functions for Unicode support in a DBCS environment
  9. int NMINTERNAL UnicodeCompare(PCWSTR s, PCWSTR t)
  10. {
  11. // Treat NULL pointers like empty strings
  12. // at the bottom of the collating order.
  13. if (IsEmptyStringW(t)) {
  14. if (IsEmptyStringW(s)) {
  15. return 0;
  16. }
  17. else {
  18. return 1;
  19. }
  20. }
  21. // Done with empty string cases,
  22. // so now do real compare.
  23. for ( ; *s == *t; s++, t++) {
  24. if (!*s) {
  25. return 0;
  26. }
  27. }
  28. return (*s > *t) ? 1 : -1;
  29. }
  30. PWSTR NMINTERNAL NewUnicodeString(PCWSTR _wszText)
  31. {
  32. PWSTR wszText = NULL;
  33. UINT nChar;
  34. if (_wszText) {
  35. nChar = lstrlenW(_wszText) + 1;
  36. wszText = new WCHAR[nChar];
  37. if (wszText) {
  38. CopyMemory((void *)wszText,
  39. _wszText,
  40. nChar * sizeof(WCHAR));
  41. }
  42. }
  43. return wszText;
  44. }
  45. PWSTR NMINTERNAL DBCSToUnicode(UINT uCodePage, PCSTR szText)
  46. {
  47. int nChar;
  48. PWSTR wszText = NULL;
  49. if (szText) {
  50. nChar = MultiByteToWideChar(uCodePage,
  51. 0, // character-type options
  52. szText,
  53. -1, // NULL terminated string
  54. NULL, // return buffer (not used)
  55. 0); // getting length of Unicode string
  56. if (nChar) {
  57. wszText = new WCHAR[nChar];
  58. if (wszText) {
  59. nChar = MultiByteToWideChar(uCodePage,
  60. 0, // character-type options
  61. szText,
  62. -1, // NULL terminated string
  63. wszText, // return buffer
  64. nChar); // length of return buffer
  65. if (!nChar) {
  66. delete wszText;
  67. wszText = NULL;
  68. }
  69. }
  70. }
  71. }
  72. return wszText;
  73. }
  74. PSTR NMINTERNAL UnicodeToDBCS(UINT uCodePage, PCWSTR wszText)
  75. {
  76. int nChar;
  77. PSTR szText = NULL;
  78. if (wszText) {
  79. nChar = WideCharToMultiByte(uCodePage,
  80. 0, // character-type options
  81. wszText,
  82. -1, // NULL terminated string
  83. NULL, // return buffer (not used)
  84. 0, // getting length of DBCS string
  85. NULL,
  86. NULL);
  87. if (nChar) {
  88. szText = new CHAR[nChar];
  89. if (szText) {
  90. nChar = WideCharToMultiByte(uCodePage,
  91. 0, // character-type options
  92. wszText,
  93. -1, // NULL terminated string
  94. szText, // return buffer
  95. nChar, // length of return buffer
  96. NULL,
  97. NULL);
  98. if (!nChar) {
  99. delete szText;
  100. szText = NULL;
  101. }
  102. }
  103. }
  104. }
  105. return szText;
  106. }
  107. BOOL NMINTERNAL UnicodeIsNumber(PCWSTR wszText)
  108. {
  109. // If there are no characters, then treat it as not being a number.
  110. if (!wszText || !*wszText) {
  111. return FALSE;
  112. }
  113. // If any characters are not digits, then return FALSE.
  114. do {
  115. if ((*wszText < L'0') || (*wszText > L'9')) {
  116. return FALSE;
  117. }
  118. } while(*++wszText);
  119. // Got here so all characters are digits.
  120. return TRUE;
  121. }
  122. /* G U I D T O S Z */
  123. /*----------------------------------------------------------------------------
  124. %%Function: GuidToSz
  125. Convert the guid to a special hex string.
  126. Assumes lpchDest has space for at least sizeof(GUID)*2 +6 chars.
  127. LENGTH_SZGUID_FORMATTED is 30 and includes space for the null terminator.
  128. Note the difference between this and UuidToString (or StringFromGUID2)
  129. GUID Format: {12345678-1234-1234-1234567890123456}
  130. ----------------------------------------------------------------------------*/
  131. VOID NMINTERNAL GuidToSz(GUID * pguid, LPTSTR lpchDest)
  132. {
  133. ASSERT(NULL != pguid);
  134. ASSERT(NULL != lpchDest);
  135. wsprintf(lpchDest, TEXT("{%08X-%04X-%04X-%02X%02X-"),
  136. pguid->Data1, pguid->Data2, pguid->Data3, pguid->Data4[0], pguid->Data4[1]);
  137. lpchDest += 1+8+1+4+1+4+1+2+2+1;
  138. for (int i = 2; i < 8; i++)
  139. {
  140. wsprintf(lpchDest, TEXT("%02X"), pguid->Data4[i]);
  141. lpchDest += 2;
  142. }
  143. lstrcpy(lpchDest, TEXT("}") );
  144. }
  145. /* S Z F I N D L A S T C H */
  146. /*----------------------------------------------------------------------------
  147. %%Function: SzFindLastCh
  148. Returns a pointer to the ch within the lpsz or NULL if not found
  149. ----------------------------------------------------------------------------*/
  150. LPTSTR NMINTERNAL SzFindLastCh(LPTSTR lpsz, TCHAR ch)
  151. {
  152. LPTSTR lpchRet;
  153. for (lpchRet = NULL; *lpsz; lpsz = CharNext(lpsz))
  154. {
  155. if (ch == *lpsz)
  156. lpchRet = lpsz;
  157. }
  158. return lpchRet;
  159. }
  160. /* T R I M S Z */
  161. /*-------------------------------------------------------------------------
  162. %%Function: TrimSz
  163. Trim the whitespace around string.
  164. Returns the number of characters in the string.
  165. (chars/bytes in ANSI and DBCS, WCHARs/words in UNICODE)
  166. -------------------------------------------------------------------------*/
  167. UINT NMINTERNAL TrimSz(PTCHAR psz)
  168. {
  169. UINT ich; // character index into rgwCharType
  170. PTCHAR pchFirst;
  171. PTCHAR pchLast;
  172. PTCHAR pchCurr;
  173. WORD rgwCharType[MAX_PATH];
  174. if ((NULL == psz) || (0 == lstrlen(psz)))
  175. {
  176. return 0;
  177. }
  178. if (!GetStringTypeEx(LOCALE_SYSTEM_DEFAULT, CT_CTYPE1, psz, -1, rgwCharType))
  179. {
  180. WARNING_OUT(("TrimSz: Problem with GetStringTypeEx"));
  181. return 0;
  182. }
  183. // search for first non-space
  184. pchFirst = psz;
  185. ich = 0;
  186. while (_T('\0') != *pchFirst)
  187. {
  188. if (!(C1_SPACE & rgwCharType[ich]))
  189. break;
  190. pchFirst = CharNext(pchFirst);
  191. ich++;
  192. }
  193. if (_T('\0') == *pchFirst)
  194. {
  195. // The entire string is empty!
  196. *psz = _T('\0');
  197. return 0;
  198. }
  199. // search for last non-space
  200. pchCurr = pchFirst;
  201. pchLast = pchCurr;
  202. while (_T('\0') != *pchCurr)
  203. {
  204. if (!(C1_SPACE & rgwCharType[ich]))
  205. {
  206. pchLast = pchCurr;
  207. }
  208. pchCurr = CharNext(pchCurr);
  209. ich++;
  210. }
  211. ASSERT(_T('\0') != *pchLast);
  212. // Null terminate the string
  213. pchLast = CharNext(pchLast);
  214. *pchLast = _T('\0');
  215. // Update the original string
  216. lstrcpy(psz, pchFirst);
  217. // Return the new length
  218. return lstrlen(psz);
  219. }
  220. // Implement lstrcpyW when not on a Unicode platform
  221. #if !defined(UNICODE)
  222. /* L S T R C P Y W */
  223. /*-------------------------------------------------------------------------
  224. %%Function: LStrCpyW
  225. -------------------------------------------------------------------------*/
  226. LPWSTR NMINTERNAL LStrCpyW(LPWSTR pszDest, LPWSTR pszSrc)
  227. {
  228. ASSERT(NULL != pszDest);
  229. ASSERT(NULL != pszSrc);
  230. if ((NULL != pszDest) && (NULL != pszSrc))
  231. {
  232. LPWSTR pszT = pszDest;
  233. while (0 != (*pszT++ = *pszSrc++))
  234. ;
  235. }
  236. return pszDest;
  237. }
  238. /* L S T R C P Y N W */
  239. /*-------------------------------------------------------------------------
  240. %%Function: LStrCpyNW
  241. -------------------------------------------------------------------------*/
  242. LPWSTR NMINTERNAL LStrCpyNW(LPWSTR pszDest, LPCWSTR pszSrc, INT iMaxLength)
  243. {
  244. ASSERT(NULL != pszDest);
  245. ASSERT(NULL != pszSrc);
  246. if ((NULL != pszDest) && (NULL != pszSrc))
  247. {
  248. LPWSTR pszT = pszDest;
  249. while ((--iMaxLength > 0) &&
  250. (0 != (*pszT++ = *pszSrc++)))
  251. {
  252. /*EXPLICIT */ ;
  253. }
  254. if (0 == iMaxLength)
  255. {
  256. *pszT = L'\0';
  257. }
  258. }
  259. return pszDest;
  260. }
  261. #endif // !defined(UNICODE)
  262. /* _ S T R C H R */
  263. /*-------------------------------------------------------------------------
  264. %%Function: _StrChr
  265. -------------------------------------------------------------------------*/
  266. LPCTSTR NMINTERNAL _StrChr ( LPCTSTR pcsz, TCHAR c )
  267. {
  268. LPCTSTR pcszFound = NULL;
  269. if (pcsz)
  270. {
  271. while (*pcsz)
  272. {
  273. if (*pcsz == c)
  274. {
  275. pcszFound = pcsz;
  276. break;
  277. }
  278. pcsz = CharNext(pcsz);
  279. }
  280. }
  281. return pcszFound;
  282. }
  283. /* _ S T R C M P N */
  284. /*-------------------------------------------------------------------------
  285. %%Function: _StrCmpN
  286. This does a case-sensitive compare of two strings, pcsz1 and pcsz2, of
  287. at most cchMax characters. If we reach the end of either string, we
  288. also stop, and the strings match if the other string is also at its end.
  289. This function is NOT DBCS safe.
  290. -------------------------------------------------------------------------*/
  291. int NMINTERNAL _StrCmpN(LPCTSTR pcsz1, LPCTSTR pcsz2, UINT cchMax)
  292. {
  293. UINT ich;
  294. for (ich = 0; ich < cchMax; ich++)
  295. {
  296. if (*pcsz1 != *pcsz2)
  297. {
  298. // No match.
  299. return((*pcsz1 > *pcsz2) ? 1 : -1);
  300. }
  301. //
  302. // Are we at the end (if we're here, both strings are at the
  303. // end. If only one is, the above compare code kicks in.
  304. //
  305. if ('\0' == *pcsz1)
  306. return 0;
  307. pcsz1++;
  308. pcsz2++;
  309. }
  310. // If we get here, cchMax characters matched, so success.
  311. return 0;
  312. }
  313. /* _ S T R S T R */
  314. /*-------------------------------------------------------------------------
  315. %%Function: _StrStr
  316. -------------------------------------------------------------------------*/
  317. // BUGBUG - This function is *not* DBCS-safe
  318. LPCTSTR NMINTERNAL _StrStr (LPCTSTR pcsz1, LPCTSTR pcsz2)
  319. {
  320. PTSTR pszcp = (PTSTR) pcsz1;
  321. PTSTR pszs1, pszs2;
  322. if ( !*pcsz2 )
  323. return pcsz1;
  324. while (*pszcp)
  325. {
  326. pszs1 = pszcp;
  327. pszs2 = (PTSTR) pcsz2;
  328. while ( *pszs1 && *pszs2 && !(*pszs1-*pszs2) )
  329. pszs1++, pszs2++;
  330. if (!*pszs2)
  331. return pszcp;
  332. pszcp++;
  333. }
  334. return NULL;
  335. }
  336. /* _ S T R S T R */
  337. /*-------------------------------------------------------------------------
  338. %%Function: _StrStr
  339. -------------------------------------------------------------------------*/
  340. // BUGBUG - This function is *not* DBCS-safe
  341. LPCWSTR _StrStrW(LPCWSTR pcsz1, LPCWSTR pcsz2)
  342. {
  343. PWSTR pszcp = (PWSTR) pcsz1;
  344. while (*pszcp)
  345. {
  346. PWSTR psz1 = pszcp;
  347. PWSTR psz2 = (PWSTR) pcsz2;
  348. while ( *psz1 && *psz2 && !(*psz1-*psz2) )
  349. {
  350. psz1++;
  351. psz2++;
  352. }
  353. if (!*psz2)
  354. return pszcp;
  355. pszcp++;
  356. }
  357. return NULL;
  358. }
  359. /* _ S T R P B R K */
  360. /*-------------------------------------------------------------------------
  361. %%Function: _StrPbrkA, _StrPbrkW
  362. Private, DBCS-safe version of CRT strpbrk function. Like strchr but
  363. accepts more than one character for which to search. The ANSI version
  364. does not support searching for DBCS chars.
  365. In the Unicode version, we do a nested search. In the ANSI version,
  366. we build up a table of chars and use this to scan the string.
  367. -------------------------------------------------------------------------*/
  368. LPSTR NMINTERNAL _StrPbrkA(LPCSTR pcszString, LPCSTR pcszSearch)
  369. {
  370. ASSERT(NULL != pcszString && NULL != pcszSearch);
  371. BYTE rgbSearch[(UCHAR_MAX + 1) / CHAR_BIT];
  372. ZeroMemory(rgbSearch, sizeof(rgbSearch));
  373. // Scan the search string
  374. while ('\0' != *pcszSearch)
  375. {
  376. ASSERT(!IsDBCSLeadByte(*pcszSearch));
  377. // Set the appropriate bit in the appropriate byte
  378. rgbSearch[*pcszSearch / CHAR_BIT] |= (1 << (*pcszSearch % CHAR_BIT));
  379. pcszSearch++;
  380. }
  381. // Scan the source string, compare to the bits in the search array
  382. while ('\0' != *pcszString)
  383. {
  384. if (rgbSearch[*pcszString / CHAR_BIT] & (1 << (*pcszString % CHAR_BIT)))
  385. {
  386. // We have a match
  387. return (LPSTR) pcszString;
  388. }
  389. pcszString = CharNextA(pcszString);
  390. }
  391. // If we get here, there was no match
  392. return NULL;
  393. }
  394. LPWSTR NMINTERNAL _StrPbrkW(LPCWSTR pcszString, LPCWSTR pcszSearch)
  395. {
  396. ASSERT(NULL != pcszString && NULL != pcszSearch);
  397. // Scan the string, matching each character against those in the search string
  398. while (L'\0' != *pcszString)
  399. {
  400. LPCWSTR pcszCurrent = pcszSearch;
  401. while (L'\0' != *pcszCurrent)
  402. {
  403. if (*pcszString == *pcszCurrent)
  404. {
  405. // We have a match
  406. return (LPWSTR) pcszString;
  407. }
  408. // pcszCurrent = CharNextW(pcszCurrent);
  409. pcszCurrent++;
  410. }
  411. // pcszString = CharNextW(pcszString);
  412. pcszString++;
  413. }
  414. // If we get here, there was no match
  415. return NULL;
  416. }
  417. // BUGBUG - Are DecimalStringToUINT and StrToInt the same?
  418. /* D E C I M A L S T R I N G T O U I N T */
  419. /*-------------------------------------------------------------------------
  420. %%Function: DecimalStringToUINT
  421. -------------------------------------------------------------------------*/
  422. UINT NMINTERNAL DecimalStringToUINT(LPCTSTR pcszString)
  423. {
  424. ASSERT(pcszString);
  425. UINT uRet = 0;
  426. LPTSTR pszStr = (LPTSTR) pcszString;
  427. while (_T('\0') != pszStr[0])
  428. {
  429. ASSERT((pszStr[0] >= _T('0')) &&
  430. (pszStr[0] <= _T('9')));
  431. uRet = (10 * uRet) + (BYTE) (pszStr[0] - _T('0'));
  432. pszStr++; // NOTE: DBCS characters are not allowed!
  433. }
  434. return uRet;
  435. }
  436. /****************************************************************************
  437. FUNCTION: StrToInt
  438. PURPOSE: The atoi equivalent, to avoid using the C runtime lib
  439. PARAMETERS: lpSrc - pointer to a source string to convert to integer
  440. RETURNS: 0 for failure, the integer otherwise
  441. (what if the string was converted to 0 ?)
  442. ****************************************************************************/
  443. int WINAPI RtStrToInt(LPCTSTR lpSrc) // atoi()
  444. {
  445. int n = 0;
  446. BOOL bNeg = FALSE;
  447. if (*lpSrc == _T('-')) {
  448. bNeg = TRUE;
  449. lpSrc++;
  450. }
  451. while (((*lpSrc) >= _T('0') && (*lpSrc) <= _T('9')))
  452. {
  453. n *= 10;
  454. n += *lpSrc - _T('0');
  455. lpSrc++;
  456. }
  457. return bNeg ? -n : n;
  458. }
  459. /* _ S T R L W R W */
  460. /*-------------------------------------------------------------------------
  461. %%Function: _StrLwrW
  462. -------------------------------------------------------------------------*/
  463. // BUGBUG - This function does *not* handle all UNICODE character sets
  464. LPWSTR NMINTERNAL _StrLwrW(LPWSTR pwszSrc)
  465. {
  466. for (PWSTR pwszCur = pwszSrc; (L'\0' != *pwszCur); pwszCur++)
  467. {
  468. if ( (*pwszCur >= L'A') && (*pwszCur <= L'Z') )
  469. {
  470. *pwszCur = *pwszCur - L'A' + L'a';
  471. }
  472. }
  473. return pwszSrc;
  474. }