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>
26 #include <unicode/utrans.h>
31 UParseError parse_error;
32 UTransliterator * trans;
35 struct icu_transform * icu_transform_create(const char *id, char action,
39 struct icu_buf_utf16 *id16 = icu_buf_utf16_create(0);
40 struct icu_buf_utf16 *rules16 = icu_buf_utf16_create(0);
42 struct icu_transform *transform
43 = (struct icu_transform *) xmalloc(sizeof(struct icu_transform));
45 transform->action = action;
49 icu_utf16_from_utf8_cstr(id16, id, status);
51 icu_utf16_from_utf8_cstr(rules16, rules, status);
53 switch (transform->action)
58 = utrans_openU(id16->utf16,
63 &transform->parse_error, status);
68 = utrans_openU(id16->utf16,
73 &transform->parse_error, status);
76 *status = U_UNSUPPORTED_ERROR;
79 icu_buf_utf16_destroy(rules16);
80 icu_buf_utf16_destroy(id16);
82 if (U_SUCCESS(*status))
85 /* freeing if failed */
86 icu_transform_destroy(transform);
90 void icu_transform_destroy(struct icu_transform * transform)
95 utrans_close(transform->trans);
100 int icu_transform_trans(struct icu_transform * transform,
101 struct icu_buf_utf16 * dest16,
102 const struct icu_buf_utf16 * src16,
105 if (!transform || !transform->trans
106 || !src16 || !dest16)
109 if (!src16->utf16_len)
110 { /* guarding for empty source string */
111 icu_buf_utf16_clear(dest16);
115 if (!icu_buf_utf16_copy(dest16, src16))
118 utrans_transUChars (transform->trans,
119 dest16->utf16, &(dest16->utf16_len),
121 0, &(dest16->utf16_len), status);
123 if (U_FAILURE(*status))
124 icu_buf_utf16_clear(dest16);
126 return dest16->utf16_len;
130 #endif /* YAZ_HAVE_ICU */
135 * c-file-style: "Stroustrup"
136 * indent-tabs-mode: nil
138 * vim: shiftwidth=4 tabstop=8 expandtab