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( ...@@ -100,7 +100,9 @@ SAL_DLLPUBLIC void SAL_CALL rtl_uStringbuffer_ensureCapacity(
@param This The string, on that the operation should take place @param This The string, on that the operation should take place
@param capacity the capacity of the string buffer @param capacity the capacity of the string buffer
@param offset the offset. @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. @param len the number of characters to append.
*/ */
SAL_DLLPUBLIC void SAL_CALL rtl_uStringbuffer_insert( SAL_DLLPUBLIC void SAL_CALL rtl_uStringbuffer_insert(
......
...@@ -474,6 +474,7 @@ public: ...@@ -474,6 +474,7 @@ public:
OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len) OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
{ {
assert( len >= 0 ); assert( len >= 0 );
assert( len == 0 || str != 0 );
rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len ); rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
return *this; return *this;
} }
...@@ -733,6 +734,28 @@ public: ...@@ -733,6 +734,28 @@ public:
return insertUtf32(getLength(), c); 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. Inserts the string into this string buffer.
...@@ -797,6 +820,7 @@ public: ...@@ -797,6 +820,7 @@ public:
{ {
assert( offset >= 0 && offset <= pData->length ); assert( offset >= 0 && offset <= pData->length );
assert( len >= 0 ); assert( len >= 0 );
assert( len == 0 || str != 0 );
rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len ); rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
return *this; return *this;
} }
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include <osl/nlsupport.h> #include <osl/nlsupport.h>
#include <osl/diagnose.h> #include <osl/diagnose.h>
#include <osl/process.h> #include <osl/process.h>
#include <rtl/string.hxx>
#include <rtl/ustring.hxx>
#include "nlsupport.hxx" #include "nlsupport.hxx"
...@@ -844,7 +846,7 @@ rtl_TextEncoding osl_getTextEncodingFromLocale( rtl_Locale * pLocale ) ...@@ -844,7 +846,7 @@ rtl_TextEncoding osl_getTextEncodingFromLocale( rtl_Locale * pLocale )
void _imp_getProcessLocale( rtl_Locale ** ppLocale ) void _imp_getProcessLocale( rtl_Locale ** ppLocale )
{ {
static char *locale = NULL; static char const *locale = NULL;
/* basic thread safeness */ /* basic thread safeness */
// pthread_mutex_lock( &aLocalMutex ); // pthread_mutex_lock( &aLocalMutex );
...@@ -852,12 +854,16 @@ void _imp_getProcessLocale( rtl_Locale ** ppLocale ) ...@@ -852,12 +854,16 @@ void _imp_getProcessLocale( rtl_Locale ** ppLocale )
/* Only fetch the locale once and cache it */ /* Only fetch the locale once and cache it */
if ( NULL == locale ) if ( NULL == locale )
{ {
rtl::OUString loc16(macosx_getLocale());
locale = (char *)malloc( 128 ); rtl::OString loc8;
if ( locale ) if (loc16.convertToString(
macosx_getLocale( locale, 128 ); &loc8, RTL_TEXTENCODING_UTF8,
else (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
fprintf( stderr, "nlsupport.c: locale allocation returned NULL!\n" ); | 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 */ /* handle the case where OS specific method of finding locale fails */
...@@ -873,7 +879,7 @@ void _imp_getProcessLocale( rtl_Locale ** ppLocale ) ...@@ -873,7 +879,7 @@ void _imp_getProcessLocale( rtl_Locale ** ppLocale )
locale = getenv( "LANG" ); locale = getenv( "LANG" );
if( NULL == locale ) if( NULL == locale )
locale = strdup("C"); locale = "C";
} }
/* return the locale */ /* return the locale */
......
...@@ -24,11 +24,13 @@ ...@@ -24,11 +24,13 @@
#include <rtl/locale.h> #include <rtl/locale.h>
namespace rtl { class OUString; }
void _imp_getProcessLocale( rtl_Locale ** ); void _imp_getProcessLocale( rtl_Locale ** );
int _imp_setProcessLocale( rtl_Locale * ); int _imp_setProcessLocale( rtl_Locale * );
#if defined IOS || defined MACOSX #if defined IOS || defined MACOSX
void macosx_getLocale(char *locale, sal_uInt32 bufferLen); rtl::OUString macosx_getLocale();
#endif #endif
#endif #endif
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
#include <postmac.h> #include <postmac.h>
#include <rtl/ustrbuf.hxx>
#include <nlsupport.hxx> #include <nlsupport.hxx>
namespace namespace
...@@ -59,11 +61,17 @@ namespace ...@@ -59,11 +61,17 @@ namespace
return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref); 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. /** Grab current locale from system.
*/ */
void macosx_getLocale(char *locale, sal_uInt32 bufferLen) rtl::OUString macosx_getLocale()
{ {
CFStringRef sref = getProcessLocale(); CFStringRef sref = getProcessLocale();
CFStringGuard sGuard(sref); CFStringGuard sGuard(sref);
...@@ -75,22 +83,21 @@ void macosx_getLocale(char *locale, sal_uInt32 bufferLen) ...@@ -75,22 +83,21 @@ void macosx_getLocale(char *locale, sal_uInt32 bufferLen)
CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("-")); CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("-"));
CFArrayGuard arrGuard(subs); CFArrayGuard arrGuard(subs);
CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0); rtl::OUStringBuffer buf;
CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII); append(buf, (CFStringRef)CFArrayGetValueAtIndex(subs, 0));
// country also available? Assumption: if the array contains more than one // country also available? Assumption: if the array contains more than one
// value the second value is always the country! // value the second value is always the country!
if (CFArrayGetCount(subs) > 1) if (CFArrayGetCount(subs) > 1)
{ {
strlcat(locale, "_", bufferLen - strlen(locale)); buf.append("_");
append(buf, (CFStringRef)CFArrayGetValueAtIndex(subs, 1));
CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1);
CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII);
} }
// Append 'UTF-8' to the locale because the Mac OS X file // Append 'UTF-8' to the locale because the Mac OS X file
// system interface is UTF-8 based and sal tries to determine // system interface is UTF-8 based and sal tries to determine
// the file system locale from the locale information // 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: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "sal/types.h" #include "sal/types.h"
#include <algorithm> #include <algorithm>
#include <cassert>
#include <float.h> #include <float.h>
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
...@@ -256,6 +257,7 @@ struct UStringTraits ...@@ -256,6 +257,7 @@ struct UStringTraits
sal_Int32 * pCapacity, sal_Int32 * pOffset, sal_Int32 * pCapacity, sal_Int32 * pOffset,
sal_Unicode const * pChars, sal_Int32 nLen) sal_Unicode const * pChars, sal_Int32 nLen)
{ {
assert(pChars != nullptr);
rtl_uStringbuffer_insert(pBuffer, pCapacity, *pOffset, pChars, nLen); rtl_uStringbuffer_insert(pBuffer, pCapacity, *pOffset, pChars, nLen);
*pOffset += nLen; *pOffset += nLen;
} }
......
...@@ -140,11 +140,14 @@ void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This, ...@@ -140,11 +140,14 @@ void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) ); memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
/* insert the new characters */ /* insert the new characters */
if( len == 1 ) if( str != nullptr )
/* optimized for 1 character */ {
pBuf[offset] = *str; if( len == 1 )
else if( len > 1 ) /* optimized for 1 character */
memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) ); pBuf[offset] = *str;
else if( len > 1 )
memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) );
}
(*This)->length = nOldLen + len; (*This)->length = nOldLen + len;
pBuf[ nOldLen + len ] = 0; 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