Commit 88214df2 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Added params and nickname property support

parent 21a1e530
......@@ -5,6 +5,7 @@
#include <list>
#include <map>
#include <memory>
#include <vector>
using namespace::std;
......@@ -16,17 +17,50 @@ namespace belcard {
virtual ~BelCardGeneric() { } //put a virtual destructor to enable polymorphism and dynamic casting.
};
class BelCardParam : public BelCardGeneric {
private:
string _name;
string _value;
public:
static shared_ptr<BelCardParam> create() {
return make_shared<BelCardParam>();
}
BelCardParam() : BelCardGeneric() {
}
virtual void setName(const string &name) {
_name = name;
}
virtual const string &getName() const {
return _name;
}
virtual void setValue(const string &value) {
_value = value;
}
virtual const string &getValue() const {
return _value;
}
virtual string toString() {
return _name + "=" + _value;
}
};
class BelCardProperty : public BelCardGeneric {
protected:
string _group;
string _name;
string _value;
list<shared_ptr<BelCardParam>> _params;
public:
static shared_ptr<BelCardProperty> create() {
return make_shared<BelCardProperty>();
}
BelCardProperty() {
BelCardProperty() : BelCardGeneric() {
}
......@@ -51,8 +85,23 @@ namespace belcard {
return _value;
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
_params.push_back(param);
}
virtual const list<shared_ptr<BelCardParam>> &getParams() const {
return _params;
}
virtual string toString() {
return (_group.length() > 0 ? _group + "." : "") + _name + ":" + _value + "\r\n";
string property;
if (_group.length() > 0)
property += _group + ".";
property += _name;
for (auto it = _params.begin(); it != _params.end(); ++it) {
property += ";" + (*it)->toString();
}
property += ":" + _value + "\r\n";
return property;
}
};
......@@ -65,6 +114,10 @@ namespace belcard {
BelCardFN() : BelCardProperty() {
setName("FN");
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
};
class BelCardN : public BelCardProperty {
......@@ -72,8 +125,8 @@ namespace belcard {
string _family_name;
string _given_name;
string _additional_name;
string _honorific_prefixes;
string _honorific_suffixes;
string _prefixes;
string _suffixes;
public:
static shared_ptr<BelCardN> create() {
return make_shared<BelCardN>();
......@@ -82,12 +135,72 @@ namespace belcard {
BelCardN() : BelCardProperty() {
setName("N");
}
virtual void setFamilyName(const string &value) {
_family_name = value;
setValue(_family_name + ";" + _given_name + ";" + _additional_name + ";" + _prefixes + ";" + _suffixes);
}
virtual const string &getFamilyName() const {
return _family_name;
}
virtual void setGivenName(const string &value) {
_given_name = value;
setValue(_family_name + ";" + _given_name + ";" + _additional_name + ";" + _prefixes + ";" + _suffixes);
}
virtual const string &getGivenName() const {
return _given_name;
}
virtual void setAdditionalName(const string &value) {
_additional_name = value;
setValue(_family_name + ";" + _given_name + ";" + _additional_name + ";" + _prefixes + ";" + _suffixes);
}
virtual const string &getAdditionalName() const {
return _additional_name;
}
virtual void setPrefixes(const string &value) {
_prefixes = value;
setValue(_family_name + ";" + _given_name + ";" + _additional_name + ";" + _prefixes + ";" + _suffixes);
}
virtual const string &getPrefixes() const {
return _prefixes;
}
virtual void setSuffixes(const string &value) {
_suffixes = value;
setValue(_family_name + ";" + _given_name + ";" + _additional_name + ";" + _prefixes + ";" + _suffixes);
}
virtual const string &getSuffixes() const {
return _suffixes;
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
};
class BelCardNickname : public BelCardProperty {
public:
static shared_ptr<BelCardNickname> create() {
return make_shared<BelCardNickname>();
}
BelCardNickname() : BelCardProperty() {
setName("NICKNAME");
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
};
class BelCard : public BelCardGeneric {
private:
shared_ptr<BelCardFN> _fn;
shared_ptr<BelCardN> _n;
list<shared_ptr<BelCardNickname>> _nicknames;
list<shared_ptr<BelCardProperty>> _properties;
public:
......@@ -115,6 +228,14 @@ namespace belcard {
return _n;
}
void addNickname(const shared_ptr<BelCardNickname> &nickname) {
_nicknames.push_back(nickname);
addProperty(nickname);
}
const list<shared_ptr<BelCardNickname>> &getNicknames() const {
return _nicknames;
}
void addProperty(const shared_ptr<BelCardProperty> &property) {
_properties.push_back(property);
}
......
......@@ -22,15 +22,31 @@ 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("N", make_sfn(&BelCard::setN));
->setCollector("N", make_sfn(&BelCard::setN))
->setCollector("NICKNAME", make_sfn(&BelCard::addNickname));
parser.setHandler("any-param", make_fn(&BelCardParam::create))
->setCollector("param-name", make_sfn(&BelCardParam::setName))
->setCollector("param-value", make_sfn(&BelCardParam::setValue));
parser.setHandler("FN", make_fn(&BelCardFN::create))
->setCollector("group", make_sfn(&BelCardFN::setGroup))
->setCollector("any-param", make_sfn(&BelCardFN::addParam))
->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));
->setCollector("any-param", make_sfn(&BelCardN::addParam))
->setCollector("N-fn", make_sfn(&BelCardN::setFamilyName))
->setCollector("N-gn", make_sfn(&BelCardN::setGivenName))
->setCollector("N-an", make_sfn(&BelCardN::setAdditionalName))
->setCollector("N-prefixes", make_sfn(&BelCardN::setPrefixes))
->setCollector("N-suffixes", make_sfn(&BelCardN::setSuffixes));
parser.setHandler("NICKNAME", make_fn(&BelCardNickname::create))
->setCollector("group", make_sfn(&BelCardNickname::setGroup))
->setCollector("any-param", make_sfn(&BelCardNickname::addParam))
->setCollector("NICKNAME-value", make_sfn(&BelCardNickname::setValue));
ifstream istr("vcardtest.vcf");
if (!istr.is_open()) {
......
......@@ -5,21 +5,28 @@ vcard = "BEGIN:VCARD" CRLF
1*property
"END:VCARD" CRLF
property = (FN / N) CRLF
property = (FN / N / NICKNAME) CRLF
FN = [group "."] "FN" *(";" FN-param) ":" FN-value
FN-param = any-param
FN = [group "."] "FN" *(";" any-param) ":" FN-value
FN-value = text
N = [group "."] "N" *(";" N-param) ":" N-value
N-param = any-param
N-value = list-component 4(";" list-component)
N = [group "."] "N" *(";" any-param) ":" N-value
N-value = N-fn ";" N-gn ";" N-an ";" N-prefixes ";" N-suffixes
N-fn = component *("," component)
N-gn = component *("," component)
N-an = component *("," component)
N-prefixes = component *("," component)
N-suffixes = component *("," component)
group = 1*(ALPHA / DIGIT / "-")
NICKNAME = [group "."] "NICKNAME" *(";" any-param) ":" NICKNAME-value
NICKNAME-value = text-list
any-param = (iana-token / x-name) "=" param-value *("," param-value)
group = 1*(ALPHA / DIGIT / "-")
param-value = *SAFE-CHAR / DQUOTE *QSAFE-CHAR DQUOTE
any-param = param-name "=" param-value
param-name = (iana-token / x-name)
param-value = param-value-component *("," param-value-component)
param-value-component = *SAFE-CHAR / DQUOTE *QSAFE-CHAR DQUOTE
iana-token = 1*(ALPHA / DIGIT / "-")
......@@ -29,6 +36,7 @@ COMPONENT-CHAR = "\\" / "\," / "\;" / "\n" / WSP / NON-ASCII / %x21-2B / %x2D-3A
component = *COMPONENT-CHAR
list-component = component *("," component)
text-list = text *("," text)
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