]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/legacy/oldcol.cpp
2 *******************************************************************************
4 * © 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
7 *******************************************************************************
8 *******************************************************************************
10 * Copyright (C) 2001-2005, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
14 * file name: oldcol.cpp
16 * tab size: 8 (not used)
19 * created on: 2001jul24
20 * created by: Vladimir Weinstein
23 /******************************************************************************
24 * This is the module that uses old collation
25 ******************************************************************************/
29 #include <unicode/putil.h>
30 #include <unicode/ucol.h>
32 // Very simple example code - sticks a sortkey in the buffer
33 // Not much error checking
34 int32_t getSortKey_legacy(const char *locale
, const UChar
*string
, int32_t sLen
, uint8_t *buffer
, int32_t bLen
) {
35 UErrorCode status
= U_ZERO_ERROR
;
36 UCollator
*coll
= ucol_open(locale
, &status
);
37 if(U_FAILURE(status
)) {
40 int32_t result
= ucol_getSortKey(coll
, string
, sLen
, buffer
, bLen
);
45 // This one can be used for passing to qsort function
46 // Not thread safe or anything
47 static UCollator
*compareCollator
= NULL
;
49 int compare_legacy(const void *string1
, const void *string2
) {
50 if(compareCollator
!= NULL
) {
51 UCollationResult res
= ucol_strcoll(compareCollator
, (UChar
*) string1
, -1, (UChar
*) string2
, -1);
52 if(res
== UCOL_LESS
) {
54 } else if(res
== UCOL_GREATER
) {
64 void initCollator_legacy(const char *locale
) {
65 UErrorCode status
= U_ZERO_ERROR
;
66 compareCollator
= ucol_open(locale
, &status
);
70 fprintf(stderr
, "initCollator_legacy(%s): error opening collator, %s!\n", locale
, u_errorName(status
));
71 fprintf(stderr
, "Note: ICU data directory is %s\n", u_getDataDirectory());
72 fprintf(stderr
, "Read the README!\n");
77 void closeCollator_legacy(void) {
78 if(compareCollator
!= NULL
)
80 ucol_close(compareCollator
);
84 fprintf(stderr
, "closeCollator_legacy(): collator was already NULL!\n");
86 compareCollator
= NULL
;
90 extern "C" void test_legacy(UChar data
[][5], uint32_t size
, uint32_t maxlen
, uint8_t keys
[4][32]) {
97 fprintf(stderr
, "Entered legacy, version: [%d.%d.%d.%d]\nMoving to sortkeys\n", uvi
[0], uvi
[1], uvi
[2], uvi
[3]);
99 for(i
= 0; i
<size
; i
++) {
100 keySize
= getSortKey_legacy("ja", data
[i
], -1, keys
[i
], 32);
101 fprintf(stderr
, "For i=%d, size of sortkey is %d\n", i
, keySize
);
104 fprintf(stderr
, "Done sortkeys, doing qsort test\n");
106 initCollator_legacy("ja");
107 qsort(data
, size
, maxlen
*sizeof(UChar
), compare_legacy
);
108 closeCollator_legacy();
110 fprintf(stderr
, "Done legacy!\n");