]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | ********************************************************************** | |
3 | * Copyright (C) 1999-2011, International Business Machines | |
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" | |
12 | #include "umutex.h" | |
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) | |
25 | : UMemory(), ruleSet(status), variableNames(status), | |
26 | variables(0), variablesAreOwned(TRUE) | |
27 | { | |
28 | if (U_FAILURE(status)) { | |
29 | return; | |
30 | } | |
31 | variableNames.setValueDeleter(uprv_deleteUObject); | |
32 | variables = 0; | |
33 | variablesLength = 0; | |
34 | } | |
35 | ||
36 | TransliterationRuleData::TransliterationRuleData(const TransliterationRuleData& other) : | |
37 | UMemory(other), ruleSet(other.ruleSet), | |
38 | variablesAreOwned(TRUE), | |
39 | variablesBase(other.variablesBase), | |
40 | variablesLength(other.variablesLength) | |
41 | { | |
42 | UErrorCode status = U_ZERO_ERROR; | |
43 | int32_t i = 0; | |
44 | variableNames.setValueDeleter(uprv_deleteUObject); | |
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); | |
50 | // Exit out if value could not be created. | |
51 | if (value == NULL) { | |
52 | return; | |
53 | } | |
54 | variableNames.put(*(UnicodeString*)e->key.pointer, value, status); | |
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 | } | |
65 | for (i=0; i<variablesLength; ++i) { | |
66 | variables[i] = other.variables[i]->clone(); | |
67 | if (variables[i] == NULL) { | |
68 | status = U_MEMORY_ALLOCATION_ERROR; | |
69 | break; | |
70 | } | |
71 | } | |
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 | } | |
82 | ||
83 | // Do this last, _after_ setting up variables[]. | |
84 | ruleSet.setData(this); // ruleSet must already be frozen | |
85 | } | |
86 | ||
87 | TransliterationRuleData::~TransliterationRuleData() { | |
88 | if (variablesAreOwned && variables != 0) { | |
89 | for (int32_t i=0; i<variablesLength; ++i) { | |
90 | delete variables[i]; | |
91 | } | |
92 | } | |
93 | uprv_free(variables); | |
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 | ||
114 | ||
115 | U_NAMESPACE_END | |
116 | ||
117 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ |