Leaked source code of windows server 2003
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.

107 lines
3.2 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: CompareString.cpp
  4. //
  5. // Module: as required
  6. //
  7. // Synopsis: Using lstrcmpi to do a case-insensitive comparison of two strings
  8. // can have unexpected result on certain locales if one of the strings
  9. // is a constant. The 2 functions here are the preferred replacements.
  10. //
  11. // Note that these functions are also present in CMUTIL.dll. However,
  12. // a dependency on cmutil is a Bad Thing (TM) for modules that do not
  13. // sim-ship with it (this includes customactions and CMAK).
  14. //
  15. // Copyright (c) 1998-2002 Microsoft Corporation
  16. //
  17. // Author: SumitC Created 12-Sep-2001
  18. //
  19. //+----------------------------------------------------------------------------
  20. #include "windows.h"
  21. #include "CompareString.h"
  22. //
  23. // The following is to ensure that we don't try to use U functions (i.e. CMutoa
  24. // functions) in a module that doesn't support it.
  25. //
  26. #ifndef _CMUTIL_STRINGS_CPP_
  27. #ifndef CompareStringU
  28. #ifdef UNICODE
  29. #define CompareStringU CompareStringW
  30. #else
  31. #define CompareStringU CompareStringA
  32. #endif
  33. #endif
  34. #endif
  35. //+----------------------------------------------------------------------------
  36. //
  37. // Function: SafeCompareStringA
  38. //
  39. // Synopsis: implementation of lstrcmpi that is sensitive to locale variations
  40. //
  41. // Arguments: LPCTSTR lpString1, lpString2 - strings to compare
  42. //
  43. // Returns: int (-1, 0 or +1). In case of error, -1 is returned.
  44. //
  45. //+----------------------------------------------------------------------------
  46. int SafeCompareStringA(LPCSTR lpString1, LPCSTR lpString2)
  47. {
  48. int iReturn = -1;
  49. DWORD lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  50. iReturn = CompareStringA(lcid, NORM_IGNORECASE, lpString1, -1, lpString2, -1);
  51. if (iReturn == 0)
  52. {
  53. iReturn = -1;
  54. }
  55. else
  56. {
  57. iReturn -= CSTR_EQUAL; // to make the return values -1 or 0 or 1
  58. }
  59. return iReturn;
  60. }
  61. #if defined(UNICODE) || defined(_CMUTIL_STRINGS_CPP_)
  62. //+----------------------------------------------------------------------------
  63. //
  64. // Function: SafeCompareStringW
  65. //
  66. // Synopsis: implementation of lstrcmpi that is sensitive to locale variations
  67. //
  68. // Arguments: LPCTSTR lpString1, lpString2 - strings to compare
  69. //
  70. // Returns: int (-1, 0 or +1). In case of error, -1 is returned.
  71. //
  72. //+----------------------------------------------------------------------------
  73. int SafeCompareStringW(LPCWSTR lpString1, LPCWSTR lpString2)
  74. {
  75. int iReturn = -1;
  76. if (OS_NT51)
  77. {
  78. iReturn = CompareStringU(LOCALE_INVARIANT, NORM_IGNORECASE, lpString1, -1, lpString2, -1);
  79. }
  80. else
  81. {
  82. DWORD lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  83. iReturn = CompareStringU(lcid, NORM_IGNORECASE, lpString1, -1, lpString2, -1);
  84. }
  85. if (iReturn == 0)
  86. {
  87. iReturn = -1;
  88. }
  89. else
  90. {
  91. iReturn -= CSTR_EQUAL; // to make the return values -1 or 0 or 1
  92. }
  93. return iReturn;
  94. }
  95. #endif // UNICODE