X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Ficu_transform.c;h=05214b7e8ee9c5c3f26365d031aaf50b289c3e6e;hp=a3d9e162af9db5a043bd622fb16c10e56398362e;hb=1932238af8876622f567da122fb52fb3791c9514;hpb=88d3bedf772316f87e1996f655ccf8d1e2589755 diff --git a/src/icu_transform.c b/src/icu_transform.c index a3d9e16..05214b7 100644 --- a/src/icu_transform.c +++ b/src/icu_transform.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2010 Index Data + * Copyright (C) 1995-2013 Index Data * See the file LICENSE for details. */ @@ -19,6 +19,7 @@ #include +#include #include #include #include @@ -29,12 +30,25 @@ struct icu_transform { char action; UParseError parse_error; - UTransliterator * trans; + UTransliterator *trans; }; -struct icu_transform * icu_transform_create(const char *id, char action, - const char *rules, - UErrorCode *status) +struct icu_transform *icu_transform_clone(struct icu_transform *old) +{ + struct icu_transform *transform + = (struct icu_transform *) xmalloc(sizeof(struct icu_transform)); + UErrorCode status = U_ZERO_ERROR; + assert(old); + transform->action = old->action; + assert(old->trans); + transform->trans = utrans_clone(old->trans, &status); + assert(transform->trans); + return transform; +} + +struct icu_transform *icu_transform_create(const char *id, char action, + const char *rules, + UErrorCode *status) { struct icu_buf_utf16 *id16 = icu_buf_utf16_create(0); struct icu_buf_utf16 *rules16 = icu_buf_utf16_create(0); @@ -47,6 +61,7 @@ struct icu_transform * icu_transform_create(const char *id, char action, if (id) icu_utf16_from_utf8_cstr(id16, id, status); + if (rules) icu_utf16_from_utf8_cstr(rules16, rules, status); @@ -55,10 +70,10 @@ struct icu_transform * icu_transform_create(const char *id, char action, case 'f': case 'F': transform->trans - = utrans_openU(id16->utf16, + = utrans_openU(id16->utf16, id16->utf16_len, UTRANS_FORWARD, - rules16->utf16, + rules16->utf16, rules16->utf16_len, &transform->parse_error, status); break; @@ -68,7 +83,7 @@ struct icu_transform * icu_transform_create(const char *id, char action, = utrans_openU(id16->utf16, id16->utf16_len, UTRANS_REVERSE , - rules16->utf16, + rules16->utf16, rules16->utf16_len, &transform->parse_error, status); break; @@ -78,7 +93,7 @@ struct icu_transform * icu_transform_create(const char *id, char action, } icu_buf_utf16_destroy(rules16); icu_buf_utf16_destroy(id16); - + if (U_SUCCESS(*status)) return transform; @@ -87,7 +102,7 @@ struct icu_transform * icu_transform_create(const char *id, char action, return 0; } -void icu_transform_destroy(struct icu_transform * transform) +void icu_transform_destroy(struct icu_transform *transform) { if (transform) { @@ -97,12 +112,12 @@ void icu_transform_destroy(struct icu_transform * transform) } } -int icu_transform_trans(struct icu_transform * transform, - struct icu_buf_utf16 * dest16, - const struct icu_buf_utf16 * src16, +int icu_transform_trans(struct icu_transform *transform, + struct icu_buf_utf16 *dest16, + const struct icu_buf_utf16 *src16, UErrorCode *status) { - if (!transform || !transform->trans + if (!transform || !transform->trans || !src16 || !dest16) return 0; @@ -115,14 +130,14 @@ int icu_transform_trans(struct icu_transform * transform, if (!icu_buf_utf16_copy(dest16, src16)) return 0; - utrans_transUChars (transform->trans, - dest16->utf16, &(dest16->utf16_len), - dest16->utf16_cap, - 0, &(dest16->utf16_len), status); + utrans_transUChars(transform->trans, + dest16->utf16, &(dest16->utf16_len), + dest16->utf16_cap, + 0, &(dest16->utf16_len), status); if (U_FAILURE(*status)) icu_buf_utf16_clear(dest16); - + return dest16->utf16_len; }