1/*
2**********************************************************************
3*   Copyright (C) 1997-2011, 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
24static const char kLocaleScript[] = "LocaleScript";
25
26/* TODO: this is a bad API should be deprecated */
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
45    if(uprv_strchr(nameOrAbbrOrLocale, '-')==NULL && uprv_strchr(nameOrAbbrOrLocale, '_')==NULL ){
46        /* try long and abbreviated script names first */
47        code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
48
49    }
50    if(code==(UScriptCode)UCHAR_INVALID_CODE){
51       /* Do not propagate error codes from just not finding a locale bundle. */
52        UErrorCode localErrorCode = U_ZERO_ERROR;
53        UResourceBundle* resB = ures_open(NULL,nameOrAbbrOrLocale,&localErrorCode);
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            }
77            ures_close(resD);
78        }
79        ures_close(resB);
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){
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