Home | History | Annotate | Download | only in legacy
      1 /*
      2  *******************************************************************************
      3  *
      4  *   Copyright (C) 2001-2005, 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/putil.h>
     24 #include <unicode/ucol.h>
     25 
     26 // Very simple example code - sticks a sortkey in the buffer
     27 // Not much error checking
     28 int32_t getSortKey_legacy(const char *locale, const UChar *string, int32_t sLen, uint8_t *buffer, int32_t bLen) {
     29   UErrorCode status = U_ZERO_ERROR;
     30   UCollator *coll = ucol_open(locale, &status);
     31   if(U_FAILURE(status)) {
     32     return -1;
     33   }
     34   int32_t result = ucol_getSortKey(coll, string, sLen, buffer, bLen);
     35   ucol_close(coll);
     36   return result;
     37 }
     38 
     39 // This one can be used for passing to qsort function
     40 // Not thread safe or anything
     41 static UCollator *compareCollator = NULL;
     42 
     43 int compare_legacy(const void *string1, const void *string2) {
     44   if(compareCollator != NULL) {
     45     UCollationResult res = ucol_strcoll(compareCollator, (UChar *) string1, -1, (UChar *) string2, -1);
     46     if(res == UCOL_LESS) {
     47       return -1;
     48     } else if(res == UCOL_GREATER) {
     49       return 1;
     50     } else {
     51       return 0;
     52     }
     53   } else {
     54     return 0;
     55   }
     56 }
     57 
     58 void initCollator_legacy(const char *locale) {
     59   UErrorCode status = U_ZERO_ERROR;
     60   compareCollator = ucol_open(locale, &status);
     61 
     62   if(U_FAILURE(status))
     63   {
     64     fprintf(stderr, "initCollator_legacy(%s): error opening collator, %s!\n", locale, u_errorName(status));
     65     fprintf(stderr, "Note: ICU data directory is %s\n", u_getDataDirectory());
     66     fprintf(stderr, "Read the README!\n");
     67     exit(0);
     68   }
     69 }
     70 
     71 void closeCollator_legacy(void) {
     72   if(compareCollator != NULL)
     73   {
     74     ucol_close(compareCollator);
     75   }
     76   else
     77   {
     78     fprintf(stderr, "closeCollator_legacy(): collator was already NULL!\n");
     79   }
     80   compareCollator = NULL;
     81 }
     82 
     83 
     84 extern "C" void test_legacy(UChar data[][5], uint32_t size, uint32_t maxlen, uint8_t keys[4][32]) {
     85   uint32_t i = 0;
     86   int32_t keySize = 0;
     87 
     88   UVersionInfo uvi;
     89 
     90   u_getVersion(uvi);
     91   fprintf(stderr, "Entered legacy, version: [%d.%d.%d.%d]\nMoving to sortkeys\n", uvi[0], uvi[1], uvi[2], uvi[3]);
     92 
     93   for(i = 0; i<size; i++) {
     94     keySize = getSortKey_legacy("ja", data[i], -1, keys[i], 32);
     95     fprintf(stderr, "For i=%d, size of sortkey is %d\n", i, keySize);
     96   }
     97 
     98   fprintf(stderr, "Done sortkeys, doing qsort test\n");
     99 
    100   initCollator_legacy("ja");
    101   qsort(data, size, maxlen*sizeof(UChar), compare_legacy);
    102   closeCollator_legacy();
    103 
    104   fprintf(stderr, "Done legacy!\n");
    105 }
    106 
    107 
    108