]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/uscript.c
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / common / uscript.c
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
46f4442e 3* Copyright (C) 1997-2008, International Business Machines
b75a7d8f
A
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"
374ca955 19#include "unicode/putil.h"
b75a7d8f
A
20#include "uprops.h"
21#include "cmemory.h"
22#include "cstring.h"
23
24static const char kLocaleScript[] = "LocaleScript";
25
73c04bcf 26/* TODO: this is a bad API should be deprecated */
b75a7d8f
A
27U_CAPI int32_t U_EXPORT2
28uscript_getCode(const char* nameOrAbbrOrLocale,
29 UScriptCode* fillIn,
30 int32_t capacity,
31 UErrorCode* err){
32
33 UScriptCode code = USCRIPT_INVALID_CODE;
34 int32_t numFilled=0;
35 int32_t len=0;
36 /* check arguments */
37 if(err==NULL ||U_FAILURE(*err)){
38 return numFilled;
39 }
40 if(nameOrAbbrOrLocale==NULL || fillIn == NULL || capacity<0){
41 *err = U_ILLEGAL_ARGUMENT_ERROR;
42 return numFilled;
43 }
44
46f4442e 45 if(uprv_strchr(nameOrAbbrOrLocale, '-')==NULL && uprv_strchr(nameOrAbbrOrLocale, '_')==NULL ){
73c04bcf
A
46 /* try long and abbreviated script names first */
47 code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
48
49 }
b75a7d8f 50 if(code==(UScriptCode)UCHAR_INVALID_CODE){
73c04bcf 51 /* Do not propagate error codes from just not finding a locale bundle. */
b75a7d8f 52 UErrorCode localErrorCode = U_ZERO_ERROR;
374ca955 53 UResourceBundle* resB = ures_open(NULL,nameOrAbbrOrLocale,&localErrorCode);
b75a7d8f
A
54 if(U_SUCCESS(localErrorCode)&& localErrorCode != U_USING_DEFAULT_WARNING){
55 UResourceBundle* resD = ures_getByKey(resB,kLocaleScript,NULL,&localErrorCode);
56 if(U_SUCCESS(localErrorCode) ){
57 len =0;
58 while(ures_hasNext(resD)){
59 const UChar* name = ures_getNextString(resD,&len,NULL,&localErrorCode);
60 if(U_SUCCESS(localErrorCode)){
61 char cName[50] = {'\0'};
62 u_UCharsToChars(name,cName,len);
63 code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, cName);
64 /* got the script code now fill in the buffer */
65 if(numFilled<=capacity){
66 *(fillIn)++=code;
67 numFilled++;
68 }else{
69 ures_close(resD);
70 ures_close(resB);
71 *err=U_BUFFER_OVERFLOW_ERROR;
72 return len;
73 }
74 }
75 }
76 }
73c04bcf 77 ures_close(resD);
b75a7d8f
A
78 }
79 ures_close(resB);
73c04bcf
A
80 code = USCRIPT_INVALID_CODE;
81 }
82 if(code==(UScriptCode)UCHAR_INVALID_CODE){
83 /* still not found .. try long and abbreviated script names again */
84 code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
85 }
86 if(code!=(UScriptCode)UCHAR_INVALID_CODE){
b75a7d8f
A
87 /* we found it */
88 if(numFilled<=capacity){
89 *(fillIn)++=code;
90 numFilled++;
91 }else{
92 *err=U_BUFFER_OVERFLOW_ERROR;
93 return len;
94 }
95 }
96 return numFilled;
97}
98
99U_CAPI const char* U_EXPORT2
100uscript_getName(UScriptCode scriptCode){
101 return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode,
102 U_LONG_PROPERTY_NAME);
103}
104
105U_CAPI const char* U_EXPORT2
106uscript_getShortName(UScriptCode scriptCode){
107 return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode,
108 U_SHORT_PROPERTY_NAME);
109}
110