2 *******************************************************************************
4 * Copyright (C) 2001 - 2005, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: newcol.cpp
10 * tab size: 8 (not used)
13 * created on: 2001jul24
14 * created by: Vladimir Weinstein
17 /******************************************************************************
18 * This is the module that uses new collation
19 ******************************************************************************/
23 #include "unicode/ucol.h"
25 // Very simple example code - sticks a sortkey in the buffer
26 // Not much error checking
27 int32_t getSortKey_current(const char *locale, const UChar *string, int32_t sLen, uint8_t *buffer, int32_t bLen) {
28 UErrorCode status = U_ZERO_ERROR;
29 UCollator *coll = ucol_open(locale, &status);
30 if(U_FAILURE(status)) {
33 int32_t result = ucol_getSortKey(coll, string, sLen, buffer, bLen);
38 // This one can be used for passing to qsort function
39 // Not thread safe or anything
40 static UCollator *compareCollator = NULL;
42 int compare_current(const void *string1, const void *string2) {
43 if(compareCollator != NULL) {
44 UCollationResult res = ucol_strcoll(compareCollator, (UChar *) string1, -1, (UChar *) string2, -1);
45 if(res == UCOL_LESS) {
47 } else if(res == UCOL_GREATER) {
57 void initCollator_current(const char *locale) {
58 UErrorCode status = U_ZERO_ERROR;
59 compareCollator = ucol_open(locale, &status);
62 void closeCollator_current(void) {
63 ucol_close(compareCollator);
64 compareCollator = NULL;
68 extern "C" void test_current(UChar data[][5], uint32_t size, uint32_t maxlen, uint8_t keys[][32]) {
74 fprintf(stderr, "Entered current, version: [%d.%d.%d.%d]\nMoving to sortkeys\n", uvi[0], uvi[1], uvi[2], uvi[3]);
76 for(i = 0; i<size; i++) {
77 keySize = getSortKey_current("ja", data[i], -1, keys[i], 32);
78 fprintf(stderr, "For i=%d, size of sortkey is %d\n", i, keySize);
81 fprintf(stderr, "Done sortkeys, doing qsort test\n");
83 initCollator_current("ja");
84 qsort(data, size, maxlen*sizeof(UChar), compare_current);
85 closeCollator_current();
87 fprintf(stderr, "Done current!\n");