]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ********************************************************************** | |
4388f060 | 3 | * Copyright (C) 1999-2011, International Business Machines |
b75a7d8f A |
4 | * Corporation and others. All Rights Reserved. |
5 | ********************************************************************** | |
6 | * Date Name Description | |
7 | * 11/17/99 aliu Creation. | |
8 | ********************************************************************** | |
9 | */ | |
10 | ||
11 | #include "unicode/utypes.h" | |
374ca955 | 12 | #include "umutex.h" |
b75a7d8f A |
13 | |
14 | #if !UCONFIG_NO_TRANSLITERATION | |
15 | ||
16 | #include "unicode/unistr.h" | |
17 | #include "unicode/uniset.h" | |
18 | #include "rbt_data.h" | |
19 | #include "hash.h" | |
20 | #include "cmemory.h" | |
21 | ||
22 | U_NAMESPACE_BEGIN | |
23 | ||
24 | TransliterationRuleData::TransliterationRuleData(UErrorCode& status) | |
73c04bcf A |
25 | : UMemory(), ruleSet(status), variableNames(status), |
26 | variables(0), variablesAreOwned(TRUE) | |
b75a7d8f A |
27 | { |
28 | if (U_FAILURE(status)) { | |
29 | return; | |
30 | } | |
4388f060 | 31 | variableNames.setValueDeleter(uprv_deleteUObject); |
b75a7d8f A |
32 | variables = 0; |
33 | variablesLength = 0; | |
34 | } | |
35 | ||
36 | TransliterationRuleData::TransliterationRuleData(const TransliterationRuleData& other) : | |
37 | UMemory(other), ruleSet(other.ruleSet), | |
73c04bcf | 38 | variablesAreOwned(TRUE), |
b75a7d8f A |
39 | variablesBase(other.variablesBase), |
40 | variablesLength(other.variablesLength) | |
41 | { | |
42 | UErrorCode status = U_ZERO_ERROR; | |
46f4442e | 43 | int32_t i = 0; |
4388f060 | 44 | variableNames.setValueDeleter(uprv_deleteUObject); |
73c04bcf A |
45 | int32_t pos = -1; |
46 | const UHashElement *e; | |
47 | while ((e = other.variableNames.nextElement(pos)) != 0) { | |
48 | UnicodeString* value = | |
49 | new UnicodeString(*(const UnicodeString*)e->value.pointer); | |
46f4442e A |
50 | // Exit out if value could not be created. |
51 | if (value == NULL) { | |
52 | return; | |
53 | } | |
73c04bcf | 54 | variableNames.put(*(UnicodeString*)e->key.pointer, value, status); |
b75a7d8f A |
55 | } |
56 | ||
57 | variables = 0; | |
58 | if (other.variables != 0) { | |
59 | variables = (UnicodeFunctor **)uprv_malloc(variablesLength * sizeof(UnicodeFunctor *)); | |
60 | /* test for NULL */ | |
61 | if (variables == 0) { | |
62 | status = U_MEMORY_ALLOCATION_ERROR; | |
63 | return; | |
64 | } | |
46f4442e | 65 | for (i=0; i<variablesLength; ++i) { |
b75a7d8f | 66 | variables[i] = other.variables[i]->clone(); |
46f4442e A |
67 | if (variables[i] == NULL) { |
68 | status = U_MEMORY_ALLOCATION_ERROR; | |
69 | break; | |
70 | } | |
b75a7d8f | 71 | } |
46f4442e A |
72 | } |
73 | // Remove the array and exit if memory allocation error occured. | |
74 | if (U_FAILURE(status)) { | |
75 | for (int32_t n = i-1; n >= 0; n++) { | |
76 | delete variables[n]; | |
77 | } | |
78 | uprv_free(variables); | |
79 | variables = NULL; | |
80 | return; | |
81 | } | |
b75a7d8f A |
82 | |
83 | // Do this last, _after_ setting up variables[]. | |
84 | ruleSet.setData(this); // ruleSet must already be frozen | |
85 | } | |
86 | ||
87 | TransliterationRuleData::~TransliterationRuleData() { | |
73c04bcf | 88 | if (variablesAreOwned && variables != 0) { |
b75a7d8f A |
89 | for (int32_t i=0; i<variablesLength; ++i) { |
90 | delete variables[i]; | |
91 | } | |
b75a7d8f | 92 | } |
73c04bcf | 93 | uprv_free(variables); |
b75a7d8f A |
94 | } |
95 | ||
96 | UnicodeFunctor* | |
97 | TransliterationRuleData::lookup(UChar32 standIn) const { | |
98 | int32_t i = standIn - variablesBase; | |
99 | return (i >= 0 && i < variablesLength) ? variables[i] : 0; | |
100 | } | |
101 | ||
102 | UnicodeMatcher* | |
103 | TransliterationRuleData::lookupMatcher(UChar32 standIn) const { | |
104 | UnicodeFunctor *f = lookup(standIn); | |
105 | return (f != 0) ? f->toMatcher() : 0; | |
106 | } | |
107 | ||
108 | UnicodeReplacer* | |
109 | TransliterationRuleData::lookupReplacer(UChar32 standIn) const { | |
110 | UnicodeFunctor *f = lookup(standIn); | |
111 | return (f != 0) ? f->toReplacer() : 0; | |
112 | } | |
113 | ||
374ca955 | 114 | |
b75a7d8f A |
115 | U_NAMESPACE_END |
116 | ||
117 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ |