CCL: slight reformat
[yaz-moved-to-github.git] / src / icu_chain.c
index bcb27b4..a318e64 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2013 Index Data
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
  */
 
@@ -22,6 +22,7 @@
 #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>
@@ -250,13 +251,10 @@ struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
 
     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)
@@ -267,31 +265,24 @@ struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
     {
         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;
@@ -346,6 +337,9 @@ struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
         }
         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;
         }
@@ -363,6 +357,7 @@ struct icu_iter {
     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;
@@ -370,6 +365,8 @@ struct icu_iter {
     int token_count;
     size_t org_start;
     size_t org_len;
+    size_t utf8_base;
+    size_t utf16_base;
     struct icu_chain_step *steps;
 };
 
@@ -421,6 +418,11 @@ struct icu_buf_utf16 *icu_iter_invoke(yaz_icu_iter_t iter,
                 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);
@@ -487,6 +489,7 @@ yaz_icu_iter_t icu_iter_create(struct icu_chain *chain)
     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;
@@ -501,6 +504,7 @@ void icu_iter_first(yaz_icu_iter_t iter, const char *src8cstr)
     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);
 }
@@ -513,6 +517,7 @@ void icu_iter_destroy(yaz_icu_iter_t iter)
         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);
     }
@@ -561,27 +566,48 @@ int icu_iter_get_token_number(yaz_icu_iter_t 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;
-    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,
@@ -634,6 +660,13 @@ void icu_chain_get_org_info(struct icu_chain *chain, size_t *start, size_t *len)
         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 */