feat: parse directory with -d option (#4)
This commit is contained in:
parent
e08b922be3
commit
cce512bbf3
|
@ -74,5 +74,6 @@ make win32
|
|||
|
||||
## 使用
|
||||
|
||||
1. 命令行下使用 `ncmdump [files]...`
|
||||
2. 直接 ncm 拖拽文件到二进制文件上
|
||||
1. 多文件:命令行下使用 `ncmdump file1 file2...`
|
||||
2. 文件夹批量处理:命令行下使用 `ncmdump -d folder`
|
||||
3. 直接 ncm 拖拽文件到二进制文件上
|
||||
|
|
81
main.cpp
81
main.cpp
|
@ -1,27 +1,84 @@
|
|||
#include "ncmcrypt.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void processFile(const std::string& filePath) {
|
||||
if (fs::exists(filePath) == false) {
|
||||
std::cerr << "Error: file '" << filePath << "' does not exist." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
int i;
|
||||
for (i = 1; i < argc; i++) {
|
||||
NeteaseCrypt crypt(argv[i]);
|
||||
NeteaseCrypt crypt(filePath);
|
||||
crypt.Dump();
|
||||
crypt.FixMetadata();
|
||||
|
||||
std::cout << crypt.dumpFilepath() << std::endl;
|
||||
}
|
||||
} catch (std::invalid_argument e) {
|
||||
std::cout << "Exception: " << e.what() << std::endl;
|
||||
std::cout << "Done: " << crypt.dumpFilepath() << std::endl;
|
||||
} catch (const std::invalid_argument& e) {
|
||||
std::cout << "Exception: '" << filePath << "'" << e.what() << std::endl;
|
||||
} catch (...) {
|
||||
std::cout << "Unexcept exception!" << std::endl;
|
||||
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<std::string> 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;
|
||||
|
|
|
@ -113,13 +113,13 @@ NeteaseMusicMetadata::NeteaseMusicMetadata(cJSON* raw) {
|
|||
}
|
||||
|
||||
bool NeteaseCrypt::openFile(std::string const& path) {
|
||||
try {
|
||||
mFile.open(path, std::ios::in | std::ios::binary);
|
||||
} catch (...) {
|
||||
if (!mFile.is_open()) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool NeteaseCrypt::isNcmFile() {
|
||||
unsigned int header;
|
||||
|
|
Loading…
Reference in New Issue