tizen 2.3.1 release
[framework/api/base-utils.git] / src / utils_i18n_ubrk.c
1 /*
2  * Copyright (c) 2015 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 <unicode/ubrk.h>
18 #include <utils_i18n_ubrk.h>
19 #include <utils_i18n_private.h>
20
21
22 int i18n_ubrk_create (i18n_ubreak_iterator_type_e type, const char *locale, const i18n_uchar *text, int32_t text_length, i18n_ubreak_iterator_h *break_iter)
23 {
24     if (type < I18N_UBRK_CHARACTER || type > I18N_UBRK_SENTENCE || NULL == break_iter) {
25         return I18N_ERROR_INVALID_PARAMETER;
26     }
27     if (text_length < -1) {
28         return I18N_ERROR_INVALID_PARAMETER;
29     }
30     UErrorCode icu_error = U_ZERO_ERROR;
31     *break_iter = (i18n_ubreak_iterator_h) ubrk_open((UBreakIteratorType)type, locale, text, text_length, &icu_error);
32     ERR("ErrorCode : %d", icu_error);
33
34     i18n_error_code_e i18n_error;
35     ERR_MAPPING(icu_error, i18n_error);
36     return i18n_error;
37 }
38
39 int i18n_ubrk_create_rules (const i18n_uchar *rules, int32_t rules_length, const i18n_uchar *text, int32_t text_length, i18n_ubreak_iterator_h *break_iter, i18n_uparse_error_s *parse_err)
40 {
41     if (rules == NULL || rules_length < -1 || text_length < -1 || break_iter == NULL) {
42         return I18N_ERROR_INVALID_PARAMETER;
43     }
44
45     UErrorCode icu_error = U_ZERO_ERROR;
46     *break_iter = (i18n_ubreak_iterator_h) ubrk_openRules (rules, rules_length, text, text_length, (UParseError*)parse_err, &icu_error);
47     ERR("ErrorCode : %d", icu_error);
48
49     i18n_error_code_e i18n_error;
50     ERR_MAPPING(icu_error, i18n_error);
51     return i18n_error;
52 }
53
54 int i18n_ubrk_safe_clone (const i18n_ubreak_iterator_h break_iter, void *stack_buffer, int32_t *p_buffer_size, i18n_ubreak_iterator_h *break_iter_clone)
55 {
56     if (break_iter == NULL || break_iter_clone == NULL) {
57         return I18N_ERROR_INVALID_PARAMETER;
58     }
59     UErrorCode icu_error = U_ZERO_ERROR;
60     *break_iter_clone = (i18n_ubreak_iterator_h) ubrk_safeClone((const UBreakIterator*)break_iter, stack_buffer, p_buffer_size, &icu_error);
61     ERR("ErrorCode : %d", icu_error);
62
63     i18n_error_code_e i18n_error;
64     ERR_MAPPING(icu_error, i18n_error);
65     return i18n_error;
66 }
67
68 int i18n_ubrk_destroy (i18n_ubreak_iterator_h break_iter)
69 {
70     if (break_iter == NULL) {
71         return I18N_ERROR_INVALID_PARAMETER;
72     }
73
74     ubrk_close((UBreakIterator*)break_iter);
75     return I18N_ERROR_NONE;
76 }
77
78 int i18n_ubrk_set_text (i18n_ubreak_iterator_h break_iter, const i18n_uchar *text, int32_t text_length)
79 {
80     if(text == NULL || break_iter == NULL || text_length < 0){
81         return I18N_ERROR_INVALID_PARAMETER;
82     }
83
84     UErrorCode icu_error = U_ZERO_ERROR;
85     ubrk_setText((UBreakIterator*)break_iter, text, text_length, &icu_error);
86     ERR("ErrorCode : %d", icu_error);
87
88     i18n_error_code_e i18n_error;
89     ERR_MAPPING(icu_error, i18n_error);
90     return i18n_error;
91 }
92
93 int32_t i18n_ubrk_current (const i18n_ubreak_iterator_h break_iter)
94 {
95     if (break_iter == NULL) {
96         set_last_result(I18N_ERROR_INVALID_PARAMETER);
97         return -1;
98     }
99     set_last_result(I18N_ERROR_NONE);
100     return ubrk_current ((const UBreakIterator*)break_iter);
101 }
102
103 int32_t i18n_ubrk_next (i18n_ubreak_iterator_h break_iter)
104 {
105     if (break_iter == NULL) {
106         set_last_result(I18N_ERROR_INVALID_PARAMETER);
107         return -1;
108     }
109
110     set_last_result(I18N_ERROR_NONE);
111     return ubrk_next((UBreakIterator*)break_iter);
112 }
113
114 int32_t i18n_ubrk_previous (i18n_ubreak_iterator_h break_iter)
115 {
116     if (break_iter == NULL) {
117         set_last_result(I18N_ERROR_INVALID_PARAMETER);
118         return -1;
119     }
120
121     set_last_result(I18N_ERROR_NONE);
122     return ubrk_previous ((UBreakIterator*)break_iter);
123 }
124
125 int32_t i18n_ubrk_first (i18n_ubreak_iterator_h break_iter)
126 {
127     if (break_iter == NULL) {
128         set_last_result(I18N_ERROR_INVALID_PARAMETER);
129         return -1;
130     }
131
132     set_last_result(I18N_ERROR_NONE);
133     return ubrk_first ((UBreakIterator*)break_iter);
134 }
135
136 int32_t i18n_ubrk_last (i18n_ubreak_iterator_h break_iter)
137 {
138     if( break_iter == NULL) {
139         set_last_result(I18N_ERROR_INVALID_PARAMETER);
140         return -1;
141     }
142
143     set_last_result(I18N_ERROR_NONE);
144     return ubrk_last ((UBreakIterator*)break_iter);
145 }
146
147 int32_t i18n_ubrk_preceding (i18n_ubreak_iterator_h break_iter, int32_t offset)
148 {
149     if (break_iter == NULL) {
150         set_last_result(I18N_ERROR_INVALID_PARAMETER);
151         return -1;
152     }
153
154     set_last_result(I18N_ERROR_NONE);
155     return ubrk_preceding((UBreakIterator*)break_iter, offset);
156 }
157
158 int32_t i18n_ubrk_following (i18n_ubreak_iterator_h break_iter, int32_t offset)
159 {
160     if (break_iter == NULL) {
161         set_last_result(I18N_ERROR_INVALID_PARAMETER);
162         return -1;
163     }
164
165     set_last_result(I18N_ERROR_NONE);
166     return ubrk_following((UBreakIterator*)break_iter, offset);
167 }
168
169 const char *i18n_ubrk_get_available (int32_t index)
170 {
171     if(index < 0) {
172         set_last_result(I18N_ERROR_INVALID_PARAMETER);
173         return NULL;
174     }
175
176     set_last_result(I18N_ERROR_NONE);
177     return ubrk_getAvailable(index);
178 }
179
180 int32_t i18n_ubrk_count_available (void)
181 {
182     set_last_result(I18N_ERROR_NONE);
183     return ubrk_countAvailable();
184 }
185
186 i18n_ubool i18n_ubrk_is_boundary (i18n_ubreak_iterator_h break_iter, int32_t offset)
187 {
188     if (break_iter == NULL) {
189         set_last_result(I18N_ERROR_INVALID_PARAMETER);
190         return false;
191     }
192
193     set_last_result(I18N_ERROR_NONE);
194     return ubrk_isBoundary((UBreakIterator*)break_iter, offset);
195 }
196
197 int32_t i18n_ubrk_get_rule_status (i18n_ubreak_iterator_h break_iter)
198 {
199     if (break_iter == NULL) {
200         set_last_result(I18N_ERROR_INVALID_PARAMETER);
201         return 0;
202     }
203
204     set_last_result(I18N_ERROR_NONE);
205     return ubrk_getRuleStatus((UBreakIterator*)break_iter);
206 }
207
208 int32_t i18n_ubrk_get_rule_status_vec (i18n_ubreak_iterator_h break_iter, int32_t *fill_in_vec, int32_t capacity)
209 {
210     if (break_iter == NULL || capacity < 0) {
211         set_last_result(I18N_ERROR_INVALID_PARAMETER);
212         return 0;
213     }
214
215     UErrorCode icu_error = U_ZERO_ERROR;
216     int32_t result_ubrk_getRuleStatusVec = ubrk_getRuleStatusVec((UBreakIterator*)break_iter, fill_in_vec, capacity, &icu_error);
217     ERR("ErrorCode : %d", icu_error);
218
219     i18n_error_code_e i18n_error;
220     ERR_MAPPING(icu_error, i18n_error);
221     set_last_result(i18n_error);
222     return result_ubrk_getRuleStatusVec;
223 }
224
225 const char *i18n_ubrk_get_locale_by_type (const i18n_ubreak_iterator_h break_iter, i18n_ulocale_data_locale_type_e type)
226 {
227     if (type < I18N_ULOCALE_DATA_LOCALE_TYPE_ACTUAL_LOCALE ||
228         type > I18N_ULOCALE_DATA_LOCALE_TYPE_LIMIT) {
229         set_last_result(I18N_ERROR_INVALID_PARAMETER);
230         return 0;
231     }
232     UErrorCode icu_error = U_ZERO_ERROR;
233     const char * result_ubrk_getLocaleByType = ubrk_getLocaleByType((const UBreakIterator*)break_iter, type, &icu_error);
234     ERR("ErrorCode : %d", icu_error);
235
236     i18n_error_code_e i18n_error;
237     ERR_MAPPING(icu_error, i18n_error);
238     set_last_result(i18n_error);
239     return result_ubrk_getLocaleByType;
240 }