home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / tools / chardir / chardir.cpp next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  4.4 KB  |  157 lines

  1. /*
  2.  * (C) Copyright IBM Corp. 1998 - All Rights Reserved
  3.  * @version    1.0 06/19/98
  4.  * @author    Helena Shih
  5.  * Based on Taligent international support for C++
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <iostream.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include "cmemory.h"
  14. #include "ucmp8.h"
  15. CompactByteArray* charDirArray = 0;
  16.  
  17. static const UChar LAST_CHAR_CODE_IN_FILE = 0xFFFD;
  18. const char charDirStrings[] = "L  R  EN ES ET AN CS B  S  WS ON LRELROAL RLERLOPDFNSMBN ";
  19.  
  20. int tagValues[] = { 
  21.     0,  // kLeftToRight              = 0, 
  22.     1,  // kRightToLeft              = 1,
  23.     2,  // kEuropeanNumber           = 2,
  24.     3,  // kEuropeanNumberSeparator  = 3,
  25.     4,  // kEuropeanNumberTerminator = 4,
  26.     5,  // kArabicNumber             = 5,
  27.     6,  // kCommonNumberSeparator    = 6,
  28.     7,  // kParagraphSeparator       = 7,
  29.     8,  // kSegmentSeparator         = 8,
  30.     9,  // kWhiteSpaceNeutral        = 9, 
  31.     10, // kOtherNeutral             = 10,
  32.     11, // kLeftToRightEmbedding     = 11,
  33.     12, // kLeftToRightOverride      = 12,
  34.     13, // kRightToLeftArabic        = 13,
  35.     14, // kRightToLeftEmbedding     = 14,
  36.     15, // kRightToLeftOverride      = 15,
  37.     16, // kPopDirectionalFormat     = 16,
  38.     17, // kNonSpacingMark           = 17,
  39.     18  // kBoundaryNeutral          = 18,
  40.     
  41. };
  42.  
  43. int MakeProp(char* str) 
  44. {
  45.     int result = 0;
  46.     char* matchPosition;
  47.     matchPosition = strstr(charDirStrings, str);
  48.     if (matchPosition == 0) fprintf(stderr, "unrecognized type letter %s\n", str);
  49.     else result = ((matchPosition - charDirStrings) / 3);
  50.     return result;
  51. }
  52.  
  53. CompactByteArray*
  54. getArray(FILE *input)
  55. {
  56.     if (charDirArray == 0) {
  57.         char    buffer[1000];
  58.         char*    bufferPtr;
  59.         int  set = FALSE;
  60.  
  61.         try {
  62.             charDirArray = ucmp8_open(0);
  63.             int32_t unicode;
  64.             char *next;
  65.             char dir[4];
  66.             while (TRUE) {
  67.                 // Clear buffer first.
  68.                 bufferPtr = fgets(buffer, 999, input);
  69.                 if (bufferPtr == NULL) break;
  70.                 if (bufferPtr[0] == '#' || bufferPtr[0] == '\n' || bufferPtr[0] == 0) continue;
  71.                 sscanf(bufferPtr, "%X", &unicode);
  72.                 assert(0 <= unicode && unicode < 65536);
  73.                 for (int i = 0; i < 4; i++) {
  74.                     bufferPtr = strchr(bufferPtr, ';');
  75.                     assert(bufferPtr != NULL);
  76.                     bufferPtr++;
  77.                 }
  78.                 assert(bufferPtr != NULL);
  79.                 next = strchr(bufferPtr, ';');
  80.                 *next = 0;
  81.                /* for (int j = 0; j < 3; j++) {
  82.                    if (bufferPtr+j!= next)
  83.                         dir[j] = bufferPtr[j];
  84.                    else
  85.                         dir[j] = ' ';
  86.                 }*/
  87.                 for(int j=0; bufferPtr+j != next; j++)
  88.                     dir[j] = bufferPtr[j];
  89.                 while(j<3)
  90.                 {
  91.                     dir[j] = ' ';
  92.                     j++;
  93.                 }
  94.                 dir[3] = 0;
  95.                 ucmp8_set(charDirArray, (UChar)unicode, (int8_t)tagValues[MakeProp(dir)]);
  96.                 }
  97.  
  98.             if (input) fclose(input);
  99.             ucmp8_compact(charDirArray, 1);
  100.         }
  101.         catch (...) {
  102.             fprintf(stderr, "Error Occured while parsing unicode data file.\n");
  103.         }
  104.     }
  105.     return charDirArray;
  106. }
  107.  
  108. void 
  109. writeArrays()
  110. {
  111.     const int8_t* values = ucmp8_getArray(charDirArray);
  112.     const uint16_t* indexes = ucmp8_getIndex(charDirArray);
  113.     int32_t i;
  114.     int32_t cnt = ucmp8_getCount(charDirArray);
  115.     cout << "\nconst t_uint32 Unicode::fCharDirIndices[] = {\n    ";
  116.     for (i = 0; i < ucmp8_getkIndexCount()-1; i++)
  117.     {
  118.         cout << "(uint16_t)" << ((indexes[i] >= 0) ? (int)indexes[i] : (int)(indexes[i]+ucmp8_getkUnicodeCount()))
  119.                          << ", ";
  120.         if (i != 0)
  121.             if (i % 3 == 0)
  122.                 cout << "\n    ";
  123.     }
  124.     cout << "    (uint16_t)" << ((indexes[ucmp8_getkIndexCount()-1] >= 0) ? (int)indexes[i] : (int)(indexes[i]+ucmp8_getkUnicodeCount()))
  125.                        << " };\n";
  126.     cout << "\nconst int8_t Unicode::fCharDirValues[] = {\n    ";
  127.     for (i = 0; i < cnt-1; i++)
  128.     {
  129.         cout << "(int8_t)" << (int)values[i] << ", ";
  130.         if (i != 0)
  131.             if (i % 5 == 0)
  132.                 cout << "\n    ";
  133.     }
  134.     cout << "    (int8_t)" << (int)values[cnt-1] << " }\n";
  135.     cout << "const int32_t Unicode::fCharDirCount = " << cnt << ";\n";
  136. }
  137. /**
  138.  * The main function builds the CharType data array and prints it to System.out
  139.  */
  140. void main(int argc, char** argv)
  141. {
  142.     CompactByteArray* arrays = 0;
  143.     FILE *input = 0;
  144.     if (argc != 2) {
  145.         printf("Usage : chartype filename\n\n");
  146.         exit(1);
  147.     }
  148.     input = fopen(argv[1], "r");
  149.     if (input == 0) {
  150.         printf("Cannot open the input file: %s\n\n", argv[1]);
  151.         exit(1);
  152.     }
  153.     arrays = getArray(input);
  154.     writeArrays();
  155. }
  156.  
  157.