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

Remove unique properties when trying to set a new one if needed + moved...

Remove unique properties when trying to set a new one if needed + moved fold/unfold to belcard_utils file
parent cae6b30f
......@@ -14,6 +14,7 @@ belcard_include_HEADERS = belcard.hpp \
belcard_security.hpp \
belcard_calendar.hpp \
belcard_parser.hpp \
vcard_grammar.hpp
vcard_grammar.hpp \
belcard_utils.hpp
EXTRA_DIST=$(belcard_include_HEADERS)
......@@ -176,6 +176,10 @@ namespace belcard {
void addProperty(const shared_ptr<BelCardProperty> &property);
const list<shared_ptr<BelCardProperty>> &getProperties() const;
void removeProperty(const shared_ptr<BelCardProperty> &property);
const string toFoldedString();
const bool assertRFCCompliance();
virtual void serialize(ostream &output) const;
};
......
......@@ -20,9 +20,6 @@ namespace belcard {
~BelCardParser();
shared_ptr<BelCard> parse(const string &input);
string fold(string input);
string unfold(string input);
};
}
......
#ifndef belcard_utils_hpp
#define belcard_utils_hpp
#include <string>
using namespace::std;
string belcard_fold(string input);
string belcard_unfold(string input);
#endif
\ No newline at end of file
......@@ -18,7 +18,8 @@ libbelcard_la_SOURCES=belcard_parser.cpp \
belcard_explanatory.cpp \
belcard_security.cpp \
belcard_calendar.cpp \
vcard_grammar.cpp
vcard_grammar.cpp \
belcard_utils.cpp
AM_CPPFLAGS=-I$(top_srcdir)/include
#include "belcard/belcard.hpp"
#include "belcard/belcard_utils.hpp"
using namespace::std;
using namespace::belr;
......@@ -45,10 +46,15 @@ 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) {
if (_kind) {
removeProperty(_kind);
}
_kind = kind;
addProperty(_kind);
}
......@@ -57,6 +63,9 @@ const shared_ptr<BelCardKind> &BelCard::getKind() const {
}
void BelCard::setFullName(const shared_ptr<BelCardFullName> &fn) {
if (_fn) {
removeProperty(_fn);
}
_fn = fn;
addProperty(_fn);
}
......@@ -65,6 +74,9 @@ const shared_ptr<BelCardFullName> &BelCard::getFullName() const {
}
void BelCard::setName(const shared_ptr<BelCardName> &n) {
if (_n) {
removeProperty(_n);
}
_n = n;
addProperty(_n);
}
......@@ -73,6 +85,9 @@ const shared_ptr<BelCardName> &BelCard::getName() const {
}
void BelCard::setBirthday(const shared_ptr<BelCardBirthday> &bday) {
if (_bday) {
removeProperty(_bday);
}
_bday = bday;
addProperty(_bday);
}
......@@ -81,6 +96,9 @@ const shared_ptr<BelCardBirthday> &BelCard::getBirthday() const {
}
void BelCard::setAnniversary(const shared_ptr<BelCardAnniversary> &anniversary) {
if (_anniversary) {
removeProperty(_anniversary);
}
_anniversary = anniversary;
addProperty(_anniversary);
}
......@@ -89,6 +107,9 @@ const shared_ptr<BelCardAnniversary> &BelCard::getAnniversary() const {
}
void BelCard::setGender(const shared_ptr<BelCardGender> &gender) {
if (_gender) {
removeProperty(_gender);
}
_gender = gender;
addProperty(_gender);
}
......@@ -97,6 +118,9 @@ const shared_ptr<BelCardGender> &BelCard::getGender() const {
}
void BelCard::setProductId(const shared_ptr<BelCardProductId> &pid) {
if (_pid) {
removeProperty(_pid);
}
_pid = pid;
addProperty(_pid);
}
......@@ -105,6 +129,9 @@ const shared_ptr<BelCardProductId> &BelCard::getProductId() const {
}
void BelCard::setRevision(const shared_ptr<BelCardRevision> &rev) {
if (_rev) {
removeProperty(_rev);
}
_rev = rev;
addProperty(_rev);
}
......@@ -113,6 +140,9 @@ const shared_ptr<BelCardRevision> &BelCard::getRevision() const {
}
void BelCard::setUniqueId(const shared_ptr<BelCardUniqueId> &uid) {
if (_uid) {
removeProperty(_uid);
}
_uid = uid;
addProperty(_uid);
}
......@@ -342,6 +372,9 @@ void BelCard::addProperty(const shared_ptr<BelCardProperty> &property) {
const list<shared_ptr<BelCardProperty>> &BelCard::getProperties() const {
return _properties;
}
void BelCard::removeProperty(const shared_ptr<BelCardProperty> &property) {
_properties.remove(property);
}
void BelCard::serialize(ostream& output) const {
output << "BEGIN:VCARD\r\nVERSION:4.0\r\n";
......@@ -349,4 +382,18 @@ void BelCard::serialize(ostream& output) const {
output << (**it);
}
output << "END:VCARD\r\n";
}
const string BelCard::toFoldedString() {
string vcard = this->toString();
return belcard_fold(vcard);
}
const bool BelCard::assertRFCCompliance() {
if (!_fn) {
cerr << "FN is not set" << endl;
return false;
}
return true;
}
\ No newline at end of file
#include "belcard/belcard_parser.hpp"
#include "belcard/belcard.hpp"
#include "belcard/belcard_utils.hpp"
using namespace::std;
using namespace::belr;
......@@ -14,7 +15,7 @@ BelCardParser::~BelCardParser() {
}
shared_ptr<BelCard> BelCardParser::parse(const string &input) {
string vcard = unfold(input);
string vcard = belcard_unfold(input);
Parser<shared_ptr<BelCardGeneric>> parser(_grammar);
......@@ -70,39 +71,4 @@ shared_ptr<BelCard> BelCardParser::parse(const string &input) {
shared_ptr<BelCardGeneric> ret = parser.parseInput("vcard", vcard, &parsedSize);
shared_ptr<BelCard> belCard = dynamic_pointer_cast<BelCard>(ret);
return belCard;
}
string BelCardParser::fold(string input) {
size_t crlf = 0;
size_t next_crlf = 0;
while (next_crlf != string::npos) {
next_crlf = input.find("\r\n", crlf);
if (next_crlf != string::npos) {
if (next_crlf - crlf > 75) {
input.insert(crlf + 74, "\r\n ");
crlf += 76;
} else {
crlf = next_crlf + 2;
}
}
}
return input;
}
string BelCardParser::unfold(string input) {
size_t crlf = input.find("\r\n");
while (crlf != string::npos) {
if (isspace(input[crlf + 2])) {
input.erase(crlf, 3);
} else {
crlf += 2;
}
crlf = input.find("\r\n", crlf);
}
return input;
}
\ No newline at end of file
#include "belcard/belcard_utils.hpp"
using namespace::std;
string belcard_fold(string input) {
size_t crlf = 0;
size_t next_crlf = 0;
while (next_crlf != string::npos) {
next_crlf = input.find("\r\n", crlf);
if (next_crlf != string::npos) {
if (next_crlf - crlf > 75) {
input.insert(crlf + 74, "\r\n ");
crlf += 76;
} else {
crlf = next_crlf + 2;
}
}
}
return input;
}
string belcard_unfold(string input) {
size_t crlf = input.find("\r\n");
while (crlf != string::npos) {
if (isspace(input[crlf + 2])) {
input.erase(crlf, 3);
} else {
crlf += 2;
}
crlf = input.find("\r\n", crlf);
}
return input;
}
\ No newline at end of file
#include "belcard/belcard_parser.hpp"
#include "belcard/belcard_utils.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace::belr;
using namespace::belcard;
int main(int argc, char *argv[]) {
const char *file = NULL;
......@@ -25,10 +22,8 @@ int main(int argc, char *argv[]) {
vcardStream << istr.rdbuf();
string vcard = vcardStream.str();
BelCardParser *parser = new BelCardParser();
vcard = parser->fold(vcard);
vcard = belcard_fold(vcard);
cout << vcard << endl;
delete parser;
return 0;
}
\ No newline at end of file
......@@ -28,7 +28,7 @@ int main(int argc, char *argv[]) {
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard = parser->parse(vcard);
cout << parser->fold(belCard->toString()) << endl;
cout << belCard->toFoldedString() << endl;
delete parser;
return 0;
......
#include "belcard/belcard_parser.hpp"
#include "belcard/belcard_utils.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace::belr;
using namespace::belcard;
int main(int argc, char *argv[]) {
const char *file = NULL;
......@@ -25,10 +22,8 @@ int main(int argc, char *argv[]) {
vcardStream << istr.rdbuf();
string vcard = vcardStream.str();
BelCardParser *parser = new BelCardParser();
vcard = parser->unfold(vcard);
vcard = belcard_unfold(vcard);
cout << vcard << endl;
delete parser;
return 0;
}
\ No newline at end of file
#include "belcard/belcard_parser.hpp"
#include "belcard/belcard_utils.hpp"
#include "common/bc_tester_utils.h"
#include <iostream>
......@@ -25,10 +26,11 @@ static void folding(void) {
string vcard = openFile("vcards/foldtest.vcf");
BelCardParser *parser = new BelCardParser();
string folded_vcard = parser->fold(vcard);
string folded_vcard = belcard_fold(vcard);
string unfolded_vcard = openFile("vcards/unfoldtest.vcf");
BC_ASSERT_EQUAL(unfolded_vcard.compare(folded_vcard), 0, int, "%d");
delete parser;
}
......@@ -36,10 +38,11 @@ static void unfolding(void) {
string vcard = openFile("vcards/unfoldtest.vcf");
BelCardParser *parser = new BelCardParser();
string unfolded_vcard = parser->unfold(vcard);
string unfolded_vcard = belcard_unfold(vcard);
string folded_vcard = openFile("vcards/foldtest.vcf");
BC_ASSERT_EQUAL(folded_vcard.compare(unfolded_vcard), 0, int, "%d");
delete parser;
}
......@@ -49,7 +52,11 @@ static void vcard_parsing(void) {
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard = parser->parse(vcard);
BC_ASSERT_TRUE(belCard != NULL);
//TODO: find a way to check the belCard object
BC_ASSERT_TRUE(belCard->assertRFCCompliance());
string vcard2 = belCard->toFoldedString();
BC_ASSERT_EQUAL(vcard2.compare(vcard), 0, int, "%d");
delete parser;
}
......
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
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