aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/icu/common/uenum.cpp
blob: c8af2459395d1d631fe20bf5156f30424db6282e (plain) (blame)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// © 2016 and later: Unicode, Inc. and others. 
// License & terms of use: http://www.unicode.org/copyright.html 
/* 
******************************************************************************* 
* 
*   Copyright (C) 2002-2012, International Business Machines 
*   Corporation and others.  All Rights Reserved. 
* 
******************************************************************************* 
*   file name:  uenum.c 
*   encoding:   UTF-8 
*   tab size:   8 (not used) 
*   indentation:2 
* 
*   created on: 2002jul08 
*   created by: Vladimir Weinstein 
*/ 
 
#include "unicode/putil.h" 
#include "uenumimp.h" 
#include "cmemory.h" 
 
/* Layout of the baseContext buffer. */ 
typedef struct { 
    int32_t len;  /* number of bytes available starting at 'data' */ 
    char    data; /* actual data starts here */ 
} _UEnumBuffer; 
 
/* Extra bytes to allocate in the baseContext buffer. */ 
static const int32_t PAD = 8; 
 
/* Return a pointer to the baseContext buffer, possibly allocating 
   or reallocating it if at least 'capacity' bytes are not available. */ 
static void* _getBuffer(UEnumeration* en, int32_t capacity) { 
 
    if (en->baseContext != NULL) { 
        if (((_UEnumBuffer*) en->baseContext)->len < capacity) { 
            capacity += PAD; 
            en->baseContext = uprv_realloc(en->baseContext, 
                                           sizeof(int32_t) + capacity); 
            if (en->baseContext == NULL) { 
                return NULL; 
            } 
            ((_UEnumBuffer*) en->baseContext)->len = capacity; 
        } 
    } else { 
        capacity += PAD; 
        en->baseContext = uprv_malloc(sizeof(int32_t) + capacity); 
        if (en->baseContext == NULL) { 
            return NULL; 
        } 
        ((_UEnumBuffer*) en->baseContext)->len = capacity; 
    } 
     
    return (void*) & ((_UEnumBuffer*) en->baseContext)->data; 
} 
 
U_CAPI void U_EXPORT2 
uenum_close(UEnumeration* en) 
{ 
    if (en) { 
        if (en->close != NULL) { 
            if (en->baseContext) { 
                uprv_free(en->baseContext); 
            } 
            en->close(en); 
        } else { /* this seems dangerous, but we better kill the object */ 
            uprv_free(en); 
        } 
    } 
} 
 
U_CAPI int32_t U_EXPORT2 
uenum_count(UEnumeration* en, UErrorCode* status) 
{ 
    if (!en || U_FAILURE(*status)) { 
        return -1; 
    } 
    if (en->count != NULL) { 
        return en->count(en, status); 
    } else { 
        *status = U_UNSUPPORTED_ERROR; 
        return -1; 
    } 
} 
 
/* Don't call this directly. Only uenum_unext should be calling this. */ 
U_CAPI const UChar* U_EXPORT2 
uenum_unextDefault(UEnumeration* en, 
            int32_t* resultLength, 
            UErrorCode* status) 
{ 
    UChar *ustr = NULL; 
    int32_t len = 0; 
    if (en->next != NULL) { 
        const char *cstr = en->next(en, &len, status); 
        if (cstr != NULL) { 
            ustr = (UChar*) _getBuffer(en, (len+1) * sizeof(UChar)); 
            if (ustr == NULL) { 
                *status = U_MEMORY_ALLOCATION_ERROR; 
            } else { 
                u_charsToUChars(cstr, ustr, len+1); 
            } 
        } 
    } else { 
        *status = U_UNSUPPORTED_ERROR; 
    } 
    if (resultLength) { 
        *resultLength = len; 
    } 
    return ustr; 
} 
 
/* Don't call this directly. Only uenum_next should be calling this. */ 
U_CAPI const char* U_EXPORT2 
uenum_nextDefault(UEnumeration* en, 
            int32_t* resultLength, 
            UErrorCode* status) 
{ 
    if (en->uNext != NULL) { 
        char *tempCharVal; 
        const UChar *tempUCharVal = en->uNext(en, resultLength, status); 
        if (tempUCharVal == NULL) { 
            return NULL; 
        } 
        tempCharVal = (char*) 
            _getBuffer(en, (*resultLength+1) * sizeof(char)); 
        if (!tempCharVal) { 
            *status = U_MEMORY_ALLOCATION_ERROR; 
            return NULL; 
        } 
        u_UCharsToChars(tempUCharVal, tempCharVal, *resultLength + 1); 
        return tempCharVal; 
    } else { 
        *status = U_UNSUPPORTED_ERROR; 
        return NULL; 
    } 
} 
 
U_CAPI const UChar* U_EXPORT2 
uenum_unext(UEnumeration* en, 
            int32_t* resultLength, 
            UErrorCode* status) 
{ 
    if (!en || U_FAILURE(*status)) { 
        return NULL; 
    } 
    if (en->uNext != NULL) { 
        return en->uNext(en, resultLength, status); 
    } else { 
        *status = U_UNSUPPORTED_ERROR; 
        return NULL; 
    } 
} 
 
U_CAPI const char* U_EXPORT2 
uenum_next(UEnumeration* en, 
          int32_t* resultLength, 
          UErrorCode* status) 
{ 
    if (!en || U_FAILURE(*status)) { 
        return NULL; 
    } 
    if (en->next != NULL) { 
        if (resultLength != NULL) { 
            return en->next(en, resultLength, status); 
        } 
        else { 
            int32_t dummyLength=0; 
            return en->next(en, &dummyLength, status); 
        } 
    } else { 
        *status = U_UNSUPPORTED_ERROR; 
        return NULL; 
    } 
} 
 
U_CAPI void U_EXPORT2 
uenum_reset(UEnumeration* en, UErrorCode* status) 
{ 
    if (!en || U_FAILURE(*status)) { 
        return; 
    } 
    if (en->reset != NULL) { 
        en->reset(en, status); 
    } else { 
        *status = U_UNSUPPORTED_ERROR; 
    } 
}