From 89b1f2f30894008d0aea0464694b5d5d4a27b092 Mon Sep 17 00:00:00 2001 From: LykovaYA Date: Thu, 21 Nov 2024 21:55:33 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=A4=D0=B8=D0=BD=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab34.cpp | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lab34.cpp b/lab34.cpp index bc43e34..16c691a 100644 --- a/lab34.cpp +++ b/lab34.cpp @@ -41,20 +41,16 @@ write_data(void* items, size_t item_size, size_t item_count, void* ctx) { } Input -download(const string& address, const string& argument) { +download(const string& address, bool verbose = false) { stringstream buffer; CURL* curl = curl_easy_init(); - if ((argument != "") && (address != "-verbose") && (argument != "-verbose")) { - cout << "Wrong argument, use correct input -verbose or leave input empty" << endl; - exit(2); - } if (curl) { CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, argument == "-verbose" || argument == "" ? address.c_str() : argument.c_str()); + curl_easy_setopt(curl, CURLOPT_URL, address.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); - if (address == "-verbose" || argument == "-verbose") { - curl_easy_setopt(curl, CURLOPT_VERBOSE, argument == "-verbose" ? argument.c_str() : address.c_str()); + if (verbose) { + curl_easy_setopt(curl, CURLOPT_VERBOSE, "-verbose"); } res = curl_easy_perform(curl); curl_easy_cleanup(curl); @@ -69,11 +65,26 @@ download(const string& address, const string& argument) { int main(int argc, char* argv[]) { Input input; - if (argc > 2) { - input = download(argv[1], argv[2]); + string link; + bool flag = false; + if (argc == 2) { + input = download(argv[1]); } - else if (argc == 2) { - input = download(argv[1], ""); + else if (argc > 2) { + for (size_t i = 1; i < argc; i++) { + string cur = argv[i]; + if (cur.find("://") != string::npos) { + link = cur; + } + else if (cur == "-verbose") { + flag = true; + } + else if (cur.find("-") != string::npos && cur != "-verbose") { + cout << "Wrong argument, use correct input -verbose or leave input empty" << endl; + exit(2); + } + } + input = download(link, flag); } else { input = input_data(cin, true);