1 /*
2 *******************************************************************************
3 *
4 * © 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html
6 *
7 *******************************************************************************
8 *******************************************************************************
9 *
10 * Copyright (C) 2001-2005, International Business Machines
11 * Corporation and others. All Rights Reserved.
12 *
13 *******************************************************************************
14 * file name: oldcol.cpp
15 * encoding: UTF-8
16 * tab size: 8 (not used)
17 * indentation:4
18 *
19 * created on: 2001jul24
20 * created by: Vladimir Weinstein
21 */
22
23 /******************************************************************************
24 * This is the module that uses old collation
25 ******************************************************************************/
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unicode/putil.h>
30 #include <unicode/ucol.h>
31
32 // Very simple example code - sticks a sortkey in the buffer
33 // Not much error checking
getSortKey_legacy(const char * locale,const UChar * string,int32_t sLen,uint8_t * buffer,int32_t bLen)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)) {
38 return -1;
39 }
40 int32_t result = ucol_getSortKey(coll, string, sLen, buffer, bLen);
41 ucol_close(coll);
42 return result;
43 }
44
45 // This one can be used for passing to qsort function
46 // Not thread safe or anything
47 static UCollator *compareCollator = NULL;
48
compare_legacy(const void * string1,const void * string2)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) {
53 return -1;
54 } else if(res == UCOL_GREATER) {
55 return 1;
56 } else {
57 return 0;
58 }
59 } else {
60 return 0;
61 }
62 }
63
initCollator_legacy(const char * locale)64 void initCollator_legacy(const char *locale) {
65 UErrorCode status = U_ZERO_ERROR;
66 compareCollator = ucol_open(locale, &status);
67
68 if(U_FAILURE(status))
69 {
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");
73 exit(0);
74 }
75 }
76
closeCollator_legacy(void)77 void closeCollator_legacy(void) {
78 if(compareCollator != NULL)
79 {
80 ucol_close(compareCollator);
81 }
82 else
83 {
84 fprintf(stderr, "closeCollator_legacy(): collator was already NULL!\n");
85 }
86 compareCollator = NULL;
87 }
88
89
test_legacy(UChar data[][5],uint32_t size,uint32_t maxlen,uint8_t keys[4][32])90 extern "C" void test_legacy(UChar data[][5], uint32_t size, uint32_t maxlen, uint8_t keys[4][32]) {
91 uint32_t i = 0;
92 int32_t keySize = 0;
93
94 UVersionInfo uvi;
95
96 u_getVersion(uvi);
97 fprintf(stderr, "Entered legacy, version: [%d.%d.%d.%d]\nMoving to sortkeys\n", uvi[0], uvi[1], uvi[2], uvi[3]);
98
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);
102 }
103
104 fprintf(stderr, "Done sortkeys, doing qsort test\n");
105
106 initCollator_legacy("ja");
107 qsort(data, size, maxlen*sizeof(UChar), compare_legacy);
108 closeCollator_legacy();
109
110 fprintf(stderr, "Done legacy!\n");
111 }
112
113
114