home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / extra / ustdio / locbund.c next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  5.9 KB  |  222 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998           *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  9. *                                                                             *
  10. *******************************************************************************
  11. *
  12. * File locbund.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   11/18/98    stephen        Creation.
  18. *******************************************************************************
  19. */
  20.  
  21. #include "locbund.h"
  22.  
  23. #include "ustring.h"
  24. #include "uloc.h"
  25.  
  26. ULocaleBundle*        
  27. u_locbund_new(const char *loc)
  28. {
  29.   ULocaleBundle *result = (ULocaleBundle*) malloc(sizeof(ULocaleBundle));
  30.   int32_t len;
  31.  
  32.   if(result == 0)
  33.     return 0;
  34.  
  35.   len = (loc == 0 ? strlen(uloc_getDefault()) : strlen(loc));
  36.   result->fLocale = (char*) malloc(len);
  37.   if(result->fLocale == 0) {
  38.     free(result);
  39.     return 0;
  40.   }
  41.   
  42.   strcpy(result->fLocale, (loc == 0 ? uloc_getDefault() : loc) );
  43.   
  44.   result->fNumberFormat     = 0;
  45.   result->fPercentFormat     = 0;
  46.   result->fCurrencyFormat     = 0;
  47.   result->fScientificFormat     = 0;
  48.   result->fSpelloutFormat     = 0;
  49.   result->fDateFormat         = 0;
  50.   result->fTimeFormat         = 0;
  51.  
  52.   return result;
  53. }
  54.  
  55. ULocaleBundle*
  56. u_locbund_clone(const ULocaleBundle *bundle)
  57. {
  58.   ULocaleBundle *result = (ULocaleBundle*)malloc(sizeof(ULocaleBundle));
  59.   UErrorCode status = U_ZERO_ERROR;
  60.  
  61.   if(result == 0)
  62.     return 0;
  63.   
  64.   result->fLocale = (char*) malloc(strlen(bundle->fLocale));
  65.   if(result->fLocale == 0) {
  66.     free(result);
  67.     return 0;
  68.   }
  69.   
  70.   strcpy(result->fLocale, bundle->fLocale );
  71.   
  72.   result->fLocale         = bundle->fLocale;
  73.   
  74.   result->fNumberFormat     = (bundle->fNumberFormat == 0 ? 0 :
  75.                    unum_clone(bundle->fNumberFormat, &status));
  76.   result->fPercentFormat     = (bundle->fPercentFormat == 0 ? 0 :
  77.                    unum_clone(bundle->fPercentFormat, 
  78.                           &status));
  79.   result->fCurrencyFormat     = (bundle->fCurrencyFormat == 0 ? 0 :
  80.                    unum_clone(bundle->fCurrencyFormat, 
  81.                           &status));
  82.   result->fScientificFormat     = (bundle->fScientificFormat == 0 ? 0 :
  83.                    unum_clone(bundle->fScientificFormat, 
  84.                           &status));
  85.   result->fSpelloutFormat     = (bundle->fSpelloutFormat == 0 ? 0 :
  86.                    unum_clone(bundle->fSpelloutFormat, 
  87.                           &status));
  88.   result->fDateFormat         = (bundle->fDateFormat == 0 ? 0 :
  89.                    udat_clone(bundle->fDateFormat, &status));
  90.   result->fTimeFormat         = (bundle->fTimeFormat == 0 ? 0 :
  91.                    udat_clone(bundle->fTimeFormat, &status));
  92.  
  93.   return result;
  94. }
  95.  
  96. void
  97. u_locbund_delete(ULocaleBundle *bundle)
  98. {
  99.   free(bundle->fLocale);
  100.  
  101.   if(bundle->fNumberFormat != 0)
  102.     unum_close(bundle->fNumberFormat);
  103.   if(bundle->fPercentFormat != 0)
  104.     unum_close(bundle->fPercentFormat);
  105.   if(bundle->fCurrencyFormat != 0)
  106.     unum_close(bundle->fCurrencyFormat);
  107.   if(bundle->fScientificFormat != 0)
  108.     unum_close(bundle->fScientificFormat);
  109.   if(bundle->fSpelloutFormat != 0)
  110.     unum_close(bundle->fSpelloutFormat);
  111.   if(bundle->fDateFormat != 0)
  112.     udat_close(bundle->fDateFormat);
  113.   if(bundle->fTimeFormat != 0)
  114.     udat_close(bundle->fTimeFormat);
  115.  
  116.   free(bundle);
  117. }
  118.  
  119. UNumberFormat*        
  120. u_locbund_getNumberFormat(ULocaleBundle *bundle)
  121. {
  122.   UErrorCode status = U_ZERO_ERROR;
  123.   
  124.   if(bundle->fNumberFormat == 0) {
  125.     bundle->fNumberFormat = unum_open(UNUM_DEFAULT, bundle->fLocale, &status);
  126.     if(U_FAILURE(status))
  127.       return 0;
  128.   }
  129.   
  130.   return bundle->fNumberFormat;
  131. }
  132.  
  133. UNumberFormat*    
  134. u_locbund_getPercentFormat(ULocaleBundle *bundle)
  135. {
  136.   UErrorCode status = U_ZERO_ERROR;
  137.   
  138.   if(bundle->fPercentFormat == 0) {
  139.     bundle->fPercentFormat = unum_open(UNUM_PERCENT, bundle->fLocale, &status);
  140.     if(U_FAILURE(status))
  141.       return 0;
  142.   }
  143.   
  144.   return bundle->fPercentFormat;
  145. }
  146.  
  147. UNumberFormat*    
  148. u_locbund_getCurrencyFormat(ULocaleBundle *bundle)
  149. {
  150.   UErrorCode status = U_ZERO_ERROR;
  151.   
  152.   if(bundle->fCurrencyFormat == 0) {
  153.     bundle->fCurrencyFormat = unum_open(UNUM_CURRENCY, bundle->fLocale, 
  154.                     &status);
  155.     if(U_FAILURE(status))
  156.       return 0;
  157.   }
  158.   
  159.   return bundle->fCurrencyFormat;
  160. }
  161.  
  162. #define PAT_SIZE 512
  163.  
  164. UNumberFormat*    
  165. u_locbund_getScientificFormat(ULocaleBundle *bundle)
  166. {
  167.   UErrorCode status = U_ZERO_ERROR;
  168.   UChar pattern [PAT_SIZE];
  169.  
  170.   if(bundle->fScientificFormat == 0) {
  171.     /* create the pattern */
  172.     u_uastrcpy(pattern, "0.000000E00");
  173.     
  174.     bundle->fScientificFormat = unum_openPattern(pattern, -1,
  175.                          bundle->fLocale, &status);
  176.     
  177.     if(U_FAILURE(status))
  178.       return 0;
  179.   }
  180.   
  181.   return bundle->fScientificFormat;
  182. }
  183.  
  184. UNumberFormat*
  185. u_locbund_getSpelloutFormat(ULocaleBundle *bundle)
  186. {
  187.   UErrorCode status = U_ZERO_ERROR;
  188.   
  189.   if(bundle->fSpelloutFormat == 0) {
  190.     bundle->fSpelloutFormat = unum_open(UNUM_SPELLOUT, bundle->fLocale, 
  191.                     &status);
  192.   }
  193.   
  194.   return bundle->fSpelloutFormat;
  195. }
  196.  
  197. UDateFormat*
  198. u_locbund_getDateFormat(ULocaleBundle *bundle)
  199. {
  200.   UErrorCode status = U_ZERO_ERROR;
  201.   
  202.   if(bundle->fDateFormat == 0) {
  203.     bundle->fDateFormat = udat_open(UDAT_NONE, UDAT_DEFAULT, 
  204.                     bundle->fLocale, 0, 0, &status);
  205.   }
  206.   
  207.   return bundle->fDateFormat;
  208. }
  209.  
  210. UDateFormat*
  211. u_locbund_getTimeFormat(ULocaleBundle *bundle)
  212. {
  213.   UErrorCode status = U_ZERO_ERROR;
  214.   
  215.   if(bundle->fTimeFormat == 0) {
  216.     bundle->fTimeFormat = udat_open(UDAT_DEFAULT, UDAT_NONE, 
  217.                     bundle->fLocale, 0, 0, &status);
  218.   }
  219.  
  220.   return bundle->fTimeFormat;
  221. }
  222.