home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / tools / genrb / rblist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  3.9 KB  |  178 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998, 1999     *
  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 rblist.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   06/01/99    stephen     Creation.
  18. *******************************************************************************
  19. */
  20.  
  21. #include "rblist.h"
  22. #include "ustr.h"
  23. #include "ustring.h"
  24. #include "cmemory.h"
  25.  
  26. /* Protos */
  27. static void rblist_grow(struct SRBItemList *list, UErrorCode *status);
  28.  
  29. struct SRBItem* 
  30. make_rbitem(const UChar *tag, 
  31.         const struct SList *data, 
  32.         UErrorCode *status)
  33. {
  34.   struct SRBItem *item;
  35.   UChar *s;
  36.  
  37.   if(U_FAILURE(*status)) return 0;
  38.   
  39.   item = (struct SRBItem*) icu_malloc(sizeof(struct SRBItem));
  40.   if(item == 0) {
  41.     *status = U_MEMORY_ALLOCATION_ERROR;
  42.     return 0;
  43.   }
  44.  
  45.   s = (UChar*) icu_malloc(sizeof(UChar) * (u_strlen(tag) + 1));
  46.   if(s == 0) {
  47.     *status = U_MEMORY_ALLOCATION_ERROR;
  48.     return 0;
  49.   }
  50.   u_strcpy(s, tag);
  51.  
  52.   item->fTag = s;
  53.   item->fData = (struct SList*) data;
  54.  
  55.   return item;
  56. }
  57.  
  58. struct SRBItemList*
  59. rblist_open(UErrorCode *status)
  60. {
  61.   struct SRBItemList *list;
  62.   
  63.   if(U_FAILURE(*status)) return 0;
  64.   
  65.   list = (struct SRBItemList*) icu_malloc(sizeof(struct SRBItemList));
  66.   if(list == 0) {
  67.     *status = U_MEMORY_ALLOCATION_ERROR;
  68.     return 0;
  69.   }
  70.  
  71.   list->fLocale = 0;
  72.  
  73.   list->fData = 0;
  74.   list->fCount = 0;
  75.   list->fCapacity = 32;
  76.  
  77.   rblist_grow(list, status);
  78.  
  79.   return list;
  80. }
  81.  
  82. void rblist_close(struct SRBItemList *list,
  83.          UErrorCode *status)
  84. {
  85.   int32_t i;
  86.  
  87.   if(U_FAILURE(*status)) return;
  88.   
  89.   /* deallocate each list */
  90.   for(i = 0; i < list->fCount; ++i) {
  91.     
  92.     switch(list->fData[i]->fData->fType) {
  93.     case eStringList:
  94.       strlist_close(list->fData[i]->fData, status);
  95.       break;
  96.       
  97.     case eStringList2d:
  98.       strlist2d_close(list->fData[i]->fData, status);
  99.       break;
  100.       
  101.     case eTaggedList:
  102.       taglist_close(list->fData[i]->fData, status);
  103.       break;
  104.  
  105.     case eEmpty:
  106.       break;
  107.     }
  108.   }
  109.   icu_free(list->fData);
  110.   icu_free(list->fLocale);
  111.  
  112.   icu_free(list);
  113. }
  114.  
  115. void rblist_setlocale(struct SRBItemList *list, 
  116.               const UChar *locale, 
  117.               UErrorCode *status)
  118. {
  119.   if(U_FAILURE(*status)) return;
  120.  
  121.   /* Allocate enough space */
  122.   list->fLocale = (UChar*) icu_realloc(list->fLocale, 
  123.                        sizeof(UChar) * (u_strlen(locale) + 1));
  124.   if(list->fLocale == 0) {
  125.     *status = U_MEMORY_ALLOCATION_ERROR;
  126.     return;
  127.   }
  128.  
  129.   u_strcpy(list->fLocale, locale);
  130. }
  131.  
  132. void rblist_add(struct SRBItemList *list,
  133.         struct SRBItem *s,
  134.         UErrorCode *status)
  135. {
  136.   int32_t index;
  137.   
  138.   if(U_FAILURE(*status)) return;
  139.   
  140.   index = list->fCount;
  141.   
  142.   if(list->fCount == list->fCapacity) 
  143.     rblist_grow(list, status);
  144.   
  145.   list->fData[index] = s;
  146.   ++(list->fCount);
  147. }
  148.  
  149. static void 
  150. rblist_grow(struct SRBItemList *list,
  151.         UErrorCode *status)
  152. {
  153.   int32_t i;
  154.   int32_t newCapacity;
  155.   struct SRBItem **newData;
  156.   
  157.   if(U_FAILURE(*status)) return;
  158.   
  159.   newCapacity = list->fCapacity << 1; 
  160.   
  161.   /* allocate space for the array of SRBItems */
  162.   newData = (struct SRBItem**) 
  163.     icu_malloc(sizeof(struct SRBItem*) * newCapacity);
  164.   if(newData == 0) {
  165.     *status = U_MEMORY_ALLOCATION_ERROR;
  166.     return;
  167.   }
  168.  
  169.   /* copy each item */
  170.   for(i = 0; i < list->fCount; ++i) {
  171.     newData[i] = list->fData[i];
  172.   }
  173.   
  174.   icu_free(list->fData);
  175.   list->fData = newData;
  176.   list->fCapacity = newCapacity;
  177. }
  178.