refactor: code structure

This commit is contained in:
TaurusXin 2024-03-21 14:04:16 +08:00
parent d65dad255b
commit a54f25eea3
1 changed files with 52 additions and 34 deletions

View File

@ -13,51 +13,70 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
void displayHelp() { void displayHelp()
{
std::cout << "Usage: ncmdump [-d] [-h] file1 file2 ..." << std::endl; std::cout << "Usage: ncmdump [-d] [-h] file1 file2 ..." << std::endl;
std::cout << "Options:" << 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; std::cout << " -h, --help Display this help message" << std::endl;
} }
void processFile(const fs::path& filePath) { void processFile(const fs::path &filePath)
if (fs::exists(filePath) == false) { {
if (fs::exists(filePath) == false)
{
std::cerr << BOLDRED << "Error: " << RESET << "file '" << filePath.string() << "' does not exist." << std::endl; std::cerr << BOLDRED << "Error: " << RESET << "file '" << filePath.string() << "' does not exist." << std::endl;
return; return;
} }
try { try
{
NeteaseCrypt crypt(filePath.string()); NeteaseCrypt crypt(filePath.string());
crypt.Dump(); crypt.Dump();
crypt.FixMetadata(); crypt.FixMetadata();
std::cout << BOLDGREEN << "Done: '" << RESET << crypt.dumpFilepath().string() << "'" << std::endl; std::cout << BOLDGREEN << "Done: '" << RESET << crypt.dumpFilepath().string() << "'" << std::endl;
} catch (const std::invalid_argument& e) { }
catch (const std::invalid_argument &e)
{
std::cerr << BOLDRED << "Exception: " << RESET << RED << e.what() << RESET << " '" << filePath.string() << "'" << std::endl; std::cerr << BOLDRED << "Exception: " << RESET << RED << e.what() << RESET << " '" << filePath.string() << "'" << std::endl;
} catch (...) { }
catch (...)
{
std::cerr << BOLDRED << "Unexpected exception while processing file: " << RESET << filePath.string() << std::endl; std::cerr << BOLDRED << "Unexpected exception while processing file: " << RESET << filePath.string() << std::endl;
} }
} }
void processFilesInFolder(const fs::path& folderPath) { void processFilesInFolder(const fs::path &folderPath)
for (const auto& entry : fs::directory_iterator(folderPath)) { {
if (fs::is_regular_file(entry.status())) { for (const auto &entry : fs::directory_iterator(folderPath))
{
if (fs::is_regular_file(entry.status()))
{
processFile(entry.path()); processFile(entry.path());
} }
} }
} }
#if defined(_WIN32) #if defined(_WIN32)
int wmain(int argc, wchar_t* argv[]) int wmain(int argc, wchar_t *wideargv[])
#else #else
int main(int argc, char **argv) int main(int argc, char **argv)
#endif #endif
{ {
#if defined(_WIN32) #if defined(_WIN32)
SetConsoleOutputCP(CP_UTF8); SetConsoleOutputCP(CP_UTF8);
char **argv = (char **)malloc(sizeof(char *) * argc);
for (int i = 0; i < argc; ++i)
{
int utf8_size = WideCharToMultiByte(CP_UTF8, 0, wideargv[i], -1, NULL, 0, NULL, NULL);
argv[i] = (char *)malloc(utf8_size);
WideCharToMultiByte(CP_UTF8, 0, wideargv[i], -1, argv[i], utf8_size, NULL, NULL);
}
#endif #endif
if (argc <= 1) { if (argc <= 1)
{
displayHelp(); displayHelp();
return 1; return 1;
} }
@ -67,49 +86,48 @@ int main(int argc, char **argv)
bool folderProvided = false; bool folderProvided = false;
#if defined(_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 COMPARE_STR(s1, s2) (strcmp(s1, s2) == 0)
#define HELP_SHORT "-h" #define HELP_SHORT "-h"
#define HELP_LONG "--help" #define HELP_LONG "--help"
#define FOLDER "-d" #define FOLDER "-d"
#endif for (int i = 1; i < argc; ++i)
for (int i = 1; i < argc; ++i) { {
if (COMPARE_STR(argv[i], HELP_SHORT) || COMPARE_STR(argv[i], HELP_LONG)) { if (COMPARE_STR(argv[i], HELP_SHORT) || COMPARE_STR(argv[i], HELP_LONG))
{
displayHelp(); displayHelp();
return 0; return 0;
} else if (COMPARE_STR(argv[i], FOLDER)) { }
else if (COMPARE_STR(argv[i], FOLDER))
{
processFolders = true; processFolders = true;
if (i + 1 < argc && argv[i + 1][0] != '-') { if (i + 1 < argc && argv[i + 1][0] != '-')
{
folderProvided = true; folderProvided = true;
processFilesInFolder(argv[i + 1]); processFilesInFolder(argv[i + 1]);
// Skip the folder name // Skip the folder name
++i; ++i;
} else { }
else
{
std::cerr << "Error: -d option requires a folder path." << std::endl; std::cerr << "Error: -d option requires a folder path." << std::endl;
return 1; return 1;
} }
} else { }
#if defined(_WIN32) else
int utf8_size = WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, NULL, 0, NULL, NULL); {
char* u8str = new char[utf8_size];
WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, u8str, utf8_size, NULL, NULL);
fs::path path(u8str);
#else
fs::path path(argv[i]); fs::path path(argv[i]);
#endif
files.push_back(path); files.push_back(path);
} }
} }
for (const auto& file : files) { for (const auto &file : files)
if (processFolders && fs::is_directory(file)) { {
if (processFolders && fs::is_directory(file))
{
processFilesInFolder(file); processFilesInFolder(file);
} else { }
else
{
processFile(file); processFile(file);
} }
} }