Esempio n. 1
0
QString Encoder::decrypt(QString input) {

    input.remove(0,4); // Remove the n_n_ at the beginning, this is just used for recognizing types
    input.remove(input.length()-1,1); // Remove the _ at the end, this is also not part of the original string

#ifndef Q_OS_MAC
    QTextEncoder* encoder = QTextCodec::codecForName("Windows-1252")->makeEncoder(); //Encode in Windows-1252
    QTextDecoder* decoder = QTextCodec::codecForName("UTF-8")->makeDecoder();        //Decode as if it's UTF-8

    QByteArray outputData = encoder->fromUnicode(input);

    input = decoder->toUnicode(outputData,outputData.length());
#endif

    QString output;


    for(int i=input.length() -1; i>=0; i--) {
        output += (QChar)((input[i].unicode()-32)%255);
    }

    //qDebug() << output.mid(2,output.length());

    output.replace("Ý"," ");
    return output.mid(0,output.length());


}
static PyObject *meth_QTextEncoder_fromUnicode(PyObject *sipSelf, PyObject *sipArgs)
{
    PyObject *sipParseErr = NULL;

    {
        const QString* a0;
        int a0State = 0;
        QTextEncoder *sipCpp;

        if (sipParseArgs(&sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QTextEncoder, &sipCpp, sipType_QString,&a0, &a0State))
        {
            QByteArray*sipRes;

            Py_BEGIN_ALLOW_THREADS
            sipRes = new QByteArray(sipCpp->fromUnicode(*a0));
            Py_END_ALLOW_THREADS
            sipReleaseType(const_cast<QString *>(a0),sipType_QString,a0State);

            return sipConvertFromNewType(sipRes,sipType_QByteArray,NULL);
        }
    }

    /* Raise an exception if the arguments couldn't be parsed. */
    sipNoMethod(sipParseErr, sipName_QTextEncoder, sipName_fromUnicode, doc_QTextEncoder_fromUnicode);

    return NULL;
}
Esempio n. 3
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;
}
Esempio n. 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 = (*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;
}
Esempio n. 6
0
QByteArray TextView::encode(QString text)
{
    QByteArray ret;
    if (currentCodec != nullptr) { // safeguard
        QTextEncoder *encoder = currentCodec->makeEncoder(QTextCodec::ConvertInvalidToNull | QTextCodec::IgnoreHeader);

        ret = encoder->fromUnicode(text);
        if (encoder->hasFailure()) {
            logger->logError(tr("Some error(s) occured during the encoding process [%1]").arg(QString::fromUtf8(currentCodec->name())),LOGID);
            ui->codecsComboBox->setStyleSheet(GuiStyles::ComboBoxError);
        } else {
            ui->codecsComboBox->setStyleSheet(qApp->styleSheet());
        }
        delete encoder;
    }  else {
        logger->logError(tr(":encode() currentCodec is nullptr T_T"),LOGID);
    }
    return ret;
}
Esempio n. 7
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;
}
Esempio n. 8
0
void main( int argc, char * argv[] ) {
  if ( argc == 2 ) {
    QUtf7Codec * codec = new QUtf7Codec;

    QTextEncoder * enc;

    QString arg = QString::fromLatin1( argv[1] );
    int len;

    cout << "Original string:\n"
	 << "\"" << argv[1] << "\"\n" << endl;

    cout << "Encode optional direct set and whitespace:\n" << endl;
    codec->setEncodeWhitespace(TRUE);
    codec->setEncodeOptionalDirect(TRUE);
    enc = codec->makeEncoder();

    len = arg.length();
    cout << (enc->fromUnicode( arg, len )).data()
	 << "\n" << endl;

    cout << "Same as above, but call fromUnicode() char-wise:\n" << endl;

    delete enc;
    enc = codec->makeEncoder();

    for ( int i = 0 ; i < arg.length() ; i++ ) {
      len = 1;
      cout << (enc->fromUnicode( QString(arg[i]), len )).data();
    }
    cout << "\n" << endl;

    

    delete enc;

    cout << "Encode optional direct set and not whitespace:\n" << endl;
    codec->setEncodeWhitespace(FALSE);
    codec->setEncodeOptionalDirect(TRUE);
    enc = codec->makeEncoder();

    len = arg.length();
    cout << (enc->fromUnicode( arg, len )).data()
	 << "\n" << endl;

    delete enc;
    

    cout << "Don't encode optional direct set, but whitespace:\n" << endl;
    codec->setEncodeWhitespace(TRUE);
    codec->setEncodeOptionalDirect(FALSE);
    enc = codec->makeEncoder();

    len = arg.length();
    cout << (enc->fromUnicode( arg, len )).data()
	 << "\n" << endl;

    delete enc;
    

    cout << "Encode neither optional direct set, nor whitespace:\n" << endl;
    codec->setEncodeWhitespace(FALSE);
    codec->setEncodeOptionalDirect(FALSE);
    enc = codec->makeEncoder();

    len = arg.length();
    cout << (enc->fromUnicode( arg, len )).data()
	 << "\n" << endl;

    cout << "Same as above, but call fromUnicode() char-wise:\n" << endl;

    delete enc;
    enc = codec->makeEncoder();

    for ( int i = 0 ; i < arg.length() ; i++ ) {
      len = 1;
      cout << (enc->fromUnicode( QString(arg[i]), len )).data();
    }
    cout << "\n" << endl;

    
    delete enc;
    
    delete codec;
  } else {
    qWarning("usage: testutf7encoder string_to_encode\n");
  }
}
//------------------------------------------------------------------------------
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;
}
Esempio n. 10
0
QByteArray IRCServer::encode(const QString& text)
{
    QTextEncoder* encoder = codec_m->makeEncoder();
    return encoder->fromUnicode(text);
}
Esempio n. 11
0
void KviQueryWindow::ownMessage(const QString & szBuffer, bool bUserFeedback)
{
	if(!connection())
	{
		outputNoFmt(KVI_OUT_SYSTEMWARNING, __tr2qs("This query has no active targets, no message sent"));
		return;
	}

	//my full mask as seen by other users
	QString szTmpMask = connection()->userInfo()->nickName() + "!" + connection()->userInfo()->userName() + "@" + connection()->userInfo()->hostName();
	QByteArray szMyFullMask = connection()->encodeText(szTmpMask); //target name
	QByteArray szName = connection()->encodeText(windowName());    //message
	QByteArray szData = encodeText(szBuffer);
	const char * pcData = szData.data();

	/* max length of a PRIVMSG text. Max buffer length for our sszEnd is 512 byte, but we have to
	* remember that the server will prepszEnd to the message our full mask and truncate the resulting
	* at 512 bytes again..
	* So we have:
	* :[email protected] PRIVMSG #channel :text of message(CrLf)
	* [email protected]#channeltext of message
	* 512(rfc) -2(CrLf) -2(:) -3(spaces) -7(PRIVMSG) = 498
	* usable bytes, excluding our full mask and the target name.
	*/
	int iMaxMsgLen = 498 - szName.length() - szMyFullMask.length();

	// our copy of the message
	QString szTmpBuffer(szBuffer);

	if(!pcData)
		return;

#ifdef COMPILE_CRYPT_SUPPORT
	if(cryptSessionInfo())
	{
		if(cryptSessionInfo()->m_bDoEncrypt)
		{
			if(*pcData != KviControlCodes::CryptEscape)
			{
				KviCString szEncrypted;
				cryptSessionInfo()->m_pEngine->setMaxEncryptLen(500 - szName.length());
				switch(cryptSessionInfo()->m_pEngine->encrypt(pcData, szEncrypted))
				{
					case KviCryptEngine::Encrypted:
						if(!connection()->sendFmtData("PRIVMSG %s :%s", szName.data(), szEncrypted.ptr()))
							return;
						if(bUserFeedback)
							m_pConsole->outputPrivmsg(this, KVI_OUT_OWNPRIVMSGCRYPTED,
							    QString(), QString(), QString(), szBuffer, KviConsoleWindow::NoNotifications);
						break;
					case KviCryptEngine::Encoded:
					{
						if(!connection()->sendFmtData("PRIVMSG %s :%s", szName.data(), szEncrypted.ptr()))
							return;
						if(bUserFeedback)
						{
							// ugly,but we must redecode here
							QString szRedecoded = decodeText(szEncrypted.ptr());
							m_pConsole->outputPrivmsg(this, KVI_OUT_OWNPRIVMSG,
							    QString(), QString(), QString(), szRedecoded, KviConsoleWindow::NoNotifications);
						}
					}
					break;
					default: // also case KviCryptEngine::EncryptError
					{
						QString szEngineError = cryptSessionInfo()->m_pEngine->lastError();
						output(KVI_OUT_SYSTEMERROR,
						    __tr2qs("The encryption engine was unable to encrypt the current message (%Q): %s, no data sent to the server"),
						    &szBuffer, &szEngineError);
					}
					break;
				}
				userAction(connection()->currentNickName(), KVI_USERACTION_PRIVMSG);
				return;
			}
			else
			{
				//eat the escape code
				pcData++;
				szTmpBuffer.remove(0, 1);
				//let the normal function do it
			}
		}
	}
#endif

	if(szData.length() > iMaxMsgLen)
	{
		/* Multi message; we want to split the message, preferably on a word boundary,
		 * and sszEnd each message part as a different PRIVMSG
		 * Due to encoding stuff, this is frikin'time eater
		 */
		QTextEncoder * pEncoder = makeEncoder(); // our temp encoder
		QByteArray szTmp;                        // used to calculate the length of an encoded message
		int iPos;                                // contains the index where to truncate szTmpBuffer
		int iC;                                  // cycles counter (debugging/profiling purpose)
		float fPosDiff;                          // optimization internal; aggressivity factor
		QString szCurSubString;                  // truncated parts as reported to the user

		// run until we've something remaining in the message
		while(szTmpBuffer.length())
		{
			// init counters
			iPos = szTmpBuffer.length();
			iC = 0;

			// first part (optimization): quickly find an high index that is _surely_lesser_
			// than the correct one
			while(1)
			{
				iC++;
				szTmp = pEncoder->fromUnicode(szTmpBuffer.left(iPos));

				if(szTmp.length() <= iMaxMsgLen)
					break;

				//if szTmp.length() == 0 we already have break'ed out from here,
				// so we can safely use it as a divisor
				fPosDiff = (float)iMaxMsgLen / (float)szTmp.length();
				iPos = (int)(iPos * fPosDiff); // cut the string at each cycle
				                               //printf("OPTIMIZATION: fPosDiff %f, iPos %d\n", fPosDiff, iPos);
			}
			//printf("Multi message: %d optimization cyles", iC);

			// now, do it the simple way: increment our index until we perfectly fit into the
			// available space
			while(1)
			{
				iC++;

				szTmp = pEncoder->fromUnicode(szTmpBuffer.left(iPos));

				// perfect match
				if(iPos == szTmpBuffer.length())
					break;

				if(szTmp.length() > iMaxMsgLen)
				{
					// overflowed.. last one was the good one
					iPos--;
					szTmp = pEncoder->fromUnicode(szTmpBuffer.left(iPos));
					break;
				}
				else
				{
					//there's still free space.. add another char
					iPos++;
				}
			}
			//printf(", finished at %d cycles, truncated at pos %d\n", iC, iPos);

			//prepare the feedback string for the user
			szCurSubString = szTmpBuffer.left(iPos);

			//sszEnd the string to the server
			if(connection()->sendFmtData("PRIVMSG %s :%s", szName.data(), szTmp.data()))
			{
				//feeedback the user
				if(bUserFeedback)
					m_pConsole->outputPrivmsg(this, KVI_OUT_OWNPRIVMSG, QString(), QString(), QString(), szCurSubString, KviConsoleWindow::NoNotifications);
				userAction(connection()->currentNickName(), KVI_USERACTION_PRIVMSG);
			}
			else
			{
				// skipped a part in this multi message.. we don't want to continue
				return;
			}

			// remove the sent part of the string
			szTmpBuffer.remove(0, iPos);
			//printf("Sent %d chars, %d remaining in the Qstring\n",iPos, szTmpBuffer.length());
		}
	}
	else
	{
		if(connection()->sendFmtData("PRIVMSG %s :%s", szName.data(), pcData))
		{
			if(bUserFeedback)
				m_pConsole->outputPrivmsg(this, KVI_OUT_OWNPRIVMSG, QString(), QString(), QString(), szTmpBuffer, KviConsoleWindow::NoNotifications);
			userAction(connection()->currentNickName(), KVI_USERACTION_PRIVMSG);
		}
	}
}