home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / i18n / ucol.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-22  |  10.9 KB  |  413 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright Taligent, Inc.,  1996                                       *
  6. *   (C) Copyright International Business Machines Corporation,  1998-1999     *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  10. *                                                                             *
  11. *******************************************************************************
  12. */
  13.  
  14. #include "ucol.h"
  15.  
  16. #include "uloc.h"
  17. #include "coll.h"
  18. #include "tblcoll.h"
  19. #include "coleitr.h"
  20. #include "ustring.h"
  21.  
  22. /*===============================================
  23. =================================================
  24.     ---> MOVE SOMEWHERE ELSE !!! <---
  25. =================================================
  26. ===============================================*/
  27. #include "normlzr.h"
  28. #include "cpputils.h"
  29. U_CAPI int32_t
  30. u_normalize(const UChar*            source,
  31.         int32_t                 sourceLength, 
  32.         UNormalizationMode      mode, 
  33.         int32_t                 option,
  34.         UChar*                  result,
  35.         int32_t                 resultLength,
  36.         UErrorCode*             status)
  37. {
  38.   if(U_FAILURE(*status)) return -1;
  39.  
  40.   Normalizer::EMode normMode;
  41.   switch(mode) {
  42.   case UCOL_NO_NORMALIZATION:
  43.     normMode = Normalizer::NO_OP;
  44.     break;
  45.   case UCOL_DECOMP_CAN:
  46.     normMode = Normalizer::DECOMP;
  47.     break;
  48.   case UCOL_DECOMP_COMPAT:
  49.     normMode = Normalizer::DECOMP_COMPAT;
  50.     break;
  51.   case UCOL_DECOMP_CAN_COMP_COMPAT:
  52.     normMode = Normalizer::COMPOSE;
  53.     break;
  54.   case UCOL_DECOMP_COMPAT_COMP_CAN:
  55.     normMode = Normalizer::COMPOSE_COMPAT;
  56.     break;
  57.   }
  58.  
  59.   int32_t len = (sourceLength == -1 ? u_strlen(source) : sourceLength);
  60.   const UnicodeString src((UChar*)source, len, len);
  61.   UnicodeString dst(result, 0, resultLength);
  62.   Normalizer::normalize(src, normMode, option, dst, *status);
  63.   int32_t actualLen;
  64.   T_fillOutputParams(&dst, result, resultLength, &actualLen, status);
  65.   return actualLen;
  66. }
  67.  
  68. U_CAPI UCollator*
  69. ucol_open(    const    char         *loc,
  70.         UErrorCode      *status)
  71. {
  72.   if(U_FAILURE(*status)) return 0;
  73.  
  74.   Collator *col = 0;
  75.  
  76.   if(loc == 0) 
  77.     col = Collator::createInstance(*status);
  78.   else
  79.     col = Collator::createInstance(Locale().init(loc), *status);
  80.  
  81.   if(col == 0) {
  82.     *status = U_MEMORY_ALLOCATION_ERROR;
  83.     return 0;
  84.   }
  85.  
  86.   return (UCollator*)col;
  87. }
  88.  
  89. U_CAPI UCollator*
  90. ucol_openRules(    const    UChar                  *rules,
  91.         int32_t                 rulesLength,
  92.         UNormalizationMode      mode,
  93.         UCollationStrength      strength,
  94.         UErrorCode              *status)
  95. {
  96.   if(U_FAILURE(*status)) return 0;
  97.  
  98.   int32_t len = (rulesLength == -1 ? u_strlen(rules) : rulesLength);
  99.   const UnicodeString ruleString((UChar*)rules, len, len);
  100.  
  101.   Normalizer::EMode normMode;
  102.   switch(mode) {
  103.   case UCOL_NO_NORMALIZATION:
  104.     normMode = Normalizer::NO_OP;
  105.     break;
  106.   case UCOL_DECOMP_CAN:
  107.     normMode = Normalizer::DECOMP;
  108.     break;
  109.   case UCOL_DECOMP_COMPAT:
  110.     normMode = Normalizer::DECOMP_COMPAT;
  111.     break;
  112.   case UCOL_DECOMP_CAN_COMP_COMPAT:
  113.     normMode = Normalizer::COMPOSE;
  114.     break;
  115.   case UCOL_DECOMP_COMPAT_COMP_CAN:
  116.     normMode = Normalizer::COMPOSE_COMPAT;
  117.     break;
  118.   }
  119.  
  120.   RuleBasedCollator *col = 0;
  121.   col = new RuleBasedCollator(ruleString, 
  122.                   (Collator::ECollationStrength) strength, 
  123.                   normMode, 
  124.                   *status);
  125.  
  126.   if(col == 0) {
  127.     *status = U_MEMORY_ALLOCATION_ERROR;
  128.     return 0;
  129.   }
  130.  
  131.   return (UCollator*) col;
  132. }
  133.  
  134. U_CAPI void
  135. ucol_close(UCollator *coll)
  136. {
  137.   delete (Collator*)coll;
  138. }
  139.  
  140. U_CAPI UCollationResult
  141. ucol_strcoll(    const    UCollator    *coll,
  142.         const    UChar        *source,
  143.         int32_t            sourceLength,
  144.         const    UChar        *target,
  145.         int32_t            targetLength)
  146. {
  147.   int32_t srcLen = (sourceLength == -1 ? u_strlen(source) : sourceLength);
  148.   const UnicodeString tempSource((UChar*)source, sourceLength, sourceLength);
  149.   int32_t targLen = (targetLength == -1 ? u_strlen(target) : targetLength);
  150.   const UnicodeString tempTarget((UChar*)target, targLen, targLen);
  151.   return (UCollationResult) ((Collator*)coll)->compare(tempSource, tempTarget);
  152. }
  153.  
  154. U_CAPI bool_t
  155. ucol_greater(    const    UCollator        *coll,
  156.         const    UChar            *source,
  157.         int32_t            sourceLength,
  158.         const    UChar            *target,
  159.         int32_t            targetLength)
  160. {
  161.   return (ucol_strcoll(coll, source, sourceLength, target, targetLength) 
  162.       == UCOL_GREATER);
  163. }
  164.  
  165. U_CAPI bool_t
  166. ucol_greaterOrEqual(    const    UCollator    *coll,
  167.             const    UChar        *source,
  168.             int32_t        sourceLength,
  169.             const    UChar        *target,
  170.             int32_t        targetLength)
  171. {
  172.   return (ucol_strcoll(coll, source, sourceLength, target, targetLength) 
  173.       != UCOL_LESS);
  174. }
  175.  
  176. U_CAPI bool_t
  177. ucol_equal(        const    UCollator        *coll,
  178.             const    UChar            *source,
  179.             int32_t            sourceLength,
  180.             const    UChar            *target,
  181.             int32_t            targetLength)
  182. {
  183.   return (ucol_strcoll(coll, source, sourceLength, target, targetLength) 
  184.       == UCOL_EQUAL);
  185. }
  186.  
  187. U_CAPI UCollationStrength
  188. ucol_getStrength(const UCollator *coll)
  189. {
  190.   return (UCollationStrength) ((Collator*)coll)->getStrength();
  191. }
  192.  
  193. U_CAPI void
  194. ucol_setStrength(    UCollator                *coll,
  195.             UCollationStrength        strength)
  196. {
  197.   ((Collator*)coll)->setStrength((Collator::ECollationStrength)strength);
  198. }
  199.  
  200. U_CAPI UNormalizationMode
  201. ucol_getNormalization(const UCollator* coll)
  202. {
  203.   switch(((Collator*)coll)->getDecomposition()) {
  204.   case Normalizer::NO_OP:
  205.     return UCOL_NO_NORMALIZATION;
  206.  
  207.   case Normalizer::COMPOSE:
  208.     return UCOL_DECOMP_COMPAT_COMP_CAN;
  209.  
  210.   case Normalizer::COMPOSE_COMPAT:
  211.     return UCOL_DECOMP_CAN_COMP_COMPAT;
  212.  
  213.   case Normalizer::DECOMP:
  214.     return UCOL_DECOMP_COMPAT;
  215.  
  216.   case Normalizer::DECOMP_COMPAT:
  217.     return UCOL_DECOMP_COMPAT;
  218.  
  219.   default:
  220.     break;
  221.   }
  222.   return UCOL_NO_NORMALIZATION;
  223. }
  224.  
  225. U_CAPI void
  226. ucol_setNormalization(  UCollator            *coll,
  227.             UNormalizationMode    mode)
  228. {
  229.   Normalizer::EMode normMode;
  230.   switch(mode) {
  231.   case UCOL_NO_NORMALIZATION:
  232.     normMode = Normalizer::NO_OP;
  233.     break;
  234.   case UCOL_DECOMP_CAN:
  235.     normMode = Normalizer::DECOMP;
  236.     break;
  237.   case UCOL_DECOMP_COMPAT:
  238.     normMode = Normalizer::DECOMP_COMPAT;
  239.     break;
  240.   case UCOL_DECOMP_COMPAT_COMP_CAN:
  241.     normMode = Normalizer::COMPOSE;
  242.     break;
  243.   case UCOL_DECOMP_CAN_COMP_COMPAT:
  244.     normMode = Normalizer::COMPOSE_COMPAT;
  245.     break;
  246.   }
  247.  
  248.   ((Collator*)coll)->setDecomposition(normMode);
  249. }
  250.  
  251. U_CAPI int32_t
  252. ucol_getDisplayName(    const    char        *objLoc,
  253.             const    char        *dispLoc,
  254.             UChar             *result,
  255.             int32_t         resultLength,
  256.             UErrorCode        *status)
  257. {
  258.   if(U_FAILURE(*status)) return -1;
  259.  
  260.   UnicodeString dst(result, resultLength, resultLength);
  261.   Collator::getDisplayName(Locale().init(objLoc), Locale().init(dispLoc), dst);
  262.   int32_t actLen;
  263.   T_fillOutputParams(&dst, result, resultLength, &actLen, status);
  264.   return actLen;
  265. }
  266.  
  267. U_CAPI const char*
  268. ucol_getAvailable(int32_t index)
  269. {
  270.   return uloc_getAvailable(index);
  271. }
  272.  
  273. U_CAPI int32_t
  274. ucol_countAvailable()
  275. {
  276.   return uloc_countAvailable();
  277. }
  278.  
  279. U_CAPI const UChar*
  280. ucol_getRules(    const    UCollator        *coll, 
  281.         int32_t            *length)
  282. {
  283.   const UnicodeString& rules = ((RuleBasedCollator*)coll)->getRules();
  284.   *length = rules.size();
  285.   return rules.getUChars();
  286. }
  287.  
  288. U_CAPI int32_t
  289. ucol_getSortKey(const    UCollator    *coll,
  290.         const    UChar        *source,
  291.         int32_t        sourceLength,
  292.         uint8_t        *result,
  293.         int32_t        resultLength)
  294. {
  295.   int32_t         count;
  296.   const uint8_t*     bytes = NULL;
  297.   CollationKey         key;
  298.   int32_t         copyLen;
  299.   int32_t         len = (sourceLength == -1 ? u_strlen(source) 
  300.                    : sourceLength);
  301.   UnicodeString     string((UChar*)source, len, len);
  302.   UErrorCode         status = U_ZERO_ERROR;
  303.  
  304.   ((Collator*)coll)->getCollationKey(string, key, status);
  305.   if(U_FAILURE(status)) 
  306.     return 0;
  307.  
  308.   bytes = key.getByteArray(count);
  309.   
  310.   copyLen = icu_min(count, resultLength);
  311.   icu_arrayCopy((const int8_t*)bytes, (int8_t*)result, copyLen);
  312.  
  313.   //  if(count > resultLength) {
  314.   //    *status = U_BUFFER_OVERFLOW_ERROR;
  315.   //  }
  316.  
  317.   return count;
  318. }
  319.  
  320. U_CAPI int32_t
  321. ucol_keyHashCode(    const    uint8_t*    key, 
  322.             int32_t        length)
  323. {
  324.   CollationKey newKey(key, length);
  325.   return newKey.hashCode();
  326. }
  327.  
  328. UCollationElements*
  329. ucol_openElements(    const    UCollator            *coll,
  330.             const    UChar                *text,
  331.             int32_t              textLength,
  332.             UErrorCode *status)
  333. {
  334.   int32_t len = (textLength == -1 ? u_strlen(text) : textLength);
  335.   const UnicodeString src((UChar*)text, len, len);
  336.  
  337.   CollationElementIterator *iter = 0;
  338.   iter = ((RuleBasedCollator*)coll)->createCollationElementIterator(src);
  339.   if(iter == 0) {
  340.     *status = U_MEMORY_ALLOCATION_ERROR;
  341.     return 0;
  342.   }
  343.  
  344.   return (UCollationElements*) iter;
  345. }
  346.  
  347. U_CAPI void
  348. ucol_closeElements(UCollationElements *elems)
  349. {
  350.   delete (CollationElementIterator*)elems;
  351. }
  352.  
  353. U_CAPI void
  354. ucol_reset(UCollationElements *elems)
  355. {
  356.   ((CollationElementIterator*)elems)->reset();
  357. }
  358.  
  359. U_CAPI int32_t
  360. ucol_next(    UCollationElements    *elems,
  361.         UErrorCode            *status)
  362. {
  363.   if(U_FAILURE(*status)) return UCOL_NULLORDER;
  364.  
  365.   return ((CollationElementIterator*)elems)->next(*status);
  366. }
  367.  
  368. U_CAPI int32_t
  369. ucol_previous(    UCollationElements    *elems,
  370.         UErrorCode            *status)
  371. {
  372.   if(U_FAILURE(*status)) return UCOL_NULLORDER;
  373.  
  374.   return ((CollationElementIterator*)elems)->previous(*status);
  375. }
  376.  
  377. U_CAPI int32_t
  378. ucol_getMaxExpansion(    const    UCollationElements    *elems,
  379.             int32_t                order)
  380. {
  381.   return ((CollationElementIterator*)elems)->getMaxExpansion(order);
  382. }
  383.  
  384. U_CAPI void
  385. ucol_setText(UCollationElements        *elems,
  386.          const    UChar                    *text,
  387.          int32_t                    textLength,
  388.          UErrorCode                *status)
  389. {
  390.   if(U_FAILURE(*status)) return;
  391.  
  392.   int32_t len = (textLength == -1 ? u_strlen(text) : textLength);
  393.   const UnicodeString src((UChar*)text, len, len);
  394.  
  395.   ((CollationElementIterator*)elems)->setText(src, *status);
  396. }
  397.  
  398. U_CAPI UTextOffset
  399. ucol_getOffset(const UCollationElements *elems)
  400. {
  401.   return ((CollationElementIterator*)elems)->getOffset();
  402. }
  403.  
  404. U_CAPI void
  405. ucol_setOffset(    UCollationElements    *elems,
  406.         UTextOffset            offset,
  407.         UErrorCode            *status)
  408. {
  409.   if(U_FAILURE(*status)) return;
  410.   
  411.   ((CollationElementIterator*)elems)->setOffset(offset, *status);
  412. }
  413.