Commit 21a1e530 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Updated belcard parser to support groups in properties and property N

parent 2feaec09
......@@ -18,6 +18,7 @@ namespace belcard {
class BelCardProperty : public BelCardGeneric {
protected:
string _group;
string _name;
string _value;
public:
......@@ -29,19 +30,30 @@ namespace belcard {
}
virtual void setName(const string & name) {
virtual void setGroup(const string &group) {
_group = group;
}
virtual const string &getGroup() const {
return _group;
}
virtual void setName(const string &name) {
_name = name;
}
virtual const string & getName() const {
virtual const string &getName() const {
return _name;
}
virtual void setValue(const string & value) {
virtual void setValue(const string &value) {
_value = value;
}
virtual const string & getValue() const {
virtual const string &getValue() const {
return _value;
}
virtual string toString() {
return (_group.length() > 0 ? _group + "." : "") + _name + ":" + _value + "\r\n";
}
};
class BelCardFN : public BelCardProperty {
......@@ -55,9 +67,27 @@ namespace belcard {
}
};
class BelCardN : public BelCardProperty {
private:
string _family_name;
string _given_name;
string _additional_name;
string _honorific_prefixes;
string _honorific_suffixes;
public:
static shared_ptr<BelCardN> create() {
return make_shared<BelCardN>();
}
BelCardN() : BelCardProperty() {
setName("N");
}
};
class BelCard : public BelCardGeneric {
private:
shared_ptr<BelCardFN> _fn;
shared_ptr<BelCardN> _n;
list<shared_ptr<BelCardProperty>> _properties;
public:
......@@ -77,12 +107,29 @@ namespace belcard {
return _fn;
}
void setN(const shared_ptr<BelCardN> &n) {
_n = n;
addProperty(_n);
}
const shared_ptr<BelCardN> &getN() const {
return _n;
}
void addProperty(const shared_ptr<BelCardProperty> &property) {
_properties.push_back(property);
}
const list<shared_ptr<BelCardProperty>> &getProperties() const {
return _properties;
}
string toString() {
string vcard = "BEGIN:VCARD\r\nVERSION:4.0\r\n";
for (auto it = _properties.begin(); it != _properties.end(); ++it) {
vcard += (*it)->toString();
}
vcard += "END:VCARD\r\n";
return vcard;
}
};
}
......
#include "belcard/belcard.hpp"
#include <belr/grammarbuilder.hh>
#include <belr/abnf.hh>
#include <belr/parser-impl.cc>
using namespace::std;
using namespace::belr;
namespace belcard {
......
......@@ -21,23 +21,35 @@ int main(int argc, char *argv[]) {
Parser<shared_ptr<BelCardGeneric>> parser(grammar);
parser.setHandler("vcard", make_fn(&BelCard::create))
->setCollector("FN", make_sfn(&BelCard::setFN));
->setCollector("FN", make_sfn(&BelCard::setFN))
->setCollector("N", make_sfn(&BelCard::setN));
parser.setHandler("FN", make_fn(&BelCardFN::create))
->setCollector("group", make_sfn(&BelCardFN::setGroup))
->setCollector("FN-value", make_sfn(&BelCardFN::setValue));
parser.setHandler("N", make_fn(&BelCardN::create))
->setCollector("group", make_sfn(&BelCardN::setGroup))
->setCollector("N-value", make_sfn(&BelCardN::setValue));
ifstream istr("vcardtest.vcf");
if (!istr.is_open()) {
return -1;
}
stringstream vcard;
vcard << istr.rdbuf();
stringstream vcardStream;
vcardStream << istr.rdbuf();
string vcard = vcardStream.str();
size_t parsedSize = 0;
shared_ptr<BelCardGeneric> ret = parser.parseInput("vcard", vcard.str(), &parsedSize);
shared_ptr<BelCardGeneric> ret = parser.parseInput("vcard", vcard, &parsedSize);
shared_ptr<BelCard> belCard = dynamic_pointer_cast<BelCard>(ret);
cout << "FN is " << belCard->getFN()->getValue() << endl;
if (belCard) {
string outputVCard = belCard->toString();
cout << outputVCard << endl;
} else {
cerr << "Error: returned pointer is null" << endl;
}
return 0;
}
\ No newline at end of file
......@@ -5,12 +5,18 @@ vcard = "BEGIN:VCARD" CRLF
1*property
"END:VCARD" CRLF
property = FN
property = (FN / N) CRLF
FN = "FN" *(";" FN-param) ":" FN-value CRLF
FN = [group "."] "FN" *(";" FN-param) ":" FN-value
FN-param = any-param
FN-value = text
N = [group "."] "N" *(";" N-param) ":" N-value
N-param = any-param
N-value = list-component 4(";" list-component)
group = 1*(ALPHA / DIGIT / "-")
any-param = (iana-token / x-name) "=" param-value *("," param-value)
param-value = *SAFE-CHAR / DQUOTE *QSAFE-CHAR DQUOTE
......@@ -19,6 +25,10 @@ iana-token = 1*(ALPHA / DIGIT / "-")
x-name = "x-" 1*(ALPHA / DIGIT / "-")
COMPONENT-CHAR = "\\" / "\," / "\;" / "\n" / WSP / NON-ASCII / %x21-2B / %x2D-3A / %x3C-5B / %x5D-7E
component = *COMPONENT-CHAR
list-component = component *("," component)
text = *TEXT-CHAR
TEXT-CHAR = "\\" / "\," / "\n" / WSP / NON-ASCII / %x21-2B / %x2D-5B / %x5D-7E
NON-ASCII = UTF8-2 / UTF8-3 / UTF8-4
......
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