Commit 985a53ef authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Fixed and improved serialization

parent 716f427b
......@@ -62,15 +62,9 @@ namespace belcard {
list<shared_ptr<BelCardProperty>> _properties;
public:
static shared_ptr<BelCard> create();
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser);
BelCard();
friend ostream &operator<<(ostream &output, const BelCard &card) {
output << card.serialize();
return output;
}
void setKind(const shared_ptr<BelCardKind> &kind);
const shared_ptr<BelCardKind> &getKind() const;
......@@ -183,8 +177,8 @@ namespace belcard {
void addProperty(const shared_ptr<BelCardProperty> &property);
const list<shared_ptr<BelCardProperty>> &getProperties() const;
string serialize() const;
};
virtual void serialize(ostream &output) const;
};
}
#endif
\ No newline at end of file
......@@ -53,21 +53,7 @@ namespace belcard {
void setLabelParam(const shared_ptr<BelCardLabelParam> &param);
const shared_ptr<BelCardLabelParam> &getLabelParam() const;
friend ostream &operator<<(ostream &output, const BelCardAddress &addr) {
if (addr.getGroup().length() > 0) {
output << addr.getGroup() << ".";
}
output << addr.getName();
for (auto it = addr.getParams().begin(); it != addr.getParams().end(); ++it) {
output << ";" << (**it);
}
output << ":" << addr.getPostOfficeBox() << ";" << addr.getExtendedAddress()
<< ";" << addr.getStreet() << ";" << addr.getLocality() << ";" << addr.getRegion()
<< ";" << addr.getPostalCode() << ";" << addr.getCountry() << "\r\n";
return output;
}
virtual void serialize(ostream &output) const;
};
}
......
......@@ -4,6 +4,7 @@
#include "belcard/vcard_grammar.hpp"
#include <memory>
#include <sstream>
using namespace::std;
......@@ -16,8 +17,20 @@ namespace belcard {
}
BelCardGeneric() { }
virtual ~BelCardGeneric() { } // A virtual destructor enables polymorphism and dynamic casting.
virtual ~BelCardGeneric() { } //put a virtual destructor to enable polymorphism and dynamic casting.
virtual void serialize(ostream &output) const = 0; // Force heriting classes to define this
friend ostream &operator<<(ostream &output, const BelCardGeneric &me) {
me.serialize(output);
return output;
}
virtual string toString() const {
stringstream output;
output << *this;
return output.str();
}
};
}
......
......@@ -50,19 +50,7 @@ namespace belcard {
void setSuffixes(const string &value);
const string &getSuffixes() const;
friend ostream &operator<<(ostream &output, const BelCardName &name) {
if (name.getGroup().length() > 0) {
output << name.getGroup() << ".";
}
output << name.getName();
for (auto it = name.getParams().begin(); it != name.getParams().end(); ++it) {
output << ";" << (**it);
}
output << ":" << name.getFamilyName() + ";" + name.getGivenName() + ";" + name.getAdditionalName() + ";" + name.getPrefixes() + ";" + name.getSuffixes() << "\r\n";
return output;
}
virtual void serialize(ostream &output) const;
};
class BelCardNickname : public BelCardProperty {
......
......@@ -32,12 +32,7 @@ namespace belcard {
virtual void setValue(const string &value) ;
virtual const string &getValue() const;
virtual string serialize() const;
friend ostream &operator<<(ostream &output, const BelCardParam &param) {
output << param.getName() << "=" << param.getValue();;
return output;
}
virtual void serialize(ostream &output) const;
};
class BelCardLanguageParam : public BelCardParam {
......
......@@ -94,13 +94,8 @@ namespace belcard {
virtual void addParam(const shared_ptr<BelCardParam> &param);
virtual const list<shared_ptr<BelCardParam>> &getParams() const;
virtual string serialize() const;
friend ostream &operator<<(ostream &output, const BelCardProperty &prop) {
output << prop.serialize();
return output;
}
virtual void serialize(ostream &output) const;
};
}
#endif
\ No newline at end of file
......@@ -4,12 +4,8 @@ using namespace::std;
using namespace::belr;
using namespace::belcard;
shared_ptr<BelCard> BelCard::create() {
return make_shared<BelCard>();
}
void BelCard::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("vcard", make_fn(&BelCard::create))
parser->setHandler("vcard", make_fn(BelCardGeneric::create<BelCard>))
->setCollector("X-PROPERTY", make_sfn(&BelCard::addExtendedProperty))
->setCollector("SOURCE", make_sfn(&BelCard::addSource))
->setCollector("KIND", make_sfn(&BelCard::setKind))
......@@ -48,7 +44,7 @@ void BelCard::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser
->setCollector("CALURI", make_sfn(&BelCard::addCALURI));
}
BelCard::BelCard() {
BelCard::BelCard() : BelCardGeneric() {
}
......@@ -347,14 +343,10 @@ const list<shared_ptr<BelCardProperty>> &BelCard::getProperties() const {
return _properties;
}
string BelCard::serialize() const {
stringstream output;
void BelCard::serialize(ostream& output) const {
output << "BEGIN:VCARD\r\nVERSION:4.0\r\n";
for (auto it = getProperties().begin(); it != getProperties().end(); ++it) {
output << (**it);
}
output << "END:VCARD\r\n";
return output.str();
}
\ No newline at end of file
......@@ -89,4 +89,18 @@ void BelCardAddress::setLabelParam(const shared_ptr<BelCardLabelParam> &param) {
}
const shared_ptr<BelCardLabelParam> &BelCardAddress::getLabelParam() const {
return _label_param;
}
void BelCardAddress::serialize(ostream& output) const {
if (getGroup().length() > 0) {
output << getGroup() << ".";
}
output << getName();
for (auto it = getParams().begin(); it != getParams().end(); ++it) {
output << ";" << (**it);
}
output << ":" << getPostOfficeBox() << ";" << getExtendedAddress()
<< ";" << getStreet() << ";" << getLocality() << ";" << getRegion()
<< ";" << getPostalCode() << ";" << getCountry() << "\r\n";
}
\ No newline at end of file
......@@ -83,6 +83,18 @@ const string &BelCardName::getSuffixes() const {
return _suffixes;
}
void BelCardName::serialize(ostream& output) const {
if (getGroup().length() > 0) {
output << getGroup() << ".";
}
output << getName();
for (auto it = getParams().begin(); it != getParams().end(); ++it) {
output << ";" << (**it);
}
output << ":" << getFamilyName() + ";" + getGivenName() + ";" + getAdditionalName() + ";" + getPrefixes() + ";" + getSuffixes() << "\r\n";
}
shared_ptr<BelCardNickname> BelCardNickname::parse(const string& input) {
return BelCardProperty::parseProperty<BelCardNickname>("NICKNAME", input);
}
......
......@@ -44,6 +44,10 @@ BelCardParam::BelCardParam() : BelCardGeneric() {
}
void BelCardParam::serialize(ostream& output) const {
output << getName() << "=" << getValue();
}
void BelCardParam::setName(const string &name) {
_name = name;
}
......@@ -58,12 +62,6 @@ const string &BelCardParam::getValue() const {
return _value;
}
string BelCardParam::serialize() const {
stringstream output;
output << *this;
return output.str();
}
shared_ptr<BelCardLanguageParam> BelCardLanguageParam::parse(const string& input) {
return BelCardParam::parseParam<BelCardLanguageParam>("LANGUAGE-param", input);
}
......
......@@ -136,9 +136,7 @@ const list<shared_ptr<BelCardParam>> &BelCardProperty::getParams() const {
return _params;
}
string BelCardProperty::serialize() const {
stringstream output;
void BelCardProperty::serialize(ostream& output) const {
if (getGroup().length() > 0) {
output << getGroup() << ".";
}
......@@ -148,6 +146,4 @@ string BelCardProperty::serialize() const {
output << ";" << (**it);
}
output << ":" << getValue() << "\r\n";
return output.str();
}
\ No newline at end of file
......@@ -4,6 +4,7 @@
#include <fstream>
#include <sstream>
using namespace::std;
using namespace::belr;
using namespace::belcard;
......@@ -27,7 +28,7 @@ int main(int argc, char *argv[]) {
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard = parser->parse(vcard);
cout << parser->fold(belCard->serialize()) << endl;
cout << parser->fold(belCard->toString()) << endl;
delete parser;
return 0;
......
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