intl/locale/src/windows/nsCollationWin.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/locale/src/windows/nsCollationWin.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +#include "nsCollationWin.h"
    1.11 +#include "nsIServiceManager.h"
    1.12 +#include "nsIComponentManager.h"
    1.13 +#include "nsILocaleService.h"
    1.14 +#include "nsIPlatformCharset.h"
    1.15 +#include "nsWin32Locale.h"
    1.16 +#include "nsCOMPtr.h"
    1.17 +#include "prmem.h"
    1.18 +#include "plstr.h"
    1.19 +#include <windows.h>
    1.20 +
    1.21 +#undef CompareString
    1.22 +
    1.23 +NS_IMPL_ISUPPORTS(nsCollationWin, nsICollation)
    1.24 +
    1.25 +
    1.26 +nsCollationWin::nsCollationWin() : mCollation(nullptr)
    1.27 +{
    1.28 +}
    1.29 +
    1.30 +nsCollationWin::~nsCollationWin() 
    1.31 +{
    1.32 +  if (mCollation)
    1.33 +    delete mCollation;
    1.34 +}
    1.35 +
    1.36 +nsresult nsCollationWin::Initialize(nsILocale* locale) 
    1.37 +{
    1.38 +  NS_ASSERTION(!mCollation, "Should only be initialized once.");
    1.39 +
    1.40 +  nsresult res;
    1.41 +
    1.42 +  mCollation = new nsCollation;
    1.43 +
    1.44 +  // default LCID (en-US)
    1.45 +  mLCID = 1033;
    1.46 +
    1.47 +  nsAutoString localeStr;
    1.48 +
    1.49 +  // get locale string, use app default if no locale specified
    1.50 +  if (!locale) {
    1.51 +    nsCOMPtr<nsILocaleService> localeService = 
    1.52 +             do_GetService(NS_LOCALESERVICE_CONTRACTID);
    1.53 +    if (localeService) {
    1.54 +      nsCOMPtr<nsILocale> appLocale;
    1.55 +      res = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
    1.56 +      if (NS_SUCCEEDED(res)) {
    1.57 +        res = appLocale->GetCategory(NS_LITERAL_STRING("NSILOCALE_COLLATE"), 
    1.58 +                                     localeStr);
    1.59 +      }
    1.60 +    }
    1.61 +  }
    1.62 +  else {
    1.63 +    res = locale->GetCategory(NS_LITERAL_STRING("NSILOCALE_COLLATE"), 
    1.64 +                              localeStr);
    1.65 +  }
    1.66 +
    1.67 +  // Get LCID and charset name from locale, if available
    1.68 +  LCID lcid;
    1.69 +  res = nsWin32Locale::GetPlatformLocale(localeStr, &lcid);
    1.70 +  if (NS_SUCCEEDED(res)) {
    1.71 +    mLCID = lcid;
    1.72 +  }
    1.73 +
    1.74 +  nsCOMPtr <nsIPlatformCharset> platformCharset = 
    1.75 +      do_GetService(NS_PLATFORMCHARSET_CONTRACTID);
    1.76 +  if (platformCharset) {
    1.77 +    nsAutoCString mappedCharset;
    1.78 +    res = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset);
    1.79 +    if (NS_SUCCEEDED(res)) {
    1.80 +      mCollation->SetCharset(mappedCharset.get());
    1.81 +    }
    1.82 +  }
    1.83 +
    1.84 +  return NS_OK;
    1.85 +}
    1.86 +
    1.87 +
    1.88 +NS_IMETHODIMP nsCollationWin::CompareString(int32_t strength, 
    1.89 +                                            const nsAString & string1, 
    1.90 +                                            const nsAString & string2, 
    1.91 +                                            int32_t *result)
    1.92 +{
    1.93 +  int retval;
    1.94 +  nsresult res;
    1.95 +  DWORD dwMapFlags = 0;
    1.96 +
    1.97 +  if (strength == kCollationCaseInSensitive)
    1.98 +    dwMapFlags |= NORM_IGNORECASE;
    1.99 +
   1.100 +  retval = ::CompareStringW(mLCID, 
   1.101 +                            dwMapFlags,
   1.102 +                            (LPCWSTR) PromiseFlatString(string1).get(), 
   1.103 +                            -1,
   1.104 +                            (LPCWSTR) PromiseFlatString(string2).get(), 
   1.105 +                            -1);
   1.106 +  if (retval) {
   1.107 +    res = NS_OK;
   1.108 +    *result = retval - 2;
   1.109 +  } else {
   1.110 +    res = NS_ERROR_FAILURE;
   1.111 +  }
   1.112 +
   1.113 +  return res;
   1.114 +}
   1.115 + 
   1.116 +
   1.117 +nsresult nsCollationWin::AllocateRawSortKey(int32_t strength, 
   1.118 +                                            const nsAString& stringIn, uint8_t** key, uint32_t* outLen)
   1.119 +{
   1.120 +  int byteLen;
   1.121 +  void *buffer;
   1.122 +  nsresult res = NS_OK;
   1.123 +  DWORD dwMapFlags = LCMAP_SORTKEY;
   1.124 +
   1.125 +  if (strength == kCollationCaseInSensitive)
   1.126 +    dwMapFlags |= NORM_IGNORECASE;
   1.127 +
   1.128 +  byteLen = LCMapStringW(mLCID, dwMapFlags, 
   1.129 +                         (LPCWSTR) PromiseFlatString(stringIn).get(),
   1.130 +                         -1, nullptr, 0);
   1.131 +  buffer = PR_Malloc(byteLen);
   1.132 +  if (!buffer) {
   1.133 +    res = NS_ERROR_OUT_OF_MEMORY;
   1.134 +  } else {
   1.135 +    *key = (uint8_t *)buffer;
   1.136 +    *outLen = LCMapStringW(mLCID, dwMapFlags, 
   1.137 +                           (LPCWSTR) PromiseFlatString(stringIn).get(),
   1.138 +                           -1, (LPWSTR) buffer, byteLen);
   1.139 +  }
   1.140 +  return res;
   1.141 +}
   1.142 +
   1.143 +nsresult nsCollationWin::CompareRawSortKey(const uint8_t* key1, uint32_t len1, 
   1.144 +                                           const uint8_t* key2, uint32_t len2, 
   1.145 +                                           int32_t* result)
   1.146 +{
   1.147 +  *result = PL_strcmp((const char *)key1, (const char *)key2);
   1.148 +  return NS_OK;
   1.149 +}

mercurial