1/* 2******************************************************************************* 3* Copyright (C) 1996-2010, International Business Machines 4* Corporation and others. All Rights Reserved. 5******************************************************************************* 6* file name: ucol_res.cpp 7* encoding: US-ASCII 8* tab size: 8 (not used) 9* indentation:4 10* 11* Description: 12* This file contains dependencies that the collation run-time doesn't normally 13* need. This mainly contains resource bundle usage and collation meta information 14* 15* Modification history 16* Date Name Comments 17* 1996-1999 various members of ICU team maintained C API for collation framework 18* 02/16/2001 synwee Added internal method getPrevSpecialCE 19* 03/01/2001 synwee Added maxexpansion functionality. 20* 03/16/2001 weiv Collation framework is rewritten in C and made UCA compliant 21* 12/08/2004 grhoten Split part of ucol.cpp into ucol_res.cpp 22*/ 23 24#include "unicode/utypes.h" 25 26#if !UCONFIG_NO_COLLATION 27#include "unicode/uloc.h" 28#include "unicode/coll.h" 29#include "unicode/tblcoll.h" 30#include "unicode/caniter.h" 31#include "unicode/ustring.h" 32 33#include "ucol_bld.h" 34#include "ucol_imp.h" 35#include "ucol_tok.h" 36#include "ucol_elm.h" 37#include "uresimp.h" 38#include "ustr_imp.h" 39#include "cstring.h" 40#include "umutex.h" 41#include "ucln_in.h" 42#include "ustrenum.h" 43#include "putilimp.h" 44#include "utracimp.h" 45#include "cmemory.h" 46#include "uenumimp.h" 47#include "ulist.h" 48 49U_NAMESPACE_USE 50 51// static UCA. There is only one. Collators don't use it. 52// It is referenced only in ucol_initUCA and ucol_cleanup 53static UCollator* _staticUCA = NULL; 54// static pointer to udata memory. Inited in ucol_initUCA 55// used for cleanup in ucol_cleanup 56static UDataMemory* UCA_DATA_MEM = NULL; 57 58U_CDECL_BEGIN 59static UBool U_CALLCONV 60ucol_res_cleanup(void) 61{ 62 if (UCA_DATA_MEM) { 63 udata_close(UCA_DATA_MEM); 64 UCA_DATA_MEM = NULL; 65 } 66 if (_staticUCA) { 67 ucol_close(_staticUCA); 68 _staticUCA = NULL; 69 } 70 return TRUE; 71} 72 73static UBool U_CALLCONV 74isAcceptableUCA(void * /*context*/, 75 const char * /*type*/, const char * /*name*/, 76 const UDataInfo *pInfo){ 77 /* context, type & name are intentionally not used */ 78 if( pInfo->size>=20 && 79 pInfo->isBigEndian==U_IS_BIG_ENDIAN && 80 pInfo->charsetFamily==U_CHARSET_FAMILY && 81 pInfo->dataFormat[0]==UCA_DATA_FORMAT_0 && /* dataFormat="UCol" */ 82 pInfo->dataFormat[1]==UCA_DATA_FORMAT_1 && 83 pInfo->dataFormat[2]==UCA_DATA_FORMAT_2 && 84 pInfo->dataFormat[3]==UCA_DATA_FORMAT_3 && 85 pInfo->formatVersion[0]==UCA_FORMAT_VERSION_0 && 86 pInfo->formatVersion[1]>=UCA_FORMAT_VERSION_1// && 87 //pInfo->formatVersion[1]==UCA_FORMAT_VERSION_1 && 88 //pInfo->formatVersion[2]==UCA_FORMAT_VERSION_2 && // Too harsh 89 //pInfo->formatVersion[3]==UCA_FORMAT_VERSION_3 && // Too harsh 90 ) { 91 UVersionInfo UCDVersion; 92 u_getUnicodeVersion(UCDVersion); 93 return (UBool)(pInfo->dataVersion[0]==UCDVersion[0] 94 && pInfo->dataVersion[1]==UCDVersion[1]); 95 //&& pInfo->dataVersion[2]==ucaDataInfo.dataVersion[2] 96 //&& pInfo->dataVersion[3]==ucaDataInfo.dataVersion[3]); 97 } else { 98 return FALSE; 99 } 100} 101U_CDECL_END 102 103/* do not close UCA returned by ucol_initUCA! */ 104UCollator * 105ucol_initUCA(UErrorCode *status) { 106 if(U_FAILURE(*status)) { 107 return NULL; 108 } 109 UBool needsInit; 110 UMTX_CHECK(NULL, (_staticUCA == NULL), needsInit); 111 112 if(needsInit) { 113 UDataMemory *result = udata_openChoice(U_ICUDATA_COLL, UCA_DATA_TYPE, UCA_DATA_NAME, isAcceptableUCA, NULL, status); 114 115 if(U_SUCCESS(*status)){ 116 UCollator *newUCA = ucol_initCollator((const UCATableHeader *)udata_getMemory(result), NULL, NULL, status); 117 if(U_SUCCESS(*status)){ 118 // Initalize variables for implicit generation 119 uprv_uca_initImplicitConstants(status); 120 121 umtx_lock(NULL); 122 if(_staticUCA == NULL) { 123 UCA_DATA_MEM = result; 124 _staticUCA = newUCA; 125 newUCA = NULL; 126 result = NULL; 127 } 128 umtx_unlock(NULL); 129 130 ucln_i18n_registerCleanup(UCLN_I18N_UCOL_RES, ucol_res_cleanup); 131 if(newUCA != NULL) { 132 ucol_close(newUCA); 133 udata_close(result); 134 } 135 }else{ 136 ucol_close(newUCA); 137 udata_close(result); 138 } 139 } 140 else { 141 udata_close(result); 142 } 143 } 144 return _staticUCA; 145} 146 147U_CAPI void U_EXPORT2 148ucol_forgetUCA(void) 149{ 150 _staticUCA = NULL; 151 UCA_DATA_MEM = NULL; 152} 153 154/****************************************************************************/ 155/* Following are the open/close functions */ 156/* */ 157/****************************************************************************/ 158static UCollator* 159tryOpeningFromRules(UResourceBundle *collElem, UErrorCode *status) { 160 int32_t rulesLen = 0; 161 const UChar *rules = ures_getStringByKey(collElem, "Sequence", &rulesLen, status); 162 return ucol_openRules(rules, rulesLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, status); 163} 164 165 166// API in ucol_imp.h 167 168U_CFUNC UCollator* 169ucol_open_internal(const char *loc, 170 UErrorCode *status) 171{ 172 UErrorCode intStatus = U_ZERO_ERROR; 173 const UCollator* UCA = ucol_initUCA(status); 174 175 /* New version */ 176 if(U_FAILURE(*status)) return 0; 177 178 179 180 UCollator *result = NULL; 181 UResourceBundle *b = ures_open(U_ICUDATA_COLL, loc, status); 182 183 /* we try to find stuff from keyword */ 184 UResourceBundle *collations = ures_getByKey(b, "collations", NULL, status); 185 UResourceBundle *collElem = NULL; 186 char keyBuffer[256]; 187 // if there is a keyword, we pick it up and try to get elements 188 if(!uloc_getKeywordValue(loc, "collation", keyBuffer, 256, status) || 189 !uprv_strcmp(keyBuffer,"default")) { /* Treat 'zz@collation=default' as 'zz'. */ 190 // no keyword. we try to find the default setting, which will give us the keyword value 191 intStatus = U_ZERO_ERROR; 192 // finding default value does not affect collation fallback status 193 UResourceBundle *defaultColl = ures_getByKeyWithFallback(collations, "default", NULL, &intStatus); 194 if(U_SUCCESS(intStatus)) { 195 int32_t defaultKeyLen = 0; 196 const UChar *defaultKey = ures_getString(defaultColl, &defaultKeyLen, &intStatus); 197 u_UCharsToChars(defaultKey, keyBuffer, defaultKeyLen); 198 keyBuffer[defaultKeyLen] = 0; 199 } else { 200 *status = U_INTERNAL_PROGRAM_ERROR; 201 return NULL; 202 } 203 ures_close(defaultColl); 204 } 205 collElem = ures_getByKeyWithFallback(collations, keyBuffer, collations, status); 206 collations = NULL; // We just reused the collations object as collElem. 207 208 UResourceBundle *binary = NULL; 209 210 if(*status == U_MISSING_RESOURCE_ERROR) { /* We didn't find the tailoring data, we fallback to the UCA */ 211 *status = U_USING_DEFAULT_WARNING; 212 result = ucol_initCollator(UCA->image, result, UCA, status); 213 if (U_FAILURE(*status)) { 214 goto clean; 215 } 216 // if we use UCA, real locale is root 217 ures_close(b); 218 b = ures_open(U_ICUDATA_COLL, "", status); 219 ures_close(collElem); 220 collElem = ures_open(U_ICUDATA_COLL, "", status); 221 if(U_FAILURE(*status)) { 222 goto clean; 223 } 224 result->hasRealData = FALSE; 225 } else if(U_SUCCESS(*status)) { 226 intStatus = U_ZERO_ERROR; 227 228 binary = ures_getByKey(collElem, "%%CollationBin", NULL, &intStatus); 229 230 if(intStatus == U_MISSING_RESOURCE_ERROR) { /* we didn't find the binary image, we should use the rules */ 231 binary = NULL; 232 result = tryOpeningFromRules(collElem, status); 233 if(U_FAILURE(*status)) { 234 goto clean; 235 } 236 } else if(U_SUCCESS(intStatus)) { /* otherwise, we'll pick a collation data that exists */ 237 int32_t len = 0; 238 const uint8_t *inData = ures_getBinary(binary, &len, status); 239 if(U_FAILURE(*status)) { 240 goto clean; 241 } 242 UCATableHeader *colData = (UCATableHeader *)inData; 243 if(uprv_memcmp(colData->UCAVersion, UCA->image->UCAVersion, sizeof(UVersionInfo)) != 0 || 244 uprv_memcmp(colData->UCDVersion, UCA->image->UCDVersion, sizeof(UVersionInfo)) != 0 || 245 colData->version[0] != UCOL_BUILDER_VERSION) 246 { 247 *status = U_DIFFERENT_UCA_VERSION; 248 result = tryOpeningFromRules(collElem, status); 249 } else { 250 if(U_FAILURE(*status)){ 251 goto clean; 252 } 253 if((uint32_t)len > (paddedsize(sizeof(UCATableHeader)) + paddedsize(sizeof(UColOptionSet)))) { 254 result = ucol_initCollator((const UCATableHeader *)inData, result, UCA, status); 255 if(U_FAILURE(*status)){ 256 goto clean; 257 } 258 result->hasRealData = TRUE; 259 } else { 260 result = ucol_initCollator(UCA->image, result, UCA, status); 261 ucol_setOptionsFromHeader(result, (UColOptionSet *)(inData+((const UCATableHeader *)inData)->options), status); 262 if(U_FAILURE(*status)){ 263 goto clean; 264 } 265 result->hasRealData = FALSE; 266 } 267 result->freeImageOnClose = FALSE; 268 } 269 } else { // !U_SUCCESS(binaryStatus) 270 if(U_SUCCESS(*status)) { 271 *status = intStatus; // propagate underlying error 272 } 273 goto clean; 274 } 275 intStatus = U_ZERO_ERROR; 276 result->rules = ures_getStringByKey(collElem, "Sequence", &result->rulesLength, &intStatus); 277 result->freeRulesOnClose = FALSE; 278 } else { /* There is another error, and we're just gonna clean up */ 279 goto clean; 280 } 281 282 intStatus = U_ZERO_ERROR; 283 result->ucaRules = ures_getStringByKey(b,"UCARules",NULL,&intStatus); 284 285 if(loc == NULL) { 286 loc = ures_getLocaleByType(b, ULOC_ACTUAL_LOCALE, status); 287 } 288 result->requestedLocale = uprv_strdup(loc); 289 /* test for NULL */ 290 if (result->requestedLocale == NULL) { 291 *status = U_MEMORY_ALLOCATION_ERROR; 292 goto clean; 293 } 294 loc = ures_getLocaleByType(collElem, ULOC_ACTUAL_LOCALE, status); 295 result->actualLocale = uprv_strdup(loc); 296 /* test for NULL */ 297 if (result->actualLocale == NULL) { 298 *status = U_MEMORY_ALLOCATION_ERROR; 299 goto clean; 300 } 301 loc = ures_getLocaleByType(b, ULOC_ACTUAL_LOCALE, status); 302 result->validLocale = uprv_strdup(loc); 303 /* test for NULL */ 304 if (result->validLocale == NULL) { 305 *status = U_MEMORY_ALLOCATION_ERROR; 306 goto clean; 307 } 308 309 ures_close(b); 310 ures_close(collElem); 311 ures_close(binary); 312 return result; 313 314clean: 315 ures_close(b); 316 ures_close(collElem); 317 ures_close(binary); 318 ucol_close(result); 319 return NULL; 320} 321 322U_CAPI UCollator* 323ucol_open(const char *loc, 324 UErrorCode *status) 325{ 326 U_NAMESPACE_USE 327 328 UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN); 329 UTRACE_DATA1(UTRACE_INFO, "locale = \"%s\"", loc); 330 UCollator *result = NULL; 331 332#if !UCONFIG_NO_SERVICE 333 result = Collator::createUCollator(loc, status); 334 if (result == NULL) 335#endif 336 { 337 result = ucol_open_internal(loc, status); 338 } 339 UTRACE_EXIT_PTR_STATUS(result, *status); 340 return result; 341} 342 343U_CAPI UCollator* U_EXPORT2 344ucol_openRules( const UChar *rules, 345 int32_t rulesLength, 346 UColAttributeValue normalizationMode, 347 UCollationStrength strength, 348 UParseError *parseError, 349 UErrorCode *status) 350{ 351 UColTokenParser src; 352 UColAttributeValue norm; 353 UParseError tErr; 354 355 if(status == NULL || U_FAILURE(*status)){ 356 return 0; 357 } 358 359 if(rules == NULL || rulesLength < -1) { 360 *status = U_ILLEGAL_ARGUMENT_ERROR; 361 return 0; 362 } 363 364 if(rulesLength == -1) { 365 rulesLength = u_strlen(rules); 366 } 367 368 if(parseError == NULL){ 369 parseError = &tErr; 370 } 371 372 switch(normalizationMode) { 373 case UCOL_OFF: 374 case UCOL_ON: 375 case UCOL_DEFAULT: 376 norm = normalizationMode; 377 break; 378 default: 379 *status = U_ILLEGAL_ARGUMENT_ERROR; 380 return 0; 381 } 382 383 UCollator *result = NULL; 384 UCATableHeader *table = NULL; 385 UCollator *UCA = ucol_initUCA(status); 386 387 if(U_FAILURE(*status)){ 388 return NULL; 389 } 390 391 ucol_tok_initTokenList(&src, rules, rulesLength, UCA, status); 392 ucol_tok_assembleTokenList(&src,parseError, status); 393 394 if(U_FAILURE(*status)) { 395 /* if status is U_ILLEGAL_ARGUMENT_ERROR, src->current points at the offending option */ 396 /* if status is U_INVALID_FORMAT_ERROR, src->current points after the problematic part of the rules */ 397 /* so something might be done here... or on lower level */ 398#ifdef UCOL_DEBUG 399 if(*status == U_ILLEGAL_ARGUMENT_ERROR) { 400 fprintf(stderr, "bad option starting at offset %i\n", src.current-src.source); 401 } else { 402 fprintf(stderr, "invalid rule just before offset %i\n", src.current-src.source); 403 } 404#endif 405 goto cleanup; 406 } 407 408 if(src.resultLen > 0 || src.removeSet != NULL) { /* we have a set of rules, let's make something of it */ 409 /* also, if we wanted to remove some contractions, we should make a tailoring */ 410 table = ucol_assembleTailoringTable(&src, status); 411 if(U_SUCCESS(*status)) { 412 // builder version 413 table->version[0] = UCOL_BUILDER_VERSION; 414 // no tailoring information on this level 415 table->version[1] = table->version[2] = table->version[3] = 0; 416 // set UCD version 417 u_getUnicodeVersion(table->UCDVersion); 418 // set UCA version 419 uprv_memcpy(table->UCAVersion, UCA->image->UCAVersion, sizeof(UVersionInfo)); 420 result = ucol_initCollator(table, 0, UCA, status); 421 if (U_FAILURE(*status)) { 422 goto cleanup; 423 } 424 result->hasRealData = TRUE; 425 result->freeImageOnClose = TRUE; 426 } 427 } else { /* no rules, but no error either */ 428 // must be only options 429 // We will init the collator from UCA 430 result = ucol_initCollator(UCA->image, 0, UCA, status); 431 // Check for null result 432 if (U_FAILURE(*status)) { 433 goto cleanup; 434 } 435 // And set only the options 436 UColOptionSet *opts = (UColOptionSet *)uprv_malloc(sizeof(UColOptionSet)); 437 /* test for NULL */ 438 if (opts == NULL) { 439 *status = U_MEMORY_ALLOCATION_ERROR; 440 goto cleanup; 441 } 442 uprv_memcpy(opts, src.opts, sizeof(UColOptionSet)); 443 ucol_setOptionsFromHeader(result, opts, status); 444 result->freeOptionsOnClose = TRUE; 445 result->hasRealData = FALSE; 446 result->freeImageOnClose = FALSE; 447 } 448 449 if(U_SUCCESS(*status)) { 450 UChar *newRules; 451 result->dataVersion[0] = UCOL_BUILDER_VERSION; 452 if(rulesLength > 0) { 453 newRules = (UChar *)uprv_malloc((rulesLength+1)*U_SIZEOF_UCHAR); 454 /* test for NULL */ 455 if (newRules == NULL) { 456 *status = U_MEMORY_ALLOCATION_ERROR; 457 goto cleanup; 458 } 459 uprv_memcpy(newRules, rules, rulesLength*U_SIZEOF_UCHAR); 460 newRules[rulesLength]=0; 461 result->rules = newRules; 462 result->rulesLength = rulesLength; 463 result->freeRulesOnClose = TRUE; 464 } 465 result->ucaRules = NULL; 466 result->actualLocale = NULL; 467 result->validLocale = NULL; 468 result->requestedLocale = NULL; 469 ucol_setAttribute(result, UCOL_STRENGTH, strength, status); 470 ucol_setAttribute(result, UCOL_NORMALIZATION_MODE, norm, status); 471 } else { 472cleanup: 473 if(result != NULL) { 474 ucol_close(result); 475 } else { 476 if(table != NULL) { 477 uprv_free(table); 478 } 479 } 480 result = NULL; 481 } 482 483 ucol_tok_closeTokenList(&src); 484 485 return result; 486} 487 488U_CAPI int32_t U_EXPORT2 489ucol_getRulesEx(const UCollator *coll, UColRuleOption delta, UChar *buffer, int32_t bufferLen) { 490 UErrorCode status = U_ZERO_ERROR; 491 int32_t len = 0; 492 int32_t UCAlen = 0; 493 const UChar* ucaRules = 0; 494 const UChar *rules = ucol_getRules(coll, &len); 495 if(delta == UCOL_FULL_RULES) { 496 /* take the UCA rules and append real rules at the end */ 497 /* UCA rules will be probably coming from the root RB */ 498 ucaRules = coll->ucaRules; 499 if (ucaRules) { 500 UCAlen = u_strlen(ucaRules); 501 } 502 /* 503 ucaRules = ures_getStringByKey(coll->rb,"UCARules",&UCAlen,&status); 504 UResourceBundle* cresb = ures_getByKeyWithFallback(coll->rb, "collations", NULL, &status); 505 UResourceBundle* uca = ures_getByKeyWithFallback(cresb, "UCA", NULL, &status); 506 ucaRules = ures_getStringByKey(uca,"Sequence",&UCAlen,&status); 507 ures_close(uca); 508 ures_close(cresb); 509 */ 510 } 511 if(U_FAILURE(status)) { 512 return 0; 513 } 514 if(buffer!=0 && bufferLen>0){ 515 *buffer=0; 516 if(UCAlen > 0) { 517 u_memcpy(buffer, ucaRules, uprv_min(UCAlen, bufferLen)); 518 } 519 if(len > 0 && bufferLen > UCAlen) { 520 u_memcpy(buffer+UCAlen, rules, uprv_min(len, bufferLen-UCAlen)); 521 } 522 } 523 return u_terminateUChars(buffer, bufferLen, len+UCAlen, &status); 524} 525 526static const UChar _NUL = 0; 527 528U_CAPI const UChar* U_EXPORT2 529ucol_getRules( const UCollator *coll, 530 int32_t *length) 531{ 532 if(coll->rules != NULL) { 533 *length = coll->rulesLength; 534 return coll->rules; 535 } 536 else { 537 *length = 0; 538 return &_NUL; 539 } 540} 541 542U_CAPI UBool U_EXPORT2 543ucol_equals(const UCollator *source, const UCollator *target) { 544 UErrorCode status = U_ZERO_ERROR; 545 // if pointers are equal, collators are equal 546 if(source == target) { 547 return TRUE; 548 } 549 int32_t i = 0, j = 0; 550 // if any of attributes are different, collators are not equal 551 for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) { 552 if(ucol_getAttribute(source, (UColAttribute)i, &status) != ucol_getAttribute(target, (UColAttribute)i, &status) || U_FAILURE(status)) { 553 return FALSE; 554 } 555 } 556 557 int32_t sourceRulesLen = 0, targetRulesLen = 0; 558 const UChar *sourceRules = ucol_getRules(source, &sourceRulesLen); 559 const UChar *targetRules = ucol_getRules(target, &targetRulesLen); 560 561 if(sourceRulesLen == targetRulesLen && u_strncmp(sourceRules, targetRules, sourceRulesLen) == 0) { 562 // all the attributes are equal and the rules are equal - collators are equal 563 return(TRUE); 564 } 565 // hard part, need to construct tree from rules and see if they yield the same tailoring 566 UBool result = TRUE; 567 UParseError parseError; 568 UColTokenParser sourceParser, targetParser; 569 int32_t sourceListLen = 0, targetListLen = 0; 570 ucol_tok_initTokenList(&sourceParser, sourceRules, sourceRulesLen, source->UCA, &status); 571 ucol_tok_initTokenList(&targetParser, targetRules, targetRulesLen, target->UCA, &status); 572 sourceListLen = ucol_tok_assembleTokenList(&sourceParser, &parseError, &status); 573 targetListLen = ucol_tok_assembleTokenList(&targetParser, &parseError, &status); 574 575 if(sourceListLen != targetListLen) { 576 // different number of resets 577 result = FALSE; 578 } else { 579 UColToken *sourceReset = NULL, *targetReset = NULL; 580 UChar *sourceResetString = NULL, *targetResetString = NULL; 581 int32_t sourceStringLen = 0, targetStringLen = 0; 582 for(i = 0; i < sourceListLen; i++) { 583 sourceReset = sourceParser.lh[i].reset; 584 sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF); 585 sourceStringLen = sourceReset->source >> 24; 586 for(j = 0; j < sourceListLen; j++) { 587 targetReset = targetParser.lh[j].reset; 588 targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF); 589 targetStringLen = targetReset->source >> 24; 590 if(sourceStringLen == targetStringLen && (u_strncmp(sourceResetString, targetResetString, sourceStringLen) == 0)) { 591 sourceReset = sourceParser.lh[i].first; 592 targetReset = targetParser.lh[j].first; 593 while(sourceReset != NULL && targetReset != NULL) { 594 sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF); 595 sourceStringLen = sourceReset->source >> 24; 596 targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF); 597 targetStringLen = targetReset->source >> 24; 598 if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) { 599 result = FALSE; 600 goto returnResult; 601 } 602 // probably also need to check the expansions 603 if(sourceReset->expansion) { 604 if(!targetReset->expansion) { 605 result = FALSE; 606 goto returnResult; 607 } else { 608 // compare expansions 609 sourceResetString = sourceParser.source+(sourceReset->expansion& 0xFFFFFF); 610 sourceStringLen = sourceReset->expansion >> 24; 611 targetResetString = targetParser.source+(targetReset->expansion & 0xFFFFFF); 612 targetStringLen = targetReset->expansion >> 24; 613 if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) { 614 result = FALSE; 615 goto returnResult; 616 } 617 } 618 } else { 619 if(targetReset->expansion) { 620 result = FALSE; 621 goto returnResult; 622 } 623 } 624 sourceReset = sourceReset->next; 625 targetReset = targetReset->next; 626 } 627 if(sourceReset != targetReset) { // at least one is not NULL 628 // there are more tailored elements in one list 629 result = FALSE; 630 goto returnResult; 631 } 632 633 634 break; 635 } 636 } 637 // couldn't find the reset anchor, so the collators are not equal 638 if(j == sourceListLen) { 639 result = FALSE; 640 goto returnResult; 641 } 642 } 643 } 644 645returnResult: 646 ucol_tok_closeTokenList(&sourceParser); 647 ucol_tok_closeTokenList(&targetParser); 648 return result; 649 650} 651 652U_CAPI int32_t U_EXPORT2 653ucol_getDisplayName( const char *objLoc, 654 const char *dispLoc, 655 UChar *result, 656 int32_t resultLength, 657 UErrorCode *status) 658{ 659 U_NAMESPACE_USE 660 661 if(U_FAILURE(*status)) return -1; 662 UnicodeString dst; 663 if(!(result==NULL && resultLength==0)) { 664 // NULL destination for pure preflighting: empty dummy string 665 // otherwise, alias the destination buffer 666 dst.setTo(result, 0, resultLength); 667 } 668 Collator::getDisplayName(Locale(objLoc), Locale(dispLoc), dst); 669 return dst.extract(result, resultLength, *status); 670} 671 672U_CAPI const char* U_EXPORT2 673ucol_getAvailable(int32_t index) 674{ 675 int32_t count = 0; 676 const Locale *loc = Collator::getAvailableLocales(count); 677 if (loc != NULL && index < count) { 678 return loc[index].getName(); 679 } 680 return NULL; 681} 682 683U_CAPI int32_t U_EXPORT2 684ucol_countAvailable() 685{ 686 int32_t count = 0; 687 Collator::getAvailableLocales(count); 688 return count; 689} 690 691#if !UCONFIG_NO_SERVICE 692U_CAPI UEnumeration* U_EXPORT2 693ucol_openAvailableLocales(UErrorCode *status) { 694 U_NAMESPACE_USE 695 696 // This is a wrapper over Collator::getAvailableLocales() 697 if (U_FAILURE(*status)) { 698 return NULL; 699 } 700 StringEnumeration *s = Collator::getAvailableLocales(); 701 if (s == NULL) { 702 *status = U_MEMORY_ALLOCATION_ERROR; 703 return NULL; 704 } 705 return uenum_openFromStringEnumeration(s, status); 706} 707#endif 708 709// Note: KEYWORDS[0] != RESOURCE_NAME - alan 710 711static const char RESOURCE_NAME[] = "collations"; 712 713static const char* const KEYWORDS[] = { "collation" }; 714 715#define KEYWORD_COUNT (sizeof(KEYWORDS)/sizeof(KEYWORDS[0])) 716 717U_CAPI UEnumeration* U_EXPORT2 718ucol_getKeywords(UErrorCode *status) { 719 UEnumeration *result = NULL; 720 if (U_SUCCESS(*status)) { 721 return uenum_openCharStringsEnumeration(KEYWORDS, KEYWORD_COUNT, status); 722 } 723 return result; 724} 725 726U_CAPI UEnumeration* U_EXPORT2 727ucol_getKeywordValues(const char *keyword, UErrorCode *status) { 728 if (U_FAILURE(*status)) { 729 return NULL; 730 } 731 // hard-coded to accept exactly one collation keyword 732 // modify if additional collation keyword is added later 733 if (keyword==NULL || uprv_strcmp(keyword, KEYWORDS[0])!=0) 734 { 735 *status = U_ILLEGAL_ARGUMENT_ERROR; 736 return NULL; 737 } 738 return ures_getKeywordValues(U_ICUDATA_COLL, RESOURCE_NAME, status); 739} 740 741static const UEnumeration defaultKeywordValues = { 742 NULL, 743 NULL, 744 ulist_close_keyword_values_iterator, 745 ulist_count_keyword_values, 746 uenum_unextDefault, 747 ulist_next_keyword_value, 748 ulist_reset_keyword_values_iterator 749}; 750 751#include <stdio.h> 752 753U_CAPI UEnumeration* U_EXPORT2 754ucol_getKeywordValuesForLocale(const char* /*key*/, const char* locale, 755 UBool /*commonlyUsed*/, UErrorCode* status) { 756 /* Get the locale base name. */ 757 char localeBuffer[ULOC_FULLNAME_CAPACITY] = ""; 758 uloc_getBaseName(locale, localeBuffer, sizeof(localeBuffer), status); 759 760 /* Create the 2 lists 761 * -values is the temp location for the keyword values 762 * -results hold the actual list used by the UEnumeration object 763 */ 764 UList *values = ulist_createEmptyList(status); 765 UList *results = ulist_createEmptyList(status); 766 UEnumeration *en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration)); 767 if (U_FAILURE(*status) || en == NULL) { 768 if (en == NULL) { 769 *status = U_MEMORY_ALLOCATION_ERROR; 770 } else { 771 uprv_free(en); 772 } 773 ulist_deleteList(values); 774 ulist_deleteList(results); 775 return NULL; 776 } 777 778 memcpy(en, &defaultKeywordValues, sizeof(UEnumeration)); 779 en->context = results; 780 781 /* Open the resource bundle for collation with the given locale. */ 782 UResourceBundle bundle, collations, collres, defres; 783 ures_initStackObject(&bundle); 784 ures_initStackObject(&collations); 785 ures_initStackObject(&collres); 786 ures_initStackObject(&defres); 787 788 ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status); 789 790 while (U_SUCCESS(*status)) { 791 ures_getByKey(&bundle, RESOURCE_NAME, &collations, status); 792 ures_resetIterator(&collations); 793 while (U_SUCCESS(*status) && ures_hasNext(&collations)) { 794 ures_getNextResource(&collations, &collres, status); 795 const char *key = ures_getKey(&collres); 796 /* If the key is default, get the string and store it in results list only 797 * if results list is empty. 798 */ 799 if (uprv_strcmp(key, "default") == 0) { 800 if (ulist_getListSize(results) == 0) { 801 char *defcoll = (char *)uprv_malloc(sizeof(char) * ULOC_KEYWORDS_CAPACITY); 802 int32_t defcollLength = ULOC_KEYWORDS_CAPACITY; 803 804 ures_getNextResource(&collres, &defres, status); 805#if U_CHARSET_FAMILY==U_ASCII_FAMILY 806 /* optimize - use the utf-8 string */ 807 ures_getUTF8String(&defres, defcoll, &defcollLength, TRUE, status); 808#else 809 { 810 const UChar* defString = ures_getString(&defres, &defcollLength, status); 811 if(U_SUCCESS(*status)) { 812 if(defcollLength+1 > ULOC_KEYWORDS_CAPACITY) { 813 *status = U_BUFFER_OVERFLOW_ERROR; 814 } else { 815 u_UCharsToChars(defString, defcoll, defcollLength+1); 816 } 817 } 818 } 819#endif 820 821 ulist_addItemBeginList(results, defcoll, TRUE, status); 822 } 823 } else { 824 ulist_addItemEndList(values, key, FALSE, status); 825 } 826 } 827 828 /* If the locale is "" this is root so exit. */ 829 if (uprv_strlen(localeBuffer) == 0) { 830 break; 831 } 832 /* Get the parent locale and open a new resource bundle. */ 833 uloc_getParent(localeBuffer, localeBuffer, sizeof(localeBuffer), status); 834 ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status); 835 } 836 837 ures_close(&defres); 838 ures_close(&collres); 839 ures_close(&collations); 840 ures_close(&bundle); 841 842 if (U_SUCCESS(*status)) { 843 char *value = NULL; 844 ulist_resetList(values); 845 while ((value = (char *)ulist_getNext(values)) != NULL) { 846 if (!ulist_containsString(results, value, (int32_t)uprv_strlen(value))) { 847 ulist_addItemEndList(results, value, FALSE, status); 848 if (U_FAILURE(*status)) { 849 break; 850 } 851 } 852 } 853 } 854 855 ulist_deleteList(values); 856 857 if (U_FAILURE(*status)){ 858 uenum_close(en); 859 en = NULL; 860 } else { 861 ulist_resetList(results); 862 } 863 864 return en; 865} 866 867U_CAPI int32_t U_EXPORT2 868ucol_getFunctionalEquivalent(char* result, int32_t resultCapacity, 869 const char* keyword, const char* locale, 870 UBool* isAvailable, UErrorCode* status) 871{ 872 // N.B.: Resource name is "collations" but keyword is "collation" 873 return ures_getFunctionalEquivalent(result, resultCapacity, U_ICUDATA_COLL, 874 "collations", keyword, locale, 875 isAvailable, TRUE, status); 876} 877 878/* returns the locale name the collation data comes from */ 879U_CAPI const char * U_EXPORT2 880ucol_getLocale(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) { 881 return ucol_getLocaleByType(coll, type, status); 882} 883 884U_CAPI const char * U_EXPORT2 885ucol_getLocaleByType(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) { 886 const char *result = NULL; 887 if(status == NULL || U_FAILURE(*status)) { 888 return NULL; 889 } 890 UTRACE_ENTRY(UTRACE_UCOL_GETLOCALE); 891 UTRACE_DATA1(UTRACE_INFO, "coll=%p", coll); 892 893 switch(type) { 894 case ULOC_ACTUAL_LOCALE: 895 result = coll->actualLocale; 896 break; 897 case ULOC_VALID_LOCALE: 898 result = coll->validLocale; 899 break; 900 case ULOC_REQUESTED_LOCALE: 901 result = coll->requestedLocale; 902 break; 903 default: 904 *status = U_ILLEGAL_ARGUMENT_ERROR; 905 } 906 UTRACE_DATA1(UTRACE_INFO, "result = %s", result); 907 UTRACE_EXIT_STATUS(*status); 908 return result; 909} 910 911U_CFUNC void U_EXPORT2 912ucol_setReqValidLocales(UCollator *coll, char *requestedLocaleToAdopt, char *validLocaleToAdopt, char *actualLocaleToAdopt) 913{ 914 if (coll) { 915 if (coll->validLocale) { 916 uprv_free(coll->validLocale); 917 } 918 coll->validLocale = validLocaleToAdopt; 919 if (coll->requestedLocale) { // should always have 920 uprv_free(coll->requestedLocale); 921 } 922 coll->requestedLocale = requestedLocaleToAdopt; 923 if (coll->actualLocale) { 924 uprv_free(coll->actualLocale); 925 } 926 coll->actualLocale = actualLocaleToAdopt; 927 } 928} 929 930U_CAPI USet * U_EXPORT2 931ucol_getTailoredSet(const UCollator *coll, UErrorCode *status) 932{ 933 U_NAMESPACE_USE 934 935 if(status == NULL || U_FAILURE(*status)) { 936 return NULL; 937 } 938 if(coll == NULL || coll->UCA == NULL) { 939 *status = U_ILLEGAL_ARGUMENT_ERROR; 940 return NULL; 941 } 942 UParseError parseError; 943 UColTokenParser src; 944 int32_t rulesLen = 0; 945 const UChar *rules = ucol_getRules(coll, &rulesLen); 946 UBool startOfRules = TRUE; 947 // we internally use the C++ class, for the following reasons: 948 // 1. we need to utilize canonical iterator, which is a C++ only class 949 // 2. canonical iterator returns UnicodeStrings - USet cannot take them 950 // 3. USet is internally really UnicodeSet, C is just a wrapper 951 UnicodeSet *tailored = new UnicodeSet(); 952 UnicodeString pattern; 953 UnicodeString empty; 954 CanonicalIterator it(empty, *status); 955 956 957 // The idea is to tokenize the rule set. For each non-reset token, 958 // we add all the canonicaly equivalent FCD sequences 959 ucol_tok_initTokenList(&src, rules, rulesLen, coll->UCA, status); 960 while (ucol_tok_parseNextToken(&src, startOfRules, &parseError, status) != NULL) { 961 startOfRules = FALSE; 962 if(src.parsedToken.strength != UCOL_TOK_RESET) { 963 const UChar *stuff = src.source+(src.parsedToken.charsOffset); 964 it.setSource(UnicodeString(stuff, src.parsedToken.charsLen), *status); 965 pattern = it.next(); 966 while(!pattern.isBogus()) { 967 if(Normalizer::quickCheck(pattern, UNORM_FCD, *status) != UNORM_NO) { 968 tailored->add(pattern); 969 } 970 pattern = it.next(); 971 } 972 } 973 } 974 ucol_tok_closeTokenList(&src); 975 return (USet *)tailored; 976} 977 978#endif /* #if !UCONFIG_NO_COLLATION */ 979