Commit dfe66cbb authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Added possibility to parse multiple vcards

parent ef16fe85
......@@ -21,7 +21,7 @@
using namespace::std;
using namespace::belr;
namespace belcard {
namespace belcard {
class BelCard : public BelCardGeneric {
private:
shared_ptr<BelCardKind> _kind;
......@@ -196,6 +196,21 @@ namespace belcard {
virtual void serialize(ostream &output) const;
};
class BelCardList : public BelCardGeneric {
private:
list<shared_ptr<BelCard>> _vCards;
public:
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser);
BelCardList();
void addCard(const shared_ptr<BelCard> &vcard);
const list<shared_ptr<BelCard>> &getCards() const;
void serialize(ostream &output) const;
};
}
#endif
\ No newline at end of file
......@@ -15,11 +15,14 @@ namespace belcard {
ABNFGrammarBuilder _grammar_builder;
shared_ptr<Grammar> _grammar;
shared_ptr<BelCardGeneric> _parse(const string &input, const string &rule);
public:
BelCardParser();
~BelCardParser();
shared_ptr<BelCard> parse(const string &input);
shared_ptr<BelCard> parseOne(const string &input);
shared_ptr<BelCardList> parse(const string &input);
};
}
......
......@@ -49,9 +49,7 @@ void BelCard::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser
}
BelCard::BelCard() : BelCardGeneric() {
shared_ptr<BelCardProductId> prodid = BelCardGeneric::create<BelCardProductId>();
prodid->setValue("-//LINPHONE//BELCARD VERSION 1//EN");
setProductId(prodid);
}
void BelCard::setKind(const shared_ptr<BelCardKind> &kind) {
......@@ -421,7 +419,7 @@ void BelCard::serialize(ostream& output) const {
}
const string BelCard::toFoldedString() const {
string vcard = this->toString();
string vcard = toString();
return belcard_fold(vcard);
}
......@@ -431,4 +429,27 @@ const bool BelCard::assertRFCCompliance() const {
}
return true;
}
void BelCardList::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("vcard-list", make_fn(BelCardGeneric::create<BelCardList>))
->setCollector("vcard", make_sfn(&BelCardList::addCard));
}
BelCardList::BelCardList() : BelCardGeneric() {
}
void BelCardList::addCard(const shared_ptr<BelCard> &vcard) {
_vCards.push_back(vcard);
}
const list<shared_ptr<BelCard>> &BelCardList::getCards() const {
return _vCards;
}
void BelCardList::serialize(ostream &output) const {
for (auto it = getCards().begin(); it != getCards().end(); ++it) {
output << (*it)->toFoldedString();
}
}
\ No newline at end of file
......@@ -14,11 +14,10 @@ BelCardParser::~BelCardParser() {
}
shared_ptr<BelCard> BelCardParser::parse(const string &input) {
string vcard = belcard_unfold(input);
shared_ptr<BelCardGeneric> BelCardParser::_parse(const string &input, const string &rule) {
Parser<shared_ptr<BelCardGeneric>> parser(_grammar);
BelCardList::setHandlerAndCollectors(&parser);
BelCard::setHandlerAndCollectors(&parser);
BelCardParam::setAllParamsHandlersAndCollectors(&parser);
BelCardProperty::setHandlerAndCollectors(&parser);
......@@ -72,7 +71,20 @@ shared_ptr<BelCard> BelCardParser::parse(const string &input) {
BelCardDeathDate::setHandlerAndCollectors(&parser);
size_t parsedSize = 0;
shared_ptr<BelCardGeneric> ret = parser.parseInput("vcard", vcard, &parsedSize);
shared_ptr<BelCardGeneric> ret = parser.parseInput(rule, input, &parsedSize);
return ret;
}
shared_ptr<BelCard> BelCardParser::parseOne(const string &input) {
string vcard = belcard_unfold(input);
shared_ptr<BelCardGeneric> ret = _parse(vcard, "vcard");
shared_ptr<BelCard> belCard = dynamic_pointer_cast<BelCard>(ret);
return belCard;
}
shared_ptr<BelCardList> BelCardParser::parse(const string &input) {
string vcards = belcard_unfold(input);
shared_ptr<BelCardGeneric> ret = _parse(vcards, "vcard-list");
shared_ptr<BelCardList> belCards = dynamic_pointer_cast<BelCardList>(ret);
return belCards;
}
\ No newline at end of file
vcard-entity = 1*vcard
vcard-list = vcard *vcard
vcard = "BEGIN:VCARD" CRLF
"VERSION:4.0" CRLF
......
......@@ -27,8 +27,8 @@ int main(int argc, char *argv[]) {
string vcard = vcardStream.str();
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard = parser->parse(vcard);
cout << belCard->toFoldedString() << endl;
shared_ptr<BelCardList> belCards = parser->parse(vcard);
cout << *belCards << endl;
delete parser;
return 0;
......
......@@ -50,9 +50,8 @@ static void vcard_parsing(void) {
string vcard = openFile("vcards/vcard.vcf");
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard = parser->parse(vcard);
BC_ASSERT_TRUE(belCard != NULL);
shared_ptr<BelCard> belCard = parser->parseOne(vcard);
BC_ASSERT_TRUE_FATAL(belCard != NULL);
BC_ASSERT_TRUE(belCard->assertRFCCompliance());
string vcard2 = belCard->toFoldedString();
......@@ -61,6 +60,20 @@ static void vcard_parsing(void) {
delete parser;
}
static void vcards_parsing(void) {
string vcards = openFile("vcards/vcards.vcf");
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCardList> belCards = parser->parse(vcards);
BC_ASSERT_TRUE_FATAL(belCards != NULL);
BC_ASSERT_TRUE(belCards->getCards().size() == 2);
string vcards2 = belCards->toString();
BC_ASSERT_EQUAL(vcards2.compare(vcards), 0, int, "%d");
delete parser;
}
static void create_vcard_from_api(void) {
shared_ptr<BelCard> belCard = BelCard::create<BelCard>();
BC_ASSERT_TRUE_FATAL(belCard != NULL);
......@@ -73,8 +86,9 @@ static void create_vcard_from_api(void) {
string vcard = belCard->toString();
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard2 = parser->parse(vcard);
shared_ptr<BelCard> belCard2 = parser->parseOne(vcard);
BC_ASSERT_TRUE_FATAL(belCard2 != NULL);
BC_ASSERT_TRUE(belCard2->assertRFCCompliance());
string vcard2 = belCard2->toString();
BC_ASSERT_TRUE(vcard.compare(vcard2) == 0);
}
......@@ -83,6 +97,7 @@ static test_t tests[] = {
{ "Folding", folding },
{ "Unfolding", unfolding },
{ "VCard parsing", vcard_parsing },
{ "VCards parsing", vcards_parsing },
{ "VCard created from scratch", create_vcard_from_api },
};
......
This diff was suppressed by a .gitattributes entry.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment