Commit 22ce8e88 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Added kind and adr properties

parent 8f0a5c78
......@@ -31,4 +31,4 @@ belcard.kdev4
tests/belcard-tester
tests/belcard-folder
tests/belcard-unfolder
include/belcard/vcard_grammar.hpp
src/vcard_grammar.cpp
belcard_includedir=$(includedir)/belcard
vcard_grammar.hpp: vcard.grammar
xxd -i vcard.grammar | sed s/}\;/,0x00}\;/ > vcard_grammar.hpp
belcard_include_HEADERS = belcard.hpp belcard_generic.hpp belcard_identification.hpp belcard_parser.hpp vcard_grammar.hpp
belcard_include_HEADERS = belcard.hpp \
belcard_generic.hpp \
belcard_general.hpp \
belcard_identification.hpp \
belcard_addressing.hpp \
belcard_parser.hpp \
vcard_grammar.hpp
EXTRA_DIST=$(belcard_include_HEADERS)
......@@ -2,7 +2,9 @@
#define belcard_hpp
#include "belcard_generic.hpp"
#include "belcard_general.hpp"
#include "belcard_identification.hpp"
#include "belcard_addressing.hpp"
#include <string>
#include <list>
......@@ -15,6 +17,7 @@ using namespace::belr;
namespace belcard {
class BelCard : public BelCardGeneric {
private:
shared_ptr<BelCardKind> _kind;
shared_ptr<BelCardFN> _fn;
shared_ptr<BelCardN> _n;
shared_ptr<BelCardBirthday> _bday;
......@@ -22,6 +25,7 @@ namespace belcard {
shared_ptr<BelCardGender> _gender;
list<shared_ptr<BelCardNickname>> _nicknames;
list<shared_ptr<BelCardPhoto>> _photos;
list<shared_ptr<BelCardAddress>> _addr;
list<shared_ptr<BelCardProperty>> _properties;
public:
......@@ -31,19 +35,29 @@ namespace belcard {
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("vcard", make_fn(&BelCard::create))
->setCollector("KIND", make_sfn(&BelCard::setKind))
->setCollector("FN", make_sfn(&BelCard::setFN))
->setCollector("N", make_sfn(&BelCard::setN))
->setCollector("BDAY", make_sfn(&BelCard::setBirthday))
->setCollector("ANNIVERSARY", make_sfn(&BelCard::setAnniversary))
->setCollector("GENDER", make_sfn(&BelCard::setGender))
->setCollector("NICKNAME", make_sfn(&BelCard::addNickname))
->setCollector("PHOTO", make_sfn(&BelCard::addPhoto));
->setCollector("PHOTO", make_sfn(&BelCard::addPhoto))
->setCollector("ADR", make_sfn(&BelCard::addAddress));
}
BelCard() {
}
void setKind(const shared_ptr<BelCardKind> &kind) {
_kind = kind;
addProperty(_kind);
}
const shared_ptr<BelCardKind> &getKind() const {
return _kind;
}
void setFN(const shared_ptr<BelCardFN> &fn) {
_fn = fn;
addProperty(_fn);
......@@ -100,6 +114,14 @@ namespace belcard {
return _photos;
}
void addAddress(const shared_ptr<BelCardAddress> &addr) {
_addr.push_back(addr);
addProperty(addr);
}
const list<shared_ptr<BelCardAddress>> &getAddresses() const {
return _addr;
}
void addProperty(const shared_ptr<BelCardProperty> &property) {
_properties.push_back(property);
}
......
#ifndef belcard_addressing_hpp
#define belcard_addressing_hpp
#include "belcard_generic.hpp"
#include <belr/grammarbuilder.hh>
#include <belr/abnf.hh>
#include <string>
#include <list>
#include <map>
#include <memory>
using namespace::std;
using namespace::belr;
namespace belcard {
class BelCardAddress : public BelCardProperty {
private:
string _po_box;
string _extended_address;
string _street;
string _locality;
string _region;
string _postal_code;
string _country;
public:
static shared_ptr<BelCardAddress> create() {
return make_shared<BelCardAddress>();
}
static shared_ptr<BelCardAddress> parse(const string& input) {
ABNFGrammarBuilder grammar_builder;
shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
Parser<shared_ptr<BelCardGeneric>> parser(grammar);
setHandlerAndCollectors(&parser);
BelCardParam::setHandlerAndCollectors(&parser);
shared_ptr<BelCardGeneric> ret = parser.parseInput("ADR", input, NULL);
return dynamic_pointer_cast<BelCardAddress>(ret);
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("ADR", make_fn(&BelCardAddress::create))
->setCollector("group", make_sfn(&BelCardAddress::setGroup))
->setCollector("any-param", make_sfn(&BelCardAddress::addParam))
->setCollector("ADR-pobox", make_sfn(&BelCardAddress::setPostOfficeBox))
->setCollector("ADR-ext", make_sfn(&BelCardAddress::setExtendedAddress))
->setCollector("ADR-street", make_sfn(&BelCardAddress::setStreet))
->setCollector("ADR-locality", make_sfn(&BelCardAddress::setLocality))
->setCollector("ADR-region", make_sfn(&BelCardAddress::setRegion))
->setCollector("ADR-code", make_sfn(&BelCardAddress::setPostalCode))
->setCollector("ADR-country", make_sfn(&BelCardAddress::setCountry));
}
BelCardAddress() : BelCardProperty() {
setName("ADR");
}
void setPostOfficeBox(const string &value) {
_po_box = value;
}
const string &getPostOfficeBox() const {
return _po_box;
}
void setExtendedAddress(const string &value) {
_extended_address = value;
}
const string &getExtendedAddress() const {
return _extended_address;
}
void setStreet(const string &value) {
_street = value;
}
const string &getStreet() const {
return _street;
}
void setLocality(const string &value) {
_locality = value;
}
const string &getLocality() const {
return _locality;
}
void setRegion(const string &value) {
_region = value;
}
const string &getRegion() const {
return _region;
}
void setPostalCode(const string &value) {
_postal_code = value;
}
const string &getPostalCode() const {
return _postal_code;
}
void setCountry(const string &value) {
_country = value;
}
const string &getCountry() const {
return _country;
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
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;
}
};
}
#endif
\ No newline at end of file
#ifndef belcard_general_hpp
#define belcard_general_hpp
#include "belcard_generic.hpp"
#include <belr/grammarbuilder.hh>
#include <belr/abnf.hh>
#include <string>
#include <list>
#include <map>
#include <memory>
using namespace::std;
using namespace::belr;
namespace belcard {
class BelCardKind : public BelCardProperty {
public:
static shared_ptr<BelCardKind> create() {
return make_shared<BelCardKind>();
}
static shared_ptr<BelCardKind> parse(const string& input) {
ABNFGrammarBuilder grammar_builder;
shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
Parser<shared_ptr<BelCardGeneric>> parser(grammar);
setHandlerAndCollectors(&parser);
BelCardParam::setHandlerAndCollectors(&parser);
shared_ptr<BelCardGeneric> ret = parser.parseInput("KIND", input, NULL);
return dynamic_pointer_cast<BelCardKind>(ret);
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("KIND", make_fn(&BelCardKind::create))
->setCollector("group", make_sfn(&BelCardKind::setGroup))
->setCollector("any-param", make_sfn(&BelCardKind::addParam))
->setCollector("KIND-value", make_sfn(&BelCardKind::setValue));
}
BelCardKind() : BelCardProperty() {
setName("KIND");
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
};
}
#endif
\ No newline at end of file
......@@ -53,6 +53,7 @@ namespace belcard {
string _additional_name;
string _prefixes;
string _suffixes;
public:
static shared_ptr<BelCardN> create() {
return make_shared<BelCardN>();
......@@ -83,38 +84,38 @@ namespace belcard {
setName("N");
}
virtual void setFamilyName(const string &value) {
void setFamilyName(const string &value) {
_family_name = value;
}
virtual const string &getFamilyName() const {
const string &getFamilyName() const {
return _family_name;
}
virtual void setGivenName(const string &value) {
void setGivenName(const string &value) {
_given_name = value;
}
virtual const string &getGivenName() const {
const string &getGivenName() const {
return _given_name;
}
virtual void setAdditionalName(const string &value) {
void setAdditionalName(const string &value) {
_additional_name = value;
}
virtual const string &getAdditionalName() const {
const string &getAdditionalName() const {
return _additional_name;
}
virtual void setPrefixes(const string &value) {
void setPrefixes(const string &value) {
_prefixes = value;
}
virtual const string &getPrefixes() const {
const string &getPrefixes() const {
return _prefixes;
}
virtual void setSuffixes(const string &value) {
void setSuffixes(const string &value) {
_suffixes = value;
}
virtual const string &getSuffixes() const {
const string &getSuffixes() const {
return _suffixes;
}
......@@ -129,7 +130,7 @@ namespace belcard {
output << n.getName();
for (auto it = n.getParams().begin(); it != n.getParams().end(); ++it) {
output << ";" << (*it);
output << ";" << (**it);
}
output << ":" << n.getFamilyName() + ";" + n.getGivenName() + ";" + n.getAdditionalName() + ";" + n.getPrefixes() + ";" + n.getSuffixes() << "\r\n";
return output;
......
#ifndef vcard_grammar_hpp
#define vcard_grammar_hpp
extern unsigned char vcard_grammar[8000];
extern unsigned int vcard_grammar_len;
#endif
lib_LTLIBRARIES=libbelcard.la
libbelcard_la_SOURCES=belcard_parser.cpp
vcard_grammar.cpp: vcard.grammar
echo "#include \"belcard/vcard_grammar.hpp\"" > vcard_grammar.cpp
xxd -i vcard.grammar | sed s/}\;/,0x00}\;/ >> vcard_grammar.cpp
libbelcard_la_SOURCES=belcard_parser.cpp vcard_grammar.cpp
AM_CPPFLAGS=-I$(top_srcdir)/include
......@@ -5,16 +5,19 @@ vcard = "BEGIN:VCARD" CRLF
1*property
"END:VCARD" CRLF
property = FN / N / NICKNAME / BDAY / ANNIVERSARY / GENDER / PHOTO
property = KIND / FN / N / NICKNAME / BDAY / ANNIVERSARY / GENDER / PHOTO / ADR
KIND = [group "."] "KIND" *(";" any-param) ":" KIND-value CRLF
KIND-value = "individual" / "group" / "org" / "location" / iana-token / x-name
FN = [group "."] "FN" *(";" any-param) ":" FN-value CRLF
FN-value = text
N = [group "."] "N" *(";" any-param) ":" N-value CRLF
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-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)
......@@ -34,6 +37,19 @@ sex = "M" / "F" / "O" / "N" / "U"
PHOTO = [group "."] "PHOTO" *(";" any-param) ":" PHOTO-value CRLF
PHOTO-value = URI
ADR = [group "."] "ADR" *(";" any-param) ":" ADR-value CRLF
ADR-value = ADR-pobox ";" ADR-ext ";"
ADR-street ";" ADR-locality ";"
ADR-region ";" ADR-code ";"
ADR-country
ADR-pobox = component *("," component)
ADR-ext = component *("," component)
ADR-street = component *("," component)
ADR-locality = component *("," component)
ADR-region = component *("," component)
ADR-code = component *("," component)
ADR-country = component *("," component)
group = 1*(ALPHA / DIGIT / "-")
any-param = param-name "=" param-value
......
......@@ -5,11 +5,12 @@ bin_PROGRAMS=belcard-folder belcard-unfolder
if BUILD_TESTS
noinst_PROGRAMS=belcard-tester
belcard_tester_SOURCES=belcard-tester.cpp belcard-tester.hpp common/bc_tester_utils.c common/bc_tester_utils.h belcard-identification-tester.cpp
belcard_tester_SOURCES=belcard-tester.cpp belcard-tester.hpp common/bc_tester_utils.c common/bc_tester_utils.h \
belcard-general-tester.cpp belcard-identification-tester.cpp belcard-addressing-tester.cpp \
belcard-vcard-tester.cpp
belcard_tester_LDADD=$(top_builddir)/src/libbelcard.la -lbelr $(CUNIT_LIBS)
belcard_tester_AM_CPPFLAGS= -DBC_CONFIG_FILE=\"config.h\" \
-I$(top_srcdir)/include \
belcard_tester_AM_CPPFLAGS=-I$(top_srcdir)/include \
-I$(top_srcdir)/tester \
-I$(top_srcdir)/tester/common
......
#include "belcard/belcard_addressing.hpp"
#include "common/bc_tester_utils.h"
#include <sstream>
using namespace::std;
using namespace::belcard;
static void adr_property(void) {
string input = "ADR;TYPE=work:;;34 avenue de l'Europe,le Trident bat D;GRENOBLE;;38100;FRANCE\r\n";
shared_ptr<BelCardAddress> addr = BelCardAddress::parse(input);
BC_ASSERT_TRUE_FATAL(addr != NULL);
stringstream sstream;
sstream << *addr;
BC_ASSERT_EQUAL(input.compare(sstream.str()), 0, int, "%d");
}
static test_t tests[] = {
{ "Adr", adr_property },
};
test_suite_t vcard_addressing_properties_test_suite = {
"Addressing",
NULL,
NULL,
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
tests
};
\ No newline at end of file
#include "belcard/belcard_general.hpp"
#include "common/bc_tester_utils.h"
#include <sstream>
using namespace::std;
using namespace::belcard;
static void kind_property(void) {
string input = "KIND:individual\r\n";
shared_ptr<BelCardKind> kind = BelCardKind::parse(input);
BC_ASSERT_TRUE_FATAL(kind != NULL);
stringstream sstream;
sstream << *kind;
BC_ASSERT_EQUAL(input.compare(sstream.str()), 0, int, "%d");
}
static test_t tests[] = {
{ "Kind", kind_property },
};
test_suite_t vcard_general_properties_test_suite = {
"General",
NULL,
NULL,
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
tests
};
\ No newline at end of file
#include "belcard/belcard.hpp"
#include "belcard/belcard_identification.hpp"
#include "common/bc_tester_utils.h"
#include <sstream>
......@@ -6,14 +6,6 @@
using namespace::std;
using namespace::belcard;
static int tester_before_all(void) {
return 0;
}
static int tester_after_all(void) {
return 0;
}
static void fn_property(void) {
string input = "FN:Sylvain Berfini\r\n";
shared_ptr<BelCardFN> fn = BelCardFN::parse(input);
......@@ -113,8 +105,8 @@ static test_t tests[] = {
test_suite_t vcard_identification_properties_test_suite = {
"Identification",
tester_before_all,
tester_after_all,
NULL,
NULL,
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
......
......@@ -52,7 +52,10 @@ void belcard_tester_init(void(*ftester_printf)(int level, const char *fmt, va_li
if (ftester_printf == NULL) ftester_printf = log_handler;
bc_tester_init(ftester_printf, MESSAGE, ERROR);
bc_tester_add_suite(&vcard_general_properties_test_suite);
bc_tester_add_suite(&vcard_identification_properties_test_suite);
bc_tester_add_suite(&vcard_addressing_properties_test_suite);
bc_tester_add_suite(&vcard_test_suite);
}
void belcard_tester_uninit(void) {
......
......@@ -8,6 +8,9 @@ extern "C" {
#endif
extern test_suite_t vcard_identification_properties_test_suite;
extern test_suite_t vcard_general_properties_test_suite;
extern test_suite_t vcard_addressing_properties_test_suite;
extern test_suite_t vcard_test_suite;
void belcard_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args));
void belcard_tester_uninit(void);
......@@ -16,5 +19,4 @@ void belcard_tester_uninit(void);
};
#endif
#endif
#include "belcard/belcard_parser.hpp"
#include "common/bc_tester_utils.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace::std;
using namespace::belcard;
static string openFile(const char *name) {
ifstream istr(bc_tester_file(name));
if (!istr.is_open()) {
BC_FAIL(name);
}
stringstream vcardStream;
vcardStream << istr.rdbuf();
string vcard = vcardStream.str();
return vcard;
}
static void folding(void) {
string vcard = openFile("vcards/foldtest.vcf");
BelCardParser *parser = new BelCardParser();
string folded_vcard = parser->fold(vcard);
delete parser;
}
static void unfolding(void) {
string vcard = openFile("vcards/unfoldtest.vcf");
BelCardParser *parser = new BelCardParser();
string unfolded_vcard = parser->unfold(vcard);
delete parser;
}
static void vcard_parsing(void) {
string vcard = openFile("vcards/vcard.vcf");
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard = parser->parse(vcard);
delete parser;
}
static test_t tests[] = {
{ "Folding", folding },
{ "Unfolding", unfolding },
{ "VCard parsing", vcard_parsing },
};
test_suite_t vcard_test_suite = {
"VCard",
NULL,
NULL,
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
tests
};
\ No newline at end of file
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