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.

193 lines
4.7 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Copyright (c) Microsoft Corporation 1993-1994
  4. //
  5. // File: string.c
  6. //
  7. // This files contains common string routines
  8. //
  9. // History:
  10. // 10-09-93 ScottH Created
  11. //
  12. //---------------------------------------------------------------------------
  13. ///////////////////////////////////////////////////// INCLUDES
  14. #include "brfprv.h" // common headers
  15. #include "strings.h"
  16. #ifdef NOTUSED
  17. static LPTSTR s_pszNextToken = NULL;
  18. #endif // NOTUSED
  19. // Some of these are replacements for the C runtime routines.
  20. // This is so we don't have to link to the CRT libs.
  21. //
  22. // WARNING: all of these APIs do not setup DS, so you can not access
  23. // any data in the default data seg of this DLL.
  24. //
  25. // do not create any global variables... talk to chrisg if you don't
  26. // understand this
  27. /*----------------------------------------------------------
  28. Purpose: Case sensitive character comparison for DBCS
  29. Returns: FALSE if they match, TRUE if no match
  30. Cond: --
  31. */
  32. BOOL ChrCmp(
  33. WORD w1,
  34. WORD wMatch)
  35. {
  36. // Most of the time this won't match, so test it first for speed.
  37. if (LOBYTE(w1) == LOBYTE(wMatch))
  38. {
  39. if (IsDBCSLeadByte(LOBYTE(w1)))
  40. {
  41. return(w1 != wMatch);
  42. }
  43. return FALSE;
  44. }
  45. return TRUE;
  46. }
  47. #ifdef NOTUSED // REARCHITECT: this is not DBCS aware
  48. /*----------------------------------------------------------
  49. Purpose: strtok
  50. Swiped from the C 7.0 runtime sources.
  51. Returns:
  52. Cond:
  53. */
  54. LPTSTR PUBLIC StrTok(
  55. LPTSTR psz,
  56. LPCTSTR rgchTokens)
  57. {
  58. TUCHAR map[32];
  59. LPTSTR pszToken;
  60. ZeroInit(map, map);
  61. do
  62. {
  63. map[*rgchTokens >> 3] |= (1 << (*rgchTokens & 7));
  64. } while (*rgchTokens++);
  65. if (!psz)
  66. {
  67. ENTEREXCLUSIVE();
  68. {
  69. psz = s_pszNextToken;
  70. }
  71. LEAVEEXCLUSIVE();
  72. }
  73. while (map[*psz >> 3] & (1 << (*psz & 7)) && *psz)
  74. psz++;
  75. pszToken = psz;
  76. for (;; psz++)
  77. {
  78. if (map[*psz >> 3] & (1 << (*psz & 7)))
  79. {
  80. if (!*psz && psz == pszToken)
  81. return(NULL);
  82. if (*psz)
  83. *psz++ = TEXT('\0');
  84. ENTEREXCLUSIVE();
  85. {
  86. g_pszNextToken = psz;
  87. }
  88. LEAVEEXCLUSIVE();
  89. return pszToken;
  90. }
  91. }
  92. }
  93. #endif
  94. /*----------------------------------------------------------
  95. Purpose: Get a string from the resource string table. Returned
  96. ptr is a ptr to static memory. The next call to this
  97. function will wipe out the prior contents.
  98. Returns: Ptr to string
  99. Cond: --
  100. */
  101. LPTSTR PUBLIC SzFromIDS(
  102. UINT ids, // resource ID
  103. LPTSTR pszBuf,
  104. UINT cchBuf)
  105. {
  106. ASSERT(pszBuf);
  107. *pszBuf = NULL_CHAR;
  108. LoadString(g_hinst, ids, pszBuf, cchBuf);
  109. return pszBuf;
  110. }
  111. /*----------------------------------------------------------
  112. Purpose: Formats a string by allocating a buffer and loading
  113. the given resource strings to compose the string.
  114. Returns: the count of characters
  115. Cond: Caller should free the allocated buffer using GFree.
  116. */
  117. BOOL PUBLIC FmtString(
  118. LPCTSTR * ppszBuf,
  119. UINT idsFmt,
  120. LPUINT rgids,
  121. UINT cids)
  122. {
  123. UINT cch = 0;
  124. UINT cchMax;
  125. LPTSTR pszBuf;
  126. ASSERT(ppszBuf);
  127. ASSERT(rgids);
  128. ASSERT(cids > 0);
  129. cchMax = (1+cids) * MAXPATHLEN;
  130. pszBuf = GAlloc(CbFromCch(cchMax));
  131. if (pszBuf)
  132. {
  133. // The first cids DWORDS are the addresses of the offset strings
  134. // in the buffer (passed to wvsprintf)
  135. LPBYTE pszMsgs = GAlloc((cids * sizeof(DWORD_PTR)) + (cids * CbFromCch(MAXPATHLEN)));
  136. if (pszMsgs)
  137. {
  138. TCHAR szFmt[MAXPATHLEN];
  139. DWORD_PTR *rgpsz = (DWORD_PTR*)pszMsgs;
  140. LPTSTR pszT = (LPTSTR)(pszMsgs + (cids * sizeof(DWORD_PTR)));
  141. UINT i;
  142. // Load the series of strings
  143. for (i = 0; i < cids; i++, pszT += MAXPATHLEN)
  144. {
  145. rgpsz[i] = (DWORD_PTR)pszT;
  146. SzFromIDS(rgids[i], pszT, MAXPATHLEN);
  147. }
  148. // Compose the string
  149. SzFromIDS(idsFmt, szFmt, ARRAYSIZE(szFmt));
  150. cch = FormatMessage(FORMAT_MESSAGE_FROM_STRING,
  151. szFmt, 0, 0, pszBuf, cchMax, (va_list *)&rgpsz);
  152. ASSERT(cch <= cchMax);
  153. GFree(pszMsgs);
  154. }
  155. // pszBuf is freed by caller
  156. }
  157. *ppszBuf = pszBuf;
  158. return cch;
  159. }