home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / w_cmp.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  8KB  |  237 lines

  1. /***
  2. *w_cmp.c - W versions of CompareString.
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Use either CompareStringA or CompareStringW depending on which is
  8. *       available
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <internal.h>
  14. #include <dbgint.h>
  15. #include <stdlib.h>
  16. #include <setlocal.h>
  17. #include <awint.h>
  18. #include <dbgint.h>
  19.  
  20. #define USE_W   1
  21. #define USE_A   2
  22.  
  23. /***
  24. *int __cdecl wcsncnt - count wide characters in a string, up to n.
  25. *
  26. *Purpose:
  27. *       Internal local support function. Counts characters in string before NULL.
  28. *       If NULL not found in n chars, then return n.
  29. *
  30. *Entry:
  31. *       const wchar_t *string   - start of string
  32. *       int n                   - byte count
  33. *
  34. *Exit:
  35. *       returns number of wide characaters from start of string to
  36. *       NULL (exclusive), up to n.
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. static int __cdecl wcsncnt (
  43.         const wchar_t *string,
  44.         int cnt
  45.         )
  46. {
  47.         int n = cnt;
  48.         wchar_t *cp = (wchar_t *)string;
  49.  
  50.         while (n-- && *cp)
  51.             cp++;
  52.  
  53.         if (!*cp)
  54.             return cp - string;
  55.         return cnt;
  56. }
  57.  
  58. /***
  59. *int __cdecl __crtCompareStringW - Get type information about a wide string.
  60. *
  61. *Purpose:
  62. *  Internal support function. Assumes info in wide string format. Tries
  63. *  to use NLS API call CompareStringW if available and uses CompareStringA
  64. *  if it must. If neither are available it fails and returns 0.
  65. *
  66. *Entry:
  67. *  LCID     Locale      - locale context for the comparison.
  68. *  DWORD    dwCmpFlags  - see NT\Chicago docs
  69. *  LPCWSTR  lpStringn   - wide string to be compared
  70. *  int      cchCountn   - wide char (word) count (NOT including NULL)
  71. *                       (-1 if NULL terminated)
  72. *  int      code_page   - for MB/WC conversion. If 0, use __lc_codepage
  73. *
  74. *Exit:
  75. *  Success: 1 - if lpString1 <  lpString2
  76. *           2 - if lpString1 == lpString2
  77. *           3 - if lpString1 >  lpString2
  78. *  Failure: 0
  79. *
  80. *Exceptions:
  81. *
  82. *******************************************************************************/
  83.  
  84. int __cdecl __crtCompareStringW(
  85.         LCID     Locale,
  86.         DWORD    dwCmpFlags,
  87.         LPCWSTR  lpString1,
  88.         int      cchCount1,
  89.         LPCWSTR  lpString2,
  90.         int      cchCount2,
  91.         int      code_page
  92.         )
  93. {
  94.         static int f_use = 0;
  95.  
  96.         /*
  97.          * Look for unstubbed 'preferred' flavor. Otherwise use available flavor.
  98.          * Must actually call the function to ensure it's not a stub.
  99.          */
  100.  
  101.         if (0 == f_use)
  102.         {
  103.             if (0 != CompareStringW(0, 0, L"\0", 1, L"\0", 1))
  104.                 f_use = USE_W;
  105.  
  106.             else if (0 != CompareStringA(0, 0, "\0", 1, "\0", 1))
  107.                 f_use = USE_A;
  108.  
  109.             else
  110.                 return 0;
  111.         }
  112.  
  113.         /*
  114.          * CompareString will compare past NULL. Must find NULL if in string
  115.          * before cchCountn wide characters.
  116.          */
  117.  
  118.         if (cchCount1 > 0)
  119.             cchCount1= wcsncnt(lpString1, cchCount1);
  120.         if (cchCount2 > 0)
  121.             cchCount2= wcsncnt(lpString2, cchCount2);
  122.  
  123.         if (!cchCount1 || !cchCount2)
  124.             return (cchCount1 - cchCount2 == 0) ? 2 :
  125.                    (cchCount1 - cchCount2 < 0) ? 1 : 3;
  126.  
  127.         /* Use "W" version */
  128.  
  129.         if (USE_W == f_use)
  130.         {
  131.             return CompareStringW( Locale,
  132.                                    dwCmpFlags,
  133.                                    lpString1,
  134.                                    cchCount1,
  135.                                    lpString2,
  136.                                    cchCount2 );
  137.         }
  138.  
  139.         /* Use "A" version */
  140.  
  141.         if (USE_A == f_use)
  142.         {
  143.             int buff_size1;
  144.             int buff_size2;
  145.             unsigned char *buffer1;
  146.             unsigned char *buffer2;
  147.  
  148.             /*
  149.              * Use __lc_codepage for conversion if code_page not specified
  150.              */
  151.  
  152.             if (0 == code_page)
  153.                 code_page = __lc_codepage;
  154.  
  155.             /*
  156.              * Convert strings and return the requested information.
  157.              */
  158.  
  159.             /* find out how big a buffer we need (includes NULL if any) */
  160.             if ( 0 == (buff_size1 = WideCharToMultiByte( code_page,
  161.                                                          WC_COMPOSITECHECK |
  162.                                                             WC_SEPCHARS,
  163.                                                          lpString1,
  164.                                                          cchCount1,
  165.                                                          NULL,
  166.                                                          0,
  167.                                                          NULL,
  168.                                                          NULL )) )
  169.                 return 0;
  170.  
  171.             /* allocate enough space for chars */
  172.             __try {
  173.                 buffer1 = (unsigned char *)_alloca( buff_size1 * sizeof(char) );
  174.             }
  175.             __except( EXCEPTION_EXECUTE_HANDLER ) {
  176.                 buffer1 = NULL;
  177.             }
  178.  
  179.             if ( buffer1 == NULL )
  180.                 return 0;
  181.  
  182.             /* do the conversion */
  183.             if ( 0 == WideCharToMultiByte( code_page,
  184.                                            WC_COMPOSITECHECK | WC_SEPCHARS,
  185.                                            lpString1,
  186.                                            cchCount1,
  187.                                            buffer1,
  188.                                            buff_size1,
  189.                                            NULL,
  190.                                            NULL ) )
  191.                 return 0;
  192.  
  193.             /* find out how big a buffer we need (includes NULL if any) */
  194.             if ( 0 == (buff_size2 = WideCharToMultiByte( code_page,
  195.                                                          WC_COMPOSITECHECK |
  196.                                                             WC_SEPCHARS,
  197.                                                          lpString2,
  198.                                                          cchCount2,
  199.                                                          NULL,
  200.                                                          0,
  201.                                                          NULL,
  202.                                                          NULL )) )
  203.                 return 0;
  204.  
  205.             /* allocate enough space for chars */
  206.             __try {
  207.                 buffer2 = (unsigned char *)_alloca( buff_size2 * sizeof(char) );
  208.             }
  209.             __except( EXCEPTION_EXECUTE_HANDLER ) {
  210.                 buffer2 = NULL;
  211.             }
  212.  
  213.             if ( buffer2 == NULL )
  214.                 return 0;
  215.  
  216.             /* do the conversion */
  217.             if ( 0 == WideCharToMultiByte( code_page,
  218.                                            WC_COMPOSITECHECK | WC_SEPCHARS,
  219.                                            lpString2,
  220.                                            cchCount2,
  221.                                            buffer2,
  222.                                            buff_size2,
  223.                                            NULL,
  224.                                            NULL ) )
  225.                 return 0;
  226.  
  227.             return CompareStringA( Locale,
  228.                                    dwCmpFlags,
  229.                                    buffer1,
  230.                                    buff_size1,
  231.                                    buffer2,
  232.                                    buff_size2 );
  233.         }
  234.         else   /* f_use is neither USE_A nor USE_W */
  235.             return 0;
  236. }
  237.