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

Reorganized code

parent 88214df2
belcard_includedir=$(includedir)/belcard
belcard_include_HEADERS = belcard.hpp
belcard_include_HEADERS = belcard.hpp belcard_generic.hpp belcard_identification.hpp belcard_parser.hpp
EXTRA_DIST=$(belcard_include_HEADERS)
#ifndef belcard_hpp
#define belcard_hpp
#include "belcard_generic.hpp"
#include "belcard_identification.hpp"
#include <string>
#include <list>
#include <map>
#include <memory>
#include <vector>
using namespace::std;
namespace belcard {
class BelCardGeneric {
public:
BelCardGeneric() { }
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() : BelCardGeneric() {
}
virtual void setGroup(const string &group) {
_group = group;
}
virtual const string &getGroup() const {
return _group;
}
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 void addParam(const shared_ptr<BelCardParam> &param) {
_params.push_back(param);
}
virtual const list<shared_ptr<BelCardParam>> &getParams() const {
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();
}
property += ":" + _value + "\r\n";
return property;
}
};
class BelCardFN : public BelCardProperty {
public:
static shared_ptr<BelCardFN> create() {
return make_shared<BelCardFN>();
}
BelCardFN() : BelCardProperty() {
setName("FN");
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
};
class BelCardN : public BelCardProperty {
private:
string _family_name;
string _given_name;
string _additional_name;
string _prefixes;
string _suffixes;
public:
static shared_ptr<BelCardN> create() {
return make_shared<BelCardN>();
}
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;
......
#ifndef belcard_generic_hpp
#define belcard_generic_hpp
#include <string>
#include <list>
#include <map>
#include <memory>
using namespace::std;
namespace belcard {
class BelCardGeneric {
public:
BelCardGeneric() { }
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() : BelCardGeneric() {
}
virtual void setGroup(const string &group) {
_group = group;
}
virtual const string &getGroup() const {
return _group;
}
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 void addParam(const shared_ptr<BelCardParam> &param) {
_params.push_back(param);
}
virtual const list<shared_ptr<BelCardParam>> &getParams() const {
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();
}
property += ":" + _value + "\r\n";
return property;
}
};
}
#endif
\ No newline at end of file
#ifndef belcard_identification_hpp
#define belcard_identification_hpp
#include "belcard.hpp"
#include <string>
#include <list>
#include <map>
#include <memory>
using namespace::std;
namespace belcard {
class BelCardFN : public BelCardProperty {
public:
static shared_ptr<BelCardFN> create() {
return make_shared<BelCardFN>();
}
BelCardFN() : BelCardProperty() {
setName("FN");
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
};
class BelCardN : public BelCardProperty {
private:
string _family_name;
string _given_name;
string _additional_name;
string _prefixes;
string _suffixes;
public:
static shared_ptr<BelCardN> create() {
return make_shared<BelCardN>();
}
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);
}
};
}
#endif
\ No newline at end of file
#ifndef belcard_parser_hpp
#define belcard_parser_hpp
#include "belcard.hpp"
#include <belr/grammarbuilder.hh>
#include <belr/abnf.hh>
namespace belr {
class BelCardParser {
private:
ABNFGrammarBuilder _grammar_builder;
shared_ptr<Grammar> _grammar;
public:
BelCardParser();
~BelCardParser();
shared_ptr<belcard::BelCard> parse(const string &input);
};
}
#endif
\ No newline at end of file
lib_LTLIBRARIES=libbelcard.la
libbelcard_la_SOURCES=belcard.cpp
libbelcard_la_SOURCES=belcard_parser.cpp
AM_CPPFLAGS=-I$(top_srcdir)/include
#include "belcard/belcard.hpp"
#include <belr/grammarbuilder.hh>
#include <belr/abnf.hh>
#include <belr/parser-impl.cc>
using namespace::std;
using namespace::belr;
namespace belcard {
}
\ No newline at end of file
#include "belcard/belcard_parser.hpp"
#include "belcard/belcard.hpp"
#include <belr/parser-impl.cc>
using namespace::std;
using namespace::belr;
using namespace::belcard;
BelCardParser::BelCardParser() {
_grammar = _grammar_builder.createFromAbnf("vcardgrammar.txt", make_shared<CoreRules>());
}
shared_ptr<BelCard> BelCardParser::parse(const string &input) {
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("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("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));
size_t parsedSize = 0;
shared_ptr<BelCardGeneric> ret = parser.parseInput("vcard", input, &parsedSize);
shared_ptr<BelCard> belCard = dynamic_pointer_cast<BelCard>(ret);
return belCard;
}
\ No newline at end of file
#include "belcard/belcard.hpp"
#include <belr/grammarbuilder.hh>
#include <belr/abnf.hh>
#include <belr/parser-impl.cc>
#include "belcard/belcard_parser.hpp"
#include <iostream>
#include <fstream>
......@@ -12,42 +8,6 @@ using namespace::belr;
using namespace::belcard;
int main(int argc, char *argv[]) {
ABNFGrammarBuilder builder;
shared_ptr<Grammar> grammar = builder.createFromAbnf("vcardgrammar.txt", make_shared<CoreRules>());
if (!grammar) {
cerr << "Could not build grammar from vcardgrammar.txt" << endl;
return -1;
}
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("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("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()) {
return -1;
......@@ -56,9 +16,8 @@ int main(int argc, char *argv[]) {
vcardStream << istr.rdbuf();
string vcard = vcardStream.str();
size_t parsedSize = 0;
shared_ptr<BelCardGeneric> ret = parser.parseInput("vcard", vcard, &parsedSize);
shared_ptr<BelCard> belCard = dynamic_pointer_cast<BelCard>(ret);
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard = parser->parse(vcard);
if (belCard) {
string outputVCard = belCard->toString();
......
......@@ -29,7 +29,6 @@ param-value = param-value-component *("," param-value-component)
param-value-component = *SAFE-CHAR / DQUOTE *QSAFE-CHAR DQUOTE
iana-token = 1*(ALPHA / DIGIT / "-")
x-name = "x-" 1*(ALPHA / DIGIT / "-")
COMPONENT-CHAR = "\\" / "\," / "\;" / "\n" / WSP / NON-ASCII / %x21-2B / %x2D-3A / %x3C-5B / %x5D-7E
......
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