nsresult nsCollationOS2::CompareString(int32_t strength, const nsAString& string1, const nsAString& string2, int32_t* result) { nsAutoString stringNormalized1, stringNormalized2; if (strength != kCollationCaseSensitive) { nsresult res; res = mCollation->NormalizeString(string1, stringNormalized1); if (NS_FAILED(res)) { return res; } res = mCollation->NormalizeString(string2, stringNormalized2); if (NS_FAILED(res)) { return res; } } else { stringNormalized1 = string1; stringNormalized2 = string2; } LocaleObject locObj = nullptr; int ret = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &locObj); if (ret != ULS_SUCCESS) UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"C", &locObj); *result = UniStrcoll(locObj, (UniChar *)stringNormalized1.get(), (UniChar *)stringNormalized2.get()); UniFreeLocaleObject(locObj); return NS_OK; }
NS_IMETHODIMP nsOS2Locale::GetPlatformLocale(const nsAString& locale, PULONG os2Codepage) { LocaleObject locObj = NULL; int codePage; nsAutoString tempLocale(locale); tempLocale.ReplaceChar('-', '_'); int ret = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)PromiseFlatString(tempLocale).get(), &locObj); if (ret != ULS_SUCCESS) UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"C", &locObj); ret = UniQueryLocaleValue(locObj, LOCI_iCodepage, &codePage); if (ret != ULS_SUCCESS) return NS_ERROR_FAILURE; if (codePage == 437) { *os2Codepage = 850; } else { *os2Codepage = codePage; } UniFreeLocaleObject(locObj); return NS_OK; }
nsresult nsCollationOS2::AllocateRawSortKey(int32_t strength, const nsAString& stringIn, uint8_t** key, uint32_t* outLen) { nsresult res = NS_OK; nsAutoString stringNormalized; if (strength != kCollationCaseSensitive) { res = mCollation->NormalizeString(stringIn, stringNormalized); if (NS_FAILED(res)) return res; } else { stringNormalized = stringIn; } LocaleObject locObj = nullptr; int ret = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &locObj); if (ret != ULS_SUCCESS) UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"C", &locObj); res = NS_ERROR_FAILURE; // From here on out assume failure... int length = UniStrxfrm(locObj, nullptr, reinterpret_cast<const UniChar *>(stringNormalized.get()), 0); if (length >= 0) { length += 5; // Allow for the "extra" chars UniStrxfrm() // will out put (overrunning the buffer if // you let it...) // Magic, persistent buffer. If it's not twice the size we need, // we grow/reallocate it 4X so it doesn't grow often. static UniChar* pLocalBuffer = nullptr; static int iBufferLength = 100; if (iBufferLength < length*2) { if ( pLocalBuffer ) { free(pLocalBuffer); pLocalBuffer = nullptr; } iBufferLength = length*4; } if (!pLocalBuffer) pLocalBuffer = (UniChar*) malloc(sizeof(UniChar) * iBufferLength); if (pLocalBuffer) { // Do the Xfrm int uLen = UniStrxfrm(locObj, pLocalBuffer, reinterpret_cast<const UniChar *>(stringNormalized.get()), iBufferLength); // See how big the result really is uLen = UniStrlen(pLocalBuffer); // make sure it will fit in the output buffer... if (uLen < iBufferLength) { // Success! // Give 'em the real size in bytes... *key = (uint8_t *)NS_strdup((char16_t*) pLocalBuffer); *outLen = uLen * 2 + 2; res = NS_OK; } } } UniFreeLocaleObject(locObj); return res; }
// // nsLocaleService methods // nsLocaleService::nsLocaleService(void) : mSystemLocale(0), mApplicationLocale(0) { #ifdef XP_WIN nsCOMPtr<nsIWin32Locale> win32Converter = do_GetService(NS_WIN32LOCALE_CONTRACTID); NS_ASSERTION(win32Converter, "nsLocaleService: can't get win32 converter\n"); nsAutoString xpLocale; if (win32Converter) { nsresult result; // // get the system LCID // LCID win_lcid = GetSystemDefaultLCID(); if (win_lcid==0) { return;} result = win32Converter->GetXPLocale(win_lcid, xpLocale); if (NS_FAILED(result)) { return;} result = NewLocale(xpLocale, getter_AddRefs(mSystemLocale)); if (NS_FAILED(result)) { return;} // // get the application LCID // win_lcid = GetUserDefaultLCID(); if (win_lcid==0) { return;} result = win32Converter->GetXPLocale(win_lcid, xpLocale); if (NS_FAILED(result)) { return;} result = NewLocale(xpLocale, getter_AddRefs(mApplicationLocale)); if (NS_FAILED(result)) { return;} } #endif #if (defined(XP_UNIX) && !defined(XP_MACOSX)) || defined(XP_BEOS) || defined(XP_AMIGAOS) nsCOMPtr<nsIPosixLocale> posixConverter = do_GetService(NS_POSIXLOCALE_CONTRACTID); nsAutoString xpLocale, platformLocale; if (posixConverter) { nsAutoString category, category_platform; int i; nsRefPtr<nsLocale> resultLocale(new nsLocale()); if ( resultLocale == NULL ) { return; } #ifdef MOZ_WIDGET_QT const char* lang = QLocale::languageToString(QLocale::system().language()).toAscii(); #else // Get system configuration const char* lang = getenv("LANG"); #endif for( i = 0; i < LocaleListLength; i++ ) { nsresult result; // setlocale( , "") evaluates LC_* and LANG char* lc_temp = setlocale(posix_locale_category[i], ""); CopyASCIItoUTF16(LocaleList[i], category); category_platform = category; category_platform.AppendLiteral("##PLATFORM"); if (lc_temp != nsnull) { result = posixConverter->GetXPLocale(lc_temp, xpLocale); CopyASCIItoUTF16(lc_temp, platformLocale); } else { if ( lang == nsnull ) { platformLocale.AssignLiteral("en_US"); result = posixConverter->GetXPLocale("en-US", xpLocale); } else { CopyASCIItoUTF16(lang, platformLocale); result = posixConverter->GetXPLocale(lang, xpLocale); } } if (NS_FAILED(result)) { return; } resultLocale->AddCategory(category, xpLocale); resultLocale->AddCategory(category_platform, platformLocale); } mSystemLocale = do_QueryInterface(resultLocale); mApplicationLocale = do_QueryInterface(resultLocale); } // if ( NS_SUCCEEDED )... #endif // XP_UNIX || XP_BEOS #ifdef XP_OS2 nsCOMPtr<nsIOS2Locale> os2Converter = do_GetService(NS_OS2LOCALE_CONTRACTID); nsAutoString xpLocale; if (os2Converter) { nsAutoString category; int i; nsRefPtr<nsLocale> resultLocale(new nsLocale()); if ( resultLocale == NULL ) { return; } LocaleObject locale_object = NULL; int result = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &locale_object); if (result != ULS_SUCCESS) { int result = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"en_US", &locale_object); } char* lc_temp; for( i = 0; i < LocaleListLength; i++ ) { lc_temp = nsnull; UniQueryLocaleObject(locale_object, posix_locale_category[i], UNI_MBS_STRING_POINTER, (void **)&lc_temp); category.AssignWithConversion(LocaleList[i]); nsresult result; if (lc_temp != nsnull) result = os2Converter->GetXPLocale(lc_temp, xpLocale); else { char* lang = getenv("LANG"); if ( lang == nsnull ) { result = os2Converter->GetXPLocale("en-US", xpLocale); } else result = os2Converter->GetXPLocale(lang, xpLocale); } if (NS_FAILED(result)) { UniFreeMem(lc_temp); UniFreeLocaleObject(locale_object); return; } resultLocale->AddCategory(category, xpLocale); UniFreeMem(lc_temp); } UniFreeLocaleObject(locale_object); mSystemLocale = do_QueryInterface(resultLocale); mApplicationLocale = do_QueryInterface(resultLocale); } // if ( NS_SUCCEEDED )... #endif // XP_OS2 #ifdef XP_MACOSX // Get string representation of user's current locale CFLocaleRef userLocaleRef = ::CFLocaleCopyCurrent(); CFStringRef userLocaleStr = ::CFLocaleGetIdentifier(userLocaleRef); ::CFRetain(userLocaleStr); nsAutoTArray<UniChar, 32> buffer; int size = ::CFStringGetLength(userLocaleStr); if (buffer.SetLength(size + 1)) { CFRange range = ::CFRangeMake(0, size); ::CFStringGetCharacters(userLocaleStr, range, buffer.Elements()); buffer[size] = 0; // Convert the locale string to the format that Mozilla expects nsAutoString xpLocale(buffer.Elements()); xpLocale.ReplaceChar('_', '-'); nsresult rv = NewLocale(xpLocale, getter_AddRefs(mSystemLocale)); if (NS_SUCCEEDED(rv)) { mApplicationLocale = mSystemLocale; } } ::CFRelease(userLocaleStr); ::CFRelease(userLocaleRef); NS_ASSERTION(mApplicationLocale, "Failed to create locale objects"); #endif // XP_MACOSX }
// performs a locale sensitive date formatting operation on the struct tm parameter nsresult nsDateTimeFormatOS2::FormatTMTime(nsILocale* locale, const nsDateFormatSelector dateFormatSelector, const nsTimeFormatSelector timeFormatSelector, const struct tm* tmTime, nsAString &stringOut) { nsresult rc = NS_ERROR_FAILURE; UniChar uFmtD[NSDATETIME_FORMAT_BUFFER_LEN] = { 0 }; UniChar uFmtT[NSDATETIME_FORMAT_BUFFER_LEN] = { 0 }; UniChar *pString = nullptr; LocaleObject locObj = NULL; int ret = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &locObj); if (ret != ULS_SUCCESS) UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"C", &locObj); bool f24Hour = false; UniQueryLocaleItem(locObj, LOCI_iTime, &pString); if (pString[0] == '1') { f24Hour = true; } // set date format switch (dateFormatSelector) { case kDateFormatNone: UniStrcat( uFmtD, (UniChar*)L""); break; case kDateFormatLong: case kDateFormatShort: UniStrcat( uFmtD, (UniChar*)L"%x"); break; case kDateFormatYearMonth: UniQueryLocaleItem( locObj, DATESEP, &pString); UniStrcat( uFmtD, (UniChar*)L"%Y"); UniStrcat( uFmtD, pString); UniStrcat( uFmtD, (UniChar*)L"%m"); UniFreeMem(pString); break; case kDateFormatWeekday: UniStrcat( uFmtD, (UniChar*)L"%a"); break; default: UniStrcat( uFmtD, (UniChar*)L""); } // set time format switch (timeFormatSelector) { case kTimeFormatNone: UniStrcat( uFmtT, (UniChar*)L""); break; case kTimeFormatSeconds: UniQueryLocaleItem( locObj, TIMESEP, &pString); if (f24Hour) UniStrcat( uFmtT, (UniChar*)L"%H"); else UniStrcat( uFmtT, (UniChar*)L"%I"); UniStrcat( uFmtT, pString); UniStrcat( uFmtT, (UniChar*)L"%M"); UniStrcat( uFmtT, pString); UniStrcat( uFmtT, (UniChar*)L"%S"); if (!f24Hour) UniStrcat( uFmtT, (UniChar*)L" %p"); UniFreeMem(pString); break; case kTimeFormatNoSeconds: UniQueryLocaleItem( locObj, TIMESEP, &pString); if (f24Hour) UniStrcat( uFmtT, (UniChar*)L"%H"); else UniStrcat( uFmtT, (UniChar*)L"%I"); UniStrcat( uFmtT, pString); UniStrcat( uFmtT, (UniChar*)L"%M"); if (!f24Hour) UniStrcat( uFmtT, (UniChar*)L" %p"); UniFreeMem(pString); break; case kTimeFormatSecondsForce24Hour: UniQueryLocaleItem( locObj, TIMESEP, &pString); UniStrcat( uFmtT, (UniChar*)L"%H"); UniStrcat( uFmtT, pString); UniStrcat( uFmtT, (UniChar*)L"%M"); UniStrcat( uFmtT, pString); UniStrcat( uFmtT, (UniChar*)L"%S"); UniFreeMem(pString); break; case kTimeFormatNoSecondsForce24Hour: UniQueryLocaleItem( locObj, TIMESEP, &pString); UniStrcat( uFmtT, (UniChar*)L"%H"); UniStrcat( uFmtT, pString); UniStrcat( uFmtT, (UniChar*)L"%M"); UniFreeMem(pString); break; default: UniStrcat( uFmtT, (UniChar*)L""); } PRUnichar buffer[NSDATETIME_FORMAT_BUFFER_LEN] = {0}; if ((dateFormatSelector != kDateFormatNone) && (timeFormatSelector != kTimeFormatNone)) { UniStrcat( uFmtD, (UniChar*)L" "); } UniStrcat( uFmtD, uFmtT); int length = UniStrftime(locObj, reinterpret_cast<UniChar *>(buffer), NSDATETIME_FORMAT_BUFFER_LEN, uFmtD, tmTime); UniFreeLocaleObject(locObj); if ( length != 0) { stringOut.Assign(buffer, length); rc = NS_OK; } return rc; }