Rewrite providing stubs for class without ICU

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@30856 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2010-06-25 16:42:09 +00:00
parent 48a9a7cfab
commit 0bc859e03e

View file

@ -71,12 +71,31 @@ NSString * const NSPersianCalendar = @"NSPersianCalendar";
NSString * const NSIndianCalendar = @"NSIndianCalendar"; NSString * const NSIndianCalendar = @"NSIndianCalendar";
NSString * const NSISO8601Calendar = @"NSISO8601Calendar"; NSString * const NSISO8601Calendar = @"NSISO8601Calendar";
static NSLocale *autoupdatingLocale = nil;
static NSLocale *currentLocale = nil;
static NSLocale *systemLocale = nil;
static NSMutableDictionary *allLocales = nil;
static NSRecursiveLock *classLock = nil;
#if !defined(HAVE_UCI)
# if HAVE_UNICODE_ULOC_H
# define HAVE_UCI 1
# else
# define HAVE_UCI 0
# endif
#endif
#if HAVE_UNICODE_ULOC_H #if HAVE_UNICODE_ULOC_H
# include <unicode/uloc.h>
#endif
#if HAVE_UNICODE_ULOCDATA_H
# include <unicode/ulocdata.h>
#endif
#if HAVE_UNICODE_UCURR_H
# include <unicode/ucurr.h>
#endif
#include <unicode/uloc.h> #if HAVE_UCI
#include <unicode/ulocdata.h>
#include <unicode/ucurr.h>
// //
// ICU Component Keywords // ICU Component Keywords
// //
@ -129,13 +148,7 @@ static NSArray *_currencyCodesWithType (uint32_t currType)
RELEASE (currencies); RELEASE (currencies);
return result; return result;
} }
#endif
static NSLocale *autoupdatingLocale = nil;
static NSLocale *currentLocale = nil;
static NSLocale *systemLocale = nil;
static NSMutableDictionary *allLocales = nil;
static NSRecursiveLock *classLock = nil;
@implementation NSLocale @implementation NSLocale
@ -164,20 +177,33 @@ static NSRecursiveLock *classLock = nil;
+ (NSArray *) availableLocaleIdentifiers + (NSArray *) availableLocaleIdentifiers
{ {
NSArray *result; static NSArray *available = nil;
NSMutableArray *available = [[NSMutableArray alloc] initWithCapacity: 10];
int32_t i; #if HAVE_ICU
int32_t count = uloc_countAvailable (); if (nil == available)
for (i = 1 ; i <= count ; ++i)
{ {
const char *localeID = uloc_getAvailable (i); [classLock lock];
[available addObject: [NSString stringWithCString: localeID]]; if (nil == available)
} {
NSMutableArray *array;
int32_t i;
int32_t count = uloc_countAvailable ();
array = [[NSMutableArray alloc] initWithCapacity: count];
result = [NSArray arrayWithArray: available]; for (i = 1; i <= count; ++i)
RELEASE(available); {
return result; const char *localeID = uloc_getAvailable (i);
[array addObject: [NSString stringWithCString: localeID]];
}
available = [[NSArray alloc] initWithArray: array];
[array release];
}
[classLock unlock];
}
#endif
return [[available copy] autorelease];
} }
+ (NSString *) canonicalLanguageIdentifierFromString: (NSString *) string + (NSString *) canonicalLanguageIdentifierFromString: (NSString *) string
@ -195,6 +221,7 @@ static NSRecursiveLock *classLock = nil;
+ (NSLocaleLanguageDirection) characterDirectionForLanguage: + (NSLocaleLanguageDirection) characterDirectionForLanguage:
(NSString *)isoLangCode (NSString *)isoLangCode
{ {
#if HAVE_ICU
ULayoutType result; ULayoutType result;
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
@ -203,10 +230,14 @@ static NSRecursiveLock *classLock = nil;
return NSLocaleLanguageDirectionUnknown; return NSLocaleLanguageDirectionUnknown;
return _ICUToNSLocaleOrientation (result); return _ICUToNSLocaleOrientation (result);
#else
return NSLocaleLanguageDirectionLeftToRight; // FIXME
#endif
} }
+ (NSDictionary *) componentsFromLocaleIdentifier: (NSString *) string + (NSDictionary *) componentsFromLocaleIdentifier: (NSString *) string
{ {
#if HAVE_ICU
char buffer[ULOC_LANG_CAPACITY]; char buffer[ULOC_LANG_CAPACITY];
int32_t strLength; int32_t strLength;
UErrorCode error = U_ZERO_ERROR; UErrorCode error = U_ZERO_ERROR;
@ -253,6 +284,9 @@ static NSRecursiveLock *classLock = nil;
result = [NSDictionary dictionaryWithDictionary: tmpDict]; result = [NSDictionary dictionaryWithDictionary: tmpDict];
RELEASE(tmpDict); RELEASE(tmpDict);
return result; return result;
#else
return nil; // FIXME
#endif
} }
+ (id) currentLocale + (id) currentLocale
@ -262,59 +296,90 @@ static NSRecursiveLock *classLock = nil;
[classLock lock]; [classLock lock];
if (nil == currentLocale) if (nil == currentLocale)
{ {
#if HAVE_ICU
const char *cLocaleId = uloc_getDefault (); const char *cLocaleId = uloc_getDefault ();
NSString *localeId = [NSString stringWithCString: cLocaleId]; NSString *localeId = [NSString stringWithCString: cLocaleId];
currentLocale = [[NSLocale alloc] initWithLocaleIdentifier: localeId]; currentLocale = [[NSLocale alloc] initWithLocaleIdentifier: localeId];
#endif
} }
result = RETAIN(currentLocale); result = RETAIN(currentLocale);
[classLock unlock]; [classLock unlock];
return AUTORELEASE(result); return AUTORELEASE(result);
} }
+ (NSArray *) commonISOCurrencyCodes + (NSArray *) commonISOCurrencyCodes
{ {
#if HAVE_ICU
return _currencyCodesWithType (UCURR_COMMON | UCURR_NON_DEPRECATED); return _currencyCodesWithType (UCURR_COMMON | UCURR_NON_DEPRECATED);
#else
return nil; // FIXME
#endif
} }
+ (NSArray *) ISOCurrencyCodes + (NSArray *) ISOCurrencyCodes
{ {
#if HAVE_ICU
return _currencyCodesWithType (UCURR_ALL); return _currencyCodesWithType (UCURR_ALL);
#else
return nil; // FIXME
#endif
} }
+ (NSArray *) ISOCountryCodes + (NSArray *) ISOCountryCodes
{ {
NSArray *result; static NSArray *countries = nil;
NSMutableArray *countries = [[NSMutableArray alloc] initWithCapacity: 10];
const char *const *codes = uloc_getISOCountries (); if (nil == countries)
while (codes != NULL)
{ {
[countries addObject: [NSString stringWithCString: *codes]]; [classLock lock];
++codes; if (nil == countries)
{
#if HAVE_ICU
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 10];
const char *const *codes = uloc_getISOCountries ();
while (codes != NULL)
{
[array addObject: [NSString stringWithCString: *codes]];
++codes;
}
countries = [[NSArray alloc] initWithArray: array];
[array release];
#endif
}
} }
result = [NSArray arrayWithArray: countries]; return [[countries copy] autorelease];
RELEASE(countries);
return result;
} }
+ (NSArray *) ISOLanguageCodes + (NSArray *) ISOLanguageCodes
{ {
NSArray *result; static NSArray *languages = nil;
NSMutableArray *languages = [[NSMutableArray alloc] initWithCapacity: 10];
const char *const *codes = uloc_getISOCountries (); if (nil == languages)
while (codes != NULL)
{ {
[languages addObject: [NSString stringWithCString: *codes]]; [classLock lock];
++codes; if (nil == languages)
{
#if HAVE_ICU
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 10];
const char *const *codes = uloc_getISOCountries ();
while (codes != NULL)
{
[array addObject: [NSString stringWithCString: *codes]];
++codes;
}
languages = [[NSArray alloc] initWithArray: array];
[array release];
#endif
}
} }
result = [NSArray arrayWithArray: languages]; return [[languages copy] autorelease];
RELEASE(languages);
return result;
} }
+ (NSLocaleLanguageDirection) lineDirectionForLanguage: (NSString *) isoLangCode + (NSLocaleLanguageDirection) lineDirectionForLanguage: (NSString *) isoLangCode
{ {
#if HAVE_ICU
ULayoutType result; ULayoutType result;
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
@ -323,6 +388,9 @@ static NSRecursiveLock *classLock = nil;
return NSLocaleLanguageDirectionUnknown; return NSLocaleLanguageDirectionUnknown;
return _ICUToNSLocaleOrientation (result); return _ICUToNSLocaleOrientation (result);
#else
return NSLocaleLanguageDirectionTopToBottom; // FIXME
#endif
} }
+ (NSArray *) preferredLanguages + (NSArray *) preferredLanguages
@ -348,6 +416,7 @@ static NSRecursiveLock *classLock = nil;
+ (NSString *) localeIdentifierFromComponents: (NSDictionary *) dict + (NSString *) localeIdentifierFromComponents: (NSDictionary *) dict
{ {
#if HAVE_ICU
char buffer[ULOC_FULLNAME_CAPACITY]; char buffer[ULOC_FULLNAME_CAPACITY];
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
const char *language = [[dict objectForKey: NSLocaleLanguageCode] cString]; const char *language = [[dict objectForKey: NSLocaleLanguageCode] cString];
@ -375,10 +444,14 @@ static NSRecursiveLock *classLock = nil;
} }
return [NSString stringWithCString: buffer]; return [NSString stringWithCString: buffer];
#else
return nil; // FIXME
#endif
} }
+ (NSString *) localeIdentifierFromWindowsLocaleCode: (uint32_t) lcid + (NSString *) localeIdentifierFromWindowsLocaleCode: (uint32_t) lcid
{ {
#if HAVE_ICU
char buffer[ULOC_FULLNAME_CAPACITY]; char buffer[ULOC_FULLNAME_CAPACITY];
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
@ -388,15 +461,23 @@ static NSRecursiveLock *classLock = nil;
return nil; return nil;
return [NSString stringWithCString: buffer length: (NSUInteger)length]; return [NSString stringWithCString: buffer length: (NSUInteger)length];
#else
return nil; // FIXME
#endif
} }
+ (uint32_t) windowsLocaleCodeFromLocaleIdentifier: (NSString *)localeIdentifier + (uint32_t) windowsLocaleCodeFromLocaleIdentifier: (NSString *)localeIdentifier
{ {
#if HAVE_ICU
return uloc_getLCID ([localeIdentifier cString]); return uloc_getLCID ([localeIdentifier cString]);
#else
return 0; // FIXME
#endif
} }
- (NSString *) displayNameForKey: (id) key value: (id) value - (NSString *) displayNameForKey: (id) key value: (id) value
{ {
#if HAVE_ICU
int32_t length; int32_t length;
unichar buffer[ULOC_FULLNAME_CAPACITY]; unichar buffer[ULOC_FULLNAME_CAPACITY];
UErrorCode status; UErrorCode status;
@ -409,39 +490,49 @@ static NSRecursiveLock *classLock = nil;
return nil; return nil;
return [NSString stringWithCharacters: buffer length: (NSUInteger)length]; return [NSString stringWithCharacters: buffer length: (NSUInteger)length];
#else
return nil; // FIXME
#endif
} }
- (id) initWithLocaleIdentifier: (NSString *) string - (id) initWithLocaleIdentifier: (NSString*)string
{ {
NSLocale *newLocale; NSLocale *newLocale;
NSString *localeId; NSString *localeId;
int32_t length; #if HAVE_ICU
int32_t length;
char cLocaleId[ULOC_FULLNAME_CAPACITY]; char cLocaleId[ULOC_FULLNAME_CAPACITY];
UErrorCode error = U_ZERO_ERROR; UErrorCode error = U_ZERO_ERROR;
length = uloc_canonicalize ([string cString], cLocaleId, length = uloc_canonicalize ([string cString], cLocaleId,
ULOC_FULLNAME_CAPACITY, &error); ULOC_FULLNAME_CAPACITY, &error);
if (U_FAILURE(error)) if (U_FAILURE(error))
return nil;
localeId = [[NSString alloc] initWithCString: cLocaleId length: length];
if (nil == allLocales)
{ {
newLocale = [allLocales objectForKey: localeId]; [self release];
if (newLocale) return nil;
{
RELEASE(self);
return newLocale;
}
} }
else localeId = [NSString stringWithCString: cLocaleId length: length];
#else
localeId = string;
#endif
[classLock lock];
if (nil == allLocales)
{ {
allLocales = [[NSMutableDictionary alloc] initWithCapacity: 0]; allLocales = [[NSMutableDictionary alloc] initWithCapacity: 0];
} }
newLocale = [allLocales objectForKey: localeId];
_localeId = localeId; if (nil == newLocale)
_components = [[NSMutableDictionary alloc] initWithCapacity: 0]; {
[allLocales setObject: self forKey: localeId]; _localeId = [localeId copy];
[allLocales setObject: self forKey: localeId];
}
else
{
[self release];
self = [newLocale retain];
}
[classLock unlock];
return self; return self;
} }
@ -462,12 +553,15 @@ static NSRecursiveLock *classLock = nil;
if ((result = [_components objectForKey: key])) if ((result = [_components objectForKey: key]))
return result; return result;
[_components addEntriesFromDictionary: if ([_components count] == 0)
[NSLocale componentsFromLocaleIdentifier: [self localeIdentifier]]]; {
if ((result = [_components objectForKey: key])) [_components addEntriesFromDictionary:
return result; [NSLocale componentsFromLocaleIdentifier: [self localeIdentifier]]];
if ((result = [_components objectForKey: key]))
return result;
}
// FIXME: look up other keywords with uloc_getKeywordValue(). // FIXME: look up other keywords with uloc_getKeywordValue().
return nil; return nil;
} }
@ -501,5 +595,3 @@ static NSRecursiveLock *classLock = nil;
@end @end
#else
#endif