1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief ICU transforms - using utrans_-functions from ICU
16 #include <yaz/xmalloc.h>
18 #include <yaz/icu_I18N.h>
27 #include <unicode/utrans.h>
32 UParseError parse_error;
33 UTransliterator * trans;
36 struct icu_transform *icu_transform_clone(struct icu_transform *old)
38 struct icu_transform *transform
39 = (struct icu_transform *) xmalloc(sizeof(struct icu_transform));
40 UErrorCode status = U_ZERO_ERROR;
42 transform->action = old->action;
44 transform->trans = utrans_clone(old->trans, &status);
45 assert(transform->trans);
49 struct icu_transform * icu_transform_create(const char *id, char action,
53 struct icu_buf_utf16 *id16 = icu_buf_utf16_create(0);
54 struct icu_buf_utf16 *rules16 = icu_buf_utf16_create(0);
56 struct icu_transform *transform
57 = (struct icu_transform *) xmalloc(sizeof(struct icu_transform));
59 transform->action = action;
64 icu_utf16_from_utf8_cstr(id16, id, status);
65 id16->utf16[id16->utf16_len] = 0;
68 icu_utf16_from_utf8_cstr(rules16, rules, status);
70 switch (transform->action)
75 = utrans_openU(id16->utf16,
80 &transform->parse_error, status);
85 = utrans_openU(id16->utf16,
90 &transform->parse_error, status);
93 *status = U_UNSUPPORTED_ERROR;
96 icu_buf_utf16_destroy(rules16);
97 icu_buf_utf16_destroy(id16);
99 if (U_SUCCESS(*status))
102 /* freeing if failed */
103 icu_transform_destroy(transform);
107 void icu_transform_destroy(struct icu_transform * transform)
111 if (transform->trans)
112 utrans_close(transform->trans);
117 int icu_transform_trans(struct icu_transform * transform,
118 struct icu_buf_utf16 * dest16,
119 const struct icu_buf_utf16 * src16,
122 if (!transform || !transform->trans
123 || !src16 || !dest16)
126 if (!src16->utf16_len)
127 { /* guarding for empty source string */
128 icu_buf_utf16_clear(dest16);
132 if (!icu_buf_utf16_copy(dest16, src16))
135 utrans_transUChars (transform->trans,
136 dest16->utf16, &(dest16->utf16_len),
138 0, &(dest16->utf16_len), status);
140 if (U_FAILURE(*status))
141 icu_buf_utf16_clear(dest16);
143 return dest16->utf16_len;
147 #endif /* YAZ_HAVE_ICU */
152 * c-file-style: "Stroustrup"
153 * indent-tabs-mode: nil
155 * vim: shiftwidth=4 tabstop=8 expandtab