home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / tools / ulxfrm / ulxfrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  6.6 KB  |  220 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.  
  14. #include "ucmp16.h"
  15. CompactShortArray* ulxfrmArray = 0;
  16.  
  17.     enum    ECharTypeMapping {
  18.         UNASSIGNED                = 0,
  19.     UPPERCASE_LETTER        = 1,
  20.     LOWERCASE_LETTER        = 2,
  21.     TITLECASE_LETTER        = 3,
  22.     MODIFIER_LETTER            = 4,
  23.     OTHER_LETTER            = 5,
  24.     NON_SPACING_MARK        = 6,
  25.     ENCLOSING_MARK            = 7,
  26.     COMBINING_SPACING_MARK    = 8,
  27.     DECIMAL_DIGIT_NUMBER    = 9,
  28.     LETTER_NUMBER            = 10,
  29.     OTHER_NUMBER            = 11,
  30.     SPACE_SEPARATOR            = 12,
  31.     LINE_SEPARATOR            = 13,
  32.     PARAGRAPH_SEPARATOR        = 14,
  33.     CONTROL                    = 15,
  34.     FORMAT                    = 16,
  35.     PRIVATE_USE                = 17,
  36.     SURROGATE                = 18,
  37.     DASH_PUNCTUATION        = 19,
  38.     START_PUNCTUATION        = 20,
  39.     END_PUNCTUATION            = 21,
  40.     CONNECTOR_PUNCTUATION    = 22,
  41.     OTHER_PUNCTUATION        = 23,
  42.     MATH_SYMBOL                = 24,
  43.     CURRENCY_SYMBOL            = 25,
  44.     MODIFIER_SYMBOL            = 26,
  45.     OTHER_SYMBOL            = 27,
  46.     INITIAL_PUNCTUATION     = 28,
  47.     FINAL_PUNCTUATION       = 29
  48.     };
  49.  
  50. static const UChar LAST_CHAR_CODE_IN_FILE = 0xFFFD;
  51. const char tagStrings[] = "MnMcMeNdNlNoZsZlZpCcCfCsCoCnLuLlLtLmLoPcPdPsPePoSmScSkSoPiPf";
  52. const int16_t tagValues[] =
  53.     {
  54.     /* Mn */ (int16_t)NON_SPACING_MARK,
  55.     /* Mc */ (int16_t)COMBINING_SPACING_MARK,
  56.     /* Me */ (int16_t)ENCLOSING_MARK,
  57.     /* Nd */ (int16_t)DECIMAL_DIGIT_NUMBER,
  58.     /* Nl */ (int16_t)LETTER_NUMBER,
  59.     /* No */ (int16_t)OTHER_NUMBER,
  60.     /* Zs */ (int16_t)SPACE_SEPARATOR,
  61.     /* Zl */ (int16_t)LINE_SEPARATOR,
  62.     /* Zp */ (int16_t)PARAGRAPH_SEPARATOR,
  63.     /* Cc */ (int16_t)CONTROL,
  64.     /* Cf */ (int16_t)FORMAT,
  65.     /* Cs */ (int16_t)SURROGATE,
  66.     /* Co */ (int16_t)PRIVATE_USE,
  67.     /* Cn */ (int16_t)UNASSIGNED,
  68.     /* Lu */ (int16_t)UPPERCASE_LETTER,
  69.     /* Ll */ (int16_t)LOWERCASE_LETTER,
  70.     /* Lt */ (int16_t)TITLECASE_LETTER,
  71.     /* Lm */ (int16_t)MODIFIER_LETTER,
  72.     /* Lo */ (int16_t)OTHER_LETTER,
  73.     /* Pc */ (int16_t)CONNECTOR_PUNCTUATION,
  74.     /* Pd */ (int16_t)DASH_PUNCTUATION,
  75.     /* Ps */ (int16_t)START_PUNCTUATION,
  76.     /* Pe */ (int16_t)END_PUNCTUATION,
  77.     /* Po */ (int16_t)OTHER_PUNCTUATION,
  78.     /* Sm */ (int16_t)MATH_SYMBOL,
  79.     /* Sc */ (int16_t)CURRENCY_SYMBOL,
  80.     /* Sk */ (int16_t)MODIFIER_SYMBOL,
  81.     /* So */ (int16_t)OTHER_SYMBOL,
  82.     /* Pi */ (int16_t)INITIAL_PUNCTUATION,
  83.     /* Pf */ (int16_t)FINAL_PUNCTUATION
  84.     };
  85. int 
  86. MakeProp(char* str) 
  87. {
  88.     int result = 0;
  89.     char* matchPosition;
  90.     
  91.     matchPosition = strstr(tagStrings, str);
  92.     if (matchPosition == 0) fprintf(stderr, "unrecognized type letter %s", str);
  93.     else result = ((matchPosition - tagStrings) / 2);
  94.     return result;
  95. }
  96.  
  97. CompactShortArray*
  98. getArray(FILE *input)
  99. {
  100.     if (ulxfrmArray == 0) {
  101.         char    buffer[1000];
  102.         char*    bufferPtr;
  103.         int  set = FALSE;
  104.         char type[3];
  105.  
  106.         try {
  107.             ulxfrmArray = ucmp16_open((int16_t)0xffff);
  108.             int32_t unicode, otherunicode, digit, i;
  109.             while (TRUE) {
  110.                 otherunicode = 0xffff;
  111.                 digit = -1;
  112.                 bufferPtr = fgets(buffer, 999, input);
  113.                 if (bufferPtr == NULL) break;
  114.                 if (bufferPtr[0] == '#' || bufferPtr[0] == '\n' || bufferPtr[0] == 0) continue;
  115.                 sscanf(bufferPtr, "%X", &unicode);
  116.                 assert(0 <= unicode && unicode < 65536);
  117.                 bufferPtr = strchr(bufferPtr, ';');
  118.                 assert(bufferPtr != NULL);
  119.                 bufferPtr = strchr(bufferPtr + 1, ';');
  120.                 strncpy(type, ++bufferPtr, 2);    // go to start of third field
  121.                 assert(type != NULL);
  122.                 type[2] = 0;
  123.                   int typeResult = tagValues[MakeProp(type)];
  124.                 // check for the decimal values
  125.                 bufferPtr++;
  126.                 for (i = 3; i < 8; i++) {
  127.                     bufferPtr = strchr(bufferPtr, ';');
  128.                     assert(bufferPtr != NULL);
  129.                     bufferPtr++;
  130.                 }
  131.                 sscanf(bufferPtr, "%X", &digit);
  132.                 if (((typeResult == DECIMAL_DIGIT_NUMBER) || (typeResult == OTHER_NUMBER)) &&
  133.                     (digit >= 0 && digit <= 9)){
  134.                     buffer[10];
  135.                     sprintf(buffer, "0x%04X", unicode);
  136.                     cout << "    { " << buffer << ", " << digit << "}, \n";
  137.                 }
  138.                 bufferPtr++;
  139.                 for (i = 8; i < 12; i++) {
  140.                     bufferPtr = strchr(bufferPtr, ';');
  141.                     assert(bufferPtr != NULL);
  142.                     bufferPtr++;
  143.                 }
  144.                 sscanf(bufferPtr, "%X", &otherunicode);
  145.                 // the Unicode char has a equivalent uppercase
  146.                 if ((typeResult == LOWERCASE_LETTER) && (0 <= otherunicode && otherunicode < 65536)) { 
  147.                     set = TRUE;
  148.                 } 
  149.                 if ((typeResult == UPPERCASE_LETTER) && !set) {
  150.                     bufferPtr++;
  151.                     sscanf(bufferPtr, "%X", &otherunicode);
  152.                     if (0 <= otherunicode && otherunicode < 65536) { 
  153.                         set = TRUE;
  154.                     } 
  155.                 }
  156.                 if ((set == TRUE) && (ucmp16_get(ulxfrmArray, (UChar)unicode) == (int16_t)0xffff))
  157.                     ucmp16_set(ulxfrmArray, (UChar)unicode, (int16_t)otherunicode);
  158.                 set = FALSE;
  159.                 }
  160.  
  161.             if (input) fclose(input);
  162.             ucmp16_compact(ulxfrmArray);
  163.         }
  164.         catch (...) {
  165.             fprintf(stderr, "Error Occured while parsing unicode data file.\n");
  166.         }
  167.     }
  168.     return ulxfrmArray;
  169. }
  170.  
  171. void 
  172. writeArrays()
  173. {
  174.     const int16_t* values = ucmp16_getArray(ulxfrmArray);
  175.     const uint16_t* indexes = ucmp16_getIndex(ulxfrmArray);
  176.     int32_t i;
  177.     int32_t cnt = ucmp16_getCount(ulxfrmArray);
  178.     cout << "\nconst uint32_t Unicode::caseIndex[] = {\n    ";
  179.     for (i = 0; i < ucmp16_getkIndexCount()-1; i++)
  180.     {
  181.         cout << "(uint16_t)" << ((indexes[i] >= 0) ? (int)indexes[i] : (int)(indexes[i]+ucmp16_getkUnicodeCount()))
  182.                          << ", ";
  183.         if (i != 0)
  184.             if (i % 3 == 0)
  185.                 cout << "\n    ";
  186.     }
  187.     cout << "    (uint16_t)" << ((indexes[ucmp16_getkIndexCount()-1] >= 0) ? (int)indexes[i] : (int)(indexes[i]+ucmp16_getkUnicodeCount()))
  188.                        << " };\n";
  189.     cout << "\nconst int16_t Unicode::caseValues[] = {\n    ";
  190.     for (i = 0; i < cnt-1; i++)
  191.     {
  192.         cout << "(int16_t)" << (int16_t)values[i] << ", ";
  193.         if (i != 0)
  194.             if (i % 5 == 0)
  195.                 cout << "\n    ";
  196.     }
  197.     cout << "    (char)" << (int16_t)values[cnt-1] << " }\n";
  198.     cout << "const int32_t Unicode::caseCount = " << cnt << ";\n";
  199. }
  200. /**
  201.  * The main function builds the CharType data array and prints it to System.out
  202.  */
  203. void main(int argc, char** argv)
  204. {
  205.     CompactShortArray* arrays = 0;
  206.     FILE *input = 0;
  207.     if (argc != 2) {
  208.         printf("Usage : chartype filename\n\n");
  209.         exit(1);
  210.     }
  211.     input = fopen(argv[1], "r");
  212.     if (input == 0) {
  213.         printf("Cannot open the input file: %s\n\n", argv[1]);
  214.         exit(1);
  215.     }
  216.     arrays = getArray(input);
  217.     writeArrays();
  218. }
  219.  
  220.