util/unicode/codecs/big5/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the utils of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <qtextcodec.h>
       
    42 #include <qbytearray.h>
       
    43 #include <qstring.h>
       
    44 #include <qdebug.h>
       
    45 #include <qfile.h>
       
    46 #include <qcoreapplication.h>
       
    47 #include <qset.h>
       
    48 
       
    49 struct Map { Map(uint u,  uint b) : uc(u),  b5(b) {} uint uc; uint b5; };
       
    50 
       
    51 int main(int argc, char **argv)
       
    52 {
       
    53     QCoreApplication app(argc, argv);
       
    54     QTextCodec *big5 = QTextCodec::codecForName("Big5-hkscs");
       
    55 
       
    56 #if 0
       
    57     QFile f("/home/lars/dev/qt-4.0/util/unicode/data/big5-eten.txt");
       
    58     f.open(QFile::ReadOnly);
       
    59 
       
    60     while (!f.atEnd()) {
       
    61         QByteArray line = f.readLine();
       
    62         if (line.startsWith("#"))
       
    63             continue;
       
    64         line.replace("0x", "");
       
    65         line.replace("U+", "");
       
    66         line.replace("\t", " ");
       
    67         line = line.simplified();
       
    68         QList<QByteArray> split = line.split(' ');
       
    69         bool ok;
       
    70         int b5 = split.at(0).toInt(&ok, 16);
       
    71         Q_ASSERT(ok);
       
    72         int uc = split.at(1).toInt(&ok, 16);
       
    73         Q_ASSERT(ok);
       
    74         if (b5 < 0x100)
       
    75             continue;
       
    76 #else
       
    77     QFile f(":/BIG5");
       
    78     f.open(QFile::ReadOnly);
       
    79 
       
    80     while (!f.atEnd()) {
       
    81         QByteArray line = f.readLine();
       
    82         if (line.startsWith("CHARMAP"))
       
    83             break;
       
    84     }
       
    85     QSet<uint> b5_ok;
       
    86     QSet<uint> uc_ok;
       
    87     QList<Map> b5_to_uc_map;
       
    88     QList<Map> uc_to_b5_map;
       
    89     while (!f.atEnd()) {
       
    90         QByteArray line = f.readLine();
       
    91         if (line.startsWith("%"))
       
    92             continue;
       
    93         if (line.startsWith("END CHARMAP"))
       
    94             break;
       
    95         line.replace("/x", "");
       
    96         line.replace("<U", "");
       
    97         line.replace(">", "");
       
    98         line.replace("\t", " ");
       
    99         line = line.simplified();
       
   100         QList<QByteArray> split = line.split(' ');
       
   101         bool ok;
       
   102         int b5 = split.at(1).toInt(&ok, 16);
       
   103         Q_ASSERT(ok);
       
   104         int uc = split.at(0).toInt(&ok, 16);
       
   105         Q_ASSERT(ok);
       
   106         if (b5 < 0x100 || uc > 0xffff)
       
   107             continue;
       
   108 #endif
       
   109 
       
   110 //         qDebug() << hex << "testing: '" << b5 << "' - '" << uc << "'";
       
   111         QByteArray ba;
       
   112 
       
   113         ba += (char)(uchar)(b5 >> 8);
       
   114         ba += (char)(uchar)(b5 & 0xff);
       
   115 
       
   116         QString s = big5->toUnicode(ba);
       
   117         Q_ASSERT(s.length() == 1);
       
   118         QString s2;
       
   119         s2 = QChar(uc);
       
   120         ba = big5->fromUnicode(s2);
       
   121         Q_ASSERT(ba.length() <= 2);
       
   122         int round;
       
   123         if (ba.length() == 1)
       
   124             round = (int)(uchar)ba[0];
       
   125         else
       
   126             round = ((int)(uchar)ba[0] << 8) + (int)(uchar)ba[1];
       
   127         if (b5 != round)
       
   128             uc_to_b5_map += Map(uc, b5);
       
   129         else
       
   130             b5_ok.insert(b5);
       
   131 
       
   132         if (s[0].unicode() != uc)
       
   133             b5_to_uc_map += Map(uc, b5);
       
   134         else
       
   135             uc_ok.insert(uc);
       
   136     };
       
   137 
       
   138     QList<QByteArray> list;
       
   139     foreach(Map m, b5_to_uc_map) {
       
   140         if (!uc_ok.contains(m.b5))
       
   141             list += QByteArray("    { 0x" + QByteArray::number(m.b5, 16) + ", 0x" + QByteArray::number(m.uc, 16) + " }\n");;
       
   142     }
       
   143     QByteArray ba;
       
   144     qSort(list);
       
   145     foreach(QByteArray a, list)
       
   146         ba += a;
       
   147     qDebug() << "struct B5Map b5_to_uc_map = {\n" << ba + "\n};";
       
   148 
       
   149     list = QList<QByteArray>();
       
   150     foreach(Map m, uc_to_b5_map)
       
   151         if (!b5_ok.contains(m.uc))
       
   152             list += QByteArray("    { 0x" + QByteArray::number(m.uc, 16) + ", 0x" + QByteArray::number(m.b5, 16) + " }\n");;
       
   153     ba = QByteArray();
       
   154     qSort(list);
       
   155     foreach(QByteArray a, list)
       
   156         ba += a;
       
   157     qDebug() << "struct B5Map uc_to_b5_map = {\n" << ba + "\n};";
       
   158 }