| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include <DTextEncoding> |
| 6 |
|
| 7 |
#include <QCoreApplication> |
| 8 |
#include <QCommandLineOption> |
| 9 |
#include <QCommandLineParser> |
| 10 |
#include <QDebug> |
| 11 |
|
| 12 |
DCORE_USE_NAMESPACE |
| 13 |
|
| 14 |
void convertFileEncoding(const QString &fromFile, |
| 15 |
const QString &toFile, |
| 16 |
const QByteArray &toEncoding, |
| 17 |
const QByteArray &fromEncoding = QByteArray()) |
| 18 |
{ |
| 19 |
QByteArray contentEncoding = fromEncoding; |
| 20 |
if (contentEncoding.isEmpty()) { |
| 21 |
bool isOk = false; |
| 22 |
contentEncoding = DTextEncoding::detectFileEncoding(fromFile, &isOk); |
| 23 |
if (!isOk) { |
| 24 |
qInfo().noquote() << QString("Detect file %1 encoding failed").arg(fromFile); |
| 25 |
return; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
QString errString; |
| 30 |
if (!DTextEncoding::convertFileEncodingTo(fromFile, toFile, toEncoding, contentEncoding, &errString)) { |
| 31 |
qInfo().noquote() << QString("Convert file %1 encoding from %2 to %3 failed. error: %4") |
| 32 |
.arg(fromFile) |
| 33 |
.arg(QString::fromUtf8(contentEncoding)) |
| 34 |
.arg(QString::fromUtf8(toEncoding)) |
| 35 |
.arg(errString); |
| 36 |
} else { |
| 37 |
qInfo().noquote() << QString("Convert file %1 encoding from %2 to %3 successed.") |
| 38 |
.arg(fromFile) |
| 39 |
.arg(QString::fromUtf8(contentEncoding)) |
| 40 |
.arg(QString::fromUtf8(toEncoding)); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
int main(int argc, char *argv[]) |
| 45 |
{ |
| 46 |
QCoreApplication app(argc, argv); |
| 47 |
QCoreApplication::setApplicationName("Text codec"); |
| 48 |
|
| 49 |
QCommandLineOption toEncodingOption({"t", "toEncoding"}, "Convert file encoding to specified encoding.", "encoding"); |
| 50 |
QCommandLineOption fromEncodingOption({"f", "fromEncoding"}, "Convert file encoding from specified encoding.", "encoding"); |
| 51 |
QCommandLineOption outputOption( |
| 52 |
{"o", "output"}, "Save converted text with file path, only supported when opening a single file.", "path"); |
| 53 |
|
| 54 |
QCommandLineParser parser; |
| 55 |
parser.setApplicationDescription("Text codec, provide encoding detection and encoding conversion."); |
| 56 |
parser.addHelpOption(); |
| 57 |
parser.addOption(toEncodingOption); |
| 58 |
parser.addOption(fromEncodingOption); |
| 59 |
parser.addOption(outputOption); |
| 60 |
parser.addPositionalArgument("file", "Open file.", "[file...]"); |
| 61 |
parser.process(app); |
| 62 |
|
| 63 |
const QStringList args = parser.positionalArguments(); |
| 64 |
if (args.isEmpty()) { |
| 65 |
parser.showHelp(); |
| 66 |
} |
| 67 |
|
| 68 |
const QStringList fileArgs = parser.positionalArguments(); |
| 69 |
if (fileArgs.isEmpty()) { |
| 70 |
qInfo().noquote() << "Not set open file."; |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
if (parser.isSet(outputOption)) { |
| 75 |
if (fileArgs.size() > 1) { |
| 76 |
qInfo().noquote() << "Output file path only supported when opening a single file."; |
| 77 |
return 0; |
| 78 |
} else if (!parser.isSet(toEncodingOption)) { |
| 79 |
qInfo().noquote() << "Convert file with not set convert encoding."; |
| 80 |
} else { |
| 81 |
QString fromFile = fileArgs.first(); |
| 82 |
QString toFile = parser.value(outputOption); |
| 83 |
QByteArray toEncoding = parser.value(toEncodingOption).toUtf8(); |
| 84 |
|
| 85 |
convertFileEncoding(fromFile, toFile, toEncoding, parser.value(fromEncodingOption).toUtf8()); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
QByteArray toEncoding = parser.value(toEncodingOption).toUtf8(); |
| 91 |
for (QString fileName : fileArgs) { |
| 92 |
if (toEncoding.isEmpty()) { |
| 93 |
// Only display file encoding. |
| 94 |
qInfo().noquote() << fileName << DTextEncoding::detectFileEncoding(fileName); |
| 95 |
} else { |
| 96 |
// Convert file encoding. |
| 97 |
convertFileEncoding(fileName, fileName, toEncoding, parser.value(fromEncodingOption).toUtf8()); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |