home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / tools / rbdump / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  6.2 KB  |  297 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 "parse.h"
  22. #include "cmemory.h"
  23. #include "cstring.h"
  24. #include "filestrm.h"
  25. #include "ustring.h"
  26. #include "uprint.h"
  27.  
  28. /* Protos */
  29. static UChar* read_ustring(FileStream *rb, UErrorCode *status);
  30. static void read_strlist(FileStream *rb, UErrorCode *status);
  31. static void read_strlist2d(FileStream *rb, UErrorCode *status);
  32. static void read_taglist(FileStream *rb, UErrorCode *status);
  33.  
  34. /* Special values */
  35. static const int32_t sBOM = 0x021C;
  36.  
  37. #define STRINGLIST 1
  38. #define STRINGLIST2D 2
  39. #define TAGGEDLIST 3
  40.  
  41. static const int32_t sSTRINGLIST = 1;
  42. static const int32_t sSTRINGLIST2D = 2;
  43. static const int32_t sTAGGEDLIST = 3;
  44.  
  45. static const int32_t sEOF = -1;
  46.  
  47.  
  48. /* Read a UChar array */
  49. static UChar*
  50. read_ustring(FileStream *rb, 
  51.          UErrorCode *status)
  52. {
  53.   int32_t len;
  54.   UChar *s;
  55.  
  56.   if(U_FAILURE(*status)) return 0;
  57.  
  58.   /* Read the string's length */
  59.   T_FileStream_read(rb, &len, sizeof(len));
  60.  
  61.   /* Allocate space for the string */
  62.   s = (UChar*) icu_malloc(sizeof(UChar) * (len + 1));
  63.   if(s == 0) {
  64.     *status = U_MEMORY_ALLOCATION_ERROR;
  65.     return 0;
  66.   }
  67.  
  68.   /*  Read the string's data */
  69.   T_FileStream_read(rb, s, sizeof(UChar) * len);
  70.  
  71.   /* Add the null terminator */
  72.   s[len] = 0x0000;
  73.  
  74.   return s;
  75. }
  76.  
  77. /* Read a string list */
  78. static void
  79. read_strlist(FileStream *rb,
  80.          UErrorCode *status)
  81. {
  82.   int32_t i, count;
  83.   UChar *name;
  84.   UChar *s;
  85.  
  86.   if(U_FAILURE(*status)) return;
  87.  
  88.   /* Read the name of this string list */
  89.   name = read_ustring(rb, status);
  90.  
  91.   /* Read the number of items in this string list */
  92.   T_FileStream_read(rb, &count, sizeof(count));
  93.  
  94.   printf("  ");
  95.   uprint(name, stdout, status);
  96.   fputc('\n', stdout);
  97.  
  98.   /* Successively read strings in the list */
  99.   for(i = 0; i < count; ++i) {
  100.     s = read_ustring(rb, status);
  101.     
  102.     /* handle error */
  103.     if(U_FAILURE(*status) || s == 0) {
  104.       goto finish;
  105.     }
  106.  
  107.     printf("    ");
  108.     uprint(s, stdout, status);
  109.     fputc('\n', stdout);
  110.     icu_free(s);
  111.   }
  112.  
  113.  finish:
  114.  
  115.   /* clean up */
  116.   icu_free(name);
  117. }
  118.  
  119. /* Read a 2-d string list */
  120. static void 
  121. read_strlist2d(FileStream *rb,
  122.            UErrorCode *status)
  123. {
  124.   int32_t i, j;
  125.   int32_t rows, itemcount;
  126.   UChar *name;
  127.   UChar *s;
  128.  
  129.   if(U_FAILURE(*status)) return;
  130.  
  131.   /* Read the name of this 2-d string list */
  132.   name = read_ustring(rb, status);
  133.  
  134.   /* Read the number of rows in this 2-d string list */
  135.   T_FileStream_read(rb, &rows, sizeof(rows));
  136.  
  137.   printf("  ");
  138.   uprint(name, stdout, status);
  139.   fputc('\n', stdout);
  140.  
  141.   /* Read each row */
  142.   for(i = 0; i < rows; ++i) {
  143.  
  144.     /* Read the number of items in this row */
  145.     T_FileStream_read(rb, &itemcount, sizeof(itemcount));
  146.  
  147.     printf("    ");
  148.  
  149.     /* Read each item */
  150.     for(j = 0; j < itemcount; ++j) {
  151.       s = read_ustring(rb, status);
  152.       
  153.       /* handle error */
  154.       if(U_FAILURE(*status) || s == 0) {
  155.     goto finish;
  156.       }
  157.       
  158.       uprint(s, stdout, status);
  159.       if(j != itemcount - 1)
  160.     fputc(',', stdout);
  161.  
  162.       icu_free(s);
  163.     }
  164.  
  165.     puts("");
  166.   }
  167.   
  168.  finish:
  169.  
  170.   /* clean up */
  171.   icu_free(name);
  172. }
  173.  
  174. /* Read a tagged list */
  175. static void 
  176. read_taglist(FileStream *rb, 
  177.          UErrorCode *status)
  178. {
  179.   int32_t i, count;
  180.   UChar *name;
  181.   UChar *tag, *value;
  182.  
  183.   if(U_FAILURE(*status)) return;
  184.  
  185.   /* Read the name of this tagged list */
  186.   name = read_ustring(rb, status);
  187.  
  188.   /* Read the number of items in this tagged list */
  189.   T_FileStream_read(rb, &count, sizeof(count));
  190.  
  191.   printf("  ");
  192.   uprint(name, stdout, status);
  193.   fputc('\n', stdout);
  194.  
  195.   /* Successively read strings in the list */
  196.   for(i = 0; i < count; ++i) {
  197.     tag = read_ustring(rb, status);
  198.     value = read_ustring(rb, status);
  199.     
  200.     /* handle error */
  201.     if(U_FAILURE(*status) || tag == 0 || value == 0) {
  202.       goto finish;
  203.     }
  204.  
  205.     printf("    ");
  206.     uprint(tag, stdout, status);
  207.     printf(": ");
  208.     uprint(value, stdout, status);
  209.     fputc('\n', stdout);
  210.     
  211.     icu_free(tag);
  212.     icu_free(value);
  213.   }
  214.  
  215.  finish:
  216.  
  217.   /* clean up */
  218.   icu_free(name);
  219. }
  220.  
  221. /* Parse a compiled rb file */
  222. void
  223. parse(FileStream *f, 
  224.       UErrorCode *status)
  225. {
  226.   int32_t bom;
  227.   int32_t itemtype;
  228.   UChar *localename;
  229.  
  230.   if(U_FAILURE(*status)) return;
  231.  
  232.   /* Read the byte order mark from the file */
  233.   T_FileStream_read(f, &bom, sizeof(bom));
  234.   
  235.   /* Verify the byte ordering matches */
  236.   if(bom != sBOM) {
  237.     *status = U_INVALID_FORMAT_ERROR;
  238.     goto finish;
  239.   }
  240.  
  241.   /* Read the locale name from the file */
  242.   localename = read_ustring(f, status);
  243.  
  244.   uprint(localename, stdout, status);
  245.   fputc('\n', stdout);
  246.  
  247.   if(U_FAILURE(*status)) {
  248.     goto finish;
  249.   }
  250.  
  251.   /* Successively read each list item */
  252.   for(;;) {
  253.  
  254.     /* Read the next item type */
  255.     T_FileStream_read(f, &itemtype, sizeof(itemtype));
  256.  
  257.     /* If we're at EOF, break */
  258.     if(itemtype == sEOF) {
  259.       goto finish;
  260.     }
  261.    
  262.     /* Parse each item */
  263.     switch(itemtype) {
  264.     case STRINGLIST:
  265.       read_strlist(f, status);
  266.       if(U_FAILURE(*status)) {
  267.     goto finish;
  268.       }
  269.       break;
  270.  
  271.     case STRINGLIST2D:
  272.       read_strlist2d(f, status);
  273.       if(U_FAILURE(*status)) {
  274.     goto finish;
  275.       }
  276.       break;
  277.  
  278.     case TAGGEDLIST:
  279.       read_taglist(f, status);
  280.       if(U_FAILURE(*status)) {
  281.     goto finish;
  282.       }
  283.       break;
  284.     }
  285.   }
  286.  
  287.   /* Check if any errors occurred during reading */
  288.   if(T_FileStream_error(f) != 0) {
  289.     *status = U_FILE_ACCESS_ERROR;
  290.   }
  291.  
  292.  finish:
  293.   
  294.   /* clean up */
  295.   icu_free(localename);
  296. }
  297.