예제 #1
0
int main(int argc, char **argv)
{
    qputenv("LC_ALL", "C");
    QCoreApplication app(argc, argv);

    QString string(QChar(0x410));
    QTextCodec *locale = QTextCodec::codecForLocale();
    QTextEncoder *encoder = locale->makeEncoder();
    QByteArray output = encoder->fromUnicode(string);
    printf("%s\n", output.data());

    return 0;
}
예제 #2
0
bool ICQClient::translate(const char *to, const char *from, string &str)
{
    if (*str.c_str() == 0)
        return true;
    if (!strcasecmp(from, to))
        return true;
    QTextCodec *fromCodec = (*from) ? codecForName(from) : QTextCodec::codecForLocale();
    QTextCodec *toCodec = (*to) ? codecForName(to) : QTextCodec::codecForLocale();
    if ((fromCodec == NULL) && (toCodec == NULL) &&
            strcasecmp(from, "UCS-2") &&
            strcasecmp(from, "UTF-8") && strcasecmp(to, "UTF-8")){
        if ((*from && strcmp(from, "ascii")) || (*to && strcmp(to, "ascii")))
            log(L_WARN, "Codec for %s -> %s not found", from, to);
        return true;
    }
    QString s;
    if (fromCodec == NULL){
        if (strcasecmp(from, "utf-8") == 0){
            s = QString::fromUtf8(str.c_str());
        }else if (strcasecmp(from, "ucs-2") == 0){
            for (unsigned i = 0; i < str.length() / 2; i += 2)
                s += QChar(*((uint*)(str.c_str() + i)));
        }else{
            if (*from && strcmp(from, "ascii"))
                log(L_WARN, "Codec for %s not found", from);
            s = QString::fromLocal8Bit(str.c_str());
        }
    }else{
        QTextDecoder *decoder = fromCodec->makeDecoder();
        s = decoder->toUnicode(str.c_str(), strlen(str.c_str()));
    }
    if (s.length() == 0){
        s = "";
        return true;
    }
    if (toCodec == NULL){
        if (strcasecmp(to, "utf-8")){
            if (*to && strcmp(to, "ascii"))
                log(L_WARN, "Codec for %s not found", to);
            str = s.local8Bit();
        }else{
            str = s.utf8();
        }
    }else{
        QTextEncoder *encoder = toCodec->makeEncoder();
        int size = s.length();
        str = encoder->fromUnicode(s, size);
    }
    return true;
}
//------------------------------------------------------------------------------
bool MainWindow::CheckMeCabHome(QSettings *ini_settings)
{
    bool stat = true;
    QString meCabStdDir, meCabStdDir_x86;

    QTextCodec *codec = QTextCodec::codecForName("Shift-JIS");
    QTextEncoder *encoder = codec->makeEncoder();

    QDir dir(meCabHome);
    if(! dir.exists()){
        meCabStdDir_x86 = "c:/Program Files (x86)/MeCab/bin/";
        QDir dir_x86(meCabStdDir_x86);
        meCabStdDir = "c:/Program Files/MeCab/bin/";
        QDir dir(meCabStdDir);
        if(dir_x86.exists()){
            meCabHome = meCabStdDir_x86;
        }
        else if(dir.exists()){
            meCabHome = meCabStdDir;
        }
        else{
            QMessageBox::StandardButton reply;
            reply = QMessageBox::critical(this, tr("ERROR"),
                tr("MeCab is not installed in the folder below.") + "\n" +
                " " + meCabStdDir + "\n" +
                " " + tr("nor") + "\n" +
                " " + meCabStdDir_x86 + "\n\n" +
                tr("Install MeCab.") + "\n" +
                tr("If MeCab is installed in another folder, Change ini file.") + "\n" +
                " " + tr("ini file name") + ": " + programName + ".ini\n" +
                " " + tr("section") + ": [MeCab]\n" +
                " " + tr("name") + ": MeCab_HOME\n" +
                " " + tr("ex.") + " MeCab_HOME=C:/Program Files (x86)/MeCab/bin/\n"
                ,QMessageBox::Abort);
            meCabHome = "";
        }
        if(meCabHome != ""){
            ini_settings->setValue("MeCab/MeCab_HOME", QString(encoder->fromUnicode(meCabHome)));
        }
        else{
            stat = false;
        }
    }
    return stat;
}
예제 #4
0
bool ICQClient::translate(const char *to, const char *from, string &str)
{
    if (*str.c_str() == 0)
        return true;
    if (!strcasecmp(from, to))
        return true;
    QTextCodec *fromCodec = codecForName(from);
    QTextCodec *toCodec = codecForName(to);
    if ((fromCodec == NULL) && (toCodec == NULL)){
        if ((*from && strcmp(from, "ascii")) || (*to && strcmp(to, "ascii")))
            log(L_WARN, "Codec for %s -> %s not found", from, to);
        return true;
    }
    QString s;
    if (fromCodec == NULL){
        if (*from && strcmp(from, "ascii"))
            log(L_WARN, "Codec for %s not found", from);
        s = QString::fromLocal8Bit(str.c_str());
    }else{
        QTextDecoder *decoder = fromCodec->makeDecoder();
        s = decoder->toUnicode(str.c_str(), strlen(str.c_str()));
    }
    if (s.length() == 0){
        s = "";
        return true;
    }
    if (toCodec == NULL){
        if (*to && strcmp(to, "ascii"))
            log(L_WARN, "Codec for %s not found", to);
        str = s.local8Bit();
    }else{
        QTextEncoder *encoder = toCodec->makeEncoder();
        int size = s.length();
        str = encoder->fromUnicode(s, size);
    }
    return true;
}
//------------------------------------------------------------------------------
bool MainWindow::editOutputFile(QString inputPath, QString meCabOutputPath, QString outputPath, QString maskColumnNo)
{
    bool stat = true;
    QString errmsg = "";
    int col = 0;
    QTextCodec *codec = QTextCodec::codecForName("Shift-JIS");
    QTextDecoder *decoder = codec->makeDecoder();
    QTextEncoder *encoder = codec->makeEncoder();
    
    QFile wfile(outputPath);
    QFile rfileMeCab(meCabOutputPath);
    QFile rfile(inputPath);
    if (wfile.open(QIODevice::WriteOnly | QIODevice::Text)){
        if (rfileMeCab.open(QIODevice::ReadOnly | QIODevice::Text)){
            if(maskColumnNo != ""){
                if (rfile.open(QIODevice::ReadOnly | QIODevice::Text)){
                    bool ok;
                    col = maskColumnNo.toInt(&ok, 10);
                    if(! ok){
                        errmsg = tr("Illegal Mask Column No.") + "(" + maskColumnNo + ")";
                        rfile.close();
                        stat = false;
                    }
                }
                else{
                    // Can't open input file.
                    errmsg = tr("Can't open the intput file.") + "(" + inputPath + ")";
                    stat = false;
                }
            }
            
            if(stat){
                QString outline = "";
                while (!rfileMeCab.atEnd()) {
                    QByteArray line = rfileMeCab.readLine();
                    //QString str = decoder->toUnicode(line);
                    QString str = line;
                    if(str == "EOS\n"){
                        if(maskColumnNo != ""){
                            QByteArray wline = rfile.readLine();
                            str = wline;
                            QStringList wlist = str.split("\t", QString::KeepEmptyParts, Qt::CaseSensitive);
                            str = outline;
                            outline = "";
                            for(int ii = 0; ii < wlist.count(); ii++){
                                if(outline != ""){
                                    outline += "\t";
                                }
                                if(ii == col){
                                    outline += str;
                                }
                                else{
                                    outline += wlist[ii];
                                }
                            }
                        }
                        wfile.write((outline + "\n").toAscii());
                        outline = "";
                    }
                    else{
                        QStringList wlist = str.split("\t", QString::KeepEmptyParts, Qt::CaseSensitive);
                        if(wlist[1].left(13) == "����,�ŗL����"){
                            QString unicodeStr = decoder->toUnicode(wlist[0].toAscii());
                            QString maskStr = unicodeStr.replace(QRegExp("."), decoder->toUnicode("��"));
                            outline += encoder->fromUnicode(maskStr);
                        }
                        else{
                            outline += wlist[0];
                        }
                    }
                }
                rfileMeCab.close();
                if(maskColumnNo != ""){
                    rfile.close();
                }
            }
        }
        else{
            // Can't open MeCab output file.
            errmsg = tr("Can't open the MeCab output file.") + "(" + meCabOutputPath + ")";
            stat = false;
        }
        
        wfile.close();
    }
    else{
        // Can't open the output file.
        errmsg = tr("Can't open the output file.") + "(" + outputPath + ")";
        stat = false;
    }
    
    if(! stat){
        QMessageBox::StandardButton reply;
        reply = QMessageBox::critical(this, tr("ERROR"),
            errmsg,
            QMessageBox::Abort);
        if (reply == QMessageBox::Abort){
            close();
        }
    }
    
    return stat;
}