diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2014-11-20 07:44:43 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2014-11-20 08:34:06 +0100 |
commit | 186990395d72a803dd4a9f087fe4e05f49e69ad2 (patch) | |
tree | 44c28d013c85b708a418e6e6b5ce71907fde7285 /sal | |
parent | 8cbfce51ff659581a9602b22c36b9af1c69e51f8 (diff) |
Clean up Mac _imp_getProcessLocale
Introduces OUStringBuffer::appendUninitialized.
Change-Id: If225ec4d798e0df91cad60130e3f22ee26b88abb
Diffstat (limited to 'sal')
-rw-r--r-- | sal/osl/unx/nlsupport.cxx | 22 | ||||
-rw-r--r-- | sal/osl/unx/nlsupport.hxx | 4 | ||||
-rw-r--r-- | sal/osl/unx/osxlocale.cxx | 23 | ||||
-rw-r--r-- | sal/rtl/math.cxx | 2 | ||||
-rw-r--r-- | sal/rtl/ustrbuf.cxx | 13 |
5 files changed, 42 insertions, 22 deletions
diff --git a/sal/osl/unx/nlsupport.cxx b/sal/osl/unx/nlsupport.cxx index f6ae33c66b9f..8283619bc1f9 100644 --- a/sal/osl/unx/nlsupport.cxx +++ b/sal/osl/unx/nlsupport.cxx @@ -20,6 +20,8 @@ #include <osl/nlsupport.h> #include <osl/diagnose.h> #include <osl/process.h> +#include <rtl/string.hxx> +#include <rtl/ustring.hxx> #include "nlsupport.hxx" @@ -844,7 +846,7 @@ rtl_TextEncoding osl_getTextEncodingFromLocale( rtl_Locale * pLocale ) void _imp_getProcessLocale( rtl_Locale ** ppLocale ) { - static char *locale = NULL; + static char const *locale = NULL; /* basic thread safeness */ // pthread_mutex_lock( &aLocalMutex ); @@ -852,12 +854,16 @@ void _imp_getProcessLocale( rtl_Locale ** ppLocale ) /* Only fetch the locale once and cache it */ if ( NULL == locale ) { - - locale = (char *)malloc( 128 ); - if ( locale ) - macosx_getLocale( locale, 128 ); - else - fprintf( stderr, "nlsupport.c: locale allocation returned NULL!\n" ); + rtl::OUString loc16(macosx_getLocale()); + rtl::OString loc8; + if (loc16.convertToString( + &loc8, RTL_TEXTENCODING_UTF8, + (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR + | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))) + { + rtl_string_acquire(loc8.pData); // leak, for setenv + locale = loc8.getStr(); + } } /* handle the case where OS specific method of finding locale fails */ @@ -873,7 +879,7 @@ void _imp_getProcessLocale( rtl_Locale ** ppLocale ) locale = getenv( "LANG" ); if( NULL == locale ) - locale = strdup("C"); + locale = "C"; } /* return the locale */ diff --git a/sal/osl/unx/nlsupport.hxx b/sal/osl/unx/nlsupport.hxx index f7d6ec4a034d..799df2f54e3b 100644 --- a/sal/osl/unx/nlsupport.hxx +++ b/sal/osl/unx/nlsupport.hxx @@ -24,11 +24,13 @@ #include <rtl/locale.h> +namespace rtl { class OUString; } + void _imp_getProcessLocale( rtl_Locale ** ); int _imp_setProcessLocale( rtl_Locale * ); #if defined IOS || defined MACOSX -void macosx_getLocale(char *locale, sal_uInt32 bufferLen); +rtl::OUString macosx_getLocale(); #endif #endif diff --git a/sal/osl/unx/osxlocale.cxx b/sal/osl/unx/osxlocale.cxx index 9bc574263e6b..3fb96648ba67 100644 --- a/sal/osl/unx/osxlocale.cxx +++ b/sal/osl/unx/osxlocale.cxx @@ -27,6 +27,8 @@ #include <CoreFoundation/CoreFoundation.h> #include <postmac.h> +#include <rtl/ustrbuf.hxx> + #include <nlsupport.hxx> namespace @@ -59,11 +61,17 @@ namespace return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref); } + + void append(rtl::OUStringBuffer & buffer, CFStringRef string) { + CFIndex n = CFStringGetLength(string); + CFStringGetCharacters( + string, CFRangeMake(0, n), buffer.appendUninitialized(n)); + } } /** Grab current locale from system. */ -void macosx_getLocale(char *locale, sal_uInt32 bufferLen) +rtl::OUString macosx_getLocale() { CFStringRef sref = getProcessLocale(); CFStringGuard sGuard(sref); @@ -75,22 +83,21 @@ void macosx_getLocale(char *locale, sal_uInt32 bufferLen) CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("-")); CFArrayGuard arrGuard(subs); - CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0); - CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII); + rtl::OUStringBuffer buf; + append(buf, (CFStringRef)CFArrayGetValueAtIndex(subs, 0)); // country also available? Assumption: if the array contains more than one // value the second value is always the country! if (CFArrayGetCount(subs) > 1) { - strlcat(locale, "_", bufferLen - strlen(locale)); - - CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1); - CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII); + buf.append("_"); + append(buf, (CFStringRef)CFArrayGetValueAtIndex(subs, 1)); } // Append 'UTF-8' to the locale because the Mac OS X file // system interface is UTF-8 based and sal tries to determine // the file system locale from the locale information - strlcat(locale, ".UTF-8", bufferLen - strlen(locale)); + buf.append(".UTF-8"); + return buf.makeStringAndClear(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx index e030b9ca0c33..ac5a79387617 100644 --- a/sal/rtl/math.cxx +++ b/sal/rtl/math.cxx @@ -31,6 +31,7 @@ #include "sal/types.h" #include <algorithm> +#include <cassert> #include <float.h> #include <limits.h> #include <math.h> @@ -256,6 +257,7 @@ struct UStringTraits sal_Int32 * pCapacity, sal_Int32 * pOffset, sal_Unicode const * pChars, sal_Int32 nLen) { + assert(pChars != nullptr); rtl_uStringbuffer_insert(pBuffer, pCapacity, *pOffset, pChars, nLen); *pOffset += nLen; } diff --git a/sal/rtl/ustrbuf.cxx b/sal/rtl/ustrbuf.cxx index 759d05ccba0c..df8423ce1b19 100644 --- a/sal/rtl/ustrbuf.cxx +++ b/sal/rtl/ustrbuf.cxx @@ -140,11 +140,14 @@ void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This, memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) ); /* insert the new characters */ - if( len == 1 ) - /* optimized for 1 character */ - pBuf[offset] = *str; - else if( len > 1 ) - memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) ); + if( str != nullptr ) + { + if( len == 1 ) + /* optimized for 1 character */ + pBuf[offset] = *str; + else if( len > 1 ) + memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) ); + } (*This)->length = nOldLen + len; pBuf[ nOldLen + len ] = 0; } |