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);