/* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2013 Index Data
+ * Copyright (C) Index Data
* See the file LICENSE for details.
*/
#include <yaz/log.h>
#include <yaz/nmem.h>
#include <yaz/nmem_xml.h>
+#include <yaz/xml_get.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
if (xml_node && xml_node->type == XML_ELEMENT_NODE)
{
- xmlChar *xml_locale = xmlGetProp((xmlNode *) xml_node,
- (xmlChar *) "locale");
+ const char *xml_locale = yaz_xml_get_prop((xmlNode *) xml_node,
+ "locale");
if (xml_locale)
- {
chain = icu_chain_create((const char *) xml_locale, sort, status);
- xmlFree(xml_locale);
- }
}
if (!chain)
{
char *rule = 0;
struct icu_chain_step *step = 0;
- struct _xmlAttr *attr;
+ const char *attr_str;
nmem_reset(nmem);
if (node->type != XML_ELEMENT_NODE)
continue;
-
- for (attr = node->properties; attr; attr = attr->next)
+ attr_str = yaz_xml_get_prop(node, "rule%s", &rule);
+ if (attr_str)
{
- if (!strcmp((const char *) attr->name, "rule"))
- {
- rule = nmem_text_node_cdata(attr->children, nmem);
- }
- else
- {
- yaz_log(YLOG_WARN, "Unsupported attribute '%s' for "
- "element '%s'", attr->name, node->name);
- no_errors++;
- }
+ yaz_log(YLOG_WARN, "Unsupported attribute '%s' for "
+ "element '%s'", attr_str, node->name);
+ no_errors++;
}
if (!rule && node->children)
rule = nmem_text_node_cdata(node->children, nmem);
if (!rule && strcmp((const char *) node->name, "display"))
{
- yaz_log(YLOG_WARN, "Missing attribute rule for element %s",
+ yaz_log(YLOG_WARN, "Missing attribute 'rule' for element %s",
(const char *) node->name);
no_errors++;
continue;
}
if (step && U_FAILURE(*status))
{
+ yaz_log(YLOG_WARN, "ICU Error %d %s for element %s, rule %s",
+ *status, u_errorName(*status), node->name, rule ?
+ rule : "");
no_errors++;
break;
}
struct icu_chain *chain;
struct icu_buf_utf16 *last;
struct icu_buf_utf16 *org;
+ struct icu_buf_utf8 *org8;
UErrorCode status;
struct icu_buf_utf8 *display;
struct icu_buf_utf8 *sort8;
int token_count;
size_t org_start;
size_t org_len;
+ size_t utf8_base;
+ size_t utf16_base;
struct icu_chain_step *steps;
};
struct icu_buf_utf16 *src = dst;
icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
+ if (step->previous)
+ { /* no need to copy if it's already the same */
+ iter->utf8_base = iter->utf16_base = 0;
+ icu_buf_utf16_copy(iter->org, src);
+ }
icu_buf_utf16_destroy(src);
}
dst = icu_buf_utf16_create(0);
iter->sort8 = icu_buf_utf8_create(0);
iter->result = icu_buf_utf8_create(0);
iter->org = icu_buf_utf16_create(0);
+ iter->org8 = 0;
iter->last = 0; /* no last returned string (yet) */
iter->steps = icu_chain_step_clone(chain->csteps);
iter->token_count = 0;
icu_buf_utf16_copy(iter->org, src);
iter->token_count = 0;
iter->org_start = 0;
+ iter->utf8_base = iter->utf16_base = 0;
iter->org_len = src->utf16_len;
iter->last = icu_iter_invoke(iter, iter->steps, src);
}
icu_buf_utf8_destroy(iter->sort8);
icu_buf_utf8_destroy(iter->result);
icu_buf_utf16_destroy(iter->org);
+ icu_buf_utf8_destroy(iter->org8);
icu_chain_step_destroy(iter->steps);
xfree(iter);
}
}
-void icu_iter_get_org_info(yaz_icu_iter_t iter, size_t *start, size_t *len)
+void icu_iter_get_org_info2(yaz_icu_iter_t iter, size_t *start, size_t *len,
+ const char **cstr)
{
- /* save full length of org since we're gonna cut it */
- int32_t save_len = iter->org->utf16_len;
-
- struct icu_buf_utf8 *tmp = icu_buf_utf8_create(0);
+ int32_t len1 = 0, len2 = 0;
UErrorCode status = U_ZERO_ERROR;
- iter->org->utf16_len = iter->org_start;
- icu_utf16_to_utf8(tmp, iter->org, &status);
- if (U_SUCCESS(status))
- *start = tmp->utf8_len;
- else
- *start = 0;
- iter->org->utf16_len = iter->org_start + iter->org_len;
- icu_utf16_to_utf8(tmp, iter->org, &status);
- if (U_SUCCESS(status))
- *len = tmp->utf8_len - *start;
- else
- *len = 0;
- icu_buf_utf8_destroy(tmp);
- iter->org->utf16_len = save_len;
+ if (iter->org_start < iter->utf16_base)
+ {
+ iter->utf8_base = 0;
+ iter->utf16_base = 0;
+ }
+ u_strToUTF8(0, 0, &len1,
+ iter->org->utf16 + iter->utf16_base,
+ iter->org_start - iter->utf16_base,
+ &status);
+
+ status = U_ZERO_ERROR;
+
+ *start = len1 + iter->utf8_base;
+
+ u_strToUTF8(0, 0, &len2,
+ iter->org->utf16 + iter->utf16_base,
+ iter->org_start - iter->utf16_base + iter->org_len,
+ &status);
+
+ *len = len2 - len1;
+
+ if (cstr)
+ {
+ if (!iter->org8)
+ iter->org8 = icu_buf_utf8_create(0);
+ status = U_ZERO_ERROR;
+ icu_utf16_to_utf8(iter->org8, iter->org, &status);
+ *cstr = icu_buf_utf8_to_cstr(iter->org8);
+ }
+ iter->utf8_base = *start;
+ iter->utf16_base = iter->org_start;
+}
+
+void icu_iter_get_org_info(yaz_icu_iter_t iter, size_t *start, size_t *len)
+{
+ icu_iter_get_org_info2(iter, start, len, 0);
}
int icu_chain_assign_cstr(struct icu_chain *chain, const char *src8cstr,
icu_iter_get_org_info(chain->iter, start, len);
}
+void icu_chain_get_org_info2(struct icu_chain *chain, size_t *start,
+ size_t *len, const char **cstr)
+{
+ if (chain->iter)
+ icu_iter_get_org_info2(chain->iter, start, len, cstr);
+}
+
#endif /* YAZ_HAVE_ICU */