2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <unicode/ubrk.h>
18 #include <utils_i18n_ubrk.h>
19 #include <utils_i18n_private.h>
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)
24 if (type < I18N_UBRK_CHARACTER || type > I18N_UBRK_SENTENCE || NULL == break_iter) {
25 return I18N_ERROR_INVALID_PARAMETER;
27 if (text_length < -1) {
28 return I18N_ERROR_INVALID_PARAMETER;
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);
34 i18n_error_code_e i18n_error;
35 ERR_MAPPING(icu_error, i18n_error);
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)
41 if (rules == NULL || rules_length < -1 || text_length < -1 || break_iter == NULL) {
42 return I18N_ERROR_INVALID_PARAMETER;
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);
49 i18n_error_code_e i18n_error;
50 ERR_MAPPING(icu_error, i18n_error);
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)
56 if (break_iter == NULL || break_iter_clone == NULL) {
57 return I18N_ERROR_INVALID_PARAMETER;
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);
63 i18n_error_code_e i18n_error;
64 ERR_MAPPING(icu_error, i18n_error);
68 int i18n_ubrk_destroy (i18n_ubreak_iterator_h break_iter)
70 if (break_iter == NULL) {
71 return I18N_ERROR_INVALID_PARAMETER;
74 ubrk_close((UBreakIterator*)break_iter);
75 return I18N_ERROR_NONE;
78 int i18n_ubrk_set_text (i18n_ubreak_iterator_h break_iter, const i18n_uchar *text, int32_t text_length)
80 if(text == NULL || break_iter == NULL || text_length < 0){
81 return I18N_ERROR_INVALID_PARAMETER;
84 UErrorCode icu_error = U_ZERO_ERROR;
85 ubrk_setText((UBreakIterator*)break_iter, text, text_length, &icu_error);
86 ERR("ErrorCode : %d", icu_error);
88 i18n_error_code_e i18n_error;
89 ERR_MAPPING(icu_error, i18n_error);
93 int32_t i18n_ubrk_current (const i18n_ubreak_iterator_h break_iter)
95 if (break_iter == NULL) {
96 set_last_result(I18N_ERROR_INVALID_PARAMETER);
99 set_last_result(I18N_ERROR_NONE);
100 return ubrk_current ((const UBreakIterator*)break_iter);
103 int32_t i18n_ubrk_next (i18n_ubreak_iterator_h break_iter)
105 if (break_iter == NULL) {
106 set_last_result(I18N_ERROR_INVALID_PARAMETER);
110 set_last_result(I18N_ERROR_NONE);
111 return ubrk_next((UBreakIterator*)break_iter);
114 int32_t i18n_ubrk_previous (i18n_ubreak_iterator_h break_iter)
116 if (break_iter == NULL) {
117 set_last_result(I18N_ERROR_INVALID_PARAMETER);
121 set_last_result(I18N_ERROR_NONE);
122 return ubrk_previous ((UBreakIterator*)break_iter);
125 int32_t i18n_ubrk_first (i18n_ubreak_iterator_h break_iter)
127 if (break_iter == NULL) {
128 set_last_result(I18N_ERROR_INVALID_PARAMETER);
132 set_last_result(I18N_ERROR_NONE);
133 return ubrk_first ((UBreakIterator*)break_iter);
136 int32_t i18n_ubrk_last (i18n_ubreak_iterator_h break_iter)
138 if( break_iter == NULL) {
139 set_last_result(I18N_ERROR_INVALID_PARAMETER);
143 set_last_result(I18N_ERROR_NONE);
144 return ubrk_last ((UBreakIterator*)break_iter);
147 int32_t i18n_ubrk_preceding (i18n_ubreak_iterator_h break_iter, int32_t offset)
149 if (break_iter == NULL) {
150 set_last_result(I18N_ERROR_INVALID_PARAMETER);
154 set_last_result(I18N_ERROR_NONE);
155 return ubrk_preceding((UBreakIterator*)break_iter, offset);
158 int32_t i18n_ubrk_following (i18n_ubreak_iterator_h break_iter, int32_t offset)
160 if (break_iter == NULL) {
161 set_last_result(I18N_ERROR_INVALID_PARAMETER);
165 set_last_result(I18N_ERROR_NONE);
166 return ubrk_following((UBreakIterator*)break_iter, offset);
169 const char *i18n_ubrk_get_available (int32_t index)
172 set_last_result(I18N_ERROR_INVALID_PARAMETER);
176 set_last_result(I18N_ERROR_NONE);
177 return ubrk_getAvailable(index);
180 int32_t i18n_ubrk_count_available (void)
182 set_last_result(I18N_ERROR_NONE);
183 return ubrk_countAvailable();
186 i18n_ubool i18n_ubrk_is_boundary (i18n_ubreak_iterator_h break_iter, int32_t offset)
188 if (break_iter == NULL) {
189 set_last_result(I18N_ERROR_INVALID_PARAMETER);
193 set_last_result(I18N_ERROR_NONE);
194 return ubrk_isBoundary((UBreakIterator*)break_iter, offset);
197 int32_t i18n_ubrk_get_rule_status (i18n_ubreak_iterator_h break_iter)
199 if (break_iter == NULL) {
200 set_last_result(I18N_ERROR_INVALID_PARAMETER);
204 set_last_result(I18N_ERROR_NONE);
205 return ubrk_getRuleStatus((UBreakIterator*)break_iter);
208 int32_t i18n_ubrk_get_rule_status_vec (i18n_ubreak_iterator_h break_iter, int32_t *fill_in_vec, int32_t capacity)
210 if (break_iter == NULL || capacity < 0) {
211 set_last_result(I18N_ERROR_INVALID_PARAMETER);
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);
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;
225 const char *i18n_ubrk_get_locale_by_type (const i18n_ubreak_iterator_h break_iter, i18n_ulocale_data_locale_type_e type)
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);
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);
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;