summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-xetex/common/uscript.c
blob: 47ae56daf00e474f3477cb9ce9bb3f6609f258c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
**********************************************************************
*   Copyright (C) 1997-2006, International Business Machines
*   Corporation and others.  All Rights Reserved.
**********************************************************************
*
* File USCRIPT.C
*
* Modification History:
*
*   Date        Name        Description
*   07/06/2001    Ram         Creation.
******************************************************************************
*/

#include "unicode/uscript.h"
#include "unicode/ures.h"
#include "unicode/uchar.h"
#include "unicode/putil.h"
#include "uprops.h"
#include "cmemory.h"
#include "cstring.h"

static const char kLocaleScript[] = "LocaleScript";
static const char kHyphen = '-';
static const char kUnderscore = '_';

/* TODO: this is a bad API should be deprecated */
U_CAPI int32_t  U_EXPORT2
uscript_getCode(const char* nameOrAbbrOrLocale,
                UScriptCode* fillIn,
                int32_t capacity,
                UErrorCode* err){

    UScriptCode code = USCRIPT_INVALID_CODE;
    int32_t numFilled=0;
    int32_t len=0;
    /* check arguments */
    if(err==NULL ||U_FAILURE(*err)){
        return numFilled;
    }
    if(nameOrAbbrOrLocale==NULL || fillIn == NULL || capacity<0){
        *err = U_ILLEGAL_ARGUMENT_ERROR;
        return numFilled;
    }

    if(uprv_strchr(nameOrAbbrOrLocale, kHyphen)==NULL && uprv_strchr(nameOrAbbrOrLocale, kUnderscore)==NULL ){
        /* try long and abbreviated script names first */
        code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
        
    }
    if(code==(UScriptCode)UCHAR_INVALID_CODE){
       /* Do not propagate error codes from just not finding a locale bundle. */
        UErrorCode localErrorCode = U_ZERO_ERROR;
        UResourceBundle* resB = ures_open(NULL,nameOrAbbrOrLocale,&localErrorCode);
        if(U_SUCCESS(localErrorCode)&& localErrorCode != U_USING_DEFAULT_WARNING){
            UResourceBundle* resD = ures_getByKey(resB,kLocaleScript,NULL,&localErrorCode);
            if(U_SUCCESS(localErrorCode) ){
                len =0;
                while(ures_hasNext(resD)){
                    const UChar* name = ures_getNextString(resD,&len,NULL,&localErrorCode);
                    if(U_SUCCESS(localErrorCode)){
                        char cName[50] = {'\0'};
                        u_UCharsToChars(name,cName,len);
                        code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, cName);
                        /* got the script code now fill in the buffer */
                        if(numFilled<=capacity){ 
                            *(fillIn)++=code;
                            numFilled++;
                        }else{
                            ures_close(resD);
                            ures_close(resB);
                            *err=U_BUFFER_OVERFLOW_ERROR;
                            return len;
                        }
                    }
                }
            }
            ures_close(resD); 
        }
        ures_close(resB);
        code = USCRIPT_INVALID_CODE;
    }
    if(code==(UScriptCode)UCHAR_INVALID_CODE){
       /* still not found .. try long and abbreviated script names again */
        code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
    }
    if(code!=(UScriptCode)UCHAR_INVALID_CODE){
        /* we found it */
        if(numFilled<=capacity){ 
            *(fillIn)++=code;
            numFilled++;
        }else{
            *err=U_BUFFER_OVERFLOW_ERROR;
            return len;
        }
    }
    return numFilled;
}

U_CAPI const char*  U_EXPORT2
uscript_getName(UScriptCode scriptCode){
    return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode,
                                  U_LONG_PROPERTY_NAME);
}

U_CAPI const char*  U_EXPORT2
uscript_getShortName(UScriptCode scriptCode){
    return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode,
                                  U_SHORT_PROPERTY_NAME);
}