From cce512bbf319b9aa8d336fc61e718d5dbf290081 Mon Sep 17 00:00:00 2001 From: TaurusXin Date: Fri, 5 Jan 2024 18:34:42 +0800 Subject: [PATCH] feat: parse directory with -d option (#4) --- README.md | 5 +-- main.cpp | 99 +++++++++++++++++++++++++++++++++++++++++----------- ncmcrypt.cpp | 20 +++++------ 3 files changed, 91 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 7de8761..8d5c18f 100644 --- a/README.md +++ b/README.md @@ -74,5 +74,6 @@ make win32 ## 使用 -1. 命令行下使用 `ncmdump [files]...` -2. 直接 ncm 拖拽文件到二进制文件上 +1. 多文件:命令行下使用 `ncmdump file1 file2...` +2. 文件夹批量处理:命令行下使用 `ncmdump -d folder` +3. 直接 ncm 拖拽文件到二进制文件上 diff --git a/main.cpp b/main.cpp index 6baa9f2..82ab2aa 100644 --- a/main.cpp +++ b/main.cpp @@ -1,28 +1,85 @@ #include "ncmcrypt.h" +#include #include +#include +#include -int main(int argc, char *argv[]) { +namespace fs = std::filesystem; - if (argc <= 1) { - std::cout << "Please input file path!" << std::endl; - return 1; - } +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 << " -h, --help Display this help message" << std::endl; +} - try { - int i; - for (i = 1; i < argc; i++) { - NeteaseCrypt crypt(argv[i]); - crypt.Dump(); - crypt.FixMetadata(); - - std::cout << crypt.dumpFilepath() << std::endl; - } - } catch (std::invalid_argument e) { - std::cout << "Exception: " << e.what() << std::endl; - } catch (...) { - std::cout << "Unexcept exception!" << std::endl; - } +void processFile(const std::string& filePath) { + if (fs::exists(filePath) == false) { + std::cerr << "Error: file '" << filePath << "' does not exist." << std::endl; + return; + } - return 0; -} \ No newline at end of file + try { + NeteaseCrypt crypt(filePath); + crypt.Dump(); + crypt.FixMetadata(); + + std::cout << "Done: " << crypt.dumpFilepath() << std::endl; + } catch (const std::invalid_argument& e) { + std::cout << "Exception: '" << filePath << "'" << e.what() << std::endl; + } catch (...) { + std::cout << "Unexpected exception while processing file: " << filePath << std::endl; + } +} + +void processFilesInFolder(const std::string& folderPath) { + for (const auto& entry : fs::directory_iterator(folderPath)) { + if (fs::is_regular_file(entry.status())) { + processFile(entry.path().string()); + } + } +} + +int main(int argc, char* argv[]) { + if (argc <= 1) { + displayHelp(); + return 1; + } + + std::vector files; + bool processFolders = false; + bool folderProvided = false; + + for (int i = 1; i < argc; ++i) { + std::string arg = argv[i]; + + if (arg == "-h" || arg == "--help") { + displayHelp(); + return 0; + } else if (arg == "-d") { + processFolders = true; + if (i + 1 < argc && argv[i + 1][0] != '-') { + folderProvided = true; + processFilesInFolder(argv[i + 1]); + // Skip the folder name + ++i; + } else { + std::cerr << "Error: -d option requires a folder path." << std::endl; + return 1; + } + } else { + files.push_back(arg); + } + } + + for (const auto& file : files) { + if (processFolders && fs::is_directory(file)) { + processFilesInFolder(file); + } else { + processFile(file); + } + } + + return 0; +} diff --git a/ncmcrypt.cpp b/ncmcrypt.cpp index 1585bbf..6896265 100644 --- a/ncmcrypt.cpp +++ b/ncmcrypt.cpp @@ -113,12 +113,12 @@ NeteaseMusicMetadata::NeteaseMusicMetadata(cJSON* raw) { } bool NeteaseCrypt::openFile(std::string const& path) { - try { - mFile.open(path, std::ios::in | std::ios::binary); - } catch (...) { - return false; - } - return true; + mFile.open(path, std::ios::in | std::ios::binary); + if (!mFile.is_open()) { + return false; + } else { + return true; + } } bool NeteaseCrypt::isNcmFile() { @@ -143,7 +143,7 @@ int NeteaseCrypt::read(char *s, std::streamsize n) { int gcount = mFile.gcount(); if (gcount <= 0) { - throw std::invalid_argument("can't read file"); + throw std::invalid_argument(" can't read file"); } return gcount; @@ -292,15 +292,15 @@ NeteaseCrypt::~NeteaseCrypt() { NeteaseCrypt::NeteaseCrypt(std::string const& path) { if (!openFile(path)) { - throw std::invalid_argument("can't open file"); + throw std::invalid_argument(" can't open file"); } if (!isNcmFile()) { - throw std::invalid_argument("not netease protected file"); + throw std::invalid_argument(" not netease protected file"); } if (!mFile.seekg(2, mFile.cur)) { - throw std::invalid_argument("can't seek file"); + throw std::invalid_argument(" can't seek file"); } mFilepath = path;