From fa420c93e71cd64bcaf2124f2b522fbe4f820e99 Mon Sep 17 00:00:00 2001 From: Petr Semiletov Date: Sun, 11 Apr 2021 21:46:42 +0300 Subject: [PATCH] --- document.cpp | 4 +- tea.cpp | 9 +++- tio.cpp | 130 +++++++++++++++++++++++++++++++++++++++++++++++++-- tio.h | 37 +++++++++++++++ 4 files changed, 174 insertions(+), 6 deletions(-) diff --git a/document.cpp b/document.cpp index 3dc06d6..da69c28 100644 --- a/document.cpp +++ b/document.cpp @@ -224,7 +224,7 @@ void CSyntaxHighlighterQRegExp::load_from_xml (const QString &fname) QString color = hash_get_val (global_palette, xml.attributes().value ("color").toString(), "gray"); QTextCharFormat fmt = tformat_from_style (xml.attributes().value ("color").toString(), color, darker_val); - multiLineCommentFormat = fmt; + fmt_multi_line_comment = fmt; QString element = xml.readElementText().trimmed().remove('\n'); if (! element.isEmpty()) commentStartExpression = QRegExp (element, cs, QRegExp::RegExp); @@ -321,7 +321,7 @@ void CSyntaxHighlighterQRegExp::highlightBlock (const QString &text) else commentLength = endIndex - startIndex + commentEndExpression.matchedLength(); - setFormat (startIndex, commentLength, multiLineCommentFormat); + setFormat (startIndex, commentLength, fmt_multi_line_comment); startIndex = text.indexOf (commentStartExpression, startIndex + commentLength); } } diff --git a/tea.cpp b/tea.cpp index 9f1f61e..da023ca 100644 --- a/tea.cpp +++ b/tea.cpp @@ -1038,6 +1038,13 @@ File menu callbacks void CTEA::test() { + /* CIconvCharsetConverter c; + QByteArray ba = file_load ("/home/rox/devel/test/1251.txt"); + + QString s = c.to_utf16 ("CP1251", ba.data(), ba.size()); + qDebug() << s;*/ + + QIconvCodec c; } @@ -6586,7 +6593,7 @@ File menu menu_file = menuBar()->addMenu (tr ("File")); menu_file->setTearOffEnabled (true); - // menu_file->addAction (act_test); + menu_file->addAction (act_test); menu_file->addAction (newAct); add_to_menu (menu_file, tr ("Open"), SLOT(file_open()), "Ctrl+O", get_theme_icon_fname ("file-open.png")); diff --git a/tio.cpp b/tio.cpp index e1031f1..18d0620 100644 --- a/tio.cpp +++ b/tio.cpp @@ -46,6 +46,8 @@ DJVU read code taken fromdvutxt.c: #include +//#include + #include #include #include @@ -174,8 +176,8 @@ bool CTioPlainText::load (const QString &fname) // in.setCodec (charset.toUtf8().data()); QByteArray ba = file.readAll(); - QTextCodec *codec = QTextCodec::codecForName(charset.toUtf8().data()); - data = codec->toUnicode(ba); + QTextCodec *codec = QTextCodec::codecForName (charset.toUtf8().data()); + data = codec->toUnicode (ba); if (eol == "\r\n") data.replace (eol, "\n"); @@ -183,7 +185,7 @@ bool CTioPlainText::load (const QString &fname) if (eol == "\r") data.replace (eol, "\n"); - file.close(); + file.close(); return true; } @@ -1066,3 +1068,125 @@ QStringList CTioHandler::get_supported_exts() l.append ("txt"); return l; } + + +QString cp1251_to_utf16 (char *inbuf, int len) +{ + QString r; + char *outbuf = new char [len * 2]; + + iconv_t cd = iconv_open ("UTF-16","CP1251"); + size_t inbytesleft; + size_t outbytesleft; + + + size_t s = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); + + QByteArray ba = QByteArray::fromRawData (outbuf, len * 2); + + delete [] outbuf; + + iconv_close (cd); + return r; +} + + + +void CIconvCharsetConverter::load_charsets() +{ + charsets.prepend ("UTF-8"); + charsets.prepend ("UTF-16"); + charsets.prepend ("CP1251"); + charsets.prepend ("KOI8-R"); + charsets.prepend ("KOI8-U"); + charsets.prepend ("DOS866"); + +} + +/* + + https://code.woboq.org/qt5/qtbase/src/corelib/codecs/qiconvcodec.cpp.html + + QByteArray ba(outBytesLeft, Qt::Uninitialized); + char *outBytes = ba.data(); + + */ +/* +QString CIconvCharsetConverter::to_utf16 (const QString &enc_from, char *inbuf, int len) +{ + QString r; + //char *outbuf = new char [len * 2 + 2]; //last 2 is bom + const char *inBytes = inbuf; + + iconv_t cd = iconv_open ("UTF-8", enc_from.toLatin1().data()); + size_t inbytesleft = len; + size_t outbytesleft = len * 4 + 2; + + QByteArray ba (outbytesleft, Qt::Uninitialized); + char *outbuf = ba.data(); + + size_t total = outbytesleft; + + qDebug() << "inbytesleft: " << inbytesleft; + qDebug() << "outbytesleft: " << outbytesleft; + + while (inbytesleft > 0) + { + size_t e = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); + +// qDebug() << "e:" <