Kaydet (Commit) 18699039 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Clean up Mac _imp_getProcessLocale

Introduces OUStringBuffer::appendUninitialized.

Change-Id: If225ec4d798e0df91cad60130e3f22ee26b88abb
üst 8cbfce51
......@@ -100,7 +100,9 @@ SAL_DLLPUBLIC void SAL_CALL rtl_uStringbuffer_ensureCapacity(
@param This The string, on that the operation should take place
@param capacity the capacity of the string buffer
@param offset the offset.
@param str a character array.
@param str a character array. Since LibreOffice 4.4, as a special
case, if str is null then the len added characters are
left uninitialized.
@param len the number of characters to append.
*/
SAL_DLLPUBLIC void SAL_CALL rtl_uStringbuffer_insert(
......
......@@ -474,6 +474,7 @@ public:
OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
{
assert( len >= 0 );
assert( len == 0 || str != 0 );
rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
return *this;
}
......@@ -733,6 +734,28 @@ public:
return insertUtf32(getLength(), c);
}
/**
Unsafe way to make space for a fixed amount of characters to be appended
into this OUStringBuffer.
A call to this function must immediately be followed by code that
completely fills the uninitialized block pointed to by the return value.
@param length the length of the uninitialized block of sal_Unicode
entities; must be non-negative
@return a pointer to the start of the uninitialized block; only valid
until this OUStringBuffer's capacity changes
@since LibreOffice 4.4
*/
sal_Unicode * appendUninitialized(sal_Int32 length) {
assert(length >= 0);
sal_Int32 n = getLength();
rtl_uStringbuffer_insert(&pData, &nCapacity, n, 0, length);
return pData->buffer + n;
}
/**
Inserts the string into this string buffer.
......@@ -797,6 +820,7 @@ public:
{
assert( offset >= 0 && offset <= pData->length );
assert( len >= 0 );
assert( len == 0 || str != 0 );
rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
return *this;
}
......
......@@ -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 */
......
......@@ -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
......
......@@ -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: */
......@@ -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;
}
......
......@@ -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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment