1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 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;
63 icu_utf16_from_utf8_cstr(id16, id, status);
66 icu_utf16_from_utf8_cstr(rules16, rules, status);
68 switch (transform->action)
73 = utrans_openU(id16->utf16,
78 &transform->parse_error, status);
83 = utrans_openU(id16->utf16,
88 &transform->parse_error, status);
91 *status = U_UNSUPPORTED_ERROR;
94 icu_buf_utf16_destroy(rules16);
95 icu_buf_utf16_destroy(id16);
97 if (U_SUCCESS(*status))
100 /* freeing if failed */
101 icu_transform_destroy(transform);
105 void icu_transform_destroy(struct icu_transform * transform)
109 if (transform->trans)
110 utrans_close(transform->trans);
115 int icu_transform_trans(struct icu_transform * transform,
116 struct icu_buf_utf16 * dest16,
117 const struct icu_buf_utf16 * src16,
120 if (!transform || !transform->trans
121 || !src16 || !dest16)
124 if (!src16->utf16_len)
125 { /* guarding for empty source string */
126 icu_buf_utf16_clear(dest16);
130 if (!icu_buf_utf16_copy(dest16, src16))
133 utrans_transUChars(transform->trans,
134 dest16->utf16, &(dest16->utf16_len),
136 0, &(dest16->utf16_len), status);
138 if (U_FAILURE(*status))
139 icu_buf_utf16_clear(dest16);
141 return dest16->utf16_len;
145 #endif /* YAZ_HAVE_ICU */
150 * c-file-style: "Stroustrup"
151 * indent-tabs-mode: nil
153 * vim: shiftwidth=4 tabstop=8 expandtab