]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/legacy/oldcol.cpp
2 *******************************************************************************
4 * Copyright (C) 2001-2005, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: oldcol.cpp
10 * tab size: 8 (not used)
13 * created on: 2001jul24
14 * created by: Vladimir Weinstein
17 /******************************************************************************
18 * This is the module that uses old collation
19 ******************************************************************************/
23 #include <unicode/putil.h>
24 #include <unicode/ucol.h>
26 // Very simple example code - sticks a sortkey in the buffer
27 // Not much error checking
28 int32_t getSortKey_legacy(const char *locale
, const UChar
*string
, int32_t sLen
, uint8_t *buffer
, int32_t bLen
) {
29 UErrorCode status
= U_ZERO_ERROR
;
30 UCollator
*coll
= ucol_open(locale
, &status
);
31 if(U_FAILURE(status
)) {
34 int32_t result
= ucol_getSortKey(coll
, string
, sLen
, buffer
, bLen
);
39 // This one can be used for passing to qsort function
40 // Not thread safe or anything
41 static UCollator
*compareCollator
= NULL
;
43 int compare_legacy(const void *string1
, const void *string2
) {
44 if(compareCollator
!= NULL
) {
45 UCollationResult res
= ucol_strcoll(compareCollator
, (UChar
*) string1
, -1, (UChar
*) string2
, -1);
46 if(res
== UCOL_LESS
) {
48 } else if(res
== UCOL_GREATER
) {
58 void initCollator_legacy(const char *locale
) {
59 UErrorCode status
= U_ZERO_ERROR
;
60 compareCollator
= ucol_open(locale
, &status
);
64 fprintf(stderr
, "initCollator_legacy(%s): error opening collator, %s!\n", locale
, u_errorName(status
));
65 fprintf(stderr
, "Note: ICU data directory is %s\n", u_getDataDirectory());
66 fprintf(stderr
, "Read the README!\n");
71 void closeCollator_legacy(void) {
72 if(compareCollator
!= NULL
)
74 ucol_close(compareCollator
);
78 fprintf(stderr
, "closeCollator_legacy(): collator was already NULL!\n");
80 compareCollator
= NULL
;
84 extern "C" void test_legacy(UChar data
[][5], uint32_t size
, uint32_t maxlen
, uint8_t keys
[4][32]) {
91 fprintf(stderr
, "Entered legacy, version: [%d.%d.%d.%d]\nMoving to sortkeys\n", uvi
[0], uvi
[1], uvi
[2], uvi
[3]);
93 for(i
= 0; i
<size
; i
++) {
94 keySize
= getSortKey_legacy("ja", data
[i
], -1, keys
[i
], 32);
95 fprintf(stderr
, "For i=%d, size of sortkey is %d\n", i
, keySize
);
98 fprintf(stderr
, "Done sortkeys, doing qsort test\n");
100 initCollator_legacy("ja");
101 qsort(data
, size
, maxlen
*sizeof(UChar
), compare_legacy
);
102 closeCollator_legacy();
104 fprintf(stderr
, "Done legacy!\n");