fix: file name encoding under Windows (#3)

This commit is contained in:
TaurusXin
2024-01-06 00:20:17 +08:00
parent cce512bbf3
commit 8f5c9a9c10
4 changed files with 64 additions and 36 deletions
+46 -14
View File
@@ -5,27 +5,31 @@
#include <vector>
#include <filesystem>
#ifdef _WIN32
#include <Windows.h>
#endif
namespace fs = std::filesystem;
void displayHelp() {
std::cout << "Usage: ncmdump [-d] [-h] file1 file2 ..." << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -d Process files in a folder (requires folder path)" << std::endl;
std::cout << " -d Process files in a folder (requires folder path)" << std::endl;
std::cout << " -h, --help Display this help message" << std::endl;
}
void processFile(const std::string& filePath) {
void processFile(const fs::path& filePath) {
if (fs::exists(filePath) == false) {
std::cerr << "Error: file '" << filePath << "' does not exist." << std::endl;
std::cerr << "Error: file '" << filePath.string() << "' does not exist." << std::endl;
return;
}
try {
NeteaseCrypt crypt(filePath);
NeteaseCrypt crypt(filePath.string());
crypt.Dump();
crypt.FixMetadata();
std::cout << "Done: " << crypt.dumpFilepath() << std::endl;
std::cout << "Done: '" << crypt.dumpFilepath().string() << "'" << std::endl;
} catch (const std::invalid_argument& e) {
std::cout << "Exception: '" << filePath << "'" << e.what() << std::endl;
} catch (...) {
@@ -33,31 +37,51 @@ void processFile(const std::string& filePath) {
}
}
void processFilesInFolder(const std::string& folderPath) {
void processFilesInFolder(const fs::path& folderPath) {
for (const auto& entry : fs::directory_iterator(folderPath)) {
if (fs::is_regular_file(entry.status())) {
processFile(entry.path().string());
processFile(entry.path());
}
}
}
int main(int argc, char* argv[]) {
#ifdef _WIN32
int wmain(int argc, wchar_t* argv[])
#else
int main(int argc, char **argv)
#endif
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
if (argc <= 1) {
displayHelp();
return 1;
}
std::vector<std::string> files;
std::vector<fs::path> files;
bool processFolders = false;
bool folderProvided = false;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
#ifdef _WIN32
#define COMPARE_STR(s1, s2) (wcscmp(s1, s2) == 0)
#define HELP_SHORT L"-h"
#define HELP_LONG L"--help"
#define FOLDER L"-d"
#else
#define COMPARE_STR(s1, s2) (strcmp(s1, s2) == 0)
#define HELP_SHORT "-h"
#define HELP_LONG "--help"
#define FOLDER "-d"
#endif
if (arg == "-h" || arg == "--help") {
for (int i = 1; i < argc; ++i) {
if (COMPARE_STR(argv[i], HELP_SHORT) || COMPARE_STR(argv[i], HELP_LONG)) {
displayHelp();
return 0;
} else if (arg == "-d") {
} else if (COMPARE_STR(argv[i], FOLDER)) {
processFolders = true;
if (i + 1 < argc && argv[i + 1][0] != '-') {
folderProvided = true;
@@ -69,7 +93,15 @@ int main(int argc, char* argv[]) {
return 1;
}
} else {
files.push_back(arg);
#ifdef _WIN32
int multiByteStrSize = WideCharToMultiByte(CP_UTF8, 0, argv[1], -1, NULL, 0, NULL, NULL);
char *multiByteStr = new char[multiByteStrSize];
WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, multiByteStr, multiByteStrSize, NULL, NULL);
fs::path path(multiByteStr);
#else
fs::path path(arg);
#endif
files.push_back(path);
}
}