]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/legacy/oldcol.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / samples / legacy / oldcol.cpp
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
3*
4* Copyright (C) 2001, International Business Machines
5* Corporation and others. All Rights Reserved.
6*
7*******************************************************************************
8* file name: oldcol.cpp
9* encoding: US-ASCII
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 2001jul24
14* created by: Vladimir Weinstein
15*/
16
17/******************************************************************************
18 * This is the module that uses old collation
19 ******************************************************************************/
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <unicode/ucol.h>
24
25// Very simple example code - sticks a sortkey in the buffer
26// Not much error checking
27int32_t getSortKey_legacy(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)) {
31 return -1;
32 }
33 int32_t result = ucol_getSortKey(coll, string, sLen, buffer, bLen);
34 ucol_close(coll);
35 return result;
36}
37
38// This one can be used for passing to qsort function
39// Not thread safe or anything
40static UCollator *compareCollator = NULL;
41
42int compare_legacy(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) {
46 return -1;
47 } else if(res == UCOL_GREATER) {
48 return 1;
49 } else {
50 return 0;
51 }
52 } else {
53 return 0;
54 }
55}
56
57void initCollator_legacy(const char *locale) {
58 UErrorCode status = U_ZERO_ERROR;
59 compareCollator = ucol_open(locale, &status);
60
61 if(U_FAILURE(status))
62 {
63 fprintf(stderr, "initCollator_legacy(%s): error opening collator, %s!\n", locale, u_errorName(status));
64 fprintf(stderr, "Note: ICU data directory is %s\n", u_getDataDirectory());
65 fprintf(stderr, "Read the README!\n");
66 exit(0);
67 }
68}
69
70void closeCollator_legacy(void) {
71 if(compareCollator != NULL)
72 {
73 ucol_close(compareCollator);
74 }
75 else
76 {
77 fprintf(stderr, "closeCollator_legacy(): collator was already NULL!\n");
78 }
79 compareCollator = NULL;
80}
81
82
83void test_legacy(UChar data[][5], uint32_t size, uint32_t maxlen, uint8_t keys[4][32]) {
84 uint32_t i = 0;
85 int32_t keySize = 0;
86
87 UVersionInfo uvi;
88
89 u_getVersion(uvi);
90 fprintf(stderr, "Entered legacy, version: [%d.%d.%d.%d]\nMoving to sortkeys\n", uvi[0], uvi[1], uvi[2], uvi[3]);
91
92 for(i = 0; i<size; i++) {
93 keySize = getSortKey_legacy("ja", data[i], -1, keys[i], 32);
94 fprintf(stderr, "For i=%d, size of sortkey is %d\n", i, keySize);
95 }
96
97 fprintf(stderr, "Done sortkeys, doing qsort test\n");
98
99 initCollator_legacy("ja");
100 qsort(data, size, maxlen*sizeof(UChar), compare_legacy);
101 closeCollator_legacy();
102
103 fprintf(stderr, "Done legacy!\n");
104}
105
106