Skip to content

Commit

Permalink
finish cli mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwillow committed Oct 31, 2023
1 parent 82ecca1 commit 7203e45
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 20 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

功能:

* 批量转UTF-8/UTF-8-sig/GB18030等
* 批量转UTF-8/UTF-8 BOM/GB18030等
* 批量转CRLF/LF/CR换行符
* 转换时会检查是否丢失字符,确保转换过程可逆
* 支持命令行(使用 $ ./SmartCharsetConverter --help 查看)

运行要求:

Expand Down Expand Up @@ -48,6 +50,8 @@ v0.61 现在选择“不过滤”模式可以强行加入文件。并且,在

v0.62 现在支持拖拽文件/文件夹到程序图标上了。

v0.7 支持命令行。使用 $ ./SmartCharsetConverter --help 查看命令行参数。

# TODO

* 转换前再次检查一次字符集,已免出现加载后用户更改了字符集后转换出错的情况。
Expand Down
84 changes: 66 additions & 18 deletions src/CLIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Core.h"
#include "tstring.h"
#include "FileFunction.h"
#include "ConsoleSettings.h"

#include <sstream>
#include <iostream>
Expand Down Expand Up @@ -160,27 +161,27 @@ int CLIMain(const std::vector<std::wstring> &args) noexcept {
try {
core.SetOutputCharset(ToCharsetCode(arg));
} catch (const std::runtime_error &err) {
ssErr << L"错误:未能识别的字符集名称:" << arg << "\n";
ssErr << L"错误:未能识别的字符集名称:" << arg << L"\n";
ssErr << L"提示:使用--help charset可以查看支持的字符集名称。\n";
}
state = 0;
break;
case 40:
setTargetLineBreak = true;
core.SetEnableConvertLineBreak(true);
if (tolower(arg) == tolower(std::wstring(L"LF")) && tolower(arg) == tolower(std::wstring(L"Linux"))) {
if (tolower(arg) == tolower(std::wstring(L"LF")) || tolower(arg) == tolower(std::wstring(L"Linux"))) {
core.SetLineBreaks(Configuration::LineBreaks::LF);
break;
}
if (tolower(arg) == tolower(std::wstring(L"CRLF")) && tolower(arg) == tolower(std::wstring(L"Windows"))) {
if (tolower(arg) == tolower(std::wstring(L"CRLF")) || tolower(arg) == tolower(std::wstring(L"Windows"))) {
core.SetLineBreaks(Configuration::LineBreaks::CRLF);
break;
}
if (tolower(arg) == tolower(std::wstring(L"CR")) && tolower(arg) == tolower(std::wstring(L"Mac"))) {
if (tolower(arg) == tolower(std::wstring(L"CR")) || tolower(arg) == tolower(std::wstring(L"Mac"))) {
core.SetLineBreaks(Configuration::LineBreaks::CR);
break;
}
ssErr << L"错误:未能识别的换行符名称:" << arg << "\n";
ssErr << L"错误:未能识别的换行符名称:" << arg << L"\n";
ssErr << L"提示:使用--help可以查看换行符名称。\n";
state = 0;
break;
Expand All @@ -203,64 +204,103 @@ int CLIMain(const std::vector<std::wstring> &args) noexcept {
ssErr << L"错误:没有设置输出方式(--output_origin或者--output_dir)。" << L"\n";
} else {
if (core.GetConfig().outputTarget == Configuration::OutputTarget::ORIGIN) {
wcout << L"输出方式:原位输出\n";
ssOutput << L"输出方式:原位输出\n";
} else {
wcout << L"输出方式:输出到文件夹:" << core.GetConfig().outputDir << L"\n";
ssOutput << L"输出方式:输出到文件夹:" << core.GetConfig().outputDir << L"\n";
}
}

if (!setTargetCharset) {
ssErr << L"错误:没有设置目标字符集(--targetCharset)。" << L"\n";
} else {
wcout << L"目标字符集:" << ToViewCharsetName(core.GetConfig().outputCharset) << L"\n";
ssOutput << L"目标字符集:" << ToViewCharsetName(core.GetConfig().outputCharset) << L"\n";
}

if (setTargetLineBreak) {
wcout << L"目标换行符:" << lineBreaksMap.at(core.GetConfig().lineBreak) << L"\n";
ssOutput << L"目标换行符:" << lineBreaksMap.at(core.GetConfig().lineBreak) << L"\n";
}
wcerr << L"\n";
}

// 开始输出
wcout << std::wstring(32, L'=') << L"\n";

int retCode = 0;
if (!ssErr.str().empty()) {
SetConsoleColor(ConsoleColor::RED);
std::wcerr << L"输入参数:" << L"\n";
for (auto arg : args) {
std::wcerr << arg << L" ";
}
std::wcerr << L"\n\n";
std::wcerr << ssErr.str() << L"\n";
retCode = -1;
SetConsoleColor();
return -1;
}

std::wcout << ssOutput.str() << L"\n";
SetConsoleColor(ConsoleColor::GREEN);
std::wcout << ssOutput.str();
SetConsoleColor();

wcout << std::wstring(32, L'=') << L"\n";
wcout << L"\n";

ssErr.swap(std::wstringstream{});
ssOutput.swap(std::wstringstream{});

// 开始转换

auto AddAndConvertOneFile = [&core](int index, int total, const std::wstring &inputFilename, int &success,
int &failed) {
auto AddAndConvertOneFile = [&core, &ssOutput, setTargetLineBreak](int index, int total,
const std::wstring &inputFilename, int &success,
int &failed) {
Core::AddItemResult addedItem;
SetConsoleColor(ConsoleColor::YELLOW);
std::wcout << L"[" << to_wstring(std::to_string(index)) << L"/" << to_wstring(std::to_string(total)) << L"] "
<< inputFilename << L"\n";
SetConsoleColor();
try {
addedItem = core.AddItem(inputFilename, {});
std::wcout << L" 原大小: " << FileSizeToTString(addedItem.filesize) << L"\n";
std::wcout << L" 原字符集: " << ToViewCharsetName(addedItem.srcCharset) << L"\n";
std::wcout << L" 原换行符: " << lineBreaksMap.at(addedItem.srcLineBreak) << L"\n";
// std::wcout << L" 文本片段: " << addedItem.strPiece << L"\n";
} catch (const std::runtime_error &err) {
SetConsoleColor(ConsoleColor::RED);
wcerr << L"读入文件失败。原因: " << to_wstring(err.what()) << L"\n";
wcerr << L"\n";
SetConsoleColor();
failed++;
return;
}

Core::ConvertResult ret = core.Convert(inputFilename, addedItem.srcCharset, addedItem.srcLineBreak);
if (ret.errInfo.has_value()) {
wcout << L" 大小: " << FileSizeToTString(addedItem.filesize) << L"\n";
wcout << L" 字符集: " << ToViewCharsetName(addedItem.srcCharset) << L"\n";
wcout << L" 换行符: " << lineBreaksMap.at(addedItem.srcLineBreak) << L"\n";
SetConsoleColor(ConsoleColor::RED);
wcerr << L"转换失败。原因: " << ret.errInfo.value() << L"\n";
wcerr << L"\n";
SetConsoleColor();
failed++;
return;
}

wcout << L" 大小: " << FileSizeToTString(addedItem.filesize) << L"\n";
wcout << L" 字符集: " << ToViewCharsetName(addedItem.srcCharset) << L" -> ";

SetConsoleColor(ConsoleColor::GREEN);
wcout << ToViewCharsetName(core.GetConfig().outputCharset) << L"\n";
SetConsoleColor();

wcout << L" 换行符: " << lineBreaksMap.at(addedItem.srcLineBreak);

if (setTargetLineBreak) {
wcout << L" -> ";
SetConsoleColor(ConsoleColor::GREEN);
wcout << lineBreaksMap.at(core.GetConfig().lineBreak) << L"\n";
SetConsoleColor();
} else {
wcout << L"\n";
}

wcout << L"转换成功。\n\n";
success++;
return;
Expand All @@ -274,8 +314,10 @@ int CLIMain(const std::vector<std::wstring> &args) noexcept {
}

for (auto &path : std::filesystem::recursive_directory_iterator(inputPath)) {
inputFileNames.push_back(path.path());
continue;
if (std::filesystem::is_regular_file(path)) {
inputFileNames.push_back(path.path());
continue;
}
}
}

Expand All @@ -285,9 +327,15 @@ int CLIMain(const std::vector<std::wstring> &args) noexcept {
AddAndConvertOneFile(i + 1, total, inputFileNames[i], success, failed);
}

wcout << std::wstring(32, L'=') << L"\n";
wcout << L"总计:" << to_wstring(std::to_string(total)) << L"\n";
SetConsoleColor(ConsoleColor::GREEN);
wcout << L"成功:" << to_wstring(std::to_string(success)) << L"\n";
if (failed > 0) {
SetConsoleColor(ConsoleColor::RED);
}
wcout << L"失败:" << to_wstring(std::to_string(failed)) << L"\n";
SetConsoleColor();

return retCode;
}
14 changes: 14 additions & 0 deletions src/Common/ConsoleSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "ConsoleSettings.h"

#include <cassert>

void SetConsoleColor(ConsoleColor color) noexcept {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
assert(hConsole);
BOOL ok = SetConsoleTextAttribute(hConsole, static_cast<WORD>(color));
assert(ok);
HANDLE hConsoleErr = GetStdHandle(STD_ERROR_HANDLE);
assert(hConsoleErr);
ok = SetConsoleTextAttribute(hConsoleErr, static_cast<WORD>(color));
assert(ok);
}
7 changes: 7 additions & 0 deletions src/Common/ConsoleSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <Windows.h>

enum class ConsoleColor { GREEN = 10, BLUE = 11, RED = 12, PINK = 13, YELLOW = 14, WHITE = 15 };

void SetConsoleColor(ConsoleColor color = ConsoleColor::WHITE) noexcept;
2 changes: 1 addition & 1 deletion src/DialogMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#undef min
#undef max

const std::tstring appTitle = TEXT("智能编码集转换器 v0.63 by Tom Willow");
const std::tstring appTitle = TEXT("智能编码集转换器 v0.7 by Tom Willow");

using namespace std;

Expand Down

0 comments on commit 7203e45

Please sign in to comment.