[Base-utils][ACR-1182]Fix of setting locale in functions
[platform/core/api/base-utils.git] / src / utils_i18n_alpha_idx.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include <utils_i18n_alpha_idx.h>
18 #include <utils_i18n_private.h>
19
20 #include <malloc.h>
21 #include <string.h>
22 #include <unicode/locid.h>
23 #include <unicode/alphaindex.h>
24
25 int i18n_alpha_idx_create(const char *language, const char *country,
26                           i18n_alpha_idx_h *index)
27 {
28     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
29
30     Locale locale(language, country, 0, 0);
31     UErrorCode status = U_ZERO_ERROR;
32
33     *index = new AlphabeticIndex(locale, status);
34     retv_if(*index == NULL, I18N_ERROR_OUT_OF_MEMORY);
35
36     return _i18n_error_mapping(status);
37 }
38
39 int i18n_alpha_idx_destroy(i18n_alpha_idx_h index)
40 {
41     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
42
43     delete ((AlphabeticIndex *) index);
44
45     return I18N_ERROR_NONE;
46 }
47
48 int i18n_alpha_idx_add_labels(i18n_alpha_idx_h index,
49                               const char *language, const char *country)
50 {
51     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
52
53     Locale locale(language, country, 0, 0);
54     UErrorCode status = U_ZERO_ERROR;
55
56     ((AlphabeticIndex *) index)->addLabels(locale, status);
57
58     return _i18n_error_mapping(status);
59 }
60
61 int i18n_alpha_idx_add_record(i18n_alpha_idx_h index, const char *name,
62                               const void *data)
63 {
64     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
65     retv_if(name == NULL, I18N_ERROR_INVALID_PARAMETER);
66
67     const UnicodeString _name(name);
68     UErrorCode status = U_ZERO_ERROR;
69
70     ((AlphabeticIndex *) index)->addRecord(_name, data, status);
71
72     return _i18n_error_mapping(status);
73 }
74
75 int i18n_alpha_idx_get_next_bucket(i18n_alpha_idx_h index, bool *available)
76 {
77     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
78
79     UErrorCode status = U_ZERO_ERROR;
80
81     if (available != NULL)
82         *available = ((AlphabeticIndex *) index)->nextBucket(status);
83     else
84         ((AlphabeticIndex *) index)->nextBucket(status);
85
86     return _i18n_error_mapping(status);
87 }
88
89 int i18n_alpha_idx_get_next_record(i18n_alpha_idx_h index, bool *available)
90 {
91     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
92
93     UErrorCode status = U_ZERO_ERROR;
94
95     if (available != NULL)
96         *available = ((AlphabeticIndex *) index)->nextRecord(status);
97     else
98         ((AlphabeticIndex *) index)->nextRecord(status);
99
100     return _i18n_error_mapping(status);
101 }
102
103 int i18n_alpha_idx_get_bucket_record_count(i18n_alpha_idx_h index,
104                                            int32_t *records_count)
105 {
106     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
107     retv_if(records_count == NULL, I18N_ERROR_INVALID_PARAMETER);
108
109     *records_count = ((AlphabeticIndex *) index)->getBucketRecordCount();
110
111     return I18N_ERROR_NONE;
112 }
113
114 int i18n_alpha_idx_get_bucket_label(i18n_alpha_idx_h index,
115                                     char **label)
116 {
117     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
118     retv_if(label == NULL, I18N_ERROR_INVALID_PARAMETER);
119
120     UnicodeString _label = ((AlphabeticIndex *) index)->getBucketLabel();
121
122     std::string _label_string;
123     _label.toUTF8String(_label_string);
124
125     *label = strdup(_label_string.c_str());
126     retv_if(*label == NULL, I18N_ERROR_OUT_OF_MEMORY);
127
128     return I18N_ERROR_NONE;
129 }
130
131 const void *i18n_alpha_idx_get_record_data(i18n_alpha_idx_h index)
132 {
133     if (index == NULL) {
134         set_last_result(I18N_ERROR_INVALID_PARAMETER);
135         return NULL;
136     }
137
138     return ((AlphabeticIndex *) index)->getRecordData();
139 }
140
141 int i18n_alpha_idx_get_inflow_label(i18n_alpha_idx_h index,
142                                     char **label)
143 {
144     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
145     retv_if(label == NULL, I18N_ERROR_INVALID_PARAMETER);
146
147     UnicodeString _label = ((AlphabeticIndex *) index)->getInflowLabel();
148
149     std::string _label_string;
150     _label.toUTF8String(_label_string);
151
152     *label = strdup(_label_string.c_str());
153     retv_if(*label == NULL, I18N_ERROR_OUT_OF_MEMORY);
154
155     return I18N_ERROR_NONE;
156 }
157
158 int i18n_alpha_idx_set_inflow_label(i18n_alpha_idx_h index,
159                                     const char *label)
160 {
161     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
162     retv_if(label == NULL, I18N_ERROR_INVALID_PARAMETER);
163
164     const UnicodeString inflow_label(label);
165     UErrorCode status = U_ZERO_ERROR;
166
167     ((AlphabeticIndex *) index)->setInflowLabel(inflow_label, status);
168
169     return _i18n_error_mapping(status);
170 }
171
172 int i18n_alpha_idx_get_overflow_label(i18n_alpha_idx_h index,
173                                       char **label)
174 {
175     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
176     retv_if(label == NULL, I18N_ERROR_INVALID_PARAMETER);
177
178     const UnicodeString overflow_label = ((AlphabeticIndex *) index)->getOverflowLabel();
179
180     std::string _label_string;
181     overflow_label.toUTF8String(_label_string);
182
183     *label = strdup(_label_string.c_str());
184     retv_if(*label == NULL, I18N_ERROR_OUT_OF_MEMORY);
185
186     return I18N_ERROR_NONE;
187 }
188
189 int i18n_alpha_idx_set_overflow_label(i18n_alpha_idx_h index,
190                                       const char *label)
191 {
192     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
193     retv_if(label == NULL, I18N_ERROR_INVALID_PARAMETER);
194
195     const UnicodeString overflow_label(label);
196     UErrorCode status = U_ZERO_ERROR;
197
198     ((AlphabeticIndex *) index)->setOverflowLabel(overflow_label, status);
199
200     return _i18n_error_mapping(status);
201 }
202
203 int i18n_alpha_idx_get_underflow_label(i18n_alpha_idx_h index,
204                                        char **label)
205 {
206     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
207     retv_if(label == NULL, I18N_ERROR_INVALID_PARAMETER);
208
209     const UnicodeString overflow_label = ((AlphabeticIndex *) index)->getUnderflowLabel();
210
211     std::string _label_string;
212     overflow_label.toUTF8String(_label_string);
213
214     *label = strdup(_label_string.c_str());
215     retv_if(*label == NULL, I18N_ERROR_OUT_OF_MEMORY);
216
217     return I18N_ERROR_NONE;
218 }
219
220 int i18n_alpha_idx_set_underflow_label(i18n_alpha_idx_h index,
221                                        const char *label)
222 {
223     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
224     retv_if(label == NULL, I18N_ERROR_INVALID_PARAMETER);
225
226     const UnicodeString underflow_label(label);
227     UErrorCode status = U_ZERO_ERROR;
228
229     ((AlphabeticIndex *) index)->setUnderflowLabel(underflow_label, status);
230
231     return _i18n_error_mapping(status);
232 }
233
234 int i18n_alpha_idx_get_max_label_count(i18n_alpha_idx_h index,
235                                        int32_t *max_label_count)
236 {
237     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
238     retv_if(max_label_count == NULL, I18N_ERROR_INVALID_PARAMETER);
239
240     *max_label_count = ((AlphabeticIndex *) index)->getMaxLabelCount();
241
242     return I18N_ERROR_NONE;
243 }
244
245 int i18n_alpha_idx_set_max_label_count(i18n_alpha_idx_h index,
246                                        int32_t max_label_count)
247 {
248     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
249
250     UErrorCode status = U_ZERO_ERROR;
251
252     ((AlphabeticIndex *) index)->setMaxLabelCount(max_label_count, status);
253
254     return _i18n_error_mapping(status);
255 }
256
257 int i18n_alpha_idx_clear_records(i18n_alpha_idx_h index)
258 {
259     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
260
261     UErrorCode status = U_ZERO_ERROR;
262
263     ((AlphabeticIndex *) index)->clearRecords(status);
264
265     return _i18n_error_mapping(status);
266 }
267
268 int i18n_alpha_idx_get_bucket_count(i18n_alpha_idx_h index,
269                                     int32_t *bucket_count)
270 {
271     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
272     retv_if(bucket_count == NULL, I18N_ERROR_INVALID_PARAMETER);
273
274     UErrorCode status = U_ZERO_ERROR;
275
276     *bucket_count = ((AlphabeticIndex *) index)->getBucketCount(status);
277
278     return _i18n_error_mapping(status);
279 }
280
281 int i18n_alpha_idx_get_record_count(i18n_alpha_idx_h index,
282                                     int32_t *record_count)
283 {
284     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
285     retv_if(record_count == NULL, I18N_ERROR_INVALID_PARAMETER);
286
287     UErrorCode status = U_ZERO_ERROR;
288
289     *record_count = ((AlphabeticIndex *) index)->getRecordCount(status);
290
291     return _i18n_error_mapping(status);
292 }
293
294 int i18n_alpha_idx_get_bucket_index(i18n_alpha_idx_h index,
295                                     const char *item_name,
296                                     int32_t *bucket_index)
297 {
298     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
299     retv_if(item_name == NULL, I18N_ERROR_INVALID_PARAMETER);
300     retv_if(bucket_index == NULL, I18N_ERROR_INVALID_PARAMETER);
301
302     UnicodeString _item_name(item_name);
303     UErrorCode status = U_ZERO_ERROR;
304
305     *bucket_index = ((AlphabeticIndex *) index)->getBucketIndex(_item_name, status);
306
307     return _i18n_error_mapping(status);
308 }
309
310 int i18n_alpha_idx_get_current_bucket_index(i18n_alpha_idx_h index,
311                                             int32_t *bucket_index)
312 {
313     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
314     retv_if(bucket_index == NULL, I18N_ERROR_INVALID_PARAMETER);
315
316     *bucket_index = ((AlphabeticIndex *) index)->getBucketIndex();
317
318     return I18N_ERROR_NONE;
319 }
320
321 int i18n_alpha_idx_get_bucket_label_type(i18n_alpha_idx_h index,
322                                          i18n_alpha_idx_label_type_e *type)
323 {
324     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
325     retv_if(type == NULL, I18N_ERROR_INVALID_PARAMETER);
326
327     *type = (i18n_alpha_idx_label_type_e) ((AlphabeticIndex *) index)->getBucketLabelType();
328
329     return I18N_ERROR_NONE;
330 }
331
332 int i18n_alpha_idx_get_record_name(i18n_alpha_idx_h index,
333                                    char **record_name)
334 {
335     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
336     retv_if(record_name == NULL, I18N_ERROR_INVALID_PARAMETER);
337
338     const UnicodeString _record_name = ((AlphabeticIndex *) index)->getRecordName();
339
340     std::string _record_name_string;
341     _record_name.toUTF8String(_record_name_string);
342
343     *record_name = strdup(_record_name_string.c_str());
344     retv_if(*record_name == NULL, I18N_ERROR_OUT_OF_MEMORY);
345
346     if (_record_name.isEmpty()) {
347         free(*record_name);
348         *record_name = NULL;
349         return I18N_ERROR_INDEX_OUTOFBOUNDS;
350     }
351
352     return I18N_ERROR_NONE;
353 }
354
355 int i18n_alpha_idx_reset_bucket_iter(i18n_alpha_idx_h index)
356 {
357     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
358
359     UErrorCode status = U_ZERO_ERROR;
360
361     ((AlphabeticIndex *) index)->resetBucketIterator(status);
362
363     return _i18n_error_mapping(status);
364 }
365
366 int i18n_alpha_idx_reset_record_iter(i18n_alpha_idx_h index)
367 {
368     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
369
370     ((AlphabeticIndex *) index)->resetRecordIterator();
371
372     return I18N_ERROR_NONE;
373 }
374
375 int i18n_alpha_idx_create_from_locale_id(const char *locale_id, i18n_alpha_idx_h *index)
376 {
377     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
378
379     Locale locale(locale_id, 0, 0, 0);
380     UErrorCode status = U_ZERO_ERROR;
381
382     *index = new AlphabeticIndex(locale, status);
383     retv_if(*index == NULL, I18N_ERROR_OUT_OF_MEMORY);
384
385     return _i18n_error_mapping(status);
386 }
387
388 int i18n_alpha_idx_add_labels_with_locale_id(i18n_alpha_idx_h index,
389                               const char *locale_id)
390 {
391     retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER);
392
393     Locale locale(locale_id, 0, 0, 0);
394     UErrorCode status = U_ZERO_ERROR;
395
396     ((AlphabeticIndex *) index)->addLabels(locale, status);
397
398     return _i18n_error_mapping(status);
399 }