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;
63 icu_utf16_from_utf8_cstr(id16, id, status);
65 icu_utf16_from_utf8_cstr(rules16, rules, status);
67 switch (transform->action)
72 = utrans_openU(id16->utf16,
77 &transform->parse_error, status);
82 = utrans_openU(id16->utf16,
87 &transform->parse_error, status);
90 *status = U_UNSUPPORTED_ERROR;
93 icu_buf_utf16_destroy(rules16);
94 icu_buf_utf16_destroy(id16);
96 if (U_SUCCESS(*status))
99 /* freeing if failed */
100 icu_transform_destroy(transform);
104 void icu_transform_destroy(struct icu_transform * transform)
108 if (transform->trans)
109 utrans_close(transform->trans);
114 int icu_transform_trans(struct icu_transform * transform,
115 struct icu_buf_utf16 * dest16,
116 const struct icu_buf_utf16 * src16,
119 if (!transform || !transform->trans
120 || !src16 || !dest16)
123 if (!src16->utf16_len)
124 { /* guarding for empty source string */
125 icu_buf_utf16_clear(dest16);
129 if (!icu_buf_utf16_copy(dest16, src16))
132 utrans_transUChars (transform->trans,
133 dest16->utf16, &(dest16->utf16_len),
135 0, &(dest16->utf16_len), status);
137 if (U_FAILURE(*status))
138 icu_buf_utf16_clear(dest16);
140 return dest16->utf16_len;
144 #endif /* YAZ_HAVE_ICU */
149 * c-file-style: "Stroustrup"
150 * indent-tabs-mode: nil
152 * vim: shiftwidth=4 tabstop=8 expandtab