From 4aa097d555372d370f2485df38ddf93ecd327c59 Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Mon, 7 May 2007 12:52:04 +0000 Subject: [PATCH] pretty-formatted all ICU code and removed dead code sections --- src/icu_I18N.c | 635 +++++++++++++++++++-------------------------------- src/icu_I18N.h | 66 ++---- src/test_icu_I18N.c | 473 ++++++++++++-------------------------- 3 files changed, 403 insertions(+), 771 deletions(-) diff --git a/src/icu_I18N.c b/src/icu_I18N.c index 846ad4b..b7ba91d 100644 --- a/src/icu_I18N.c +++ b/src/icu_I18N.c @@ -1,23 +1,23 @@ -/* $Id: icu_I18N.c,v 1.6 2007-05-07 12:18:34 marc Exp $ +/* $Id: icu_I18N.c,v 1.7 2007-05-07 12:52:04 marc Exp $ Copyright (c) 2006-2007, Index Data. -This file is part of Pazpar2. + This file is part of Pazpar2. -Pazpar2 is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free -Software Foundation; either version 2, or (at your option) any later -version. + Pazpar2 is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any later + version. -Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. + Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. -You should have received a copy of the GNU General Public License -along with Pazpar2; see the file LICENSE. If not, write to the -Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. - */ + You should have received a copy of the GNU General Public License + along with Pazpar2; see the file LICENSE. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ #if HAVE_CONFIG_H #include "cconfig.h" @@ -57,65 +57,65 @@ int icu_check_status (UErrorCode status) { //if(U_FAILURE(status)) if(!U_SUCCESS(status)) - yaz_log(YLOG_WARN, - "ICU: %d %s\n", status, u_errorName(status)); - return status; + yaz_log(YLOG_WARN, + "ICU: %d %s\n", status, u_errorName(status)); + return status; } struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity) { - struct icu_buf_utf16 * buf16 - = (struct icu_buf_utf16 *) malloc(sizeof(struct icu_buf_utf16)); - - buf16->utf16 = 0; - buf16->utf16_len = 0; - buf16->utf16_cap = 0; - - if (capacity > 0){ - buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity); - buf16->utf16[0] = (UChar) 0; - buf16->utf16_cap = capacity; - } - return buf16; + struct icu_buf_utf16 * buf16 + = (struct icu_buf_utf16 *) malloc(sizeof(struct icu_buf_utf16)); + + buf16->utf16 = 0; + buf16->utf16_len = 0; + buf16->utf16_cap = 0; + + if (capacity > 0){ + buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity); + buf16->utf16[0] = (UChar) 0; + buf16->utf16_cap = capacity; + } + return buf16; }; struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16, size_t capacity) { - if (buf16){ - if (capacity > 0){ - if (0 == buf16->utf16) - buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity); - else - buf16->utf16 - = (UChar *) realloc(buf16->utf16, sizeof(UChar) * capacity); - buf16->utf16[0] = (UChar) 0; - buf16->utf16_len = 0; - buf16->utf16_cap = capacity; - } - else { - if (buf16->utf16) - free(buf16->utf16); - buf16->utf16 = 0; - buf16->utf16_len = 0; - buf16->utf16_cap = 0; + if (buf16){ + if (capacity > 0){ + if (0 == buf16->utf16) + buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity); + else + buf16->utf16 + = (UChar *) realloc(buf16->utf16, sizeof(UChar) * capacity); + buf16->utf16[0] = (UChar) 0; + buf16->utf16_len = 0; + buf16->utf16_cap = capacity; + } + else { + if (buf16->utf16) + free(buf16->utf16); + buf16->utf16 = 0; + buf16->utf16_len = 0; + buf16->utf16_cap = 0; + } } - } - return buf16; + return buf16; }; void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16) { - if (buf16){ - if (buf16->utf16) - free(buf16->utf16); - free(buf16); - } + if (buf16){ + if (buf16->utf16) + free(buf16->utf16); + free(buf16); + } }; @@ -125,19 +125,19 @@ void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16) struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity) { - struct icu_buf_utf8 * buf8 - = (struct icu_buf_utf8 *) malloc(sizeof(struct icu_buf_utf8)); - - buf8->utf8 = 0; - buf8->utf8_len = 0; - buf8->utf8_cap = 0; - - if (capacity > 0){ - buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity); - buf8->utf8[0] = (uint8_t) 0; - buf8->utf8_cap = capacity; - } - return buf8; + struct icu_buf_utf8 * buf8 + = (struct icu_buf_utf8 *) malloc(sizeof(struct icu_buf_utf8)); + + buf8->utf8 = 0; + buf8->utf8_len = 0; + buf8->utf8_cap = 0; + + if (capacity > 0){ + buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity); + buf8->utf8[0] = (uint8_t) 0; + buf8->utf8_cap = capacity; + } + return buf8; }; @@ -145,38 +145,38 @@ struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity) struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8, size_t capacity) { - if (buf8){ - if (capacity > 0){ - if (0 == buf8->utf8) - buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity); - else - buf8->utf8 - = (uint8_t *) realloc(buf8->utf8, sizeof(uint8_t) * capacity); - buf8->utf8[0] = (uint8_t) 0; - buf8->utf8_len = 0; - buf8->utf8_cap = capacity; - } - else { - if (buf8->utf8) - free(buf8->utf8); - buf8->utf8 = 0; - buf8->utf8_len = 0; - buf8->utf8_cap = 0; + if (buf8){ + if (capacity > 0){ + if (0 == buf8->utf8) + buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity); + else + buf8->utf8 + = (uint8_t *) realloc(buf8->utf8, sizeof(uint8_t) * capacity); + buf8->utf8[0] = (uint8_t) 0; + buf8->utf8_len = 0; + buf8->utf8_cap = capacity; + } + else { + if (buf8->utf8) + free(buf8->utf8); + buf8->utf8 = 0; + buf8->utf8_len = 0; + buf8->utf8_cap = 0; + } } - } - return buf8; + return buf8; }; void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8) { - if (buf8){ - if (buf8->utf8) - free(buf8->utf8); - free(buf8); - } + if (buf8){ + if (buf8->utf8) + free(buf8->utf8); + free(buf8); + } }; @@ -185,33 +185,33 @@ UErrorCode icu_utf16_from_utf8(struct icu_buf_utf16 * dest16, struct icu_buf_utf8 * src8, UErrorCode * status) { - int32_t utf16_len = 0; - - u_strFromUTF8(dest16->utf16, dest16->utf16_cap, - &utf16_len, - (const char *) src8->utf8, src8->utf8_len, status); + int32_t utf16_len = 0; - // check for buffer overflow, resize and retry - if (*status == U_BUFFER_OVERFLOW_ERROR - //|| dest16->utf16_len > dest16->utf16_cap - ){ - icu_buf_utf16_resize(dest16, utf16_len * 2); - *status = U_ZERO_ERROR; u_strFromUTF8(dest16->utf16, dest16->utf16_cap, &utf16_len, (const char *) src8->utf8, src8->utf8_len, status); - } + + // check for buffer overflow, resize and retry + if (*status == U_BUFFER_OVERFLOW_ERROR + //|| dest16->utf16_len > dest16->utf16_cap + ){ + icu_buf_utf16_resize(dest16, utf16_len * 2); + *status = U_ZERO_ERROR; + u_strFromUTF8(dest16->utf16, dest16->utf16_cap, + &utf16_len, + (const char *) src8->utf8, src8->utf8_len, status); + } //if (*status != U_BUFFER_OVERFLOW_ERROR - if (U_SUCCESS(*status) - && utf16_len < dest16->utf16_cap) - dest16->utf16_len = utf16_len; - else { - dest16->utf16[0] = (UChar) 0; - dest16->utf16_len = 0; - } + if (U_SUCCESS(*status) + && utf16_len < dest16->utf16_cap) + dest16->utf16_len = utf16_len; + else { + dest16->utf16[0] = (UChar) 0; + dest16->utf16_len = 0; + } - return *status; + return *status; }; @@ -220,344 +220,189 @@ UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16, const char * src8cstr, UErrorCode * status) { - size_t src8cstr_len = 0; - int32_t utf16_len = 0; + size_t src8cstr_len = 0; + int32_t utf16_len = 0; - src8cstr_len = strlen(src8cstr); - - u_strFromUTF8(dest16->utf16, dest16->utf16_cap, - &utf16_len, - src8cstr, src8cstr_len, status); + src8cstr_len = strlen(src8cstr); - // check for buffer overflow, resize and retry - if (*status == U_BUFFER_OVERFLOW_ERROR - //|| dest16->utf16_len > dest16->utf16_cap - ){ - icu_buf_utf16_resize(dest16, utf16_len * 2); - *status = U_ZERO_ERROR; u_strFromUTF8(dest16->utf16, dest16->utf16_cap, &utf16_len, src8cstr, src8cstr_len, status); - } - - // if (*status != U_BUFFER_OVERFLOW_ERROR - if (U_SUCCESS(*status) - && utf16_len < dest16->utf16_cap) - dest16->utf16_len = utf16_len; - else { - dest16->utf16[0] = (UChar) 0; - dest16->utf16_len = 0; - } - return *status; + // check for buffer overflow, resize and retry + if (*status == U_BUFFER_OVERFLOW_ERROR + //|| dest16->utf16_len > dest16->utf16_cap + ){ + icu_buf_utf16_resize(dest16, utf16_len * 2); + *status = U_ZERO_ERROR; + u_strFromUTF8(dest16->utf16, dest16->utf16_cap, + &utf16_len, + src8cstr, src8cstr_len, status); + } + + // if (*status != U_BUFFER_OVERFLOW_ERROR + if (U_SUCCESS(*status) + && utf16_len < dest16->utf16_cap) + dest16->utf16_len = utf16_len; + else { + dest16->utf16[0] = (UChar) 0; + dest16->utf16_len = 0; + } + + return *status; }; UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8, - struct icu_buf_utf16 * src16, - UErrorCode * status) + struct icu_buf_utf16 * src16, + UErrorCode * status) { - int32_t utf8_len = 0; + int32_t utf8_len = 0; - u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap, - &utf8_len, - src16->utf16, src16->utf16_len, status); - - // check for buffer overflow, resize and retry - if (*status == U_BUFFER_OVERFLOW_ERROR - //|| dest8->utf8_len > dest8->utf8_cap - ){ - icu_buf_utf8_resize(dest8, utf8_len * 2); - *status = U_ZERO_ERROR; u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap, &utf8_len, src16->utf16, src16->utf16_len, status); - - } - - //if (*status != U_BUFFER_OVERFLOW_ERROR - if (U_SUCCESS(*status) - && utf8_len < dest8->utf8_cap) - dest8->utf8_len = utf8_len; - else { - dest8->utf8[0] = (uint8_t) 0; - dest8->utf8_len = 0; - } - return *status; -}; - - + // check for buffer overflow, resize and retry + if (*status == U_BUFFER_OVERFLOW_ERROR + //|| dest8->utf8_len > dest8->utf8_cap + ){ + icu_buf_utf8_resize(dest8, utf8_len * 2); + *status = U_ZERO_ERROR; + u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap, + &utf8_len, + src16->utf16, src16->utf16_len, status); + } -UErrorCode icu_sortkey8_from_utf16(UCollator *coll, - struct icu_buf_utf8 * dest8, - struct icu_buf_utf16 * src16, - UErrorCode * status) -{ + //if (*status != U_BUFFER_OVERFLOW_ERROR + if (U_SUCCESS(*status) + && utf8_len < dest8->utf8_cap) + dest8->utf8_len = utf8_len; + else { + dest8->utf8[0] = (uint8_t) 0; + dest8->utf8_len = 0; + } - int32_t sortkey_len = 0; - - sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len, - dest8->utf8, dest8->utf8_cap); - - // check for buffer overflow, resize and retry - if (sortkey_len > dest8->utf8_cap) { - icu_buf_utf8_resize(dest8, sortkey_len * 2); - sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len, - dest8->utf8, dest8->utf8_cap); - } - - if (U_SUCCESS(*status) - && sortkey_len > 0) - dest8->utf8_len = sortkey_len; - else { - dest8->utf8[0] = (UChar) 0; - dest8->utf8_len = 0; - } - - return *status; + return *status; }; - - -/// CRAP FOLLOWING HERE ... - -#if 0 - -// 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) +int icu_utf16_casemap(struct icu_buf_utf16 * dest16, + struct icu_buf_utf16 * src16, + const char *locale, char action, + UErrorCode *status) { - //if(U_FAILURE(status)) - if(!U_SUCCESS(status)) - yaz_log(YLOG_WARN, - "ICU: %d %s\n", status, u_errorName(status)); - return status; -} - - -UChar* icu_utf16_from_utf8(UChar *utf16, - int32_t utf16_cap, - int32_t *utf16_len, - const char *utf8) -{ - size_t utf8_len = strlen(utf8); - return icu_utf16_from_utf8n(utf16, utf16_cap, utf16_len, - utf8, utf8_len); -} - - -UChar* icu_utf16_from_utf8n(UChar *utf16, - int32_t utf16_cap, - int32_t *utf16_len, - const char *utf8, - size_t utf8_len) -{ - UErrorCode status = U_ZERO_ERROR; - u_strFromUTF8(utf16, utf16_cap, utf16_len, utf8, (int32_t) utf8_len, - &status); - if (U_ZERO_ERROR != icu_check_status(status)) - return 0; - else - return utf16; -} - - -char* icu_utf16_to_utf8(char *utf8, - size_t utf8_cap, - size_t *utf8_len, - const UChar *utf16, - int32_t utf16_len) -{ - UErrorCode status = U_ZERO_ERROR; - u_strToUTF8(utf8, (int32_t) utf8_cap, (int32_t *)utf8_len, - utf16, utf16_len, &status); - if (U_ZERO_ERROR != icu_check_status(status)) - return 0; - else - return utf8; -} - - -int32_t icu_utf16_casemap(UChar *dest16, int32_t dest16_cap, - const UChar *src16, int32_t src16_len, - const char *locale, char action) -{ - UErrorCode status = U_ZERO_ERROR; int32_t dest16_len = 0; switch(action) { case 'l': - dest16_len = u_strToLower(dest16, dest16_cap, src16, src16_len, - locale, &status); + dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + locale, status); break; case 'u': - dest16_len = u_strToUpper(dest16, dest16_cap, src16, src16_len, - locale, &status); + dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + locale, status); break; case 't': - dest16_len = u_strToTitle(dest16, dest16_cap, src16, src16_len, - 0, locale, &status); + dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + 0, locale, status); break; case 'f': - dest16_len = u_strFoldCase(dest16, dest16_cap, src16, src16_len, - U_FOLD_CASE_DEFAULT, &status); + dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + U_FOLD_CASE_DEFAULT, status); break; default: - return 0; + return U_UNSUPPORTED_ERROR; break; } - if (U_ZERO_ERROR != icu_check_status(status)) - return 0; - else - return dest16_len; -} - - -char * icu_casemap(NMEM nmem, char *buf, size_t buf_cap, - size_t *dest8_len, const char *src8, - const char *locale, char action) -{ - size_t src8_len = strlen(src8); - int32_t buf_len = 0; - char * dest8 = 0; - - if (dest8_len) - *dest8_len = 0; + // check for buffer overflow, resize and retry + if (*status == U_BUFFER_OVERFLOW_ERROR + //|| dest16_len > dest16->utf16_cap + ){ + icu_buf_utf16_resize(dest16, dest16_len * 2); + *status = U_ZERO_ERROR; - 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); - // case mapping - buf_len = (size_t) icu_utf16_casemap((UChar *)buf, (int32_t) buf_cap, - (const UChar *)buf, (int32_t) buf_len, - locale, action); - - // converting buf to utf8 - buf = icu_utf16_to_utf8(buf, buf_cap, (size_t *) &buf_len, - (const UChar *) buf, (int32_t) buf_len); - + switch(action) { + case 'l': + dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + locale, status); + break; + case 'u': + dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + locale, status); + break; + case 't': + dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + 0, locale, status); + break; + case 'f': + dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + U_FOLD_CASE_DEFAULT, status); + break; + + default: + return U_UNSUPPORTED_ERROR; + break; + } + } - // copying out to nmem - buf[buf_len] = '\0'; - - if(dest8_len) - *dest8_len = buf_len; - - dest8 = nmem_strdup(nmem, buf); - return dest8; -} - - -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; + if (U_SUCCESS(*status) + && dest16_len < dest16->utf16_cap) + dest16->utf16_len = dest16_len; + else { + dest16->utf16[0] = (UChar) 0; + dest16->utf16_len = 0; + } + + return *status; }; -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; +UErrorCode icu_sortkey8_from_utf16(UCollator *coll, + struct icu_buf_utf8 * dest8, + struct icu_buf_utf16 * src16, + UErrorCode * status) +{ + + int32_t sortkey_len = 0; - UCollator * coll = ucol_open (locale, &status); - if (U_ZERO_ERROR != icu_check_status(status)) - buf_len = 0; + sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len, + dest8->utf8, dest8->utf8_cap); - ucol_getSortKey(coll, (const UChar *) buf, (int32_t) buf_len, - (uint8_t *) buf, (int32_t) buf_cap); - - ucol_close(coll); + // check for buffer overflow, resize and retry + if (sortkey_len > dest8->utf8_cap) { + icu_buf_utf8_resize(dest8, sortkey_len * 2); + sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len, + dest8->utf8, dest8->utf8_cap); } - - // copying out to nmem - buf[buf_len] = '\0'; - - if(dest8_len) - *dest8_len = buf_len; + if (U_SUCCESS(*status) + && sortkey_len > 0) + dest8->utf8_len = sortkey_len; + else { + dest8->utf8[0] = (UChar) 0; + dest8->utf8_len = 0; + } - dest8 = nmem_strdup(nmem, buf); - return dest8; -} + return *status; +}; -#endif diff --git a/src/icu_I18N.h b/src/icu_I18N.h index b0a91d9..803d89b 100644 --- a/src/icu_I18N.h +++ b/src/icu_I18N.h @@ -1,23 +1,23 @@ -/* $Id: icu_I18N.h,v 1.6 2007-05-07 12:18:34 marc Exp $ +/* $Id: icu_I18N.h,v 1.7 2007-05-07 12:52:04 marc Exp $ Copyright (c) 2006-2007, Index Data. -This file is part of Pazpar2. + This file is part of Pazpar2. -Pazpar2 is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free -Software Foundation; either version 2, or (at your option) any later -version. + Pazpar2 is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any later + version. -Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. + Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. -You should have received a copy of the GNU General Public License -along with Pazpar2; see the file LICENSE. If not, write to the -Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. - */ + You should have received a copy of the GNU General Public License + along with Pazpar2; see the file LICENSE. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ #ifndef ICU_I18NL_H #define ICU_I18NL_H @@ -81,6 +81,10 @@ UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8, struct icu_buf_utf16 * src16, UErrorCode * status); +int icu_utf16_casemap(struct icu_buf_utf16 * dest16, + struct icu_buf_utf16 * src16, + const char *locale, char action, + UErrorCode *status); UErrorCode icu_sortkey8_from_utf16(UCollator *coll, struct icu_buf_utf8 * dest8, @@ -88,37 +92,5 @@ UErrorCode icu_sortkey8_from_utf16(UCollator *coll, UErrorCode * status); - - - - - - - -// CRAP to Follow here ... - -#if 0 -struct icu_termmap -{ - char * sort_key; // standard C string '\0' terminated - char * norm_term; // standard C utf-8 string - char * disp_term; // standard C utf-8 string -}; - -struct icu_termmap * icu_termmap_create(NMEM nmem); - -int icu_termmap_cmp(const void *vp1, const void *vp2); - -char * icu_casemap(NMEM nmem, char *buf, size_t buf_cap, - size_t *dest8_len, const char *src8, - const char *locale, char action); - -char * icu_sortmap(NMEM nmem, char *buf, size_t buf_cap, - size_t *dest8_len, const char *src8, - const char *locale); - -#endif // 0 - - #endif // HAVE_ICU #endif // ICU_I18NL_H diff --git a/src/test_icu_I18N.c b/src/test_icu_I18N.c index 93428ad..c9d3e39 100644 --- a/src/test_icu_I18N.c +++ b/src/test_icu_I18N.c @@ -1,23 +1,23 @@ -/* $Id: test_icu_I18N.c,v 1.9 2007-05-07 12:18:34 marc Exp $ +/* $Id: test_icu_I18N.c,v 1.10 2007-05-07 12:52:04 marc Exp $ Copyright (c) 2006-2007, Index Data. -This file is part of Pazpar2. + This file is part of Pazpar2. -Pazpar2 is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free -Software Foundation; either version 2, or (at your option) any later -version. + Pazpar2 is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any later + version. -Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. + Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. -You should have received a copy of the GNU General Public License -along with Pazpar2; see the file LICENSE. If not, write to the -Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. - */ + You should have received a copy of the GNU General Public License + along with Pazpar2; see the file LICENSE. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 @@ -41,12 +41,11 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include -#include +//#include // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 #define MAX_KEY_SIZE 256 - struct icu_termmap { uint8_t sort_key[MAX_KEY_SIZE]; // standard C string '\0' terminated @@ -69,88 +68,6 @@ int icu_termmap_cmp(const void *vp1, const void *vp2) -int icu_utf16_casemap(struct icu_buf_utf16 * dest16, - struct icu_buf_utf16 * src16, - const char *locale, char action, - UErrorCode *status) -{ - int32_t dest16_len = 0; - - switch(action) { - case 'l': - dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - locale, status); - break; - case 'u': - dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - locale, status); - break; - case 't': - dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - 0, locale, status); - break; - case 'f': - dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - U_FOLD_CASE_DEFAULT, status); - break; - - default: - return U_UNSUPPORTED_ERROR; - break; - } - - // check for buffer overflow, resize and retry - if (*status == U_BUFFER_OVERFLOW_ERROR - //|| dest16_len > dest16->utf16_cap - ){ - icu_buf_utf16_resize(dest16, dest16_len * 2); - *status = U_ZERO_ERROR; - - - switch(action) { - case 'l': - dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - locale, status); - break; - case 'u': - dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - locale, status); - break; - case 't': - dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - 0, locale, status); - break; - case 'f': - dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - U_FOLD_CASE_DEFAULT, status); - break; - - default: - return U_UNSUPPORTED_ERROR; - break; - } - } - - if (U_SUCCESS(*status) - && dest16_len < dest16->utf16_cap) - dest16->utf16_len = dest16_len; - else { - dest16->utf16[0] = (UChar) 0; - dest16->utf16_len = 0; - } - - return *status; -}; - - int test_icu_casemap(const char * locale, char action, const char * src8cstr, const char * chk8cstr) @@ -207,42 +124,6 @@ int test_icu_casemap(const char * locale, char action, -#if 0 - -int test_icu_casemap(const char * locale, char action, - const char * src8, const char * check8) -{ - NMEM nmem = nmem_create(); - size_t buf_cap = 128; - char buf[buf_cap]; - const char * dest8 = 0; - size_t dest8_len = 0; - //size_t src8_len = strlen(src8); - int sucess = 0; - - //printf("original string: '%s' (%d)\n", src8, (int) src8_len); - - //these shall succeed - dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len, - src8, locale, action); - - - //printf("icu_casemap '%s:%c' '%s' (%d)\n", - // locale, action, dest8, (int) dest8_len); - - if (dest8 - && (dest8_len == strlen(check8)) - && !strcmp(check8, dest8)) - sucess = dest8_len; - - nmem_destroy(nmem); - - return sucess; -} - -#endif - - // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 void test_icu_I18N_casemap(int argc, char **argv) @@ -291,232 +172,106 @@ void test_icu_I18N_casemap(int argc, char **argv) // sucess expected YAZ_CHECK(test_icu_casemap("de", 'l', - "zWÖlf ärgerliche Würste rollen ÜBer die StRAße", - "zwölf ärgerliche würste rollen über die straße")); + "zWÖlf ärgerliche Würste rollen ÜBer die StRAße", + "zwölf ärgerliche würste rollen über die straße")); YAZ_CHECK(test_icu_casemap("de", 'u', - "zWÖlf ärgerliche Würste rollen ÜBer die StRAße", - "ZWÖLF ÄRGERLICHE WÜRSTE ROLLEN ÜBER DIE STRASSE")); + "zWÖlf ärgerliche Würste rollen ÜBer die StRAße", + "ZWÖLF ÄRGERLICHE WÜRSTE ROLLEN ÜBER DIE STRASSE")); YAZ_CHECK(test_icu_casemap("de", 'f', - "zWÖlf ärgerliche Würste rollen ÜBer die StRAße", - "zwölf ärgerliche würste rollen über die strasse")); + "zWÖlf ärgerliche Würste rollen ÜBer die StRAße", + "zwölf ärgerliche würste rollen über die strasse")); YAZ_CHECK(test_icu_casemap("de", 't', - "zWÖlf ärgerliche Würste rollen ÜBer die StRAße", - "Zwölf Ärgerliche Würste Rollen Über Die Straße")); + "zWÖlf ärgerliche Würste rollen ÜBer die StRAße", + "Zwölf Ärgerliche Würste Rollen Über Die Straße")); } -#if 0 - // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 -void test_icu_I18N_casemap_failures(int argc, char **argv) +int test_icu_sortmap(const char * locale, int src_list_len, + const char ** src_list, const char ** chk_list) { + int success = 1; - size_t buf_cap = 128; - char buf[buf_cap]; - size_t dest8_len = 0; - NMEM nmem = nmem_create(); - char * dest8 = 0; - - const char * src8 = "A ReD fOx hunTS sQUirriLs"; - //size_t src8_len = strlen(src8); - - //printf("original string: '%s' (%d)\n", src8, (int) src8_len); - - // some calling error needs investigation - dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len, - src8, "en", 't'); - YAZ_CHECK(0 == dest8_len); - //printf("icu_casemap 'en:t' '%s' (%d)\n", dest8, (int) dest8_len); - - - // attention: does not fail even if no locale 'xy_zz' defined - // it seems to default to english locale - dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len, - src8, "zz_abc", 'l'); - YAZ_CHECK(dest8_len); - //printf("icu_casemap 'zz:l' '%s' (%d)\n", dest8, (int) dest8_len); - - - // shall fail - no buf buffer defined - dest8 = icu_casemap(nmem, 0, buf_cap, &dest8_len, - src8, "en", 'l'); - YAZ_CHECK(0 == dest8_len); - //printf("icu_casemap 'en:l' '%s' (%d)\n", dest8, (int) dest8_len); + UErrorCode status = U_ZERO_ERROR; - // shall fail - no buf_cap defined - dest8 = icu_casemap(nmem, buf, 0, &dest8_len, - src8, "en", 'l'); - YAZ_CHECK(0 == dest8_len); - //printf("icu_casemap 'en:l' '%s' (%d)\n", dest8, (int) dest8_len); + struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0); + struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0); - // shall fail - no action 'x' defined - dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len, - src8, "en", 'x'); - YAZ_CHECK(0 == dest8_len); - //printf("icu_casemap 'en:x' '%s' (%d)\n", dest8, (int) dest8_len); + int i; - nmem_destroy(nmem); -} + struct icu_termmap * list[src_list_len]; + UCollator *coll = ucol_open(locale, &status); + icu_check_status(status); + if(!U_SUCCESS(status)) + return 0; -#endif + // assigning display terms and sort keys using buf 8 and buf16 + for( i = 0; i < src_list_len; i++) + { -// DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 + list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap)); -#if 0 -int test_icu_sortmap(const char * locale, size_t list_len, - const char ** src8_list, const char ** check8_list) -{ - int sucess = 1; + // copy display term + strcpy(list[i]->disp_term, src_list[i]); - size_t i = 0; + // transforming to UTF16 + icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status); + icu_check_status(status); + // computing sortkeys + icu_sortkey8_from_utf16(coll, buf8, buf16, &status); + icu_check_status(status); + + // assigning sortkeys + memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len); + //strncpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len); + //strcpy((char *) list[i]->sort_key, (const char *) buf8->utf8); + } - NMEM nmem = nmem_create(); - size_t buf_cap = 128; - char buf[buf_cap]; - struct icu_termmap ** dest8_list - = nmem_malloc(nmem, sizeof(struct icu_termmap *) * list_len); - //size_t dest8_len = 0; - //size_t src8_len = strlen(src8); - - // initializing icu_termmap - for (i = 0; i < list_len; i++){ - dest8_list[i] = icu_termmap_create(nmem); - dest8_list[i]->norm_term = nmem_strdup(nmem, src8_list[i]); - dest8_list[i]->disp_term = nmem_strdup(nmem, src8_list[i]); - //dest8_list[i]->sort_key = nmem_strdup(nmem, src8_list[i]); - //dest8_list[i]->sort_len = strlen(src8_list[i]); - dest8_list[i]->sort_key - = icu_sortmap(nmem, buf, buf_cap, 0, src8_list[i], locale); - // = icu_sortmap(nmem, buf, buf_cap, &(dest8_list[i]->sort_len), - // src8_list[i], locale); - } // do the sorting - qsort(dest8_list, list_len, + qsort(list, src_list_len, sizeof(struct icu_termmap *), icu_termmap_cmp); // checking correct sorting - for (i = 0; i < list_len; i++){ - if (0 != strcmp(dest8_list[i]->disp_term, check8_list[i])){ - sucess = 0; + for (i = 0; i < src_list_len; i++){ + if (0 != strcmp(list[i]->disp_term, chk_list[i])){ + success = 0; } } - if (1 || !sucess){ - printf("\n"); - printf("Input '%s':", locale); - for (i = 0; i < list_len; i++) - printf(" '%s'", src8_list[i]); - printf("\n"); - printf("ICU sort '%s':", locale); - for (i = 0; i < list_len; i++) - printf(" '%s'", dest8_list[i]->disp_term); - if (sucess) - printf(" OK"); - else - printf(" ERROR ??"); + if(!success){ + printf("\nERROR\n"); + printf("Input str: '%s' : ", locale); + for (i = 0; i < src_list_len; i++) { + printf(" '%s'", list[i]->disp_term); + } printf("\n"); - printf("Expected '%s':", locale); - for (i = 0; i < list_len; i++) - printf(" '%s'", check8_list[i]); - printf("\n"); - } - - nmem_destroy(nmem); - - return sucess; -} - -#else - -int test_icu_sortmap(const char * locale, int src_list_len, - const char ** src_list, const char ** chk_list) -{ - int success = 1; - - UErrorCode status = U_ZERO_ERROR; - - struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0); - struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0); - - int i; - - struct icu_termmap * list[src_list_len]; - - UCollator *coll = ucol_open(locale, &status); - icu_check_status(status); - - if(!U_SUCCESS(status)) - return 0; - - // assigning display terms and sort keys using buf 8 and buf16 - for( i = 0; i < src_list_len; i++) - { - - list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap)); - - // copy display term - strcpy(list[i]->disp_term, src_list[i]); - - // transforming to UTF16 - icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status); - icu_check_status(status); - - // computing sortkeys - icu_sortkey8_from_utf16(coll, buf8, buf16, &status); - icu_check_status(status); - - // assigning sortkeys - memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len); - //strncpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len); - //strcpy((char *) list[i]->sort_key, (const char *) buf8->utf8); - } - - - // do the sorting - qsort(list, src_list_len, - sizeof(struct icu_termmap *), icu_termmap_cmp); - - // checking correct sorting - for (i = 0; i < src_list_len; i++){ - if (0 != strcmp(list[i]->disp_term, chk_list[i])){ - success = 0; + printf("ICU sort: '%s' : ", locale); + for (i = 0; i < src_list_len; i++) { + printf(" '%s'", list[i]->disp_term); + //printf("(%d|%d)", list[i]->sort_key[0],list[i]->sort_key[1]); + } + printf("\n"); + printf("Expected: '%s' : ", locale); + for (i = 0; i < src_list_len; i++) { + printf(" '%s'", chk_list[i]); + } + printf("\n"); } - } - - if(!success){ - printf("\nERROR\n"); - printf("Input str: '%s' : ", locale); - for (i = 0; i < src_list_len; i++) { - printf(" '%s'", list[i]->disp_term); - } - printf("\n"); - printf("ICU sort: '%s' : ", locale); - for (i = 0; i < src_list_len; i++) { - printf(" '%s'", list[i]->disp_term); - //printf("(%d|%d)", list[i]->sort_key[0],list[i]->sort_key[1]); - } - printf("\n"); - printf("Expected: '%s' : ", locale); - for (i = 0; i < src_list_len; i++) { - printf(" '%s'", chk_list[i]); - } - printf("\n"); - } - ucol_close(coll); + ucol_close(coll); - icu_buf_utf8_destroy(buf8); - icu_buf_utf16_destroy(buf16); + icu_buf_utf8_destroy(buf8); + icu_buf_utf16_destroy(buf16); @@ -524,9 +279,6 @@ int test_icu_sortmap(const char * locale, int src_list_len, } -#endif - - // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 void test_icu_I18N_sortmap(int argc, char **argv) @@ -560,7 +312,7 @@ void test_icu_I18N_sortmap(int argc, char **argv) } -#endif +#endif // HAVE_ICU // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 @@ -576,13 +328,13 @@ int main(int argc, char **argv) test_icu_I18N_casemap(argc, argv); test_icu_I18N_sortmap(argc, argv); -#else +#else // HAVE_ICU printf("ICU unit tests omitted.\n" "Please install libicu36-dev and icu-doc or similar\n"); YAZ_CHECK(0 == 0); -#endif +#endif // HAVE_ICU YAZ_CHECK_TERM; } @@ -590,6 +342,69 @@ int main(int argc, char **argv) // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 + + +// CRAP to follow +#if 0 + +// DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 + +void test_icu_I18N_casemap_failures(int argc, char **argv) +{ + + size_t buf_cap = 128; + char buf[buf_cap]; + size_t dest8_len = 0; + NMEM nmem = nmem_create(); + char * dest8 = 0; + + const char * src8 = "A ReD fOx hunTS sQUirriLs"; + //size_t src8_len = strlen(src8); + + //printf("original string: '%s' (%d)\n", src8, (int) src8_len); + + // some calling error needs investigation + dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len, + src8, "en", 't'); + YAZ_CHECK(0 == dest8_len); + //printf("icu_casemap 'en:t' '%s' (%d)\n", dest8, (int) dest8_len); + + + // attention: does not fail even if no locale 'xy_zz' defined + // it seems to default to english locale + dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len, + src8, "zz_abc", 'l'); + YAZ_CHECK(dest8_len); + //printf("icu_casemap 'zz:l' '%s' (%d)\n", dest8, (int) dest8_len); + + + // shall fail - no buf buffer defined + dest8 = icu_casemap(nmem, 0, buf_cap, &dest8_len, + src8, "en", 'l'); + YAZ_CHECK(0 == dest8_len); + //printf("icu_casemap 'en:l' '%s' (%d)\n", dest8, (int) dest8_len); + + // shall fail - no buf_cap defined + dest8 = icu_casemap(nmem, buf, 0, &dest8_len, + src8, "en", 'l'); + YAZ_CHECK(0 == dest8_len); + //printf("icu_casemap 'en:l' '%s' (%d)\n", dest8, (int) dest8_len); + + // shall fail - no action 'x' defined + dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len, + src8, "en", 'x'); + YAZ_CHECK(0 == dest8_len); + //printf("icu_casemap 'en:x' '%s' (%d)\n", dest8, (int) dest8_len); + + nmem_destroy(nmem); +} + + + +#endif + + + /* * Local variables: * c-basic-offset: 4 -- 1.7.10.4