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