home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / tools / genrb / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  10.7 KB  |  478 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 list.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   06/01/99    stephen     Creation.
  18. *******************************************************************************
  19. */
  20.  
  21. #include "list.h"
  22. #include "cmemory.h"
  23. #include "ustring.h"
  24.  
  25. /* Protos */
  26. static void strlist_grow(struct SList *list, UErrorCode *status);
  27. static void strlist2d_grow(struct SList *list, UErrorCode *status);
  28. static void strlist2d_growRows(struct SList *list, UErrorCode *status);
  29. static void taglist_grow(struct SList *list, UErrorCode *status);
  30.  
  31. /* String list */
  32.  
  33. struct SList*
  34. strlist_open(UErrorCode *status)
  35. {
  36.   struct SList *list;
  37.   
  38.   if(U_FAILURE(*status)) return 0;
  39.   
  40.   list = (struct SList*) icu_malloc(sizeof(struct SList));
  41.   if(list == 0) {
  42.     *status = U_MEMORY_ALLOCATION_ERROR;
  43.     return 0;
  44.   }
  45.  
  46.   list->fType = eStringList;
  47.   
  48.   list->u.fStringList.fData = 0;
  49.   list->u.fStringList.fCount = 0;
  50.   list->u.fStringList.fCapacity = 32;
  51.  
  52.   strlist_grow(list, status);
  53.  
  54.   return list;
  55. }
  56.  
  57. void
  58. strlist_close(struct SList *list,
  59.           UErrorCode *status)
  60. {
  61.   int32_t i;
  62.  
  63.   if(U_FAILURE(*status)) return;
  64.  
  65.   if(list->fType != eStringList) {
  66.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  67.     return;
  68.   }
  69.   
  70.   /* deallocate each string */
  71.   for(i = 0; i < list->u.fStringList.fCount; ++i) {
  72.     icu_free(list->u.fStringList.fData[i]);
  73.   }
  74.   icu_free(list->u.fStringList.fData);
  75.   
  76.   list->fType = eEmpty;
  77.   icu_free(list);
  78. }
  79.  
  80. void
  81. strlist_add(struct SList *list,
  82.         const UChar *s,
  83.         UErrorCode *status)
  84. {
  85.   int32_t index;
  86.   
  87.   if(U_FAILURE(*status)) return;
  88.   
  89.   if(list->fType != eStringList) {
  90.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  91.     return;
  92.   }
  93.   
  94.   index = list->u.fStringList.fCount;
  95.   
  96.   if(list->u.fStringList.fCount == list->u.fStringList.fCapacity) 
  97.     strlist_grow(list, status);
  98.  
  99.   list->u.fStringList.fData[index] = (UChar*) 
  100.     icu_malloc(sizeof(UChar) * (u_strlen(s) + 1));
  101.   if(list->u.fStringList.fData[index] == 0) {
  102.     *status = U_MEMORY_ALLOCATION_ERROR;
  103.     return;
  104.   }
  105.  
  106.   u_strcpy(list->u.fStringList.fData[index], s);
  107.   ++(list->u.fStringList.fCount);
  108. }
  109.  
  110. static void 
  111. strlist_grow(struct SList *list,
  112.          UErrorCode *status)
  113. {
  114.   int32_t i, j;
  115.   int32_t newCapacity;
  116.   UChar **newData;
  117.   
  118.   if(U_FAILURE(*status)) return;
  119.   
  120.   if(list->fType != eStringList) {
  121.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  122.     return;
  123.   }
  124.   
  125.   newCapacity = list->u.fStringList.fCapacity << 1; 
  126.   
  127.   /* allocate space for the array of strings */
  128.   newData = (UChar**) icu_malloc(sizeof(UChar*) * newCapacity);
  129.   if(newData == 0) {
  130.     *status = U_MEMORY_ALLOCATION_ERROR;
  131.     return;
  132.   }
  133.  
  134.   /* allocate and copy each string */
  135.   for(i = 0; i < list->u.fStringList.fCount; ++i) {
  136.     newData[i] = (UChar*) 
  137.       icu_malloc(sizeof(UChar) * (u_strlen(list->u.fStringList.fData[i]) + 1));
  138.     if(newData[i] == 0) {
  139.       *status = U_MEMORY_ALLOCATION_ERROR;
  140.       for(j = 0; j < i; ++j) 
  141.     icu_free(newData[j]);
  142.       icu_free(newData);
  143.       return;
  144.     }
  145.     u_strcpy(newData[i], list->u.fStringList.fData[i]);
  146.   }
  147.   
  148.   icu_free(list->u.fStringList.fData);
  149.   list->u.fStringList.fData = newData;
  150.   list->u.fStringList.fCapacity = newCapacity;
  151. }
  152.  
  153. /* 2-d String list*/
  154.  
  155. struct SList*
  156. strlist2d_open(UErrorCode *status)
  157. {
  158.   struct SList *list;
  159.   
  160.   if(U_FAILURE(*status)) return 0;
  161.  
  162.   list = (struct SList*) icu_malloc(sizeof(struct SList));
  163.   if(list == 0) {
  164.     *status = U_MEMORY_ALLOCATION_ERROR;
  165.     return 0;
  166.   }
  167.  
  168.   list->fType = eStringList2d;
  169.   
  170.   list->u.fStringList2d.fData = 0;
  171.   list->u.fStringList2d.fCount = 0;
  172.   list->u.fStringList2d.fCapacity = 32;
  173.  
  174.   list->u.fStringList2d.fRows = 0;
  175.   list->u.fStringList2d.fRowCount = 0;
  176.   list->u.fStringList2d.fRowCapacity = 32;
  177.  
  178.   strlist2d_grow(list, status);
  179.   strlist2d_growRows(list, status);
  180.  
  181.   if(U_SUCCESS(*status)) {
  182.     list->u.fStringList2d.fRows[0] = 0;
  183.     list->u.fStringList2d.fRowCount = 1;
  184.   }
  185.  
  186.   return list;
  187. }
  188.  
  189. void 
  190. strlist2d_close(struct SList *list,
  191.         UErrorCode *status)
  192. {
  193.   int32_t i;
  194.   
  195.   if(U_FAILURE(*status)) return;
  196.   
  197.   if(list->fType != eStringList2d) {
  198.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  199.     return;
  200.   }
  201.  
  202.   /* deallocate each string */
  203.   for(i = 0; i < list->u.fStringList2d.fCount; ++i) {
  204.     icu_free(list->u.fStringList2d.fData[i]);
  205.   }
  206.   icu_free(list->u.fStringList2d.fData);
  207.  
  208.   icu_free(list->u.fStringList2d.fRows);
  209.  
  210.   list->fType = eEmpty;
  211.   icu_free(list);
  212. }
  213.  
  214. void
  215. strlist2d_newRow(struct SList *list,
  216.          UErrorCode *status)
  217. {
  218.   if(U_FAILURE(*status)) return;
  219.  
  220.   if(list->fType != eStringList2d) {
  221.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  222.     return;
  223.   }
  224.   
  225.   if(list->u.fStringList2d.fRowCount == list->u.fStringList2d.fRowCapacity)
  226.     strlist2d_growRows(list, status);
  227.   if(U_FAILURE(*status)) return;
  228.   list->u.fStringList2d.fRows[(list->u.fStringList2d.fRowCount)++] = 
  229.     list->u.fStringList2d.fCount;
  230. }
  231.  
  232. void strlist2d_add(struct SList *list,
  233.          const UChar *s,
  234.          UErrorCode *status)
  235. {
  236.   int32_t index;
  237.   
  238.   if(U_FAILURE(*status)) return;
  239.  
  240.   if(list->fType != eStringList2d) {
  241.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  242.     return;
  243.   }
  244.  
  245.   index = list->u.fStringList2d.fCount;
  246.   
  247.   if(list->u.fStringList2d.fCount == list->u.fStringList2d.fCapacity) 
  248.     strlist2d_grow(list, status);
  249.  
  250.   list->u.fStringList2d.fData[index] = (UChar*) 
  251.     icu_malloc(sizeof(UChar) * (u_strlen(s) + 1));
  252.   if(list->u.fStringList2d.fData[index] == 0) {
  253.     *status = U_MEMORY_ALLOCATION_ERROR;
  254.     return;
  255.   }
  256.  
  257.   u_strcpy(list->u.fStringList2d.fData[index], s);
  258.   ++(list->u.fStringList2d.fCount);
  259. }
  260.  
  261. static void
  262. strlist2d_grow(struct SList *list,
  263.            UErrorCode *status)
  264. {
  265.   int32_t i, j;
  266.   int32_t newCapacity;
  267.   UChar **newData;
  268.   
  269.   if(U_FAILURE(*status)) return;
  270.  
  271.   if(list->fType != eStringList2d) {
  272.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  273.     return;
  274.   }
  275.   
  276.   newCapacity = list->u.fStringList2d.fCapacity << 1; 
  277.   
  278.   /* allocate space for the array of strings */
  279.   newData = (UChar**) icu_malloc(sizeof(UChar*) * newCapacity);
  280.   if(newData == 0) {
  281.     *status = U_MEMORY_ALLOCATION_ERROR;
  282.     return;
  283.   }
  284.  
  285.   /* allocate and copy each string */
  286.   for(i = 0; i < list->u.fStringList2d.fCount; ++i) {
  287.     newData[i] = (UChar*) 
  288.       icu_malloc(sizeof(UChar) * (u_strlen(list->u.fStringList2d.fData[i]) + 1));
  289.     if(newData[i] == 0) {
  290.       *status = U_MEMORY_ALLOCATION_ERROR;
  291.       for(j = 0; j < i; ++j) 
  292.     icu_free(newData[j]);
  293.       icu_free(newData);
  294.       return;
  295.     }
  296.     u_strcpy(newData[i], list->u.fStringList2d.fData[i]);
  297.   }
  298.   
  299.   icu_free(list->u.fStringList2d.fData);
  300.   list->u.fStringList2d.fData = newData;
  301.   list->u.fStringList2d.fCapacity = newCapacity;
  302. }
  303.  
  304. static void 
  305. strlist2d_growRows(struct SList *list,
  306.            UErrorCode *status)
  307. {
  308.   int32_t i;
  309.   int32_t newCapacity;
  310.   int32_t *newRows;
  311.  
  312.   if(U_FAILURE(*status)) return;
  313.  
  314.   if(list->fType != eStringList2d) {
  315.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  316.     return;
  317.   }
  318.  
  319.   newCapacity = list->u.fStringList2d.fRowCapacity << 1;
  320.   
  321.   /* allocate space for the array of ints */
  322.   newRows = (int32_t*) icu_malloc(sizeof(int32_t) * newCapacity);
  323.   if(newRows == 0) {
  324.     *status = U_MEMORY_ALLOCATION_ERROR;
  325.   }
  326.   
  327.   /* copy each int */
  328.   for(i = 0; i < list->u.fStringList2d.fRowCount; ++i) 
  329.     newRows[i] = list->u.fStringList2d.fRows[i];
  330.   
  331.   /* clean up */
  332.   icu_free(list->u.fStringList2d.fRows);
  333.   list->u.fStringList2d.fRows = newRows;
  334.   list->u.fStringList2d.fRowCapacity = newCapacity;
  335. }
  336.  
  337. /* Tagged list */
  338.  
  339. struct SList*
  340. taglist_open(UErrorCode *status)
  341. {
  342.   struct SList *list;
  343.   
  344.   if(U_FAILURE(*status)) return 0;
  345.   
  346.   list = (struct SList*) icu_malloc(sizeof(struct SList));
  347.   if(list == 0) {
  348.     *status = U_MEMORY_ALLOCATION_ERROR;
  349.     return 0;
  350.   }
  351.  
  352.   list->fType = eTaggedList;
  353.   
  354.   list->u.fTaggedList.fData = 0;
  355.   list->u.fTaggedList.fCount = 0;
  356.   list->u.fTaggedList.fCapacity = 32;
  357.  
  358.   taglist_grow(list, status);
  359.   
  360.   return list;
  361. }
  362.  
  363. void 
  364. taglist_close(struct SList *list,
  365.           UErrorCode *status)
  366. {
  367.   if(U_FAILURE(*status)) return;
  368.  
  369.   if(list->fType != eTaggedList) {
  370.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  371.     return;
  372.   }
  373.   
  374.   icu_free(list->u.fTaggedList.fData);
  375.   
  376.   list->fType = eEmpty;
  377.   icu_free(list);
  378. }
  379.  
  380.  
  381. static void 
  382. taglist_grow(struct SList *list,
  383.          UErrorCode *status)
  384. {
  385.   int32_t i;
  386.   int32_t newCapacity;
  387.   struct SStringPair *newData;
  388.   
  389.   if(U_FAILURE(*status)) return;
  390.   
  391.   if(list->fType != eTaggedList) {
  392.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  393.     return;
  394.   }
  395.   
  396.   newCapacity = list->u.fTaggedList.fCapacity << 1; 
  397.   
  398.   /* allocate space for the array of string pairs */
  399.   newData = (struct SStringPair*) 
  400.     icu_malloc(sizeof(struct SStringPair) * newCapacity);
  401.   if(newData == 0) {
  402.     *status = U_MEMORY_ALLOCATION_ERROR;
  403.     return;
  404.   }
  405.  
  406.   /* copy each string pair */
  407.   for(i = 0; i < list->u.fTaggedList.fCount; ++i) {
  408.     newData[i] = list->u.fTaggedList.fData[i];
  409.   }
  410.   
  411.   icu_free(list->u.fTaggedList.fData);
  412.   list->u.fTaggedList.fData = newData;
  413.   list->u.fTaggedList.fCapacity = newCapacity;
  414. }
  415.  
  416. void
  417. taglist_add(struct SList *list,
  418.         const UChar *tag, 
  419.         const UChar *data,
  420.         UErrorCode *status)
  421. {
  422.   int32_t index;
  423.   struct SStringPair pair;
  424.   
  425.   if(U_FAILURE(*status)) return;
  426.   
  427.   if(list->fType != eTaggedList) {
  428.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  429.     return;
  430.   }
  431.  
  432.   pair.fKey = (UChar*) icu_malloc(sizeof(UChar) * (u_strlen(tag) + 1));
  433.   if(pair.fKey == 0) {
  434.     *status = U_MEMORY_ALLOCATION_ERROR;
  435.     return;
  436.   }
  437.  
  438.   pair.fValue = (UChar*) icu_malloc(sizeof(UChar) * (u_strlen(data) + 1));
  439.   if(pair.fValue == 0) {
  440.     *status = U_MEMORY_ALLOCATION_ERROR;
  441.     icu_free(pair.fKey);
  442.     return;
  443.   }
  444.  
  445.   u_strcpy(pair.fKey, tag);
  446.   u_strcpy(pair.fValue, data);
  447.   
  448.   index = list->u.fTaggedList.fCount;
  449.   
  450.   if(list->u.fTaggedList.fCount == list->u.fTaggedList.fCapacity)
  451.     taglist_grow(list, status);
  452.   
  453.   list->u.fTaggedList.fData[index] = pair;
  454.   ++(list->u.fTaggedList.fCount);
  455. }
  456.  
  457. const UChar* 
  458. taglist_get(const struct SList *list,
  459.         const UChar *tag,
  460.         UErrorCode *status)
  461. {
  462.   int32_t i;
  463.  
  464.   if(U_FAILURE(*status)) return 0;
  465.   
  466.   if(list->fType != eTaggedList) {
  467.     *status = U_ILLEGAL_ARGUMENT_ERROR;
  468.     return 0;
  469.   }
  470.  
  471.   for(i = 0; i < list->u.fTaggedList.fCount; ++i) {
  472.     if(u_strcmp(list->u.fTaggedList.fData[i].fKey, tag) == 0)
  473.       return list->u.fTaggedList.fData[i].fValue;
  474.   }
  475.  
  476.   return 0;
  477. }
  478.