intl/locale/src/windows/nsCollationWin.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include "nsCollationWin.h"
     8 #include "nsIServiceManager.h"
     9 #include "nsIComponentManager.h"
    10 #include "nsILocaleService.h"
    11 #include "nsIPlatformCharset.h"
    12 #include "nsWin32Locale.h"
    13 #include "nsCOMPtr.h"
    14 #include "prmem.h"
    15 #include "plstr.h"
    16 #include <windows.h>
    18 #undef CompareString
    20 NS_IMPL_ISUPPORTS(nsCollationWin, nsICollation)
    23 nsCollationWin::nsCollationWin() : mCollation(nullptr)
    24 {
    25 }
    27 nsCollationWin::~nsCollationWin() 
    28 {
    29   if (mCollation)
    30     delete mCollation;
    31 }
    33 nsresult nsCollationWin::Initialize(nsILocale* locale) 
    34 {
    35   NS_ASSERTION(!mCollation, "Should only be initialized once.");
    37   nsresult res;
    39   mCollation = new nsCollation;
    41   // default LCID (en-US)
    42   mLCID = 1033;
    44   nsAutoString localeStr;
    46   // get locale string, use app default if no locale specified
    47   if (!locale) {
    48     nsCOMPtr<nsILocaleService> localeService = 
    49              do_GetService(NS_LOCALESERVICE_CONTRACTID);
    50     if (localeService) {
    51       nsCOMPtr<nsILocale> appLocale;
    52       res = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
    53       if (NS_SUCCEEDED(res)) {
    54         res = appLocale->GetCategory(NS_LITERAL_STRING("NSILOCALE_COLLATE"), 
    55                                      localeStr);
    56       }
    57     }
    58   }
    59   else {
    60     res = locale->GetCategory(NS_LITERAL_STRING("NSILOCALE_COLLATE"), 
    61                               localeStr);
    62   }
    64   // Get LCID and charset name from locale, if available
    65   LCID lcid;
    66   res = nsWin32Locale::GetPlatformLocale(localeStr, &lcid);
    67   if (NS_SUCCEEDED(res)) {
    68     mLCID = lcid;
    69   }
    71   nsCOMPtr <nsIPlatformCharset> platformCharset = 
    72       do_GetService(NS_PLATFORMCHARSET_CONTRACTID);
    73   if (platformCharset) {
    74     nsAutoCString mappedCharset;
    75     res = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset);
    76     if (NS_SUCCEEDED(res)) {
    77       mCollation->SetCharset(mappedCharset.get());
    78     }
    79   }
    81   return NS_OK;
    82 }
    85 NS_IMETHODIMP nsCollationWin::CompareString(int32_t strength, 
    86                                             const nsAString & string1, 
    87                                             const nsAString & string2, 
    88                                             int32_t *result)
    89 {
    90   int retval;
    91   nsresult res;
    92   DWORD dwMapFlags = 0;
    94   if (strength == kCollationCaseInSensitive)
    95     dwMapFlags |= NORM_IGNORECASE;
    97   retval = ::CompareStringW(mLCID, 
    98                             dwMapFlags,
    99                             (LPCWSTR) PromiseFlatString(string1).get(), 
   100                             -1,
   101                             (LPCWSTR) PromiseFlatString(string2).get(), 
   102                             -1);
   103   if (retval) {
   104     res = NS_OK;
   105     *result = retval - 2;
   106   } else {
   107     res = NS_ERROR_FAILURE;
   108   }
   110   return res;
   111 }
   114 nsresult nsCollationWin::AllocateRawSortKey(int32_t strength, 
   115                                             const nsAString& stringIn, uint8_t** key, uint32_t* outLen)
   116 {
   117   int byteLen;
   118   void *buffer;
   119   nsresult res = NS_OK;
   120   DWORD dwMapFlags = LCMAP_SORTKEY;
   122   if (strength == kCollationCaseInSensitive)
   123     dwMapFlags |= NORM_IGNORECASE;
   125   byteLen = LCMapStringW(mLCID, dwMapFlags, 
   126                          (LPCWSTR) PromiseFlatString(stringIn).get(),
   127                          -1, nullptr, 0);
   128   buffer = PR_Malloc(byteLen);
   129   if (!buffer) {
   130     res = NS_ERROR_OUT_OF_MEMORY;
   131   } else {
   132     *key = (uint8_t *)buffer;
   133     *outLen = LCMapStringW(mLCID, dwMapFlags, 
   134                            (LPCWSTR) PromiseFlatString(stringIn).get(),
   135                            -1, (LPWSTR) buffer, byteLen);
   136   }
   137   return res;
   138 }
   140 nsresult nsCollationWin::CompareRawSortKey(const uint8_t* key1, uint32_t len1, 
   141                                            const uint8_t* key2, uint32_t len2, 
   142                                            int32_t* result)
   143 {
   144   *result = PL_strcmp((const char *)key1, (const char *)key2);
   145   return NS_OK;
   146 }

mercurial