LCOV - code coverage report
Current view: top level - unotools/source/i18n - localedatawrapper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 526 926 56.8 %
Date: 2014-04-11 Functions: 49 62 79.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <string.h>
      21             : #include <stdio.h>
      22             : #include <string>
      23             : 
      24             : #include <unotools/localedatawrapper.hxx>
      25             : #include <unotools/numberformatcodewrapper.hxx>
      26             : #include <unotools/calendarwrapper.hxx>
      27             : #include <unotools/digitgroupingiterator.hxx>
      28             : #include <tools/debug.hxx>
      29             : #include <i18nlangtag/languagetag.hxx>
      30             : 
      31             : #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
      32             : #include <com/sun/star/i18n/KNumberFormatType.hpp>
      33             : #include <com/sun/star/i18n/LocaleData.hpp>
      34             : #include <com/sun/star/i18n/CalendarFieldIndex.hpp>
      35             : #include <com/sun/star/i18n/CalendarDisplayIndex.hpp>
      36             : #include <com/sun/star/i18n/NumberFormatIndex.hpp>
      37             : 
      38             : #include <comphelper/processfactory.hxx>
      39             : #include <rtl/instance.hxx>
      40             : #include <rtl/ustrbuf.hxx>
      41             : #include <sal/macros.h>
      42             : 
      43             : static const int nDateFormatInvalid = -1;
      44             : static const sal_uInt16 nCurrFormatInvalid = 0xffff;
      45             : static const sal_uInt16 nCurrFormatDefault = 0;
      46             : 
      47             : using namespace ::com::sun::star;
      48             : using namespace ::com::sun::star::i18n;
      49             : using namespace ::com::sun::star::uno;
      50             : 
      51             : namespace
      52             : {
      53             :     struct InstalledLocales
      54             :         : public rtl::Static<
      55             :             uno::Sequence< lang::Locale >, InstalledLocales >
      56             :     {};
      57             : 
      58             :     struct InstalledLanguageTypes
      59             :         : public rtl::Static<
      60             :             uno::Sequence< sal_uInt16 >, InstalledLanguageTypes >
      61             :     {};
      62             : }
      63             : 
      64             : sal_uInt8 LocaleDataWrapper::nLocaleDataChecking = 0;
      65             : 
      66       12793 : LocaleDataWrapper::LocaleDataWrapper(
      67             :             const Reference< uno::XComponentContext > & rxContext,
      68             :             const LanguageTag& rLanguageTag
      69             :             )
      70             :         :
      71             :         m_xContext( rxContext ),
      72             :         xLD( LocaleData::create(rxContext) ),
      73             :         maLanguageTag( rLanguageTag ),
      74             :         bLocaleDataItemValid( false ),
      75       12793 :         bReservedWordValid( false )
      76             : {
      77       12793 :     invalidateData();
      78       12793 : }
      79             : 
      80        1210 : LocaleDataWrapper::LocaleDataWrapper(
      81             :             const LanguageTag& rLanguageTag
      82             :             )
      83             :         :
      84             :         m_xContext( comphelper::getProcessComponentContext() ),
      85             :         xLD( LocaleData::create(m_xContext) ),
      86             :         maLanguageTag( rLanguageTag ),
      87             :         bLocaleDataItemValid( false ),
      88        1210 :         bReservedWordValid( false )
      89             : {
      90        1210 :     invalidateData();
      91        1210 : }
      92             : 
      93       13806 : LocaleDataWrapper::~LocaleDataWrapper()
      94             : {
      95       13806 : }
      96             : 
      97        6574 : void LocaleDataWrapper::setLanguageTag( const LanguageTag& rLanguageTag )
      98             : {
      99        6574 :     ::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nCriticalChange );
     100        6574 :     maLanguageTag = rLanguageTag;
     101        6574 :     invalidateData();
     102        6574 : }
     103             : 
     104        2153 : const LanguageTag& LocaleDataWrapper::getLanguageTag() const
     105             : {
     106        2153 :     ::utl::ReadWriteGuard aGuard( aMutex );
     107        2153 :     return maLanguageTag;
     108             : }
     109             : 
     110       93890 : const ::com::sun::star::lang::Locale& LocaleDataWrapper::getMyLocale() const
     111             : {
     112       93890 :     ::utl::ReadWriteGuard aGuard( aMutex );
     113       93890 :     return maLanguageTag.getLocale();
     114             : }
     115             : 
     116       20577 : void LocaleDataWrapper::invalidateData()
     117             : {
     118       20577 :     aCurrSymbol = "";
     119       20577 :     aCurrBankSymbol = "";
     120       20577 :     nDateFormat = nLongDateFormat = nDateFormatInvalid;
     121       20577 :     nCurrPositiveFormat = nCurrNegativeFormat = nCurrDigits = nCurrFormatInvalid;
     122       20577 :     if ( bLocaleDataItemValid )
     123             :     {
     124       10206 :         for (sal_Int32 j=0; j<LocaleItem::COUNT; ++j)
     125        9639 :             aLocaleItem[j] = "";
     126         567 :         bLocaleDataItemValid = false;
     127             :     }
     128       20577 :     if ( bReservedWordValid )
     129             :     {
     130         468 :         for ( sal_Int16 j=0; j<reservedWords::COUNT; ++j )
     131         432 :             aReservedWord[j] = "";
     132          36 :         bReservedWordValid = false;
     133             :     }
     134       20577 :     xDefaultCalendar.reset();
     135       20577 :     if (aGrouping.getLength())
     136          36 :         aGrouping[0] = 0;
     137       20577 :     if (aDateAcceptancePatterns.getLength())
     138          10 :         aDateAcceptancePatterns = Sequence<OUString>();
     139             :     // dummies
     140       20577 :     cCurrZeroChar = '0';
     141       20577 : }
     142             : 
     143             : /* FIXME-BCP47: locale data should provide a language tag instead that could be
     144             :  * passed on. */
     145       47372 : ::com::sun::star::i18n::LanguageCountryInfo LocaleDataWrapper::getLanguageCountryInfo() const
     146             : {
     147             :     try
     148             :     {
     149       47372 :         return xLD->getLanguageCountryInfo( getMyLocale() );
     150             :     }
     151           0 :     catch (const Exception& e)
     152             :     {
     153             :         SAL_WARN( "unotools.i18n", "getLanguageCountryInfo: Exception caught " << e.Message );
     154             :     }
     155           0 :     return ::com::sun::star::i18n::LanguageCountryInfo();
     156             : }
     157             : 
     158        3249 : ::com::sun::star::i18n::LocaleDataItem LocaleDataWrapper::getLocaleItem() const
     159             : {
     160             :     try
     161             :     {
     162        3249 :         return xLD->getLocaleItem( getMyLocale() );
     163             :     }
     164           0 :     catch (const Exception& e)
     165             :     {
     166             :         SAL_WARN( "unotools.i18n", "getLocaleItem: Exception caught " << e.Message );
     167             :     }
     168           0 :     return ::com::sun::star::i18n::LocaleDataItem();
     169             : }
     170             : 
     171       25130 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::Currency2 > LocaleDataWrapper::getAllCurrencies() const
     172             : {
     173             :     try
     174             :     {
     175       25130 :         return xLD->getAllCurrencies2( getMyLocale() );
     176             :     }
     177           0 :     catch (const Exception& e)
     178             :     {
     179             :         SAL_WARN( "unotools.i18n", "getAllCurrencies: Exception caught " << e.Message );
     180             :     }
     181           0 :     return ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::Currency2 >(0);
     182             : }
     183             : 
     184           0 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > LocaleDataWrapper::getAllFormats() const
     185             : {
     186             :     try
     187             :     {
     188           0 :         return xLD->getAllFormats( getMyLocale() );
     189             :     }
     190           0 :     catch (const Exception& e)
     191             :     {
     192             :         SAL_WARN( "unotools.i18n", "getAllFormats: Exception caught " << e.Message );
     193             :     }
     194           0 :     return ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement >(0);
     195             : }
     196             : 
     197       11078 : ::com::sun::star::i18n::ForbiddenCharacters LocaleDataWrapper::getForbiddenCharacters() const
     198             : {
     199             :     try
     200             :     {
     201       11078 :         return xLD->getForbiddenCharacters( getMyLocale() );
     202             :     }
     203           0 :     catch (const Exception& e)
     204             :     {
     205             :         SAL_WARN( "unotools.i18n", "getForbiddenCharacters: Exception caught " << e.Message );
     206             :     }
     207           0 :     return ::com::sun::star::i18n::ForbiddenCharacters();
     208             : }
     209             : 
     210         727 : ::com::sun::star::uno::Sequence< OUString > LocaleDataWrapper::getReservedWord() const
     211             : {
     212             :     try
     213             :     {
     214         727 :         return xLD->getReservedWord( getMyLocale() );
     215             :     }
     216           0 :     catch ( const Exception& e )
     217             :     {
     218             :         SAL_WARN( "unotools.i18n", "getReservedWord: Exception caught " << e.Message );
     219             :     }
     220           0 :     return ::com::sun::star::uno::Sequence< OUString >(0);
     221             : }
     222             : 
     223          26 : ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > LocaleDataWrapper::getAllInstalledLocaleNames() const
     224             : {
     225          26 :     uno::Sequence< lang::Locale > &rInstalledLocales = InstalledLocales::get();
     226             : 
     227          26 :     if ( rInstalledLocales.getLength() )
     228           0 :         return rInstalledLocales;
     229             : 
     230             :     try
     231             :     {
     232          26 :         rInstalledLocales = xLD->getAllInstalledLocaleNames();
     233             :     }
     234           0 :     catch ( const Exception& e )
     235             :     {
     236             :         SAL_WARN( "unotools.i18n", "getAllInstalledLocaleNames: Exception caught " << e.Message );
     237             :     }
     238          26 :     return rInstalledLocales;
     239             : }
     240             : 
     241             : // --- Impl and helpers ----------------------------------------------------
     242             : 
     243             : // static
     244          26 : ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > LocaleDataWrapper::getInstalledLocaleNames()
     245             : {
     246             :     const uno::Sequence< lang::Locale > &rInstalledLocales =
     247          26 :         InstalledLocales::get();
     248             : 
     249          26 :     if ( !rInstalledLocales.getLength() )
     250             :     {
     251          26 :         LocaleDataWrapper aLDW( ::comphelper::getProcessComponentContext(), LanguageTag( LANGUAGE_SYSTEM) );
     252          26 :         aLDW.getAllInstalledLocaleNames();
     253             :     }
     254          26 :     return rInstalledLocales;
     255             : }
     256             : 
     257             : // static
     258           0 : ::com::sun::star::uno::Sequence< sal_uInt16 > LocaleDataWrapper::getInstalledLanguageTypes()
     259             : {
     260             :     uno::Sequence< sal_uInt16 > &rInstalledLanguageTypes =
     261           0 :         InstalledLanguageTypes::get();
     262             : 
     263           0 :     if ( rInstalledLanguageTypes.getLength() )
     264           0 :         return rInstalledLanguageTypes;
     265             : 
     266             :     ::com::sun::star::uno::Sequence< ::com::sun::star::lang::Locale > xLoc =
     267           0 :         getInstalledLocaleNames();
     268           0 :     sal_Int32 nCount = xLoc.getLength();
     269           0 :     ::com::sun::star::uno::Sequence< sal_uInt16 > xLang( nCount );
     270           0 :     sal_Int32 nLanguages = 0;
     271           0 :     for ( sal_Int32 i=0; i<nCount; i++ )
     272             :     {
     273           0 :         LanguageTag aLanguageTag( xLoc[i] );
     274           0 :         OUString aDebugLocale;
     275           0 :         if (areChecksEnabled())
     276             :         {
     277           0 :             aDebugLocale = aLanguageTag.getBcp47( false);
     278             :         }
     279             : 
     280           0 :         LanguageType eLang = aLanguageTag.getLanguageType( false);
     281           0 :         if (areChecksEnabled() && eLang == LANGUAGE_DONTKNOW)
     282             :         {
     283           0 :             OUStringBuffer aMsg("ConvertIsoNamesToLanguage: unknown MS-LCID for locale\n");
     284           0 :             aMsg.append(aDebugLocale);
     285           0 :             outputCheckMessage(aMsg.makeStringAndClear());
     286             :         }
     287             : 
     288           0 :         switch ( eLang )
     289             :         {
     290             :             case LANGUAGE_NORWEGIAN :       // no_NO, not Bokmal (nb_NO), not Nynorsk (nn_NO)
     291           0 :                 eLang = LANGUAGE_DONTKNOW;  // don't offer "Unknown" language
     292           0 :                 break;
     293             :         }
     294           0 :         if ( eLang != LANGUAGE_DONTKNOW )
     295             :         {
     296           0 :             LanguageTag aBackLanguageTag( eLang);
     297           0 :             if ( aLanguageTag != aBackLanguageTag )
     298             :             {
     299             :                 // In checks, exclude known problems because no MS-LCID defined
     300             :                 // and default for Language found.
     301           0 :                 if ( areChecksEnabled()
     302           0 :                         && aDebugLocale != "ar-SD"  // Sudan/ar
     303           0 :                         && aDebugLocale != "en-CB"  // Carribean is not a country
     304             : //                      && aDebugLocale != "en-BG"  // ?!? Bulgaria/en
     305             : //                      && aDebugLocale != "es-BR"  // ?!? Brazil/es
     306             :                     )
     307             :                 {
     308           0 :                     OUStringBuffer aMsg("ConvertIsoNamesToLanguage/ConvertLanguageToIsoNames: ambiguous locale (MS-LCID?)\n");
     309           0 :                     aMsg.append(aDebugLocale);
     310           0 :                     aMsg.appendAscii("  ->  0x");
     311           0 :                     aMsg.append(static_cast<sal_Int32>(eLang), 16);
     312           0 :                     aMsg.appendAscii("  ->  ");
     313           0 :                     aMsg.append(aBackLanguageTag.getBcp47());
     314           0 :                     outputCheckMessage( aMsg.makeStringAndClear() );
     315             :                 }
     316           0 :                 eLang = LANGUAGE_DONTKNOW;
     317           0 :             }
     318             :         }
     319           0 :         if ( eLang != LANGUAGE_DONTKNOW )
     320           0 :             xLang[ nLanguages++ ] = eLang;
     321           0 :     }
     322           0 :     if ( nLanguages < nCount )
     323           0 :         xLang.realloc( nLanguages );
     324           0 :     rInstalledLanguageTypes = xLang;
     325             : 
     326           0 :     return rInstalledLanguageTypes;
     327             : }
     328             : 
     329     2662227 : const OUString& LocaleDataWrapper::getOneLocaleItem( sal_Int16 nItem ) const
     330             : {
     331     2662227 :     ::utl::ReadWriteGuard aGuard( aMutex );
     332     2662227 :     if ( nItem >= LocaleItem::COUNT )
     333             :     {
     334             :         SAL_WARN( "unotools.i18n", "getOneLocaleItem: bounds" );
     335           0 :         return aLocaleItem[0];
     336             :     }
     337     2662227 :     if (aLocaleItem[nItem].isEmpty())
     338             :     {   // no cached content
     339        6383 :         aGuard.changeReadToWrite();
     340        6383 :         ((LocaleDataWrapper*)this)->getOneLocaleItemImpl( nItem );
     341             :     }
     342     2662227 :     return aLocaleItem[nItem];
     343             : }
     344             : 
     345        6383 : void LocaleDataWrapper::getOneLocaleItemImpl( sal_Int16 nItem )
     346             : {
     347        6383 :     if ( !bLocaleDataItemValid )
     348             :     {
     349        2437 :         aLocaleDataItem = getLocaleItem();
     350        2437 :         bLocaleDataItemValid = true;
     351             :     }
     352        6383 :     switch ( nItem )
     353             :     {
     354             :         case LocaleItem::DATE_SEPARATOR :
     355         756 :             aLocaleItem[nItem] = aLocaleDataItem.dateSeparator;
     356         756 :         break;
     357             :         case LocaleItem::THOUSAND_SEPARATOR :
     358        2284 :             aLocaleItem[nItem] = aLocaleDataItem.thousandSeparator;
     359        2284 :         break;
     360             :         case LocaleItem::DECIMAL_SEPARATOR :
     361        1721 :             aLocaleItem[nItem] = aLocaleDataItem.decimalSeparator;
     362        1721 :         break;
     363             :         case LocaleItem::TIME_SEPARATOR :
     364         726 :             aLocaleItem[nItem] = aLocaleDataItem.timeSeparator;
     365         726 :         break;
     366             :         case LocaleItem::TIME_100SEC_SEPARATOR :
     367         711 :             aLocaleItem[nItem] = aLocaleDataItem.time100SecSeparator;
     368         711 :         break;
     369             :         case LocaleItem::LIST_SEPARATOR :
     370          31 :             aLocaleItem[nItem] = aLocaleDataItem.listSeparator;
     371          31 :         break;
     372             :         case LocaleItem::SINGLE_QUOTATION_START :
     373           0 :             aLocaleItem[nItem] = aLocaleDataItem.quotationStart;
     374           0 :         break;
     375             :         case LocaleItem::SINGLE_QUOTATION_END :
     376          19 :             aLocaleItem[nItem] = aLocaleDataItem.quotationEnd;
     377          19 :         break;
     378             :         case LocaleItem::DOUBLE_QUOTATION_START :
     379           0 :             aLocaleItem[nItem] = aLocaleDataItem.doubleQuotationStart;
     380           0 :         break;
     381             :         case LocaleItem::DOUBLE_QUOTATION_END :
     382           0 :             aLocaleItem[nItem] = aLocaleDataItem.doubleQuotationEnd;
     383           0 :         break;
     384             :         case LocaleItem::MEASUREMENT_SYSTEM :
     385          74 :             aLocaleItem[nItem] = aLocaleDataItem.measurementSystem;
     386          74 :         break;
     387             :         case LocaleItem::TIME_AM :
     388          20 :             aLocaleItem[nItem] = aLocaleDataItem.timeAM;
     389          20 :         break;
     390             :         case LocaleItem::TIME_PM :
     391          20 :             aLocaleItem[nItem] = aLocaleDataItem.timePM;
     392          20 :         break;
     393             :         case LocaleItem::LONG_DATE_DAY_OF_WEEK_SEPARATOR :
     394           7 :             aLocaleItem[nItem] = aLocaleDataItem.LongDateDayOfWeekSeparator;
     395           7 :         break;
     396             :         case LocaleItem::LONG_DATE_DAY_SEPARATOR :
     397           7 :             aLocaleItem[nItem] = aLocaleDataItem.LongDateDaySeparator;
     398           7 :         break;
     399             :         case LocaleItem::LONG_DATE_MONTH_SEPARATOR :
     400           7 :             aLocaleItem[nItem] = aLocaleDataItem.LongDateMonthSeparator;
     401           7 :         break;
     402             :         case LocaleItem::LONG_DATE_YEAR_SEPARATOR :
     403           0 :             aLocaleItem[nItem] = aLocaleDataItem.LongDateYearSeparator;
     404           0 :         break;
     405             :         default:
     406             :             SAL_WARN( "unotools.i18n", "getOneLocaleItemImpl: which one?" );
     407             :     }
     408        6383 : }
     409             : 
     410        1457 : void LocaleDataWrapper::getOneReservedWordImpl( sal_Int16 nWord )
     411             : {
     412        1457 :     if ( !bReservedWordValid )
     413             :     {
     414         727 :         aReservedWordSeq = getReservedWord();
     415         727 :         bReservedWordValid = true;
     416             :     }
     417             :     DBG_ASSERT( nWord < aReservedWordSeq.getLength(), "getOneReservedWordImpl: which one?" );
     418        1457 :     if ( nWord < aReservedWordSeq.getLength() )
     419        1457 :         aReservedWord[nWord] = aReservedWordSeq[nWord];
     420        1457 : }
     421             : 
     422       29390 : const OUString& LocaleDataWrapper::getOneReservedWord( sal_Int16 nWord ) const
     423             : {
     424       29390 :     ::utl::ReadWriteGuard aGuard( aMutex );
     425       29390 :     if ( nWord < 0 || nWord >= reservedWords::COUNT )
     426             :     {
     427             :         SAL_WARN( "unotools.i18n", "getOneReservedWord: bounds" );
     428           0 :         nWord = reservedWords::FALSE_WORD;
     429             :     }
     430       29390 :     if (aReservedWord[nWord].isEmpty())
     431             :     {   // no cached content
     432        1457 :         aGuard.changeReadToWrite();
     433        1457 :         ((LocaleDataWrapper*)this)->getOneReservedWordImpl( nWord );
     434             :     }
     435       29390 :     return aReservedWord[nWord];
     436             : }
     437             : 
     438        9418 : MeasurementSystem LocaleDataWrapper::mapMeasurementStringToEnum( const OUString& rMS ) const
     439             : {
     440             : //! TODO: could be cached too
     441        9418 :     if ( rMS.equalsIgnoreAsciiCase( "metric" ) )
     442           0 :         return MEASURE_METRIC;
     443             : //! TODO: other measurement systems? => extend enum MeasurementSystem
     444        9418 :     return MEASURE_US;
     445             : }
     446             : 
     447           0 : void LocaleDataWrapper::getDefaultCalendarImpl()
     448             : {
     449           0 :     if (!xDefaultCalendar)
     450             :     {
     451           0 :         Sequence< Calendar2 > xCals = getAllCalendars();
     452           0 :         sal_Int32 nCount = xCals.getLength();
     453           0 :         sal_Int32 nDef = 0;
     454           0 :         if (nCount > 1)
     455             :         {
     456           0 :             const Calendar2* pArr = xCals.getArray();
     457           0 :             for (sal_Int32 i=0; i<nCount; ++i)
     458             :             {
     459           0 :                 if (pArr[i].Default)
     460             :                 {
     461           0 :                     nDef = i;
     462           0 :                     break;
     463             :                 }
     464             :             }
     465             :         }
     466           0 :         xDefaultCalendar.reset( new Calendar2( xCals[nDef]));
     467             :     }
     468           0 : }
     469             : 
     470           0 : const ::boost::shared_ptr< ::com::sun::star::i18n::Calendar2 > LocaleDataWrapper::getDefaultCalendar() const
     471             : {
     472           0 :     ::utl::ReadWriteGuard aGuard( aMutex );
     473           0 :     if (!xDefaultCalendar)
     474             :     {   // no cached content
     475           0 :         aGuard.changeReadToWrite();
     476           0 :         ((LocaleDataWrapper*)this)->getDefaultCalendarImpl();
     477             :     }
     478           0 :     return xDefaultCalendar;
     479             : }
     480             : 
     481           0 : const ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > LocaleDataWrapper::getDefaultCalendarDays() const
     482             : {
     483           0 :     return getDefaultCalendar()->Days;
     484             : }
     485             : 
     486           0 : const ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > LocaleDataWrapper::getDefaultCalendarMonths() const
     487             : {
     488           0 :     return getDefaultCalendar()->Months;
     489             : }
     490             : 
     491             : // --- currencies -----------------------------------------------------
     492             : 
     493        6157 : const OUString& LocaleDataWrapper::getCurrSymbol() const
     494             : {
     495        6157 :     ::utl::ReadWriteGuard aGuard( aMutex );
     496        6157 :     if (aCurrSymbol.isEmpty())
     497             :     {
     498        6079 :         aGuard.changeReadToWrite();
     499        6079 :         ((LocaleDataWrapper*)this)->getCurrSymbolsImpl();
     500             :     }
     501        6157 :     return aCurrSymbol;
     502             : }
     503             : 
     504          41 : const OUString& LocaleDataWrapper::getCurrBankSymbol() const
     505             : {
     506          41 :     ::utl::ReadWriteGuard aGuard( aMutex );
     507          41 :     if (aCurrBankSymbol.isEmpty())
     508             :     {
     509          15 :         aGuard.changeReadToWrite();
     510          15 :         ((LocaleDataWrapper*)this)->getCurrSymbolsImpl();
     511             :     }
     512          41 :     return aCurrBankSymbol;
     513             : }
     514             : 
     515        8948 : sal_uInt16 LocaleDataWrapper::getCurrPositiveFormat() const
     516             : {
     517        8948 :     ::utl::ReadWriteGuard aGuard( aMutex );
     518        8948 :     if ( nCurrPositiveFormat == nCurrFormatInvalid )
     519             :     {
     520        6072 :         aGuard.changeReadToWrite();
     521        6072 :         ((LocaleDataWrapper*)this)->getCurrFormatsImpl();
     522             :     }
     523        8948 :     return nCurrPositiveFormat;
     524             : }
     525             : 
     526        8938 : sal_uInt16 LocaleDataWrapper::getCurrNegativeFormat() const
     527             : {
     528        8938 :     ::utl::ReadWriteGuard aGuard( aMutex );
     529        8938 :     if ( nCurrNegativeFormat == nCurrFormatInvalid )
     530             :     {
     531           0 :         aGuard.changeReadToWrite();
     532           0 :         ((LocaleDataWrapper*)this)->getCurrFormatsImpl();
     533             :     }
     534        8938 :     return nCurrNegativeFormat;
     535             : }
     536             : 
     537          26 : sal_uInt16 LocaleDataWrapper::getCurrDigits() const
     538             : {
     539          26 :     ::utl::ReadWriteGuard aGuard( aMutex );
     540          26 :     if ( nCurrDigits == nCurrFormatInvalid )
     541             :     {
     542           0 :         aGuard.changeReadToWrite();
     543           0 :         ((LocaleDataWrapper*)this)->getCurrSymbolsImpl();
     544             :     }
     545          26 :     return nCurrDigits;
     546             : }
     547             : 
     548        6094 : void LocaleDataWrapper::getCurrSymbolsImpl()
     549             : {
     550        6094 :     Sequence< Currency2 > aCurrSeq = getAllCurrencies();
     551        6094 :     sal_Int32 nCnt = aCurrSeq.getLength();
     552        6094 :     Currency2 const * const pCurrArr = aCurrSeq.getArray();
     553             :     sal_Int32 nElem;
     554        6380 :     for ( nElem = 0; nElem < nCnt; nElem++ )
     555             :     {
     556        6380 :         if ( pCurrArr[nElem].Default )
     557        6094 :             break;
     558             :     }
     559        6094 :     if ( nElem >= nCnt )
     560             :     {
     561           0 :         if (areChecksEnabled())
     562             :         {
     563           0 :             OUString aMsg( "LocaleDataWrapper::getCurrSymbolsImpl: no default currency" );
     564           0 :             outputCheckMessage( appendLocaleInfo( aMsg ) );
     565             :         }
     566           0 :         nElem = 0;
     567           0 :         if ( nElem >= nCnt )
     568             :         {
     569           0 :             if (areChecksEnabled())
     570           0 :                 outputCheckMessage(OUString("LocaleDataWrapper::getCurrSymbolsImpl: no currency at all, using ShellsAndPebbles"));
     571           0 :             aCurrSymbol = "ShellsAndPebbles";
     572           0 :             aCurrBankSymbol = aCurrSymbol;
     573           0 :             nCurrPositiveFormat = nCurrNegativeFormat = nCurrFormatDefault;
     574           0 :             nCurrDigits = 2;
     575        6094 :             return;
     576             :         }
     577             :     }
     578        6094 :     aCurrSymbol = pCurrArr[nElem].Symbol;
     579        6094 :     aCurrBankSymbol = pCurrArr[nElem].BankSymbol;
     580        6094 :     nCurrDigits = pCurrArr[nElem].DecimalPlaces;
     581             : }
     582             : 
     583       12144 : void LocaleDataWrapper::scanCurrFormatImpl( const OUString& rCode,
     584             :         sal_Int32 nStart, sal_Int32& nSign, sal_Int32& nPar,
     585             :         sal_Int32& nNum, sal_Int32& nBlank, sal_Int32& nSym )
     586             : {
     587       12144 :     nSign = nPar = nNum = nBlank = nSym = -1;
     588       12144 :     const sal_Unicode* const pStr = rCode.getStr();
     589       12144 :     const sal_Unicode* const pStop = pStr + rCode.getLength();
     590       12144 :     const sal_Unicode* p = pStr + nStart;
     591       12144 :     int nInSection = 0;
     592       12144 :     bool bQuote = false;
     593      279790 :     while ( p < pStop )
     594             :     {
     595      255502 :         if ( bQuote )
     596             :         {
     597           0 :             if ( *p == '"' && *(p-1) != '\\' )
     598           0 :                 bQuote = false;
     599             :         }
     600             :         else
     601             :         {
     602      255502 :             switch ( *p )
     603             :             {
     604             :                 case '"' :
     605           0 :                     if ( pStr == p || *(p-1) != '\\' )
     606           0 :                         bQuote = true;
     607           0 :                 break;
     608             :                 case '-' :
     609       17826 :                     if (!nInSection && nSign == -1)
     610        5682 :                         nSign = p - pStr;
     611       17826 :                 break;
     612             :                 case '(' :
     613         390 :                     if (!nInSection && nPar == -1)
     614         390 :                         nPar = p - pStr;
     615         390 :                 break;
     616             :                 case '0' :
     617             :                 case '#' :
     618       76064 :                     if (!nInSection && nNum == -1)
     619       12144 :                         nNum = p - pStr;
     620       76064 :                 break;
     621             :                 case '[' :
     622       18216 :                     nInSection++;
     623       18216 :                 break;
     624             :                 case ']' :
     625       18216 :                     if ( nInSection )
     626             :                     {
     627       18216 :                         nInSection--;
     628       18216 :                         if (!nInSection && nBlank == -1
     629       15278 :                           && nSym != -1 && p < pStop-1 && *(p+1) == ' ' )
     630        2938 :                             nBlank = p - pStr + 1;
     631             :                     }
     632       18216 :                 break;
     633             :                 case '$' :
     634       13940 :                     if (nSym == -1 && nInSection && *(p-1) == '[')
     635             :                     {
     636       12144 :                         nSym = p - pStr + 1;
     637       12144 :                         if (nNum != -1 && *(p-2) == ' ')
     638        2938 :                             nBlank = p - pStr - 2;
     639             :                     }
     640       13940 :                 break;
     641             :                 case ';' :
     642        6072 :                     if ( !nInSection )
     643        6072 :                         p = pStop;
     644        6072 :                 break;
     645             :                 default:
     646      104778 :                     if (!nInSection && nSym == -1 && rCode.match(aCurrSymbol, (sal_Int32)(p - pStr)))
     647             :                     {   // currency symbol not surrounded by [$...]
     648           0 :                         nSym = p - pStr;
     649           0 :                         if (nBlank == -1 && pStr < p && *(p-1) == ' ')
     650           0 :                             nBlank = p - pStr - 1;
     651           0 :                         p += aCurrSymbol.getLength() - 1;
     652           0 :                         if (nBlank == -1 && p < pStop-2 && *(p+2) == ' ')
     653           0 :                             nBlank = p - pStr + 2;
     654             :                     }
     655             :             }
     656             :         }
     657      255502 :         p++;
     658             :     }
     659       12144 : }
     660             : 
     661        6072 : void LocaleDataWrapper::getCurrFormatsImpl()
     662             : {
     663        6072 :     NumberFormatCodeWrapper aNumberFormatCode( m_xContext, getMyLocale() );
     664             :     uno::Sequence< NumberFormatCode > aFormatSeq
     665       12144 :         = aNumberFormatCode.getAllFormatCode( KNumberFormatUsage::CURRENCY );
     666        6072 :     sal_Int32 nCnt = aFormatSeq.getLength();
     667        6072 :     if ( !nCnt )
     668             :     {   // bad luck
     669           0 :         if (areChecksEnabled())
     670             :         {
     671           0 :             OUString aMsg( "LocaleDataWrapper::getCurrFormatsImpl: no currency formats" );
     672           0 :             outputCheckMessage( appendLocaleInfo( aMsg ) );
     673             :         }
     674           0 :         nCurrPositiveFormat = nCurrNegativeFormat = nCurrFormatDefault;
     675        6072 :         return;
     676             :     }
     677             :     // find a negative code (medium preferred) and a default (medium preferred) (not necessarily the same)
     678        6072 :     NumberFormatCode const * const pFormatArr = aFormatSeq.getArray();
     679             :     sal_Int32 nElem, nDef, nNeg, nMedium;
     680        6072 :     nDef = nNeg = nMedium = -1;
     681       43884 :     for ( nElem = 0; nElem < nCnt; nElem++ )
     682             :     {
     683       37812 :         if ( pFormatArr[nElem].Type == KNumberFormatType::MEDIUM )
     684             :         {
     685       31206 :             if ( pFormatArr[nElem].Default )
     686             :             {
     687        6072 :                 nDef = nElem;
     688        6072 :                 nMedium = nElem;
     689        6072 :                 if ( pFormatArr[nElem].Code.indexOf( ';' ) >= 0 )
     690        6072 :                     nNeg = nElem;
     691             :             }
     692             :             else
     693             :             {
     694       25134 :                 if ( (nNeg == -1 || nMedium == -1) && pFormatArr[nElem].Code.indexOf( ';' ) >= 0 )
     695        5916 :                     nNeg = nElem;
     696       25134 :                 if ( nMedium == -1 )
     697        5916 :                     nMedium = nElem;
     698             :             }
     699             :         }
     700             :         else
     701             :         {
     702        6606 :             if ( nDef == -1 && pFormatArr[nElem].Default )
     703        5864 :                 nDef = nElem;
     704        6606 :             if ( nNeg == -1 && pFormatArr[nElem].Code.indexOf( ';' ) >= 0 )
     705        5864 :                 nNeg = nElem;
     706             :         }
     707             :     }
     708             : 
     709             :     // make sure it's loaded
     710        6072 :     getCurrSymbol();
     711             : 
     712             :     sal_Int32 nSign, nPar, nNum, nBlank, nSym;
     713             : 
     714             :     // positive format
     715        6072 :     nElem = (nDef >= 0 ? nDef : (nNeg >= 0 ? nNeg : 0));
     716        6072 :     scanCurrFormatImpl( pFormatArr[nElem].Code, 0, nSign, nPar, nNum, nBlank, nSym );
     717        6072 :     if (areChecksEnabled() && (nNum == -1 || nSym == -1))
     718             :     {
     719           0 :         OUString aMsg( "LocaleDataWrapper::getCurrFormatsImpl: CurrPositiveFormat?" );
     720           0 :         outputCheckMessage( appendLocaleInfo( aMsg ) );
     721             :     }
     722        6072 :     if (nBlank == -1)
     723             :     {
     724        2666 :         if ( nSym < nNum )
     725        2250 :             nCurrPositiveFormat = 0;    // $1
     726             :         else
     727         416 :             nCurrPositiveFormat = 1;    // 1$
     728             :     }
     729             :     else
     730             :     {
     731        3406 :         if ( nSym < nNum )
     732        1950 :             nCurrPositiveFormat = 2;    // $ 1
     733             :         else
     734        1456 :             nCurrPositiveFormat = 3;    // 1 $
     735             :     }
     736             : 
     737             :     // negative format
     738        6072 :     if ( nNeg < 0 )
     739           0 :         nCurrNegativeFormat = nCurrFormatDefault;
     740             :     else
     741             :     {
     742        6072 :         const OUString& rCode = pFormatArr[nNeg].Code;
     743        6072 :         sal_Int32 nDelim = rCode.indexOf(';');
     744        6072 :         scanCurrFormatImpl( rCode, nDelim+1, nSign, nPar, nNum, nBlank, nSym );
     745        6072 :         if (areChecksEnabled() && (nNum == -1 || nSym == -1 || (nPar == -1 && nSign == -1)))
     746             :         {
     747           0 :             OUString aMsg( "LocaleDataWrapper::getCurrFormatsImpl: CurrNegativeFormat?" );
     748           0 :             outputCheckMessage( appendLocaleInfo( aMsg ) );
     749             :         }
     750             :         // NOTE: one of nPar or nSign are allowed to be -1
     751        6072 :         if (nBlank == -1)
     752             :         {
     753        3602 :             if ( nSym < nNum )
     754             :             {
     755        3186 :                 if ( -1 < nPar && nPar < nSym )
     756         338 :                     nCurrNegativeFormat = 0;    // ($1)
     757        2848 :                 else if ( -1 < nSign && nSign < nSym )
     758        1756 :                     nCurrNegativeFormat = 1;    // -$1
     759        1092 :                 else if ( nNum < nSign )
     760           0 :                     nCurrNegativeFormat = 3;    // $1-
     761             :                 else
     762        1092 :                     nCurrNegativeFormat = 2;    // $-1
     763             :             }
     764             :             else
     765             :             {
     766         416 :                 if ( -1 < nPar && nPar < nNum )
     767           0 :                     nCurrNegativeFormat = 4;    // (1$)
     768         416 :                 else if ( -1 < nSign && nSign < nNum )
     769         416 :                     nCurrNegativeFormat = 5;    // -1$
     770           0 :                 else if ( nSym < nSign )
     771           0 :                     nCurrNegativeFormat = 7;    // 1$-
     772             :                 else
     773           0 :                     nCurrNegativeFormat = 6;    // 1-$
     774             :             }
     775             :         }
     776             :         else
     777             :         {
     778        2470 :             if ( nSym < nNum )
     779             :             {
     780         988 :                 if ( -1 < nPar && nPar < nSym )
     781          52 :                     nCurrNegativeFormat = 14;   // ($ 1)
     782         936 :                 else if ( -1 < nSign && nSign < nSym )
     783         442 :                     nCurrNegativeFormat = 9;    // -$ 1
     784         494 :                 else if ( nNum < nSign )
     785         234 :                     nCurrNegativeFormat = 12;   // $ 1-
     786             :                 else
     787         260 :                     nCurrNegativeFormat = 11;   // $ -1
     788             :             }
     789             :             else
     790             :             {
     791        1482 :                 if ( -1 < nPar && nPar < nNum )
     792           0 :                     nCurrNegativeFormat = 15;   // (1 $)
     793        1482 :                 else if ( -1 < nSign && nSign < nNum )
     794        1482 :                     nCurrNegativeFormat = 8;    // -1 $
     795           0 :                 else if ( nSym < nSign )
     796           0 :                     nCurrNegativeFormat = 10;   // 1 $-
     797             :                 else
     798           0 :                     nCurrNegativeFormat = 13;   // 1- $
     799             :             }
     800             :         }
     801        6072 :     }
     802             : }
     803             : 
     804             : // --- date -----------------------------------------------------------
     805             : 
     806         825 : DateFormat LocaleDataWrapper::getDateFormat() const
     807             : {
     808         825 :     ::utl::ReadWriteGuard aGuard( aMutex );
     809         825 :     if ( nDateFormat == nDateFormatInvalid )
     810             :     {
     811          45 :         aGuard.changeReadToWrite();
     812          45 :         ((LocaleDataWrapper*)this)->getDateFormatsImpl();
     813             :     }
     814         825 :     return (DateFormat) nDateFormat;
     815             : }
     816             : 
     817         119 : DateFormat LocaleDataWrapper::getLongDateFormat() const
     818             : {
     819         119 :     ::utl::ReadWriteGuard aGuard( aMutex );
     820         119 :     if ( nLongDateFormat == nDateFormatInvalid )
     821             :     {
     822           2 :         aGuard.changeReadToWrite();
     823           2 :         ((LocaleDataWrapper*)this)->getDateFormatsImpl();
     824             :     }
     825         119 :     return (DateFormat) nLongDateFormat;
     826             : }
     827             : 
     828          94 : DateFormat LocaleDataWrapper::scanDateFormatImpl( const OUString& rCode )
     829             : {
     830             :     // Only some european versions were translated, the ones with different
     831             :     // keyword combinations are:
     832             :     // English DMY, German TMJ, Spanish DMA, French JMA, Italian GMA,
     833             :     // Dutch DMJ, Finnish PKV
     834             : 
     835             :     // default is English keywords for every other language
     836          94 :     sal_Int32 nDay = rCode.indexOf('D');
     837          94 :     sal_Int32 nMonth = rCode.indexOf('M');
     838          94 :     sal_Int32 nYear = rCode.indexOf('Y');
     839          94 :     if (nDay == -1 || nMonth == -1 || nYear == -1)
     840             :     {   // This algorithm assumes that all three parts (DMY) are present
     841           0 :         if (nMonth == -1)
     842             :         {   // only Finnish has something else than 'M' for month
     843           0 :             nMonth = rCode.indexOf('K');
     844           0 :             if (nMonth != -1)
     845             :             {
     846           0 :                 nDay = rCode.indexOf('P');
     847           0 :                 nYear = rCode.indexOf('V');
     848             :             }
     849             :         }
     850           0 :         else if (nDay == -1)
     851             :         {   // We have a month 'M' if we reach this branch.
     852             :             // Possible languages containing 'M' but no 'D':
     853             :             // German, French, Italian
     854           0 :             nDay = rCode.indexOf('T');         // German
     855           0 :             if (nDay != -1)
     856           0 :                 nYear = rCode.indexOf('J');
     857             :             else
     858             :             {
     859           0 :                 nYear = rCode.indexOf('A');    // French, Italian
     860           0 :                 if (nYear != -1)
     861             :                 {
     862           0 :                     nDay = rCode.indexOf('J'); // French
     863           0 :                     if (nDay == -1)
     864           0 :                         nDay = rCode.indexOf('G'); // Italian
     865             :                 }
     866             :             }
     867             :         }
     868             :         else
     869             :         {   // We have a month 'M' and a day 'D'.
     870             :             // Possible languages containing 'D' and 'M' but not 'Y':
     871             :             // Spanish, Dutch
     872           0 :             nYear = rCode.indexOf('A');        // Spanish
     873           0 :             if (nYear == -1)
     874           0 :                 nYear = rCode.indexOf('J');    // Dutch
     875             :         }
     876           0 :         if (nDay == -1 || nMonth == -1 || nYear == -1)
     877             :         {
     878           0 :             if (areChecksEnabled())
     879             :             {
     880           0 :                 OUString aMsg( "LocaleDataWrapper::scanDateFormat: not all DMY present" );
     881           0 :                 outputCheckMessage( appendLocaleInfo( aMsg ) );
     882             :             }
     883           0 :             if (nDay == -1)
     884           0 :                 nDay = rCode.getLength();
     885           0 :             if (nMonth == -1)
     886           0 :                 nMonth = rCode.getLength();
     887           0 :             if (nYear == -1)
     888           0 :                 nYear = rCode.getLength();
     889             :         }
     890             :     }
     891             :     // compare with <= because each position may equal rCode.getLength()
     892          94 :     if ( nDay <= nMonth && nMonth <= nYear )
     893           2 :         return DMY;     // also if every position equals rCode.getLength()
     894          92 :     else if ( nMonth <= nDay && nDay <= nYear )
     895          92 :         return MDY;
     896           0 :     else if ( nYear <= nMonth && nMonth <= nDay )
     897           0 :         return YMD;
     898             :     else
     899             :     {
     900           0 :         if (areChecksEnabled())
     901             :         {
     902           0 :             OUString aMsg( "LocaleDataWrapper::scanDateFormat: no magic applyable" );
     903           0 :             outputCheckMessage( appendLocaleInfo( aMsg ) );
     904             :         }
     905           0 :         return DMY;
     906             :     }
     907             : }
     908             : 
     909          47 : void LocaleDataWrapper::getDateFormatsImpl()
     910             : {
     911          47 :     NumberFormatCodeWrapper aNumberFormatCode( m_xContext, getMyLocale() );
     912             :     uno::Sequence< NumberFormatCode > aFormatSeq
     913          94 :         = aNumberFormatCode.getAllFormatCode( KNumberFormatUsage::DATE );
     914          47 :     sal_Int32 nCnt = aFormatSeq.getLength();
     915          47 :     if ( !nCnt )
     916             :     {   // bad luck
     917           0 :         if (areChecksEnabled())
     918             :         {
     919           0 :             OUString aMsg( "LocaleDataWrapper::getDateFormatsImpl: no date formats" );
     920           0 :             outputCheckMessage( appendLocaleInfo( aMsg ) );
     921             :         }
     922           0 :         nDateFormat = nLongDateFormat = DMY;
     923          47 :         return;
     924             :     }
     925             :     // find the edit (21), a default (medium preferred),
     926             :     // a medium (default preferred), and a long (default preferred)
     927          47 :     NumberFormatCode const * const pFormatArr = aFormatSeq.getArray();
     928             :     sal_Int32 nElem, nEdit, nDef, nMedium, nLong;
     929          47 :     nEdit = nDef = nMedium = nLong = -1;
     930        1494 :     for ( nElem = 0; nElem < nCnt; nElem++ )
     931             :     {
     932        1447 :         if ( nEdit == -1 && pFormatArr[nElem].Index == NumberFormatIndex::DATE_SYS_DDMMYYYY )
     933          47 :             nEdit = nElem;
     934        1447 :         if ( nDef == -1 && pFormatArr[nElem].Default )
     935          47 :             nDef = nElem;
     936        1447 :         switch ( pFormatArr[nElem].Type )
     937             :         {
     938             :             case KNumberFormatType::MEDIUM :
     939             :             {
     940         700 :                 if ( pFormatArr[nElem].Default )
     941             :                 {
     942          47 :                     nDef = nElem;
     943          47 :                     nMedium = nElem;
     944             :                 }
     945         653 :                 else if ( nMedium == -1 )
     946           1 :                     nMedium = nElem;
     947             :             }
     948         700 :             break;
     949             :             case KNumberFormatType::LONG :
     950             :             {
     951         653 :                 if ( pFormatArr[nElem].Default )
     952          47 :                     nLong = nElem;
     953         606 :                 else if ( nLong == -1 )
     954           0 :                     nLong = nElem;
     955             :             }
     956         653 :             break;
     957             :         }
     958             :     }
     959          47 :     if ( nEdit == -1 )
     960             :     {
     961           0 :         if (areChecksEnabled())
     962             :         {
     963           0 :             OUString aMsg( "LocaleDataWrapper::getDateFormatsImpl: no edit" );
     964           0 :             outputCheckMessage( appendLocaleInfo( aMsg ) );
     965             :         }
     966           0 :         if ( nDef == -1 )
     967             :         {
     968           0 :             if (areChecksEnabled())
     969             :             {
     970           0 :                 OUString aMsg( "LocaleDataWrapper::getDateFormatsImpl: no default" );
     971           0 :                 outputCheckMessage( appendLocaleInfo( aMsg ) );
     972             :             }
     973           0 :             if ( nMedium != -1 )
     974           0 :                 nDef = nMedium;
     975           0 :             else if ( nLong != -1 )
     976           0 :                 nDef = nLong;
     977             :             else
     978           0 :                 nDef = 0;
     979             :         }
     980           0 :         nEdit = nDef;
     981             :     }
     982          47 :     DateFormat nDF = scanDateFormatImpl( pFormatArr[nEdit].Code );
     983          47 :     if ( pFormatArr[nEdit].Type == KNumberFormatType::LONG )
     984             :     {   // normally this is not the case
     985           0 :         nLongDateFormat = nDateFormat = nDF;
     986             :     }
     987             :     else
     988             :     {
     989          47 :         nDateFormat = nDF;
     990          47 :         if ( nLong == -1 )
     991           0 :             nLongDateFormat = nDF;
     992             :         else
     993          47 :             nLongDateFormat = scanDateFormatImpl( pFormatArr[nLong].Code );
     994          47 :     }
     995             : }
     996             : 
     997             : // --- digit grouping -------------------------------------------------
     998             : 
     999        1329 : void LocaleDataWrapper::getDigitGroupingImpl()
    1000             : {
    1001             :     /* TODO: This is a very simplified grouping setup that only serves its
    1002             :      * current purpose for Indian locales. A free-form flexible one would
    1003             :      * obtain grouping from locale data where it could be specified using, for
    1004             :      * example, codes like #,### and #,##,### that would generate the integer
    1005             :      * sequence. Needed additional API and a locale data element.
    1006             :      */
    1007             : 
    1008        1329 :     if (!aGrouping.getLength())
    1009             :     {
    1010        1317 :         aGrouping.realloc(3);   // room for {3,2,0}
    1011        1317 :         aGrouping[0] = 0;       // invalidate
    1012             :     }
    1013        1329 :     if (!aGrouping[0])
    1014             :     {
    1015        1329 :         i18n::LanguageCountryInfo aLCInfo( getLanguageCountryInfo());
    1016        2654 :         if (aLCInfo.Country.equalsIgnoreAsciiCase("IN") || // India
    1017        1325 :             aLCInfo.Country.equalsIgnoreAsciiCase("BT") )  // Bhutan
    1018             :         {
    1019           4 :             aGrouping[0] = 3;
    1020           4 :             aGrouping[1] = 2;
    1021           4 :             aGrouping[2] = 0;
    1022             :         }
    1023             :         else
    1024             :         {
    1025        1325 :             aGrouping[0] = 3;
    1026        1325 :             aGrouping[1] = 0;
    1027        1329 :         }
    1028             :     }
    1029        1329 : }
    1030             : 
    1031      129192 : const ::com::sun::star::uno::Sequence< sal_Int32 > LocaleDataWrapper::getDigitGrouping() const
    1032             : {
    1033      129192 :     ::utl::ReadWriteGuard aGuard( aMutex );
    1034      129192 :     if (!aGrouping.getLength() || aGrouping[0] == 0)
    1035             :     {   // no cached content
    1036        1329 :         aGuard.changeReadToWrite();
    1037        1329 :         ((LocaleDataWrapper*)this)->getDigitGroupingImpl();
    1038             :     }
    1039      129192 :     return aGrouping;
    1040             : }
    1041             : 
    1042             : // --- simple number formatting helpers -------------------------------
    1043             : 
    1044             : // The ImplAdd... methods are taken from class International and modified to
    1045             : // suit the needs.
    1046             : 
    1047       26090 : static sal_Unicode* ImplAddUNum( sal_Unicode* pBuf, sal_uInt64 nNumber )
    1048             : {
    1049             :     // fill temp buffer with digits
    1050             :     sal_Unicode aTempBuf[64];
    1051       26090 :     sal_Unicode* pTempBuf = aTempBuf;
    1052       65211 :     do
    1053             :     {
    1054       65211 :         *pTempBuf = (sal_Unicode)(nNumber % 10) + '0';
    1055       65211 :         pTempBuf++;
    1056       65211 :         nNumber /= 10;
    1057             :     }
    1058             :     while ( nNumber );
    1059             : 
    1060             :     // copy temp buffer to buffer passed
    1061       65211 :     do
    1062             :     {
    1063       65211 :         pTempBuf--;
    1064       65211 :         *pBuf = *pTempBuf;
    1065       65211 :         pBuf++;
    1066             :     }
    1067             :     while ( pTempBuf != aTempBuf );
    1068             : 
    1069       26090 :     return pBuf;
    1070             : }
    1071             : 
    1072         223 : static sal_Unicode* ImplAddUNum( sal_Unicode* pBuf, sal_uInt64 nNumber, int nMinLen )
    1073             : {
    1074             :     // fill temp buffer with digits
    1075             :     sal_Unicode aTempBuf[64];
    1076         223 :     sal_Unicode* pTempBuf = aTempBuf;
    1077         517 :     do
    1078             :     {
    1079         517 :         *pTempBuf = (sal_Unicode)(nNumber % 10) + '0';
    1080         517 :         pTempBuf++;
    1081         517 :         nNumber /= 10;
    1082         517 :         nMinLen--;
    1083             :     }
    1084             :     while ( nNumber );
    1085             : 
    1086             :     // fill with zeros up to the minimal length
    1087         809 :     while ( nMinLen > 0 )
    1088             :     {
    1089         363 :         *pBuf = '0';
    1090         363 :         pBuf++;
    1091         363 :         nMinLen--;
    1092             :     }
    1093             : 
    1094             :     // copy temp buffer to real buffer
    1095         517 :     do
    1096             :     {
    1097         517 :         pTempBuf--;
    1098         517 :         *pBuf = *pTempBuf;
    1099         517 :         pBuf++;
    1100             :     }
    1101             :     while ( pTempBuf != aTempBuf );
    1102             : 
    1103         223 :     return pBuf;
    1104             : }
    1105             : 
    1106        1299 : static sal_Unicode* ImplAdd2UNum( sal_Unicode* pBuf, sal_uInt16 nNumber, bool bLeading )
    1107             : {
    1108             :     DBG_ASSERT( nNumber < 100, "ImplAdd2UNum() - Number >= 100" );
    1109             : 
    1110        1299 :     if ( nNumber < 10 )
    1111             :     {
    1112        1072 :         if ( bLeading )
    1113             :         {
    1114        1049 :             *pBuf = '0';
    1115        1049 :             pBuf++;
    1116             :         }
    1117        1072 :         *pBuf = nNumber + '0';
    1118             :     }
    1119             :     else
    1120             :     {
    1121         227 :         sal_uInt16 nTemp = nNumber % 10;
    1122         227 :         nNumber /= 10;
    1123         227 :         *pBuf = nNumber + '0';
    1124         227 :         pBuf++;
    1125         227 :         *pBuf = nTemp + '0';
    1126             :     }
    1127             : 
    1128        1299 :     pBuf++;
    1129        1299 :     return pBuf;
    1130             : }
    1131             : 
    1132           0 : static sal_Unicode* ImplAdd9UNum( sal_Unicode* pBuf, sal_uInt32 nNumber, bool bLeading )
    1133             : {
    1134             :     DBG_ASSERT( nNumber < 1000000000, "ImplAdd9UNum() - Number >= 1000000000" );
    1135             : 
    1136           0 :     std::ostringstream ostr;
    1137           0 :     if (bLeading)
    1138             :     {
    1139           0 :         ostr.fill('0');
    1140           0 :         ostr.width(9);
    1141             :     }
    1142           0 :     ostr << nNumber;
    1143           0 :     std::string aStr = ostr.str();
    1144           0 :     for(const char *pAB= aStr.c_str(); *pAB != '\0'; ++pAB, ++pBuf)
    1145             :     {
    1146           0 :         *pBuf = *pAB;
    1147             :     }
    1148             : 
    1149           0 :     return pBuf;
    1150             : }
    1151             : 
    1152       23213 : inline sal_Unicode* ImplAddString( sal_Unicode* pBuf, const OUString& rStr )
    1153             : {
    1154       23213 :     if ( rStr.getLength() == 1 )
    1155       23213 :         *pBuf++ = rStr[0];
    1156           0 :     else if (rStr.isEmpty())
    1157             : ;
    1158             :     else
    1159             :     {
    1160           0 :         memcpy( pBuf, rStr.getStr(), rStr.getLength() * sizeof(sal_Unicode) );
    1161           0 :         pBuf += rStr.getLength();
    1162             :     }
    1163       23213 :     return pBuf;
    1164             : }
    1165             : 
    1166           0 : inline sal_Unicode* ImplAddString( sal_Unicode* pBuf, sal_Unicode c )
    1167             : {
    1168           0 :     *pBuf = c;
    1169           0 :     pBuf++;
    1170           0 :     return pBuf;
    1171             : }
    1172             : 
    1173           0 : inline sal_Unicode* ImplAddString( sal_Unicode* pBuf, const sal_Unicode* pCopyBuf, sal_Int32 nLen )
    1174             : {
    1175           0 :     memcpy( pBuf, pCopyBuf, nLen * sizeof(sal_Unicode) );
    1176           0 :     return pBuf + nLen;
    1177             : }
    1178             : 
    1179       26055 : sal_Unicode* LocaleDataWrapper::ImplAddFormatNum( sal_Unicode* pBuf,
    1180             :         sal_Int64 nNumber, sal_uInt16 nDecimals, bool bUseThousandSep,
    1181             :         bool bTrailingZeros ) const
    1182             : {
    1183             :     sal_Unicode aNumBuf[64];
    1184             :     sal_Unicode* pNumBuf;
    1185             :     sal_uInt16  nNumLen;
    1186       26055 :     sal_uInt16  i = 0;
    1187             : 
    1188             :     // negative number
    1189       26055 :     if ( nNumber < 0 )
    1190             :     {
    1191           0 :         nNumber *= -1;
    1192           0 :         *pBuf = '-';
    1193           0 :         pBuf++;
    1194             :     }
    1195             : 
    1196             :     // convert number
    1197       26055 :     pNumBuf = ImplAddUNum( aNumBuf, (sal_uInt64)nNumber );
    1198       26055 :     nNumLen = (sal_uInt16)(sal_uLong)(pNumBuf-aNumBuf);
    1199       26055 :     pNumBuf = aNumBuf;
    1200             : 
    1201       26055 :     if ( nNumLen <= nDecimals )
    1202             :     {
    1203             :         // strip .0 in decimals?
    1204        1130 :         if ( !nNumber && !bTrailingZeros )
    1205             :         {
    1206         563 :             *pBuf = '0';
    1207         563 :             pBuf++;
    1208             :         }
    1209             :         else
    1210             :         {
    1211             :             // LeadingZero, insert 0
    1212         567 :             if ( isNumLeadingZero() )
    1213             :             {
    1214         567 :                 *pBuf = '0';
    1215         567 :                 pBuf++;
    1216             :             }
    1217             : 
    1218             :             // append decimal separator
    1219         567 :             pBuf = ImplAddString( pBuf, getNumDecimalSep() );
    1220             : 
    1221             :             // fill with zeros
    1222        1701 :             while ( i < (nDecimals-nNumLen) )
    1223             :             {
    1224         567 :                 *pBuf = '0';
    1225         567 :                 pBuf++;
    1226         567 :                 i++;
    1227             :             }
    1228             : 
    1229             :             // append decimals
    1230        1701 :             while ( nNumLen )
    1231             :             {
    1232         567 :                 *pBuf = *pNumBuf;
    1233         567 :                 pBuf++;
    1234         567 :                 pNumBuf++;
    1235         567 :                 nNumLen--;
    1236             :             }
    1237             :         }
    1238             :     }
    1239             :     else
    1240             :     {
    1241       24925 :         const OUString& rThoSep = getNumThousandSep();
    1242             : 
    1243             :         // copy number to buffer (excluding decimals)
    1244       24925 :         sal_uInt16 nNumLen2 = nNumLen-nDecimals;
    1245       24925 :         uno::Sequence< sal_Bool > aGroupPos;
    1246       24925 :         if (bUseThousandSep)
    1247       46388 :             aGroupPos = utl::DigitGroupingIterator::createForwardSequence(
    1248       23194 :                     nNumLen2, getDigitGrouping());
    1249       67302 :         for (; i < nNumLen2; ++i )
    1250             :         {
    1251       42377 :             *pBuf = *pNumBuf;
    1252       42377 :             pBuf++;
    1253       42377 :             pNumBuf++;
    1254             : 
    1255             :             // add thousand separator?
    1256       42377 :             if ( bUseThousandSep && aGroupPos[i] )
    1257         118 :                 pBuf = ImplAddString( pBuf, rThoSep );
    1258             :         }
    1259             : 
    1260             :         // append decimals
    1261       24925 :         if ( nDecimals )
    1262             :         {
    1263       21603 :             pBuf = ImplAddString( pBuf, getNumDecimalSep() );
    1264             : 
    1265       21603 :             bool bNullEnd = true;
    1266       64809 :             while ( i < nNumLen )
    1267             :             {
    1268       21603 :                 if ( *pNumBuf != '0' )
    1269        1691 :                     bNullEnd = false;
    1270             : 
    1271       21603 :                 *pBuf = *pNumBuf;
    1272       21603 :                 pBuf++;
    1273       21603 :                 pNumBuf++;
    1274       21603 :                 i++;
    1275             :             }
    1276             : 
    1277             :             // strip .0 in decimals?
    1278       21603 :             if ( bNullEnd && !bTrailingZeros )
    1279       19912 :                 pBuf -= nDecimals+1;
    1280       24925 :         }
    1281             :     }
    1282             : 
    1283       26055 :     return pBuf;
    1284             : }
    1285             : 
    1286             : // --- simple date and time formatting --------------------------------
    1287             : 
    1288         217 : OUString LocaleDataWrapper::getDate( const Date& rDate ) const
    1289             : {
    1290         217 :     ::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical );
    1291             : //!TODO: leading zeros et al
    1292             :     sal_Unicode aBuf[128];
    1293         217 :     sal_Unicode* pBuf = aBuf;
    1294         217 :     sal_uInt16  nDay    = rDate.GetDay();
    1295         217 :     sal_uInt16  nMonth  = rDate.GetMonth();
    1296         217 :     sal_uInt16  nYear   = rDate.GetYear();
    1297             :     sal_uInt16  nYearLen;
    1298             : 
    1299             :     if ( true /* IsDateCentury() */ )
    1300         217 :         nYearLen = 4;
    1301             :     else
    1302             :     {
    1303             :         nYearLen = 2;
    1304             :         nYear %= 100;
    1305             :     }
    1306             : 
    1307         217 :     switch ( getDateFormat() )
    1308             :     {
    1309             :         case DMY :
    1310           0 :             pBuf = ImplAdd2UNum( pBuf, nDay, true /* IsDateDayLeadingZero() */ );
    1311           0 :             pBuf = ImplAddString( pBuf, getDateSep() );
    1312           0 :             pBuf = ImplAdd2UNum( pBuf, nMonth, true /* IsDateMonthLeadingZero() */ );
    1313           0 :             pBuf = ImplAddString( pBuf, getDateSep() );
    1314           0 :             pBuf = ImplAddUNum( pBuf, nYear, nYearLen );
    1315           0 :         break;
    1316             :         case MDY :
    1317         217 :             pBuf = ImplAdd2UNum( pBuf, nMonth, true /* IsDateMonthLeadingZero() */ );
    1318         217 :             pBuf = ImplAddString( pBuf, getDateSep() );
    1319         217 :             pBuf = ImplAdd2UNum( pBuf, nDay, true /* IsDateDayLeadingZero() */ );
    1320         217 :             pBuf = ImplAddString( pBuf, getDateSep() );
    1321         217 :             pBuf = ImplAddUNum( pBuf, nYear, nYearLen );
    1322         217 :         break;
    1323             :         default:
    1324           0 :             pBuf = ImplAddUNum( pBuf, nYear, nYearLen );
    1325           0 :             pBuf = ImplAddString( pBuf, getDateSep() );
    1326           0 :             pBuf = ImplAdd2UNum( pBuf, nMonth, true /* IsDateMonthLeadingZero() */ );
    1327           0 :             pBuf = ImplAddString( pBuf, getDateSep() );
    1328           0 :             pBuf = ImplAdd2UNum( pBuf, nDay, true /* IsDateDayLeadingZero() */ );
    1329             :     }
    1330             : 
    1331         217 :     return OUString(aBuf, pBuf-aBuf);
    1332             : }
    1333             : 
    1334         339 : OUString LocaleDataWrapper::getTime( const Time& rTime, bool bSec, bool b100Sec ) const
    1335             : {
    1336         339 :     ::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical );
    1337             : //!TODO: leading zeros et al
    1338             :     sal_Unicode aBuf[128];
    1339         339 :     sal_Unicode* pBuf = aBuf;
    1340         339 :     sal_uInt16  nHour = rTime.GetHour();
    1341             : 
    1342         339 :     nHour %= 24;
    1343             : 
    1344         339 :     pBuf = ImplAdd2UNum( pBuf, nHour, true /* IsTimeLeadingZero() */ );
    1345         339 :     pBuf = ImplAddString( pBuf, getTimeSep() );
    1346         339 :     pBuf = ImplAdd2UNum( pBuf, rTime.GetMin(), true );
    1347         339 :     if ( bSec )
    1348             :     {
    1349         145 :         pBuf = ImplAddString( pBuf, getTimeSep() );
    1350         145 :         pBuf = ImplAdd2UNum( pBuf, rTime.GetSec(), true );
    1351             : 
    1352         145 :         if ( b100Sec )
    1353             :         {
    1354           0 :             pBuf = ImplAddString( pBuf, getTime100SecSep() );
    1355           0 :             pBuf = ImplAdd9UNum( pBuf, rTime.GetNanoSec(), true );
    1356             :         }
    1357             :     }
    1358             : 
    1359         339 :     return OUString(aBuf, pBuf - aBuf);
    1360             : }
    1361             : 
    1362          35 : OUString LocaleDataWrapper::getLongDate( const Date& rDate, CalendarWrapper& rCal,
    1363             :         sal_Int16 nDisplayDayOfWeek, bool bDayOfMonthWithLeadingZero,
    1364             :         sal_Int16 nDisplayMonth, bool bTwoDigitYear ) const
    1365             : {
    1366          35 :     ::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical );
    1367             :     using namespace ::com::sun::star::i18n;
    1368             :     sal_Unicode     aBuf[20];
    1369             :     sal_Unicode*    pBuf;
    1370          35 :     OUString aStr;
    1371             :     sal_Int16 nVal;
    1372          35 :     rCal.setGregorianDateTime( rDate );
    1373             :     // day of week
    1374          35 :     nVal = rCal.getValue( CalendarFieldIndex::DAY_OF_WEEK );
    1375          35 :     aStr += rCal.getDisplayName( CalendarDisplayIndex::DAY, nVal, nDisplayDayOfWeek );
    1376          35 :     aStr += getLongDateDayOfWeekSep();
    1377             :     // day of month
    1378          35 :     nVal = rCal.getValue( CalendarFieldIndex::DAY_OF_MONTH );
    1379          35 :     pBuf = ImplAdd2UNum( aBuf, nVal, bDayOfMonthWithLeadingZero );
    1380          70 :     OUString aDay(aBuf, pBuf-aBuf);
    1381             :     // month of year
    1382          35 :     nVal = rCal.getValue( CalendarFieldIndex::MONTH );
    1383          70 :     OUString aMonth( rCal.getDisplayName( CalendarDisplayIndex::MONTH, nVal, nDisplayMonth ) );
    1384             :     // year
    1385          35 :     nVal = rCal.getValue( CalendarFieldIndex::YEAR );
    1386          35 :     if ( bTwoDigitYear )
    1387           0 :         pBuf = ImplAddUNum( aBuf, nVal % 100, 2 );
    1388             :     else
    1389          35 :         pBuf = ImplAddUNum( aBuf, nVal );
    1390          70 :     OUString aYear(aBuf, pBuf-aBuf);
    1391             :     // concatenate
    1392          35 :     switch ( getLongDateFormat() )
    1393             :     {
    1394             :         case DMY :
    1395           0 :             aStr += aDay + getLongDateDaySep() + aMonth + getLongDateMonthSep() + aYear;
    1396           0 :         break;
    1397             :         case MDY :
    1398          35 :             aStr += aMonth + getLongDateMonthSep() + aDay + getLongDateDaySep() + aYear;
    1399          35 :         break;
    1400             :         default:    // YMD
    1401           0 :             aStr += aYear + getLongDateYearSep() +  aMonth +  getLongDateMonthSep() + aDay;
    1402             :     }
    1403          70 :     return aStr;
    1404             : }
    1405             : 
    1406           6 : OUString LocaleDataWrapper::getDuration( const Time& rTime, bool bSec, bool b100Sec ) const
    1407             : {
    1408           6 :     ::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical );
    1409             :     sal_Unicode aBuf[128];
    1410           6 :     sal_Unicode* pBuf = aBuf;
    1411             : 
    1412           6 :     if ( rTime < Time( 0 ) )
    1413           0 :         pBuf = ImplAddString( pBuf, ' ' );
    1414             : 
    1415             :     if ( true /* IsTimeLeadingZero() */ )
    1416           6 :         pBuf = ImplAddUNum( pBuf, rTime.GetHour(), 2 );
    1417             :     else
    1418             :         pBuf = ImplAddUNum( pBuf, rTime.GetHour() );
    1419           6 :     pBuf = ImplAddString( pBuf, getTimeSep() );
    1420           6 :     pBuf = ImplAdd2UNum( pBuf, rTime.GetMin(), true );
    1421           6 :     if ( bSec )
    1422             :     {
    1423           1 :         pBuf = ImplAddString( pBuf, getTimeSep() );
    1424           1 :         pBuf = ImplAdd2UNum( pBuf, rTime.GetSec(), true );
    1425             : 
    1426           1 :         if ( b100Sec )
    1427             :         {
    1428           0 :             pBuf = ImplAddString( pBuf, getTime100SecSep() );
    1429           0 :             pBuf = ImplAdd9UNum( pBuf, rTime.GetNanoSec(), true );
    1430             :         }
    1431             :     }
    1432             : 
    1433           6 :     return OUString(aBuf, pBuf-aBuf);
    1434             : }
    1435             : 
    1436             : // --- simple number formatting ---------------------------------------
    1437             : 
    1438       26055 : inline size_t ImplGetNumberStringLengthGuess( const LocaleDataWrapper& rLoc, sal_uInt16 nDecimals )
    1439             : {
    1440             :     // approximately 3.2 bits per digit
    1441       26055 :     const size_t nDig = ((sizeof(sal_Int64) * 8) / 3) + 1;
    1442             :     // digits, separators (pessimized for insane "every digit may be grouped"), leading zero, sign
    1443             :     size_t nGuess = ((nDecimals < nDig) ?
    1444       26055 :         (((nDig - nDecimals) * rLoc.getNumThousandSep().getLength()) + nDig) :
    1445       52110 :         nDecimals) + rLoc.getNumDecimalSep().getLength() + 3;
    1446       26055 :     return nGuess;
    1447             : }
    1448             : 
    1449       26055 : OUString LocaleDataWrapper::getNum( sal_Int64 nNumber, sal_uInt16 nDecimals,
    1450             :         bool bUseThousandSep, bool bTrailingZeros ) const
    1451             : {
    1452       26055 :     ::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical );
    1453             :     sal_Unicode aBuf[128];      // big enough for 64-bit long and crazy grouping
    1454             :     // check if digits and separators will fit into fixed buffer or allocate
    1455       26055 :     size_t nGuess = ImplGetNumberStringLengthGuess( *this, nDecimals );
    1456             :     sal_Unicode* const pBuffer = (nGuess < 118 ? aBuf :
    1457       26055 :         new sal_Unicode[nGuess + 16]);
    1458             : 
    1459             :     sal_Unicode* pBuf = ImplAddFormatNum( pBuffer, nNumber, nDecimals,
    1460       26055 :         bUseThousandSep, bTrailingZeros );
    1461       26055 :     OUString aStr(pBuffer, pBuf-pBuffer);
    1462             : 
    1463       26055 :     if ( pBuffer != aBuf )
    1464           0 :         delete [] pBuffer;
    1465       26055 :     return aStr;
    1466             : }
    1467             : 
    1468           0 : OUString LocaleDataWrapper::getCurr( sal_Int64 nNumber, sal_uInt16 nDecimals,
    1469             :         const OUString& rCurrencySymbol, bool bUseThousandSep ) const
    1470             : {
    1471           0 :     ::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical );
    1472             :     sal_Unicode aBuf[192];
    1473             :     sal_Unicode aNumBuf[128];    // big enough for 64-bit long and crazy grouping
    1474           0 :     sal_Unicode cZeroChar = getCurrZeroChar();
    1475             : 
    1476             :     // check if digits and separators will fit into fixed buffer or allocate
    1477           0 :     size_t nGuess = ImplGetNumberStringLengthGuess( *this, nDecimals );
    1478             :     sal_Unicode* const pNumBuffer = (nGuess < 118 ? aNumBuf :
    1479           0 :         new sal_Unicode[nGuess + 16]);
    1480             : 
    1481             :     sal_Unicode* const pBuffer =
    1482           0 :         ((size_t(rCurrencySymbol.getLength()) + nGuess + 20) < SAL_N_ELEMENTS(aBuf) ? aBuf :
    1483           0 :         new sal_Unicode[ rCurrencySymbol.getLength() + nGuess + 20 ]);
    1484           0 :     sal_Unicode* pBuf = pBuffer;
    1485             : 
    1486             :     bool bNeg;
    1487           0 :     if ( nNumber < 0 )
    1488             :     {
    1489           0 :         bNeg = true;
    1490           0 :         nNumber *= -1;
    1491             :     }
    1492             :     else
    1493           0 :         bNeg = false;
    1494             : 
    1495             :     // convert number
    1496             :     sal_Unicode* pEndNumBuf = ImplAddFormatNum( pNumBuffer, nNumber, nDecimals,
    1497           0 :         bUseThousandSep, true );
    1498           0 :     sal_Int32 nNumLen = (sal_Int32)(sal_uLong)(pEndNumBuf-pNumBuffer);
    1499             : 
    1500             :     // replace zeros with zero character
    1501           0 :     if ( (cZeroChar != '0') && nDecimals /* && IsNumTrailingZeros() */ )
    1502             :     {
    1503             :         sal_Unicode* pTempBuf;
    1504             :         sal_uInt16  i;
    1505           0 :         bool    bZero = true;
    1506             : 
    1507           0 :         pTempBuf = pNumBuffer+nNumLen-nDecimals;
    1508           0 :         i = 0;
    1509           0 :         do
    1510             :         {
    1511           0 :             if ( *pTempBuf != '0' )
    1512             :             {
    1513           0 :                 bZero = false;
    1514           0 :                 break;
    1515             :             }
    1516             : 
    1517           0 :             pTempBuf++;
    1518           0 :             i++;
    1519             :         }
    1520             :         while ( i < nDecimals );
    1521             : 
    1522           0 :         if ( bZero )
    1523             :         {
    1524           0 :             pTempBuf = pNumBuffer+nNumLen-nDecimals;
    1525           0 :             i = 0;
    1526           0 :             do
    1527             :             {
    1528           0 :                 *pTempBuf = cZeroChar;
    1529           0 :                 pTempBuf++;
    1530           0 :                 i++;
    1531             :             }
    1532             :             while ( i < nDecimals );
    1533             :         }
    1534             :     }
    1535             : 
    1536           0 :     if ( !bNeg )
    1537             :     {
    1538           0 :         switch( getCurrPositiveFormat() )
    1539             :         {
    1540             :             case 0:
    1541           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1542           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1543           0 :                 break;
    1544             :             case 1:
    1545           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1546           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1547           0 :                 break;
    1548             :             case 2:
    1549           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1550           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1551           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1552           0 :                 break;
    1553             :             case 3:
    1554           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1555           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1556           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1557           0 :                 break;
    1558             :         }
    1559             :     }
    1560             :     else
    1561             :     {
    1562           0 :         switch( getCurrNegativeFormat() )
    1563             :         {
    1564             :             case 0:
    1565           0 :                 pBuf = ImplAddString( pBuf, '(' );
    1566           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1567           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1568           0 :                 pBuf = ImplAddString( pBuf, ')' );
    1569           0 :                 break;
    1570             :             case 1:
    1571           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1572           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1573           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1574           0 :                 break;
    1575             :             case 2:
    1576           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1577           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1578           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1579           0 :                 break;
    1580             :             case 3:
    1581           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1582           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1583           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1584           0 :                 break;
    1585             :             case 4:
    1586           0 :                 pBuf = ImplAddString( pBuf, '(' );
    1587           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1588           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1589           0 :                 pBuf = ImplAddString( pBuf, ')' );
    1590           0 :                 break;
    1591             :             case 5:
    1592           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1593           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1594           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1595           0 :                 break;
    1596             :             case 6:
    1597           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1598           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1599           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1600           0 :                 break;
    1601             :             case 7:
    1602           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1603           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1604           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1605           0 :                 break;
    1606             :             case 8:
    1607           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1608           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1609           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1610           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1611           0 :                 break;
    1612             :             case 9:
    1613           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1614           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1615           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1616           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1617           0 :                 break;
    1618             :             case 10:
    1619           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1620           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1621           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1622           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1623           0 :                 break;
    1624             :             case 11:
    1625           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1626           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1627           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1628           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1629           0 :                 break;
    1630             :             case 12:
    1631           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1632           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1633           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1634           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1635           0 :                 break;
    1636             :             case 13:
    1637           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1638           0 :                 pBuf = ImplAddString( pBuf, '-' );
    1639           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1640           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1641           0 :                 break;
    1642             :             case 14:
    1643           0 :                 pBuf = ImplAddString( pBuf, '(' );
    1644           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1645           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1646           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1647           0 :                 pBuf = ImplAddString( pBuf, ')' );
    1648           0 :                 break;
    1649             :             case 15:
    1650           0 :                 pBuf = ImplAddString( pBuf, '(' );
    1651           0 :                 pBuf = ImplAddString( pBuf, pNumBuffer, nNumLen );
    1652           0 :                 pBuf = ImplAddString( pBuf, ' ' );
    1653           0 :                 pBuf = ImplAddString( pBuf, rCurrencySymbol );
    1654           0 :                 pBuf = ImplAddString( pBuf, ')' );
    1655           0 :                 break;
    1656             :         }
    1657             :     }
    1658             : 
    1659           0 :     OUString aNumber(pBuffer, pBuf-pBuffer);
    1660             : 
    1661           0 :     if ( pBuffer != aBuf )
    1662           0 :         delete [] pBuffer;
    1663           0 :     if ( pNumBuffer != aNumBuf )
    1664           0 :         delete [] pNumBuffer;
    1665             : 
    1666           0 :     return aNumber;
    1667             : }
    1668             : 
    1669             : // --- mixed ----------------------------------------------------------
    1670             : 
    1671       46043 : LanguageTag LocaleDataWrapper::getLoadedLanguageTag() const
    1672             : {
    1673       46043 :     LanguageCountryInfo aLCInfo = getLanguageCountryInfo();
    1674       46043 :     return LanguageTag( lang::Locale( aLCInfo.Language, aLCInfo.Country, aLCInfo.Variant ));
    1675             : }
    1676             : 
    1677           0 : OUString LocaleDataWrapper::appendLocaleInfo(const OUString& rDebugMsg) const
    1678             : {
    1679           0 :     ::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical );
    1680           0 :     OUStringBuffer aDebugMsg(rDebugMsg);
    1681           0 :     aDebugMsg.append('\n');
    1682           0 :     aDebugMsg.append(maLanguageTag.getBcp47());
    1683           0 :     aDebugMsg.appendAscii(" requested\n");
    1684           0 :     LanguageTag aLoaded = getLoadedLanguageTag();
    1685           0 :     aDebugMsg.append(aLoaded.getBcp47());
    1686           0 :     aDebugMsg.appendAscii(" loaded");
    1687           0 :     return aDebugMsg.makeStringAndClear();
    1688             : }
    1689             : 
    1690             : // static
    1691           0 : void LocaleDataWrapper::outputCheckMessage( const OUString& rMsg )
    1692             : {
    1693           0 :     outputCheckMessage(OUStringToOString(rMsg, RTL_TEXTENCODING_UTF8).getStr());
    1694           0 : }
    1695             : 
    1696             : // static
    1697           0 : void LocaleDataWrapper::outputCheckMessage( const char* pStr )
    1698             : {
    1699           0 :     fprintf( stderr, "\n%s\n", pStr);
    1700           0 :     fflush( stderr);
    1701             :     OSL_TRACE("%s", pStr);
    1702           0 : }
    1703             : 
    1704             : // static
    1705          77 : void LocaleDataWrapper::evaluateLocaleDataChecking()
    1706             : {
    1707             :     // Using the rtl_Instance template here wouldn't solve all threaded write
    1708             :     // accesses, since we want to assign the result to the static member
    1709             :     // variable and would need to dereference the pointer returned and assign
    1710             :     // the value unguarded. This is the same pattern manually coded.
    1711          77 :     sal_uInt8 nCheck = nLocaleDataChecking;
    1712          77 :     if (!nCheck)
    1713             :     {
    1714          77 :         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
    1715          77 :         nCheck = nLocaleDataChecking;
    1716          77 :         if (!nCheck)
    1717             :         {
    1718             : #ifdef DBG_UTIL
    1719             :             nCheck = 1;
    1720             : #else
    1721          77 :             const char* pEnv = getenv( "OOO_ENABLE_LOCALE_DATA_CHECKS");
    1722          77 :             if (pEnv && (pEnv[0] == 'Y' || pEnv[0] == 'y' || pEnv[0] == '1'))
    1723           0 :                 nCheck = 1;
    1724             :             else
    1725          77 :                 nCheck = 2;
    1726             : #endif
    1727             :             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
    1728          77 :             nLocaleDataChecking = nCheck;
    1729          77 :         }
    1730             :     }
    1731             :     else {
    1732             :         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
    1733             :     }
    1734          77 : }
    1735             : 
    1736             : // --- XLocaleData3 ----------------------------------------------------------
    1737             : 
    1738          34 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::Calendar2 > LocaleDataWrapper::getAllCalendars() const
    1739             : {
    1740             :     try
    1741             :     {
    1742          34 :         return xLD->getAllCalendars2( getMyLocale() );
    1743             :     }
    1744           0 :     catch (const Exception& e)
    1745             :     {
    1746             :         SAL_WARN( "unotools.i18n", "getAllCalendars: Exception caught " << e.Message );
    1747             :     }
    1748           0 :     return ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::Calendar2 >(0);
    1749             : }
    1750             : 
    1751             : // --- XLocaleData4 ----------------------------------------------------------
    1752             : 
    1753          44 : ::com::sun::star::uno::Sequence< OUString > LocaleDataWrapper::getDateAcceptancePatterns() const
    1754             : {
    1755          44 :     ::utl::ReadWriteGuard aGuard( aMutex );
    1756             : 
    1757          44 :     if (aDateAcceptancePatterns.getLength())
    1758          23 :         return aDateAcceptancePatterns;
    1759             : 
    1760          21 :     aGuard.changeReadToWrite();
    1761             : 
    1762             :     try
    1763             :     {
    1764          42 :         const_cast<LocaleDataWrapper*>(this)->aDateAcceptancePatterns =
    1765          42 :             xLD->getDateAcceptancePatterns( getMyLocale() );
    1766          21 :         return aDateAcceptancePatterns;
    1767             :     }
    1768           0 :     catch (const Exception& e)
    1769             :     {
    1770             :         SAL_WARN( "unotools.i18n", "getDateAcceptancePatterns: Exception caught " << e.Message );
    1771             :     }
    1772           0 :     return ::com::sun::star::uno::Sequence< OUString >(0);
    1773             : }
    1774             : 
    1775             : // --- Override layer --------------------------------------------------------
    1776             : 
    1777         160 : void LocaleDataWrapper::setDateAcceptancePatterns(
    1778             :         const ::com::sun::star::uno::Sequence< OUString > & rPatterns )
    1779             : {
    1780         160 :     ::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nWrite );
    1781             : 
    1782         160 :     if (!aDateAcceptancePatterns.getLength() || !rPatterns.getLength())
    1783             :     {
    1784             :         try
    1785             :         {
    1786         160 :             aDateAcceptancePatterns = xLD->getDateAcceptancePatterns( getMyLocale() );
    1787             :         }
    1788           0 :         catch (const Exception& e)
    1789             :         {
    1790             :             SAL_WARN( "unotools.i18n", "setDateAcceptancePatterns: Exception caught " << e.Message );
    1791             :         }
    1792         160 :         if (!rPatterns.getLength())
    1793         160 :             return;     // just a reset
    1794           0 :         if (!aDateAcceptancePatterns.getLength())
    1795             :         {
    1796           0 :             aDateAcceptancePatterns = rPatterns;
    1797           0 :             return;
    1798             :         }
    1799             :     }
    1800             : 
    1801             :     // Never overwrite the locale's full date pattern! The first.
    1802           0 :     if (aDateAcceptancePatterns[0] == rPatterns[0])
    1803           0 :         aDateAcceptancePatterns = rPatterns;    // sane
    1804             :     else
    1805             :     {
    1806             :         // Copy existing full date pattern and append the sequence passed.
    1807             :         /* TODO: could check for duplicates and shrink target sequence */
    1808           0 :         Sequence< OUString > aTmp( rPatterns.getLength() + 1 );
    1809           0 :         OUString* pArray1 = aTmp.getArray();
    1810           0 :         const OUString* pArray2 = rPatterns.getConstArray();
    1811           0 :         pArray1[0] = aDateAcceptancePatterns[0];
    1812           0 :         for (sal_Int32 i=0; i < rPatterns.getLength(); ++i)
    1813           0 :             pArray1[i+1] = pArray2[i];
    1814           0 :         aDateAcceptancePatterns = aTmp;
    1815           0 :     }
    1816             : }
    1817             : 
    1818             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10