Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / icu / source / samples / legacy / newcol.cpp
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2001 - 2005, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 *   file name:  newcol.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 new 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
27 int32_t getSortKey_current(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
40 static UCollator *compareCollator = NULL;
41
42 int compare_current(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
57 void initCollator_current(const char *locale) {
58   UErrorCode status = U_ZERO_ERROR;
59   compareCollator = ucol_open(locale, &status);
60 }
61
62 void closeCollator_current(void) {
63   ucol_close(compareCollator);
64   compareCollator = NULL;
65 }
66
67
68 extern "C" void test_current(UChar data[][5], uint32_t size, uint32_t maxlen, uint8_t keys[][32]) {
69   uint32_t i = 0;
70   int32_t keySize = 0;
71   UVersionInfo uvi;
72
73   u_getVersion(uvi);
74   fprintf(stderr, "Entered current, version: [%d.%d.%d.%d]\nMoving to sortkeys\n", uvi[0], uvi[1], uvi[2], uvi[3]);
75
76   for(i = 0; i<size; i++) {
77     keySize = getSortKey_current("ja", data[i], -1, keys[i], 32);
78     fprintf(stderr, "For i=%d, size of sortkey is %d\n", i, keySize);
79   }
80
81   fprintf(stderr, "Done sortkeys, doing qsort test\n");
82   
83   initCollator_current("ja");
84   qsort(data, size, maxlen*sizeof(UChar), compare_current);
85   closeCollator_current();
86
87   fprintf(stderr, "Done current!\n");
88 }
89
90