strings.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 5k
Category:

Windows Kernel

Development Platform:

Visual C++

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