]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/uscript.c
ICU-6.2.8.tar.gz
[apple/icu.git] / icuSources / common / uscript.c
1 /*
2 **********************************************************************
3 * Copyright (C) 1997-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 *
7 * File USCRIPT.C
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 07/06/2001 Ram Creation.
13 ******************************************************************************
14 */
15
16 #include "unicode/uscript.h"
17 #include "unicode/ures.h"
18 #include "unicode/uchar.h"
19 #include "unicode/putil.h"
20 #include "uprops.h"
21 #include "cmemory.h"
22 #include "cstring.h"
23
24 static const char kLocaleScript[] = "LocaleScript";
25
26 U_CAPI int32_t U_EXPORT2
27 uscript_getCode(const char* nameOrAbbrOrLocale,
28 UScriptCode* fillIn,
29 int32_t capacity,
30 UErrorCode* err){
31
32 UScriptCode code = USCRIPT_INVALID_CODE;
33 int32_t numFilled=0;
34 int32_t len=0;
35 /* check arguments */
36 if(err==NULL ||U_FAILURE(*err)){
37 return numFilled;
38 }
39 if(nameOrAbbrOrLocale==NULL || fillIn == NULL || capacity<0){
40 *err = U_ILLEGAL_ARGUMENT_ERROR;
41 return numFilled;
42 }
43
44 /* try long and abbreviated script names first */
45 code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
46
47 /* we still haven't found it try locale */
48 if(code==(UScriptCode)UCHAR_INVALID_CODE){
49 /* Do not propagate error codes from just not finding a locale bundle. */
50 UErrorCode localErrorCode = U_ZERO_ERROR;
51 UResourceBundle* resB = ures_open(NULL,nameOrAbbrOrLocale,&localErrorCode);
52 if(U_SUCCESS(localErrorCode)&& localErrorCode != U_USING_DEFAULT_WARNING){
53 UResourceBundle* resD = ures_getByKey(resB,kLocaleScript,NULL,&localErrorCode);
54 if(U_SUCCESS(localErrorCode) ){
55 len =0;
56 while(ures_hasNext(resD)){
57 const UChar* name = ures_getNextString(resD,&len,NULL,&localErrorCode);
58 if(U_SUCCESS(localErrorCode)){
59 char cName[50] = {'\0'};
60 u_UCharsToChars(name,cName,len);
61 code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, cName);
62 /* got the script code now fill in the buffer */
63 if(numFilled<=capacity){
64 *(fillIn)++=code;
65 numFilled++;
66 }else{
67 ures_close(resD);
68 ures_close(resB);
69 *err=U_BUFFER_OVERFLOW_ERROR;
70 return len;
71 }
72 }
73 }
74 }
75 ures_close(resD);
76
77 }
78 ures_close(resB);
79 }else{
80 /* we found it */
81 if(numFilled<=capacity){
82 *(fillIn)++=code;
83 numFilled++;
84 }else{
85 *err=U_BUFFER_OVERFLOW_ERROR;
86 return len;
87 }
88 }
89 return numFilled;
90 }
91
92 U_CAPI const char* U_EXPORT2
93 uscript_getName(UScriptCode scriptCode){
94 return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode,
95 U_LONG_PROPERTY_NAME);
96 }
97
98 U_CAPI const char* U_EXPORT2
99 uscript_getShortName(UScriptCode scriptCode){
100 return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode,
101 U_SHORT_PROPERTY_NAME);
102 }
103