From 25bfd0cfc73cbca5dd8368f1ed6c88f6e774b6fd Mon Sep 17 00:00:00 2001 From: Clortox Date: Mon, 26 Jul 2021 19:24:55 -0400 Subject: [PATCH] Add ability to accept stdin from URI --- README.md | 2 ++ src/options.cpp | 4 +++- src/rss.cpp | 37 +++++++++++++++++++++---------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 559c1e8..b76321b 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ Usage: ./bin/rss-cli [-u FEED_URI] [CHANNEL FLAGS] [-i ITEM_INDEX] [ITEM FLAGS] Options: Required Options: [-u, --uri] URI URI of the rss stream + Also accepts '-' to take input + from stdin (stops once a newline is reached) Channel information: [-t, --title] Get title of channel diff --git a/src/options.cpp b/src/options.cpp index b867e1d..d3e072e 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -11,7 +11,9 @@ void help(char* progName){ std::cout << "[-i ITEM_INDEX] [ITEM FLAGS]\n"; std::cout << "Options:\n"; std::cout << "Required Options:\n"; - std::cout << " [-u, --uri] URI URI of the rss stream\n\n"; + std::cout << " [-u, --uri] URI URI of the rss stream\n"; + std::cout << " Also accepts '-' to take input\n"; + std::cout << " from stdin (stops once a newline is reached)\n\n"; std::cout << "Channel information:\n"; std::cout << " [-t, --title] Get title of channel\n"; diff --git a/src/rss.cpp b/src/rss.cpp index f4ec21c..3650668 100644 --- a/src/rss.cpp +++ b/src/rss.cpp @@ -77,26 +77,31 @@ std::string rss::getURI() const { return _uri; } bool rss::update() { std::string result; - CURL *curl; - CURLcode res; - curl = curl_easy_init(); + if(_uri == "-"){ + std::getline(std::cin, result); + return parse(result); + } else { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); - if(curl){ - curl_easy_setopt(curl, CURLOPT_URL, _uri.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_utils::write_to_string); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result); - res = curl_easy_perform(curl); - curl_easy_cleanup(curl); + if(curl){ + curl_easy_setopt(curl, CURLOPT_URL, _uri.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_utils::write_to_string); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result); + res = curl_easy_perform(curl); + curl_easy_cleanup(curl); - if(res == CURLE_OK){ - return parse(result); - } else { - std::cerr << "curl_easy_perform(curl) failed: " - << curl_easy_strerror(res) << std::endl; + if(res == CURLE_OK){ + return parse(result); + } else { + std::cerr << "curl_easy_perform(curl) failed: " + << curl_easy_strerror(res) << std::endl; + } } + _ok = false; + return false; } - _ok = false; - return false; } std::string rss::getTitle() const {