tweaking, still no good results with danish sorting
[pazpar2-moved-to-github.git] / src / icu_I18N.c
index 060030b..c0a7407 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: icu_I18N.c,v 1.1 2007-04-30 13:56:52 marc Exp $
+/* $Id: icu_I18N.c,v 1.4 2007-05-02 14:01:36 marc Exp $
    Copyright (c) 2006-2007, Index Data.
 
 This file is part of Pazpar2.
@@ -35,24 +35,56 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/log.h>
 
 #include <string.h>
+
 #include <unicode/ustring.h>  /* some more string fcns*/
+#include <unicode/uchar.h>    /* char names           */
+
 
 //#include <unicode/ustdio.h>
 //#include <unicode/utypes.h>   /* Basic ICU data types */
-//#include <unicode/ucol.h> 
+#include <unicode/ucol.h> 
 //#include <unicode/ucnv.h>     /* C   Converter API    */
-//#include <unicode/uchar.h>    /* char names           */
 //#include <unicode/uloc.h>
 //#include <unicode/ubrk.h>
 /* #include <unicode/unistr.h> */
 
 
+// forward declarations for helper functions
+
+int icu_check_status (UErrorCode status);
+
+UChar* icu_utf16_from_utf8(UChar *utf16,
+                           int32_t utf16_cap,
+                           int32_t *utf16_len,
+                           const char *utf8);
+
+UChar* icu_utf16_from_utf8n(UChar *utf16,
+                            int32_t utf16_cap,
+                            int32_t *utf16_len,
+                            const char *utf8, 
+                            size_t utf8_len);
+
+
+char* icu_utf16_to_utf8(char *utf8,           
+                        size_t utf8_cap,
+                        size_t *utf8_len,
+                        const UChar *utf16, 
+                        int32_t utf16_len);
+
+
+int32_t icu_utf16_casemap(UChar *dest16, int32_t dest16_cap,
+                          const UChar *src16, int32_t src16_len,
+                          const char *locale, char action);
+
+
+// source code of all functions
 
 int icu_check_status (UErrorCode status)
 {
-  if(U_FAILURE(status))
+    //if(U_FAILURE(status))
+    if(!U_SUCCESS(status))
       yaz_log(YLOG_WARN, 
-              "ICU Error: %d %s\n", status, u_errorName(status));
+              "ICU: %d %s\n", status, u_errorName(status));
   return status;
 }
 
@@ -177,6 +209,75 @@ char * icu_casemap(NMEM nmem, char *buf, size_t buf_cap,
 }
 
 
+struct icu_termmap * icu_termmap_create(NMEM nmem)
+{
+    struct icu_termmap *itmp =  nmem_malloc(nmem, sizeof(*itmp));
+    itmp->sort_key = 0;
+    itmp->norm_term = 0;
+    itmp->disp_term = 0;
+    return itmp;
+};
+
+int icu_termmap_cmp(const void *vp1, const void *vp2)
+{
+    struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
+    struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
+
+    return strcmp(itmp1->sort_key, itmp2->sort_key);
+}
+
+
+
+char * icu_sortmap(NMEM nmem, char *buf, size_t buf_cap, 
+                   size_t *dest8_len,  const char *src8,
+                   const char *locale)
+{
+    size_t src8_len = strlen(src8);
+    int32_t buf_len = 0;
+    char * dest8 = 0;
+    
+    if (dest8_len)
+        *dest8_len = 0;
+
+    if (!buf || !(buf_cap > 0) || !src8_len)
+        return 0;
+
+    // converting buf to utf16
+    buf = (char *)icu_utf16_from_utf8n((UChar *) buf, 
+                                       (int32_t) buf_cap, &buf_len,
+                                       src8, src8_len);
+    
+    // sort mapping
+    //buf_len = (size_t) icu_utf16_casemap((UChar *)buf, (int32_t) buf_cap,
+    //                                     (const UChar *)buf, (int32_t) buf_len,
+    //                                      locale, action);
+    
+    
+    {
+        UErrorCode status = U_ZERO_ERROR;
+
+        UCollator * coll = ucol_open (locale, &status);
+        if (U_ZERO_ERROR != icu_check_status(status))
+            buf_len = 0;
+
+        ucol_getSortKey(coll, (const UChar *) buf, (int32_t) buf_len, 
+                        (uint8_t *) buf, (int32_t) buf_cap);
+        
+        ucol_close(coll);
+    }
+    
+
+    // copying out to nmem
+    buf[buf_len] = '\0';
+
+    if(dest8_len)
+        *dest8_len = buf_len;
+
+    dest8 =  nmem_strdup(nmem, buf);
+    return dest8;
+}
+
+
 
 
 #endif // HAVE_ICU