Fix CDATA not parsing
This commit is contained in:
parent
484095576c
commit
adc6d41101
@ -43,6 +43,8 @@ int main(int argc, char** argv) {
|
|||||||
output += "\n" + rss_utils::rss_to_items(feed, opts);
|
output += "\n" + rss_utils::rss_to_items(feed, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "Item count: " << feed.getItemCount() << std::endl;
|
||||||
|
|
||||||
std::cout << output << std::endl;
|
std::cout << output << std::endl;
|
||||||
|
|
||||||
delete opts;
|
delete opts;
|
||||||
|
@ -93,13 +93,22 @@ option_flags* parse_options(int argc, char** argv) {
|
|||||||
current_item->description ^= 1;
|
current_item->description ^= 1;
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
ret->language ^= 1;
|
if(current_item == nullptr)
|
||||||
|
ret->language ^= 1;
|
||||||
|
else
|
||||||
|
std::cerr << "-e option not understood in context of --item; ignoring" << std::endl;
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
ret->webmaster ^= 1;
|
if(current_item == nullptr)
|
||||||
|
ret->webmaster ^= 1;
|
||||||
|
else
|
||||||
|
std::cerr << "-m option not understood in context of --item; ignoring" << std::endl;
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
ret->copyright ^= 1;
|
if(current_item == nullptr)
|
||||||
|
ret->copyright ^= 1;
|
||||||
|
else
|
||||||
|
std::cerr << "-c option not understood in context of --item; ignoring" << std::endl;
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
if(current_item == nullptr)
|
if(current_item == nullptr)
|
||||||
@ -108,19 +117,34 @@ option_flags* parse_options(int argc, char** argv) {
|
|||||||
current_item->pubdate ^= 1;
|
current_item->pubdate ^= 1;
|
||||||
break;
|
break;
|
||||||
case 'q':
|
case 'q':
|
||||||
ret->managingeditor ^= 1;
|
if(current_item == nullptr)
|
||||||
|
ret->managingeditor ^= 1;
|
||||||
|
else
|
||||||
|
std::cerr << "-q option not understood in context of --item; ignoring" << std::endl;
|
||||||
break;
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
ret->generator ^= 1;
|
if(current_item == nullptr)
|
||||||
|
ret->generator ^= 1;
|
||||||
|
else
|
||||||
|
std::cerr << "-g option not understood in context of --item; ignoring" << std::endl;
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
ret->docs ^= 1;
|
if(current_item == nullptr)
|
||||||
|
ret->docs ^= 1;
|
||||||
|
else
|
||||||
|
std::cerr << "-o option not understood in context of --item; ignoring" << std::endl;
|
||||||
break;
|
break;
|
||||||
case 'w':
|
case 'w':
|
||||||
ret->ttl ^= 1;
|
if(current_item == nullptr)
|
||||||
|
ret->ttl ^= 1;
|
||||||
|
else
|
||||||
|
std::cerr << "-w option not understood in context of --item; ignoring" << std::endl;
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
ret->builddate ^= 1;
|
if(current_item == nullptr)
|
||||||
|
ret->builddate ^= 1;
|
||||||
|
else
|
||||||
|
std::cerr << "-b option not understood in context of --item; ignoring" << std::endl;
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
if(ret->items == nullptr){
|
if(ret->items == nullptr){
|
||||||
|
68
src/rss.cpp
68
src/rss.cpp
@ -106,7 +106,7 @@ std::string rss::getTitle() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("title");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("title");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rss::getLink() const {
|
std::string rss::getLink() const {
|
||||||
@ -116,7 +116,7 @@ std::string rss::getLink() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("link");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("link");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rss::getDescription() const {
|
std::string rss::getDescription() const {
|
||||||
@ -126,7 +126,7 @@ std::string rss::getDescription() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("description");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("description");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rss::getLanguage() const {
|
std::string rss::getLanguage() const {
|
||||||
@ -136,7 +136,7 @@ std::string rss::getLanguage() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("language");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("language");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rss::getWebMaster() const {
|
std::string rss::getWebMaster() const {
|
||||||
@ -146,7 +146,7 @@ std::string rss::getWebMaster() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("webMaster");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("webMaster");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rss::getCopyright() const {
|
std::string rss::getCopyright() const {
|
||||||
@ -156,7 +156,7 @@ std::string rss::getCopyright() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("copyright");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("copyright");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rss::getPubDate() const {
|
std::string rss::getPubDate() const {
|
||||||
@ -166,7 +166,7 @@ std::string rss::getPubDate() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("pubDate");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("pubDate");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ std::string rss::getManagingEditor() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("managingEditor");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("managingEditor");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rss::getGenerator() const {
|
std::string rss::getGenerator() const {
|
||||||
@ -187,7 +187,7 @@ std::string rss::getGenerator() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("generator");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("generator");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rss::getDocs() const {
|
std::string rss::getDocs() const {
|
||||||
@ -197,7 +197,7 @@ std::string rss::getDocs() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("docs");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("docs");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rss::getTTL() const {
|
std::string rss::getTTL() const {
|
||||||
@ -207,7 +207,7 @@ std::string rss::getTTL() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("ttl");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("ttl");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,7 +218,7 @@ std::string rss::getLastBuildDate() const {
|
|||||||
rapidxml::xml_node<> *tmp = _item_node->first_node("lastBuildDate");
|
rapidxml::xml_node<> *tmp = _item_node->first_node("lastBuildDate");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
int rss::getItemCount() const {
|
int rss::getItemCount() const {
|
||||||
@ -324,11 +324,35 @@ bool rss::parse(const std::string& rss_str){
|
|||||||
return _ok;
|
return _ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string rss::cdata_to_string(const rapidxml::xml_node<>* node) const{
|
||||||
|
//this will dig till were past the cdata
|
||||||
|
std::cout << "in cdata" << std::endl;
|
||||||
|
const rapidxml::xml_node<>* tmp = node;
|
||||||
|
while(tmp->value()[0] == '\0'){ //if string is empty
|
||||||
|
tmp = tmp->first_node();
|
||||||
|
if(tmp == NULL)
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return tmp->value();
|
||||||
|
}
|
||||||
|
|
||||||
size_t rss_utils::write_to_string(void* ptr, size_t size, size_t nmemb, std::string* s){
|
size_t rss_utils::write_to_string(void* ptr, size_t size, size_t nmemb, std::string* s){
|
||||||
s->append(static_cast<char *>(ptr), size * nmemb);
|
s->append(static_cast<char *>(ptr), size * nmemb);
|
||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string item::cdata_to_string(const rapidxml::xml_node<>* node) const{
|
||||||
|
//this will dig till were past the cdata
|
||||||
|
std::cout << "in cdata" << std::endl;
|
||||||
|
const rapidxml::xml_node<>* tmp = node;
|
||||||
|
while(tmp->value()[0] == '\0'){ //if string is empty
|
||||||
|
tmp = tmp->first_node();
|
||||||
|
if(tmp == NULL)
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return tmp->value();
|
||||||
|
}
|
||||||
|
|
||||||
item::item() {}
|
item::item() {}
|
||||||
item::item(rapidxml::xml_node<>* node){
|
item::item(rapidxml::xml_node<>* node){
|
||||||
_item = node;
|
_item = node;
|
||||||
@ -356,42 +380,42 @@ std::string item::getTitle() const {
|
|||||||
rapidxml::xml_node<>* tmp = _item->first_node("title");
|
rapidxml::xml_node<>* tmp = _item->first_node("title");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string item::getLink() const {
|
std::string item::getLink() const {
|
||||||
rapidxml::xml_node<>* tmp = _item->first_node("link");
|
rapidxml::xml_node<>* tmp = _item->first_node("link");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string item::getDescription() const {
|
std::string item::getDescription() const {
|
||||||
rapidxml::xml_node<>* tmp = _item->first_node("description");
|
rapidxml::xml_node<>* tmp = _item->first_node("description");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string item::getAuthor() const {
|
std::string item::getAuthor() const {
|
||||||
rapidxml::xml_node<>* tmp = _item->first_node("author");
|
rapidxml::xml_node<>* tmp = _item->first_node("author");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string item::getCategory() const {
|
std::string item::getCategory() const {
|
||||||
rapidxml::xml_node<>* tmp = _item->first_node("category");
|
rapidxml::xml_node<>* tmp = _item->first_node("category");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string item::getComments() const {
|
std::string item::getComments() const {
|
||||||
rapidxml::xml_node<>* tmp = _item->first_node("comments");
|
rapidxml::xml_node<>* tmp = _item->first_node("comments");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -399,7 +423,7 @@ std::string item::getEnclosure() const {
|
|||||||
rapidxml::xml_node<>* tmp = _item->first_node("enclosure");
|
rapidxml::xml_node<>* tmp = _item->first_node("enclosure");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -407,20 +431,20 @@ std::string item::getGuid() const {
|
|||||||
rapidxml::xml_node<>* tmp = _item->first_node("guid");
|
rapidxml::xml_node<>* tmp = _item->first_node("guid");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string item::getPubDate() const {
|
std::string item::getPubDate() const {
|
||||||
rapidxml::xml_node<>* tmp = _item->first_node("pubDate");
|
rapidxml::xml_node<>* tmp = _item->first_node("pubDate");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string item::getSource() const {
|
std::string item::getSource() const {
|
||||||
rapidxml::xml_node<>* tmp = _item->first_node("source");
|
rapidxml::xml_node<>* tmp = _item->first_node("source");
|
||||||
if(tmp == 0)
|
if(tmp == 0)
|
||||||
return "";
|
return "";
|
||||||
return tmp->value();
|
return cdata_to_string(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -69,6 +70,7 @@ namespace rss_utils {
|
|||||||
private:
|
private:
|
||||||
bool parse(const std::string&);
|
bool parse(const std::string&);
|
||||||
std::vector<rapidxml::xml_node<>*> parseItems();
|
std::vector<rapidxml::xml_node<>*> parseItems();
|
||||||
|
std::string cdata_to_string(const rapidxml::xml_node<>*) const;
|
||||||
|
|
||||||
bool _ok;
|
bool _ok;
|
||||||
std::string _uri;
|
std::string _uri;
|
||||||
@ -99,6 +101,8 @@ namespace rss_utils {
|
|||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::string cdata_to_string(const rapidxml::xml_node<>*) const;
|
||||||
|
|
||||||
rapidxml::xml_node<> *_item;
|
rapidxml::xml_node<> *_item;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -49,6 +49,11 @@ std::string rss_utils::rss_to_items(const rss& rss_obj, const option_flags* flag
|
|||||||
int maxItem = rss_obj.getItemCount();
|
int maxItem = rss_obj.getItemCount();
|
||||||
std::string ret;
|
std::string ret;
|
||||||
|
|
||||||
|
if(maxItem == -1){
|
||||||
|
ret = "<No items in feed!>";
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
for(unsigned int i=0; i < flags->item_count; ++i, ++items){
|
for(unsigned int i=0; i < flags->item_count; ++i, ++items){
|
||||||
//if has a valid index
|
//if has a valid index
|
||||||
if(items->index >= 0 && items->index < maxItem){
|
if(items->index >= 0 && items->index < maxItem){
|
||||||
|
Loading…
Reference in New Issue
Block a user