Finish the code, add more checks
This commit is contained in:
parent
03b9022bf4
commit
160d26c47b
71
mcopy.cpp
71
mcopy.cpp
|
@ -1,18 +1,24 @@
|
||||||
|
/* mcopy
|
||||||
|
*
|
||||||
|
* Rename music by specifying a format
|
||||||
|
* Licensed under the GNU General Public License version 3.
|
||||||
|
* See included LICENSE file for copyright and license details.
|
||||||
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
#include <tag.h>
|
#include <tag.h>
|
||||||
#include <fileref.h>
|
#include <fileref.h>
|
||||||
#include <id3v1tag.h>
|
|
||||||
#include <id3v2tag.h>
|
|
||||||
|
|
||||||
#ifndef VERSION
|
#ifndef VERSION
|
||||||
#define VERSION "0.1"
|
#define VERSION "0.1"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
bool ask = true;
|
||||||
|
|
||||||
void help() {
|
void help() {
|
||||||
std::cout <<
|
std::cout <<
|
||||||
"mcopy " << VERSION << "\n\n" <<
|
"mcopy " << VERSION << "\n\n" <<
|
||||||
|
@ -45,6 +51,7 @@ void getMetadataFromFile(std::string format, std::string str) {
|
||||||
TagLib::Tag *tag = file.tag();
|
TagLib::Tag *tag = file.tag();
|
||||||
|
|
||||||
if (file.isNull() || !file.audioProperties()) {
|
if (file.isNull() || !file.audioProperties()) {
|
||||||
|
std::cout << "File '" << str << "' is not valid. Skipping.\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,14 +60,54 @@ void getMetadataFromFile(std::string format, std::string str) {
|
||||||
std::string artist = tag->artist().to8Bit(true);
|
std::string artist = tag->artist().to8Bit(true);
|
||||||
std::string track = std::to_string(tag->track());
|
std::string track = std::to_string(tag->track());
|
||||||
|
|
||||||
|
std::cout << "title: " << title << "\n";
|
||||||
|
|
||||||
|
// If information is missing, ask for the missing details
|
||||||
|
if (title.empty()) {
|
||||||
|
title = "Unknown";
|
||||||
|
|
||||||
|
if (ask) {
|
||||||
|
std::cout << "mcopy: Could not retrieve title; please specify\n> ";
|
||||||
|
std::getline(std::cin, title);
|
||||||
|
}
|
||||||
|
} else if (album.empty()) {
|
||||||
|
album = "Unknown";
|
||||||
|
|
||||||
|
if (ask) {
|
||||||
|
std::cout << "mcopy: Could not retrieve album; please specify\n> ";
|
||||||
|
std::getline(std::cin, album);
|
||||||
|
}
|
||||||
|
} else if (artist.empty()) {
|
||||||
|
artist = "Unknown";
|
||||||
|
|
||||||
|
if (ask) {
|
||||||
|
std::cout << "mcopy: Could not retrieve artist; please specify\n> ";
|
||||||
|
std::getline(std::cin, artist);
|
||||||
|
}
|
||||||
|
} else if (track.empty()) {
|
||||||
|
track = "Unknown";
|
||||||
|
|
||||||
|
if (ask) {
|
||||||
|
std::cout << "mcopy: Could not retrieve track; please specify\n> ";
|
||||||
|
std::getline(std::cin, track);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::string filename = format;
|
std::string filename = format;
|
||||||
|
|
||||||
// TODO: Replace this junk
|
// TODO: Replace this junk
|
||||||
filename = std::regex_replace(format, std::regex("@t"), title);
|
filename = std::regex_replace(filename, std::regex("@t"), title);
|
||||||
filename = std::regex_replace(filename, std::regex("@a"), album);
|
filename = std::regex_replace(filename, std::regex("@a"), album);
|
||||||
filename = std::regex_replace(filename, std::regex("@A"), artist);
|
filename = std::regex_replace(filename, std::regex("@A"), artist);
|
||||||
filename = std::regex_replace(filename, std::regex("@n"), track);
|
filename = std::regex_replace(filename, std::regex("@n"), track);
|
||||||
|
|
||||||
|
std::ifstream ef(filename);
|
||||||
|
|
||||||
|
if (static_cast<bool>(ef.good())) {
|
||||||
|
std::cout << "mcopy: File already exists, skipping.\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
std::filesystem::path fs;
|
std::filesystem::path fs;
|
||||||
std::string dir = (fs = filename).remove_filename();
|
std::string dir = (fs = filename).remove_filename();
|
||||||
std::filesystem::create_directories(dir);
|
std::filesystem::create_directories(dir);
|
||||||
|
@ -82,20 +129,26 @@ int main(int argc, char **argv) {
|
||||||
for (int i{1}; i < argc; i++) {
|
for (int i{1}; i < argc; i++) {
|
||||||
std::string arg = argv[i];
|
std::string arg = argv[i];
|
||||||
|
|
||||||
if (!arg.compare("-h")) {
|
if (!arg.compare("-h") || !arg.compare("--help")) {
|
||||||
help();
|
help();
|
||||||
exit(0);
|
exit(0);
|
||||||
} else if (!arg.compare("-v")) {
|
} else if (!arg.compare("-v") || !arg.compare("--version")) {
|
||||||
std::cout << "mcopy " << VERSION << '\n';
|
std::cout << "mcopy " << VERSION << '\n';
|
||||||
exit(0);
|
exit(0);
|
||||||
} else if (!arg.compare("-f") && i < argc) {
|
} else if (!arg.compare("-a") || !arg.compare("--ask")) {
|
||||||
format = argv[++i];
|
ask = true;
|
||||||
|
} else if (!arg.compare("-na") || !arg.compare("--no-ask")) {
|
||||||
|
ask = false;
|
||||||
|
} else if (!arg.compare("-f") || !arg.compare("--format")) {
|
||||||
|
if (argc > i+1) {
|
||||||
|
format = argv[++i];
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!format.compare("")) {
|
if (!format.compare("")) {
|
||||||
std::cout << "You must specify a format.\n";
|
std::cout << "mcopy: You must specify a format.\n";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue