From bc926557022b688306a2b72b35781f67c81e5856 Mon Sep 17 00:00:00 2001 From: Sandy Carter Date: Fri, 14 Aug 2015 23:15:15 -0400 Subject: [PATCH] Add output filename option Add -o option to specify path and downloaded file name when using --download-file Overrides --directory --- include/config.h | 1 + include/downloader.h | 2 +- main.cpp | 3 ++- src/downloader.cpp | 12 ++++++++++-- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/config.h b/include/config.h index 9b3aea2..a66bae8 100644 --- a/include/config.h +++ b/include/config.h @@ -68,6 +68,7 @@ class Config std::string sDLCSubdir; std::string sGameSubdir; std::string sFileIdString; + std::string sOutputFilename; std::string sLanguagePriority; std::string sPlatformPriority; std::string sIgnoreDLCCountRegex; diff --git a/include/downloader.h b/include/downloader.h index f305645..666eecd 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -68,7 +68,7 @@ class Downloader void checkOrphans(); void checkStatus(); void updateCache(); - void downloadFileWithId(const std::string& fileid_string); + void downloadFileWithId(const std::string& fileid_string, const std::string& output_filepath); void showWishlist(); CURL* curlhandle; Timer timer; diff --git a/main.cpp b/main.cpp index 8414012..f6f9f12 100644 --- a/main.cpp +++ b/main.cpp @@ -150,6 +150,7 @@ int main(int argc, char *argv[]) ("update-cache", bpo::value(&config.bUpdateCache)->zero_tokens()->default_value(false), "Update game details cache") ("no-platform-detection", bpo::value(&bNoPlatformDetection)->zero_tokens()->default_value(false), "Don't try to detect supported platforms from game shelf.\nSkips the initial fast platform detection and detects the supported platforms from game details which is slower but more accurate.\nUseful in case platform identifier is missing for some games in the game shelf.\nUsing --platform with --list doesn't work with this option.") ("download-file", bpo::value(&config.sFileIdString)->default_value(""), "Download a single file using fileid\nFormat: \"gamename/fileid\"\nThis option ignores all subdir options. The file is downloaded to directory specified with --directory option.") + ("output-file,o", bpo::value(&config.sOutputFilename)->default_value(""), "Set filename of file downloaded with --download-file.") ("wishlist", bpo::value(&config.bShowWishlist)->zero_tokens()->default_value(false), "Show wishlist") ("login-api", bpo::value(&config.bLoginAPI)->zero_tokens()->default_value(false), "Login (API only)") ("login-website", bpo::value(&config.bLoginHTTP)->zero_tokens()->default_value(false), "Login (website only)") @@ -510,7 +511,7 @@ int main(int argc, char *argv[]) else if (config.bUpdateCheck) // Update check has priority over download and list downloader.updateCheck(); else if (!config.sFileIdString.empty()) - downloader.downloadFileWithId(config.sFileIdString); + downloader.downloadFileWithId(config.sFileIdString, config.sOutputFilename); else if (config.bRepair) // Repair file downloader.repair(); else if (config.bDownload) // Download games diff --git a/src/downloader.cpp b/src/downloader.cpp index 279e32c..87ceca0 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -1174,6 +1174,7 @@ CURLcode Downloader::downloadFile(const std::string& url, const std::string& fil // Get directory from filepath boost::filesystem::path pathname = filepath; + pathname = boost::filesystem::absolute(pathname, boost::filesystem::current_path()); std::string directory = pathname.parent_path().string(); std::string filenameXML = pathname.filename().string() + ".xml"; std::string xml_directory; @@ -3174,13 +3175,17 @@ void Downloader::saveSerials(const std::string& serials, const std::string& file return; } -void Downloader::downloadFileWithId(const std::string& fileid_string) +void Downloader::downloadFileWithId(const std::string& fileid_string, const std::string& output_filepath) { size_t pos = fileid_string.find("/"); if (pos == std::string::npos) { std::cout << "Invalid file id " << fileid_string << ": could not find separator \"/\"" << std::endl; } + else if (!output_filepath.empty() && boost::filesystem::is_directory(output_filepath)) + { + std::cout << "Failed to create the file " << output_filepath << ": Is a directory" << std::endl; + } else { std::string gamename, fileid, url; @@ -3198,7 +3203,10 @@ void Downloader::downloadFileWithId(const std::string& fileid_string) { std::string filename, filepath; filename.assign(url.begin()+url.find_last_of("/")+1, url.begin()+url.find_first_of("?")); - filepath = Util::makeFilepath(config.sDirectory, filename, gamename); + if (output_filepath.empty()) + filepath = Util::makeFilepath(config.sDirectory, filename, gamename); + else + filepath = output_filepath; std::cout << "Downloading: " << filepath << std::endl; this->downloadFile(url, filepath, std::string(), gamename); std::cout << std::endl;