1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/uscript.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +/* 1.5 +********************************************************************** 1.6 +* Copyright (C) 1997-2011, International Business Machines 1.7 +* Corporation and others. All Rights Reserved. 1.8 +********************************************************************** 1.9 +* 1.10 +* File USCRIPT.C 1.11 +* 1.12 +* Modification History: 1.13 +* 1.14 +* Date Name Description 1.15 +* 07/06/2001 Ram Creation. 1.16 +****************************************************************************** 1.17 +*/ 1.18 + 1.19 +#include "unicode/uscript.h" 1.20 +#include "unicode/ures.h" 1.21 +#include "unicode/uchar.h" 1.22 +#include "unicode/putil.h" 1.23 +#include "uprops.h" 1.24 +#include "cmemory.h" 1.25 +#include "cstring.h" 1.26 + 1.27 +static const char kLocaleScript[] = "LocaleScript"; 1.28 + 1.29 +/* TODO: this is a bad API should be deprecated */ 1.30 +U_CAPI int32_t U_EXPORT2 1.31 +uscript_getCode(const char* nameOrAbbrOrLocale, 1.32 + UScriptCode* fillIn, 1.33 + int32_t capacity, 1.34 + UErrorCode* err){ 1.35 + 1.36 + UScriptCode code = USCRIPT_INVALID_CODE; 1.37 + int32_t numFilled=0; 1.38 + int32_t len=0; 1.39 + /* check arguments */ 1.40 + if(err==NULL ||U_FAILURE(*err)){ 1.41 + return numFilled; 1.42 + } 1.43 + if(nameOrAbbrOrLocale==NULL || fillIn == NULL || capacity<0){ 1.44 + *err = U_ILLEGAL_ARGUMENT_ERROR; 1.45 + return numFilled; 1.46 + } 1.47 + 1.48 + if(uprv_strchr(nameOrAbbrOrLocale, '-')==NULL && uprv_strchr(nameOrAbbrOrLocale, '_')==NULL ){ 1.49 + /* try long and abbreviated script names first */ 1.50 + code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale); 1.51 + 1.52 + } 1.53 + if(code==(UScriptCode)UCHAR_INVALID_CODE){ 1.54 + /* Do not propagate error codes from just not finding a locale bundle. */ 1.55 + UErrorCode localErrorCode = U_ZERO_ERROR; 1.56 + UResourceBundle* resB = ures_open(NULL,nameOrAbbrOrLocale,&localErrorCode); 1.57 + if(U_SUCCESS(localErrorCode)&& localErrorCode != U_USING_DEFAULT_WARNING){ 1.58 + UResourceBundle* resD = ures_getByKey(resB,kLocaleScript,NULL,&localErrorCode); 1.59 + if(U_SUCCESS(localErrorCode) ){ 1.60 + len =0; 1.61 + while(ures_hasNext(resD)){ 1.62 + const UChar* name = ures_getNextString(resD,&len,NULL,&localErrorCode); 1.63 + if(U_SUCCESS(localErrorCode)){ 1.64 + char cName[50] = {'\0'}; 1.65 + u_UCharsToChars(name,cName,len); 1.66 + code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, cName); 1.67 + /* got the script code now fill in the buffer */ 1.68 + if(numFilled<capacity){ 1.69 + *(fillIn)++=code; 1.70 + numFilled++; 1.71 + }else{ 1.72 + ures_close(resD); 1.73 + ures_close(resB); 1.74 + *err=U_BUFFER_OVERFLOW_ERROR; 1.75 + return len; 1.76 + } 1.77 + } 1.78 + } 1.79 + } 1.80 + ures_close(resD); 1.81 + } 1.82 + ures_close(resB); 1.83 + code = USCRIPT_INVALID_CODE; 1.84 + } 1.85 + if(code==(UScriptCode)UCHAR_INVALID_CODE){ 1.86 + /* still not found .. try long and abbreviated script names again */ 1.87 + code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale); 1.88 + } 1.89 + if(code!=(UScriptCode)UCHAR_INVALID_CODE){ 1.90 + /* we found it */ 1.91 + if(numFilled<capacity){ 1.92 + *(fillIn)++=code; 1.93 + numFilled++; 1.94 + }else{ 1.95 + *err=U_BUFFER_OVERFLOW_ERROR; 1.96 + return len; 1.97 + } 1.98 + } 1.99 + return numFilled; 1.100 +}