From ff9a8acd67f1d9012c0b0eec5a5ec82eca2b0a6f Mon Sep 17 00:00:00 2001 From: niansa Date: Thu, 30 Apr 2020 13:01:16 +0200 Subject: [PATCH] Fixed compiler warning and increased codestyle --- main.cpp | 107 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 47 deletions(-) diff --git a/main.cpp b/main.cpp index a592531..f31bbc0 100644 --- a/main.cpp +++ b/main.cpp @@ -158,7 +158,7 @@ static std::string gumbo_cleantext(GumboNode* node) { std::string contents = ""; GumboVector* children = &node->v.element.children; for (unsigned int i = 0; i < children->length; ++i) { - const std::string text = gumbo_cleantext((GumboNode*) children->data[i]); + const std::string text = gumbo_cleantext(reinterpret_cast (children->data[i])); if (i != 0 && !text.empty()) { contents.append(" "); } @@ -186,7 +186,7 @@ std::vector gumbo_get_attr(GumboNode *node, std::string attrkey, Gu } // Iterate through child nodes for (unsigned int it = 0; it < children->length; ++it) { - childnode = (GumboNode*) children->data[it]; + childnode = reinterpret_cast (children->data[it]); if (childnode->v.element.tag == expected_tag) { // If node is the expected tag; use it attrvals.push_back(gumbo_get_attribute(&childnode->v.element.attributes, attrkey.c_str())->value); } else if (childnode->type == GUMBO_NODE_ELEMENT) { // Else; iterate through its child nodes @@ -203,7 +203,7 @@ std::string gumbo_find_text_by_tag(GumboNode *node, GumboTag searchtag) { GumboVector* children = &node->v.element.children; // Iterate through childs for (unsigned int it = 0; it < children->length; ++it) { - childnode = (GumboNode*) children->data[it]; + childnode = reinterpret_cast (children->data[it]); if (childnode->v.element.tag == searchtag) { // If node is the expected tag; check content return trim(gumbo_cleantext(childnode)); } @@ -314,6 +314,60 @@ std::string get_rss_url(GumboNode *node) { // Currently unused } + +int mode_cli(char *argv[], unsigned long numposts, std::vector postsmap_numname, std::vector postsmap_nummeta, + std::vector postsmap_numurl, std::vector postsmap_numdesc, + std::vector> postsmap_numfileurls, std::vector> postsmap_numfilenames) { + // Show overview + for (unsigned long it = 0; it != numposts; it++) { + std::cout << it << ") " << postsmap_numname[it] << " – " << postsmap_nummeta[it] << std::endl; + } + std::cout << "r) Refresh" << std::endl; + // Start CLI loop + std::string userinstr; + unsigned long userin; + while (true) { + // Await user input + std::cout << "? " << std::flush; + if (!std::getline(std::cin, userinstr)) { + // On EOF + std::cout << std::endl; + break; + } + // Check string input options first + if (userinstr == "") { + continue; + } else if (userinstr == "r") { + execve(argv[0], argv, nullptr); + } + // Try to convert input to unsigned long + try { + userin = std::stoul(userinstr,nullptr,0); + } catch (const std::invalid_argument &) { + std::cerr << "Invalid input" << std::endl; + continue; + } + // Check if givenn number is valid + if (userin >= numposts) { + std::cerr << "No such post" << std::endl; + continue; + } + // If required; get posts description and cache it + // Print description + std::cout << std::endl << std::endl << postsmap_numdesc[userin] << std::endl; + // Print informations + std::cout << "Post name: " << postsmap_numname[userin] << std::endl; + std::cout << "Post URL: " << server_url << postsmap_numurl[userin] << std::endl; + if (postsmap_numfilenames[userin].size() != 0) { + std::cout << "Post files:" << std::endl; + for (unsigned long it = 0; it < postsmap_numfilenames[userin].size(); it++) { + std::cout << " – " << postsmap_numfilenames[userin][it] << ": " << server_url << postsmap_numfileurls[userin][it] << std::endl; + } + } + } + return 0; +} + int main(int argc, char *argv[]) { // Catch SIGSEGV signal(SIGSEGV, sigsegv_panic); @@ -334,7 +388,6 @@ int main(int argc, char *argv[]) { server_sid = argv[1]; room = argv[2]; std::stringstream httpcontent; - std::string rss_url; std::vector postsmap_numname; std::vector postsmap_nummeta; std::vector postsmap_numurl; @@ -381,49 +434,9 @@ int main(int argc, char *argv[]) { postsmap_numfileurls[numposts].push_back(pair.second); } } - // Show overview - std::cout << numposts << ") " << postsmap_numname[numposts] << " – " << postsmap_nummeta[numposts] << std::endl; numposts++; } - std::cout << "r) Refresh" << std::endl; - // Debug CLI - std::string userinstr; - unsigned long userin; - while (true) { - // Await user input - std::cout << "? " << std::flush; - std::getline (std::cin,userinstr); - // Check string input options first - if (userinstr == "") { - continue; - } else if (userinstr == "r") { - execve(argv[0], argv, nullptr); - } - // Try to convert input to unsigned long - try { - userin = std::stoul(userinstr,nullptr,0); - } catch (const std::invalid_argument &) { - std::cerr << "Invalid input" << std::endl; - continue; - } - // Check if givenn number is valid - if (userin >= numposts) { - std::cerr << "No such post" << std::endl; - continue; - } - // If required; get posts description and cache it - // Print description - std::cout << std::endl << std::endl << postsmap_numdesc[userin] << std::endl; - // Print informations - std::cout << "Post name: " << postsmap_numname[userin] << std::endl; - std::cout << "Post URL: " << server_url << postsmap_numurl[userin] << std::endl; - if (postsmap_numfilenames[userin].size() != 0) { - std::cout << "Post files:" << std::endl; - for (unsigned long it = 0; it < postsmap_numfilenames[userin].size(); it++) { - std::cout << " – " << postsmap_numfilenames[userin][it] << ": " << server_url << postsmap_numfileurls[userin][it] << std::endl; - } - } - } - - return 0; + // Start CLI + return mode_cli(argv, numposts, postsmap_numname, postsmap_nummeta,postsmap_numurl, + postsmap_numdesc, postsmap_numfileurls, postsmap_numfilenames); }