home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / tools / genrb / write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  5.5 KB  |  209 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 write.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   06/01/99    stephen     Creation.
  18. *******************************************************************************
  19. */
  20.  
  21. #include <stdio.h>
  22. #include "write.h"
  23. #include "cmemory.h"
  24. #include "cstring.h"
  25. #include "filestrm.h"
  26. #include "ustring.h"
  27. #include "error.h"
  28. #include "list.h"
  29.  
  30.  
  31. /* Protos */
  32. static void write_ustring(FileStream *rb, const UChar *data);
  33. static void write_strlist(FileStream *rb, const UChar *name, 
  34.               const struct SStringList *list);
  35. static void write_strlist2d(FileStream *rb, const UChar *name,
  36.                 const struct SStringList2d *list);
  37. static void write_taglist(FileStream *rb, const UChar *name,
  38.               const struct STaggedList *list);
  39.  
  40. /* Special values */
  41. static const int32_t sBOM = 0x021C;
  42.  
  43. static const int32_t sEOF = -1;
  44.  
  45. static const int32_t sSTRINGLIST = 1;
  46. static const int32_t sSTRINGLIST2D = 2;
  47. static const int32_t sTAGGEDLIST = 3;
  48.  
  49. static const UChar gCollationElementsTag [] = {
  50.   /* "CollationElements" */
  51.   0x0043, 0x006f, 0x006c, 0x006c, 0x0061, 0x0074, 0x0069, 0x006f, 0x006e,
  52.   0x0045, 0x006c, 0x0065, 0x006d, 0x0065, 0x006e, 0x0074, 0x0073, 0x0000
  53. };
  54.  
  55. /* Write a null-terminated UChar array */
  56. static void
  57. write_ustring(FileStream *rb, 
  58.           const UChar *data)
  59. {
  60.   int32_t len;
  61.  
  62.   len = u_strlen(data);
  63.  
  64.   /* Write the string's length */
  65.   T_FileStream_write(rb, &len, sizeof(len));
  66.  
  67.   /* Write the string's data */
  68.   T_FileStream_write(rb, data, sizeof(UChar) * len);
  69. }
  70.  
  71. /* Write a string list */
  72. static void
  73. write_strlist(FileStream *rb, 
  74.           const UChar *name,
  75.           const struct SStringList *list)
  76. {
  77.   int32_t i;
  78.  
  79.   /* Write out the value indicating this is a string list */
  80.   T_FileStream_write(rb, &sSTRINGLIST, sizeof(sSTRINGLIST));
  81.  
  82.   /* Write the name of this string list */
  83.   write_ustring(rb, name);
  84.  
  85.   /* Write the item count */
  86.   T_FileStream_write(rb, &list->fCount, sizeof(list->fCount));
  87.  
  88.   /* Write each string in the list */
  89.   for(i = 0; i < list->fCount; ++i) {
  90.     write_ustring(rb, list->fData[i]);
  91.   }
  92. }
  93.  
  94. /* Write a 2-d string list */
  95. static void 
  96. write_strlist2d(FileStream *rb,
  97.         const UChar *name,
  98.         const struct SStringList2d *list)
  99. {
  100.   int32_t i, j;
  101.   int32_t itemcount;
  102.  
  103.   /* Write out the value indicating this is a 2-d string list */
  104.   T_FileStream_write(rb, &sSTRINGLIST2D, sizeof(sSTRINGLIST2D));
  105.  
  106.   /* Write the name of this 2-d string list */
  107.   write_ustring(rb, name);
  108.  
  109.   /* Write the row count */
  110.   T_FileStream_write(rb, &list->fRowCount, sizeof(list->fRowCount));
  111.  
  112.   /* Write out each row */
  113.   for(i = 0; i < list->fRowCount; ++i) {
  114.     itemcount = (i == list->fRowCount - 1 ? list->fCount: list->fRows[i+1])
  115.       - list->fRows[i];
  116.  
  117.     /* Write out the count of strings in this row */
  118.     T_FileStream_write(rb, &itemcount, sizeof(itemcount));
  119.  
  120.     /* Write out each string in the row */
  121.     for(j = 0; j < itemcount; ++j) {
  122.       write_ustring(rb, list->fData[list->fRows[i] + j]);
  123.     }
  124.   }
  125. }
  126.  
  127. /* Write a tagged list */
  128. static void 
  129. write_taglist(FileStream *rb, 
  130.           const UChar *name,
  131.           const struct STaggedList *list)
  132. {
  133.   int32_t i;
  134.  
  135.   /* Write out the value indicating this is a tagged list */
  136.   T_FileStream_write(rb, &sTAGGEDLIST, sizeof(sTAGGEDLIST));
  137.  
  138.   /* Write the name of this tagged list */
  139.   write_ustring(rb, name);
  140.  
  141.   /* Write the item count */
  142.   T_FileStream_write(rb, &list->fCount, sizeof(list->fCount));
  143.  
  144.   /* Write out each key/value pair */
  145.   for(i = 0; i < list->fCount; ++i) {
  146.     write_ustring(rb, list->fData[i].fKey);
  147.     write_ustring(rb, list->fData[i].fValue);
  148.   }
  149. }
  150.  
  151. /* Write a parsed SRBItemList to a file */
  152. void
  153. rb_write(FileStream *f, 
  154.      struct SRBItemList *data, 
  155.      UErrorCode *status)
  156. {
  157.   int32_t i;
  158.   struct SRBItem *item;
  159.  
  160.   if(U_FAILURE(*status)) return;
  161.  
  162.   /* Write the byte order mark to the file */
  163.   T_FileStream_write(f, &sBOM, sizeof(sBOM));
  164.  
  165.   /* Write the locale name to the file */
  166.   write_ustring(f, data->fLocale);
  167.  
  168.   /* Successively write each list item */
  169.   for(i = 0; i < data->fCount; ++i) {
  170.  
  171.     item = data->fData[i];
  172.  
  173.     switch(item->fData->fType) {
  174.     case eStringList:
  175.       /*if(u_strcmp(item->fTag, gCollationElementsTag) == 0)
  176.     puts("got CollationElements");*/
  177.       write_strlist(f, item->fTag, &item->fData->u.fStringList);
  178.       break;
  179.  
  180.     case eStringList2d:
  181.       write_strlist2d(f, item->fTag, &item->fData->u.fStringList2d);
  182.       break;
  183.  
  184.     case eTaggedList:
  185.       write_taglist(f, item->fTag, &item->fData->u.fTaggedList);
  186.       break;
  187.  
  188.     case eEmpty:
  189.       *status = U_INTERNAL_PROGRAM_ERROR;
  190.       setErrorText("Unexpected empty item found");
  191.       goto finish;
  192.       /*break;*/
  193.     }
  194.   }
  195.  
  196.   /* Indicate the end of the data */
  197.   T_FileStream_write(f, &sEOF, sizeof(sEOF));
  198.  
  199.   /* Check if any errors occurred during writing */
  200.   if(T_FileStream_error(f) != 0) {
  201.     *status = U_FILE_ACCESS_ERROR;
  202.   }
  203.  
  204.  finish:
  205.   ;
  206.   
  207.   /* clean up */
  208. }
  209.