Commit 8e2ffb56 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Overload << operator for classes

parent 700c04e7
......@@ -107,13 +107,13 @@ namespace belcard {
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();
friend ostream &operator<<(ostream &output, const BelCard &card) {
output << "BEGIN:VCARD\r\nVERSION:4.0\r\n";
for (auto it = card.getProperties().begin(); it != card.getProperties().end(); ++it) {
output << (**it);
}
vcard += "END:VCARD\r\n";
return vcard;
output << "END:VCARD\r\n";
return output;
}
};
}
......
......@@ -52,8 +52,9 @@ namespace belcard {
return _value;
}
virtual string toString() {
return _name + "=" + _value;
friend ostream &operator<<(ostream &output, const BelCardParam &param) {
output << param.getName() << "=" << param.getValue();
return output;
}
};
......@@ -100,16 +101,17 @@ namespace belcard {
return _params;
}
virtual string toString() {
string property;
if (_group.length() > 0)
property += _group + ".";
property += _name;
for (auto it = _params.begin(); it != _params.end(); ++it) {
property += ";" + (*it)->toString();
friend ostream &operator<<(ostream &output, const BelCardProperty &prop) {
if (prop.getGroup().length() > 0) {
output << prop.getGroup() << ".";
}
property += ":" + _value + "\r\n";
return property;
output << prop.getName();
for (auto it = prop.getParams().begin(); it != prop.getParams().end(); ++it) {
output << ";" << (**it);
}
output << ":" << prop.getValue() << "\r\n";
return output;
}
};
}
......
......@@ -100,9 +100,17 @@ namespace belcard {
BelCardProperty::addParam(param);
}
virtual string toString() {
setValue(_family_name + ";" + _given_name + ";" + _additional_name + ";" + _prefixes + ";" + _suffixes);
return BelCardProperty::toString();
friend ostream &operator<<(ostream &output, const BelCardN &n) {
if (n.getGroup().length() > 0) {
output << n.getGroup() << ".";
}
output << n.getName();
for (auto it = n.getParams().begin(); it != n.getParams().end(); ++it) {
output << ";" << (*it);
}
output << ":" << n.getFamilyName() + ";" + n.getGivenName() + ";" + n.getAdditionalName() + ";" + n.getPrefixes() + ";" + n.getSuffixes() << "\r\n";
return output;
}
};
......
......@@ -19,7 +19,6 @@ namespace belcard {
~BelCardParser();
shared_ptr<BelCard> parse(const string &input);
string dumpVCard(const shared_ptr<BelCard> &card);
string fold(string input);
string unfold(string input);
......
......@@ -34,11 +34,6 @@ shared_ptr<BelCard> BelCardParser::parse(const string &input) {
return belCard;
}
string BelCardParser::dumpVCard(const shared_ptr<BelCard> &card) {
string output = card->toString();
return fold(output);
}
string BelCardParser::fold(string input) {
size_t crlf = 0;
size_t next_crlf = 0;
......
......@@ -28,8 +28,7 @@ int main(int argc, char *argv[]) {
shared_ptr<BelCard> belCard = parser->parse(vcard);
if (belCard) {
string outputVCard = belCard->toString();
cout << outputVCard << endl;
cout << (*belCard) << endl;
} else {
cerr << "Error: returned pointer is null" << endl;
}
......
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