2016-06-15 18:58:17 +00:00
|
|
|
// Copyright (C) 2016 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2001-07-10 18:33:40 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1997-2014, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2001-07-10 18:33:40 +00:00
|
|
|
**********************************************************************
|
|
|
|
*
|
|
|
|
* File USCRIPT.C
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 07/06/2001 Ram Creation.
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2002-10-30 18:25:27 +00:00
|
|
|
#include "unicode/uchar.h"
|
2014-08-28 23:51:38 +00:00
|
|
|
#include "unicode/uscript.h"
|
|
|
|
#include "unicode/uloc.h"
|
2002-05-13 21:50:59 +00:00
|
|
|
#include "cmemory.h"
|
2001-09-11 05:00:34 +00:00
|
|
|
#include "cstring.h"
|
2001-07-10 18:33:40 +00:00
|
|
|
|
2014-08-28 23:51:38 +00:00
|
|
|
static const UScriptCode JAPANESE[3] = { USCRIPT_KATAKANA, USCRIPT_HIRAGANA, USCRIPT_HAN };
|
|
|
|
static const UScriptCode KOREAN[2] = { USCRIPT_HANGUL, USCRIPT_HAN };
|
|
|
|
static const UScriptCode HAN_BOPO[2] = { USCRIPT_HAN, USCRIPT_BOPOMOFO };
|
|
|
|
|
|
|
|
static int32_t
|
|
|
|
setCodes(const UScriptCode *src, int32_t length,
|
|
|
|
UScriptCode *dest, int32_t capacity, UErrorCode *err) {
|
|
|
|
int32_t i;
|
|
|
|
if(U_FAILURE(*err)) { return 0; }
|
|
|
|
if(length > capacity) {
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
for(i = 0; i < length; ++i) {
|
|
|
|
dest[i] = src[i];
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t
|
|
|
|
setOneCode(UScriptCode script, UScriptCode *scripts, int32_t capacity, UErrorCode *err) {
|
|
|
|
if(U_FAILURE(*err)) { return 0; }
|
|
|
|
if(1 > capacity) {
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
scripts[0] = script;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t
|
|
|
|
getCodesFromLocale(const char *locale,
|
|
|
|
UScriptCode *scripts, int32_t capacity, UErrorCode *err) {
|
|
|
|
UErrorCode internalErrorCode = U_ZERO_ERROR;
|
|
|
|
char lang[8];
|
|
|
|
char script[8];
|
2014-09-11 05:25:13 +00:00
|
|
|
int32_t scriptLength;
|
2014-08-28 23:51:38 +00:00
|
|
|
if(U_FAILURE(*err)) { return 0; }
|
|
|
|
// Multi-script languages, equivalent to the LocaleScript data
|
|
|
|
// that we used to load from locale resource bundles.
|
2014-09-11 05:25:13 +00:00
|
|
|
/*length = */ uloc_getLanguage(locale, lang, UPRV_LENGTHOF(lang), &internalErrorCode);
|
2014-08-28 23:51:38 +00:00
|
|
|
if(U_FAILURE(internalErrorCode) || internalErrorCode == U_STRING_NOT_TERMINATED_WARNING) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(0 == uprv_strcmp(lang, "ja")) {
|
|
|
|
return setCodes(JAPANESE, UPRV_LENGTHOF(JAPANESE), scripts, capacity, err);
|
|
|
|
}
|
|
|
|
if(0 == uprv_strcmp(lang, "ko")) {
|
|
|
|
return setCodes(KOREAN, UPRV_LENGTHOF(KOREAN), scripts, capacity, err);
|
|
|
|
}
|
|
|
|
scriptLength = uloc_getScript(locale, script, UPRV_LENGTHOF(script), &internalErrorCode);
|
|
|
|
if(U_FAILURE(internalErrorCode) || internalErrorCode == U_STRING_NOT_TERMINATED_WARNING) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(0 == uprv_strcmp(lang, "zh") && 0 == uprv_strcmp(script, "Hant")) {
|
|
|
|
return setCodes(HAN_BOPO, UPRV_LENGTHOF(HAN_BOPO), scripts, capacity, err);
|
|
|
|
}
|
|
|
|
// Explicit script code.
|
|
|
|
if(scriptLength != 0) {
|
|
|
|
UScriptCode scriptCode = (UScriptCode)u_getPropertyValueEnum(UCHAR_SCRIPT, script);
|
|
|
|
if(scriptCode != USCRIPT_INVALID_CODE) {
|
|
|
|
if(scriptCode == USCRIPT_SIMPLIFIED_HAN || scriptCode == USCRIPT_TRADITIONAL_HAN) {
|
|
|
|
scriptCode = USCRIPT_HAN;
|
|
|
|
}
|
|
|
|
return setOneCode(scriptCode, scripts, capacity, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-07 00:54:02 +00:00
|
|
|
|
2014-08-28 23:51:38 +00:00
|
|
|
/* TODO: this is a bad API and should be deprecated, ticket #11141 */
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-10-23 02:00:50 +00:00
|
|
|
uscript_getCode(const char* nameOrAbbrOrLocale,
|
|
|
|
UScriptCode* fillIn,
|
|
|
|
int32_t capacity,
|
|
|
|
UErrorCode* err){
|
2014-08-29 03:41:29 +00:00
|
|
|
UBool triedCode;
|
|
|
|
char likely[ULOC_FULLNAME_CAPACITY];
|
|
|
|
UErrorCode internalErrorCode;
|
|
|
|
int32_t length;
|
|
|
|
|
2014-08-28 23:51:38 +00:00
|
|
|
if(U_FAILURE(*err)) {
|
|
|
|
return 0;
|
2002-04-05 02:49:56 +00:00
|
|
|
}
|
2014-08-28 23:51:38 +00:00
|
|
|
if(nameOrAbbrOrLocale==NULL ||
|
|
|
|
(fillIn == NULL ? capacity != 0 : capacity < 0)) {
|
2002-04-05 02:49:56 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
2014-08-28 23:51:38 +00:00
|
|
|
return 0;
|
2001-07-10 18:33:40 +00:00
|
|
|
}
|
2002-10-30 18:25:27 +00:00
|
|
|
|
2014-08-29 03:41:29 +00:00
|
|
|
triedCode = FALSE;
|
2008-03-14 06:41:45 +00:00
|
|
|
if(uprv_strchr(nameOrAbbrOrLocale, '-')==NULL && uprv_strchr(nameOrAbbrOrLocale, '_')==NULL ){
|
2006-02-16 22:49:34 +00:00
|
|
|
/* try long and abbreviated script names first */
|
2014-08-28 23:51:38 +00:00
|
|
|
UScriptCode code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
|
|
|
|
if(code!=USCRIPT_INVALID_CODE) {
|
|
|
|
return setOneCode(code, fillIn, capacity, err);
|
2001-07-10 18:33:40 +00:00
|
|
|
}
|
2014-08-28 23:51:38 +00:00
|
|
|
triedCode = TRUE;
|
|
|
|
}
|
2014-08-29 03:41:29 +00:00
|
|
|
internalErrorCode = U_ZERO_ERROR;
|
|
|
|
length = getCodesFromLocale(nameOrAbbrOrLocale, fillIn, capacity, err);
|
2014-08-28 23:51:38 +00:00
|
|
|
if(U_FAILURE(*err) || length != 0) {
|
|
|
|
return length;
|
2006-02-16 22:49:34 +00:00
|
|
|
}
|
2014-08-28 23:51:38 +00:00
|
|
|
(void)uloc_addLikelySubtags(nameOrAbbrOrLocale,
|
|
|
|
likely, UPRV_LENGTHOF(likely), &internalErrorCode);
|
|
|
|
if(U_SUCCESS(internalErrorCode) && internalErrorCode != U_STRING_NOT_TERMINATED_WARNING) {
|
|
|
|
length = getCodesFromLocale(likely, fillIn, capacity, err);
|
|
|
|
if(U_FAILURE(*err) || length != 0) {
|
|
|
|
return length;
|
|
|
|
}
|
2006-02-16 22:49:34 +00:00
|
|
|
}
|
2014-08-28 23:51:38 +00:00
|
|
|
if(!triedCode) {
|
|
|
|
/* still not found .. try long and abbreviated script names again */
|
|
|
|
UScriptCode code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
|
|
|
|
if(code!=USCRIPT_INVALID_CODE) {
|
|
|
|
return setOneCode(code, fillIn, capacity, err);
|
2001-10-23 02:00:50 +00:00
|
|
|
}
|
2001-07-10 18:33:40 +00:00
|
|
|
}
|
2014-08-28 23:51:38 +00:00
|
|
|
return 0;
|
2001-07-10 18:33:40 +00:00
|
|
|
}
|