Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
psemiletov committed Apr 11, 2021
1 parent 155c308 commit fa420c9
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 6 deletions.
4 changes: 2 additions & 2 deletions document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down
9 changes: 8 additions & 1 deletion tea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down Expand Up @@ -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"));
Expand Down
130 changes: 127 additions & 3 deletions tio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ DJVU read code taken fromdvutxt.c:

#include <iostream>

//#include <iconv.h>

#include <QFile>
#include <QXmlStreamReader>
#include <QDataStream>
Expand Down Expand Up @@ -174,16 +176,16 @@ 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");
else
if (eol == "\r")
data.replace (eol, "\n");

file.close();
file.close();

return true;
}
Expand Down Expand Up @@ -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:" <<e;
}
//if (e != -1)
//r = QString::fromLatin1 (outbuf, len * 2);
// r = QString::fromUtf8 (outbuf, total - outbytesleft);
qDebug() << "inbytesleft: " << inbytesleft;
qDebug() << "outbytesleft: " << outbytesleft;
//QByteArray ba = QByteArray::fromRawData (outbuf, len * 2);
//[static]QString QString::fromUtf8(const char * str, int size = -1)
iconv_close (cd);
//delete [] outbuf;
return QString (ba);
}
QStringList charsets_for_locale (const QString &loc)
{
QStringList l;
return l;
}
*/
/*
char* CIconvCharsetConverter::from_utf_16 (const QString &enc_to, const QString &data)
{
char *outbuf = new char [len * 2];
iconv_t cd = iconv_open ("UTF-16", enc_to.toLatin1().data());
size_t inbytesleft = len;
size_t outbytesleft = len * 2;
size_t e = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
if (e != -1)
r = QString::fromLatin1 (outbuf, len * 2);
//QByteArray ba = QByteArray::fromRawData (outbuf, len * 2);
//[static]QString QString::fromUtf8(const char * str, int size = -1)
iconv_close (cd);
return outbuf;
}
*/
37 changes: 37 additions & 0 deletions tio.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,41 @@ class CTioHandler: public QObject
QStringList get_supported_exts();
};


/*
class CCharsetConverter
{
Q_OBJECT
public:
static virtual QStringList get_charsets() = 0;
virtual ~CCharsetConverter() {};
};
*/
/*
class CIconvCharsetConverter: public QObject
{
Q_OBJECT
public:
QStringList charsets;
QStringList charsets_for_locale (const QString &loc);
void load_charsets();
QString to_utf16 (const QString &enc_from, char *inbuf, int len);
// char* from_utf_16 (const QString &enc_to, const QString &data);
};
*/





#endif // TIO_H

0 comments on commit fa420c9

Please sign in to comment.