Imported Upstream version 58.1
[platform/upstream/icu.git] / source / samples / legacy / newcol.cpp
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2016 and later: Unicode, Inc. and others.
5 *   License & terms of use: http://www.unicode.org/copyright.html#License
6 *
7 *******************************************************************************
8 *******************************************************************************
9 *
10 *   Copyright (C) 2001 - 2005, International Business Machines
11 *   Corporation and others.  All Rights Reserved.
12 *
13 *******************************************************************************
14 *   file name:  newcol.cpp
15 *   encoding:   US-ASCII
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 new collation
25  ******************************************************************************/
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include "unicode/ucol.h"
30
31 // Very simple example code - sticks a sortkey in the buffer
32 // Not much error checking
33 int32_t getSortKey_current(const char *locale, const UChar *string, int32_t sLen, uint8_t *buffer, int32_t bLen) {
34   UErrorCode status = U_ZERO_ERROR;
35   UCollator *coll = ucol_open(locale, &status);
36   if(U_FAILURE(status)) {
37     return -1;
38   }
39   int32_t result = ucol_getSortKey(coll, string, sLen, buffer, bLen);
40   ucol_close(coll);
41   return result;  
42 }
43
44 // This one can be used for passing to qsort function
45 // Not thread safe or anything
46 static UCollator *compareCollator = NULL;
47
48 int compare_current(const void *string1, const void *string2) {
49   if(compareCollator != NULL) {
50     UCollationResult res = ucol_strcoll(compareCollator, (UChar *) string1, -1, (UChar *) string2, -1);
51     if(res == UCOL_LESS) {
52       return -1;
53     } else if(res == UCOL_GREATER) {
54       return 1;
55     } else {
56       return 0;
57     }
58   } else {
59     return 0;
60   }
61 }
62
63 void initCollator_current(const char *locale) {
64   UErrorCode status = U_ZERO_ERROR;
65   compareCollator = ucol_open(locale, &status);
66 }
67
68 void closeCollator_current(void) {
69   ucol_close(compareCollator);
70   compareCollator = NULL;
71 }
72
73
74 extern "C" void test_current(UChar data[][5], uint32_t size, uint32_t maxlen, uint8_t keys[][32]) {
75   uint32_t i = 0;
76   int32_t keySize = 0;
77   UVersionInfo uvi;
78
79   u_getVersion(uvi);
80   fprintf(stderr, "Entered current, version: [%d.%d.%d.%d]\nMoving to sortkeys\n", uvi[0], uvi[1], uvi[2], uvi[3]);
81
82   for(i = 0; i<size; i++) {
83     keySize = getSortKey_current("ja", data[i], -1, keys[i], 32);
84     fprintf(stderr, "For i=%d, size of sortkey is %d\n", i, keySize);
85   }
86
87   fprintf(stderr, "Done sortkeys, doing qsort test\n");
88   
89   initCollator_current("ja");
90   qsort(data, size, maxlen*sizeof(UChar), compare_current);
91   closeCollator_current();
92
93   fprintf(stderr, "Done current!\n");
94 }
95
96