Add lock/unlock for YAZ log writes YAZ-843
[yaz-moved-to-github.git] / src / icu_chain.c
index b672975..a318e64 100644 (file)
@@ -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,24 +265,17 @@ 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);
@@ -366,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;
@@ -426,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);
@@ -492,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;
@@ -519,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);
     }
@@ -567,7 +566,8 @@ 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)
 {
     int32_t len1 = 0, len2 = 0;
     UErrorCode status = U_ZERO_ERROR;
@@ -593,10 +593,23 @@ void icu_iter_get_org_info(yaz_icu_iter_t iter, size_t *start, size_t *len)
 
     *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,
                           UErrorCode *status)
 {
@@ -647,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 */