1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 327f654740f2a26ad62a5c155af9199af9e69b889claireho* Copyright (C) 1997-2010, International Business Machines 4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Corporation and others. All Rights Reserved. 5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* File USCRIPT.C 8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Modification History: 10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Date Name Description 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 07/06/2001 Ram Creation. 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru****************************************************************************** 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/ 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uscript.h" 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ures.h" 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uchar.h" 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/putil.h" 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "uprops.h" 21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cmemory.h" 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cstring.h" 23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const char kLocaleScript[] = "LocaleScript"; 25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* TODO: this is a bad API should be deprecated */ 27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuscript_getCode(const char* nameOrAbbrOrLocale, 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UScriptCode* fillIn, 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t capacity, 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode* err){ 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UScriptCode code = USCRIPT_INVALID_CODE; 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t numFilled=0; 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t len=0; 36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* check arguments */ 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(err==NULL ||U_FAILURE(*err)){ 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return numFilled; 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(nameOrAbbrOrLocale==NULL || fillIn == NULL || capacity<0){ 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *err = U_ILLEGAL_ARGUMENT_ERROR; 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return numFilled; 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 4585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if(uprv_strchr(nameOrAbbrOrLocale, '-')==NULL && uprv_strchr(nameOrAbbrOrLocale, '_')==NULL ){ 46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* try long and abbreviated script names first */ 47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale); 48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(code==(UScriptCode)UCHAR_INVALID_CODE){ 51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* Do not propagate error codes from just not finding a locale bundle. */ 52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode localErrorCode = U_ZERO_ERROR; 53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UResourceBundle* resB = ures_open(NULL,nameOrAbbrOrLocale,&localErrorCode); 54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(U_SUCCESS(localErrorCode)&& localErrorCode != U_USING_DEFAULT_WARNING){ 55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UResourceBundle* resD = ures_getByKey(resB,kLocaleScript,NULL,&localErrorCode); 56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(U_SUCCESS(localErrorCode) ){ 57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru len =0; 58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while(ures_hasNext(resD)){ 59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UChar* name = ures_getNextString(resD,&len,NULL,&localErrorCode); 60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(U_SUCCESS(localErrorCode)){ 61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru char cName[50] = {'\0'}; 62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru u_UCharsToChars(name,cName,len); 63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, cName); 64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* got the script code now fill in the buffer */ 6527f654740f2a26ad62a5c155af9199af9e69b889claireho if(numFilled<capacity){ 66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *(fillIn)++=code; 67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru numFilled++; 68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru }else{ 69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ures_close(resD); 70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ures_close(resB); 71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *err=U_BUFFER_OVERFLOW_ERROR; 72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return len; 73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ures_close(resD); 78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ures_close(resB); 80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru code = USCRIPT_INVALID_CODE; 81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(code==(UScriptCode)UCHAR_INVALID_CODE){ 83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* still not found .. try long and abbreviated script names again */ 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale); 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(code!=(UScriptCode)UCHAR_INVALID_CODE){ 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* we found it */ 8827f654740f2a26ad62a5c155af9199af9e69b889claireho if(numFilled<capacity){ 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *(fillIn)++=code; 90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru numFilled++; 91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru }else{ 92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *err=U_BUFFER_OVERFLOW_ERROR; 93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return len; 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return numFilled; 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI const char* U_EXPORT2 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuscript_getName(UScriptCode scriptCode){ 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode, 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru U_LONG_PROPERTY_NAME); 103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI const char* U_EXPORT2 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuscript_getShortName(UScriptCode scriptCode){ 107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode, 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru U_SHORT_PROPERTY_NAME); 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 111