From 9229fc52267e17a39cd9df385f120a4d0318803a Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sun, 17 Aug 2003 23:18:44 +0000 Subject: [PATCH] - Moved libbife to it's own directory. - Now constructor and destructor functions prototypes are defined in the classes that need them. - Moved translate example module to it's own directory. - Fallback class now can be specified in the Parser constructor. --- Makefile | 51 +++------------------- libbife/Makefile | 23 ++++++++++ chit.cpp => libbife/chit.cpp | 0 chit.h => libbife/chit.h | 0 container.cpp => libbife/container.cpp | 0 container.h => libbife/container.h | 0 fallback.cpp => libbife/fallback.cpp | 0 fallback.h => libbife/fallback.h | 5 +++ ghit.cpp => libbife/ghit.cpp | 0 ghit.h => libbife/ghit.h | 0 hash.h => libbife/hash.h | 0 hit.cpp => libbife/hit.cpp | 0 hit.h => libbife/hit.h | 0 string.cpp => libbife/string.cpp | 0 string.h => libbife/string.h | 0 widget.cpp => libbife/widget.cpp | 0 widget.h => libbife/widget.h | 7 +++ parser.cpp | 54 ++++++++++++++++++------ parser.h | 40 +++++++++++------- parser_test.cpp | 10 ++++- test.xml | 2 +- translate/Makefile | 19 +++++++++ translate.cpp => translate/translate.cpp | 0 translate.h => translate/translate.h | 8 ++-- translate/translate_loader.cpp | 43 +++++++++++++++++++ translate_loader.cpp | 20 --------- 26 files changed, 181 insertions(+), 101 deletions(-) create mode 100644 libbife/Makefile rename chit.cpp => libbife/chit.cpp (100%) rename chit.h => libbife/chit.h (100%) rename container.cpp => libbife/container.cpp (100%) rename container.h => libbife/container.h (100%) rename fallback.cpp => libbife/fallback.cpp (100%) rename fallback.h => libbife/fallback.h (90%) rename ghit.cpp => libbife/ghit.cpp (100%) rename ghit.h => libbife/ghit.h (100%) rename hash.h => libbife/hash.h (100%) rename hit.cpp => libbife/hit.cpp (100%) rename hit.h => libbife/hit.h (100%) rename string.cpp => libbife/string.cpp (100%) rename string.h => libbife/string.h (100%) rename widget.cpp => libbife/widget.cpp (100%) rename widget.h => libbife/widget.h (83%) create mode 100644 translate/Makefile rename translate.cpp => translate/translate.cpp (100%) rename translate.h => translate/translate.h (93%) create mode 100644 translate/translate_loader.cpp delete mode 100644 translate_loader.cpp diff --git a/Makefile b/Makefile index 3809eb7..fe31c00 100644 --- a/Makefile +++ b/Makefile @@ -1,55 +1,18 @@ -DEBUG=-g3 -DDEBUG -Wall -#CPPFLAGS=-g3 -Wall -I/usr/include/libxml++-1.0 -I/usr/include/libxml2 -DDEBUG -#CPPFLAGS=-O3 -Wall -I/usr/include/libxml++-1.0 -I/usr/include/libxml2 +LIBBIFE_DIR=libbife -TAGETS=hit.o ghit.o chit.o widget.o container.o fallback.o string.o +DEBUG=-Wall -g3 -DDEBUG +CPPFLAGS=-I/usr/include/libxml++-1.0 -I/usr/include/libxml2 $(DEBUG) +LDFLAGS=-L$(LIBBIFE_DIR) -lxml++-0.1 -ldl -lbife all: parser_test -main: $(TAGETS) - - -CPPFLAGS=-fPIC $(DEBUG) - -hit.o: hit.h hit.cpp -ghit.o: hit.o ghit.h ghit.cpp -chit.o: ghit.o chit.h chit.cpp - -widget.o: widget.h widget.cpp -string.o: widget.o string.h string.cpp -container.o: widget.o container.h container.cpp -fallback.o: container.o fallback.h fallback.cpp - -translate.o: translate.h translate.cpp -translate_loader.o: translate_loader.cpp -#.o: .o .h .cpp - -# LIBBIFE -LIBBIFE_TARGETS=hit.o ghit.o chit.o widget.o string.o container.o fallback.o -libbife.so: $(LIBBIFE_TARGETS) - $(CXX) $(DEBUG) -Wl,-soname,libbife.so -shared -o libbife.so $(LIBBIFE_TARGETS) - -# TRANSLATE FALLBACK PLUG-IN -TRANSLATE_TARGETS=translate.o translate_loader.o -translate.so: libbife.so $(TRANSLATE_TARGETS) - $(CXX) $(DEBUG) -L. -lbife -Wl,-soname,translate.so -shared -o translate.so $(TRANSLATE_TARGETS) - - # Parser example. -PARSER_FLAGS=-I/usr/include/libxml++-1.0 -I/usr/include/libxml2 parser.o: parser.h parser.cpp - $(CXX) $(DEBUG) $(PARSER_FLAGS) -c parser.cpp parser_test.o: parser_test.cpp - $(CXX) $(DEBUG) $(PARSER_FLAGS) -c parser_test.cpp -PARSER_TARGETS=parser.o parser_test.o -parser_test: translate.so $(PARSER_TARGETS) - $(CXX) $(DEBUG) $(PARSER_FLAGS) -L. -lxml++-0.1 -ldl -lbife -o parser_test $(PARSER_TARGETS) - @echo - @echo ---------------------------------------------- - @echo export LD_LIBRARY_PATH=. - @echo to use ./parser_test +parser_test: parser.o parser_test.o clean: - @rm -f *.o main parser_test + @rm -f *.o parser_test + diff --git a/libbife/Makefile b/libbife/Makefile new file mode 100644 index 0000000..30429cd --- /dev/null +++ b/libbife/Makefile @@ -0,0 +1,23 @@ + +DEBUG=-Wall #-g3 -DDEBUG +CPPFLAGS=-fPIC $(DEBUG) +LDFLAGS=-shared + +all: libbife.so + +hit.o: hash.h hit.h hit.cpp +ghit.o: hit.o ghit.h ghit.cpp +chit.o: ghit.o chit.h chit.cpp + +widget.o: hit.o widget.h widget.cpp +string.o: widget.o string.h string.cpp +container.o: widget.o container.h container.cpp +fallback.o: container.o fallback.h fallback.cpp + +TAGETS=hit.o ghit.o chit.o widget.o string.o container.o fallback.o +libbife.so: $(TAGETS) + $(CXX) $(DEBUG) $(LDFLAGS) -o libbife.so $(TAGETS) + +clean: + @rm -f *.o libbife.so + diff --git a/chit.cpp b/libbife/chit.cpp similarity index 100% rename from chit.cpp rename to libbife/chit.cpp diff --git a/chit.h b/libbife/chit.h similarity index 100% rename from chit.h rename to libbife/chit.h diff --git a/container.cpp b/libbife/container.cpp similarity index 100% rename from container.cpp rename to libbife/container.cpp diff --git a/container.h b/libbife/container.h similarity index 100% rename from container.h rename to libbife/container.h diff --git a/fallback.cpp b/libbife/fallback.cpp similarity index 100% rename from fallback.cpp rename to libbife/fallback.cpp diff --git a/fallback.h b/libbife/fallback.h similarity index 90% rename from fallback.h rename to libbife/fallback.h index c54dae1..f772893 100644 --- a/fallback.h +++ b/libbife/fallback.h @@ -20,6 +20,11 @@ namespace bife { * @todo */ class Fallback: public Container { + // Typedefs + public: + /// Fallback constructor function prototype. + typedef Fallback* Constructor(const string&, const string&, const Hash&); + // Attributes. public: /// Root widget. diff --git a/ghit.cpp b/libbife/ghit.cpp similarity index 100% rename from ghit.cpp rename to libbife/ghit.cpp diff --git a/ghit.h b/libbife/ghit.h similarity index 100% rename from ghit.h rename to libbife/ghit.h diff --git a/hash.h b/libbife/hash.h similarity index 100% rename from hash.h rename to libbife/hash.h diff --git a/hit.cpp b/libbife/hit.cpp similarity index 100% rename from hit.cpp rename to libbife/hit.cpp diff --git a/hit.h b/libbife/hit.h similarity index 100% rename from hit.h rename to libbife/hit.h diff --git a/string.cpp b/libbife/string.cpp similarity index 100% rename from string.cpp rename to libbife/string.cpp diff --git a/string.h b/libbife/string.h similarity index 100% rename from string.h rename to libbife/string.h diff --git a/widget.cpp b/libbife/widget.cpp similarity index 100% rename from widget.cpp rename to libbife/widget.cpp diff --git a/widget.h b/libbife/widget.h similarity index 83% rename from widget.h rename to libbife/widget.h index de9f47b..2023870 100644 --- a/widget.h +++ b/libbife/widget.h @@ -17,6 +17,13 @@ namespace bife { * @todo */ class Widget { + // Typedefs + public: + /// Widget constructor function prototype. + typedef Widget* Constructor(const string&, const Hash&); + /// Widget destructor function prototype. + typedef void Destructor(Widget*); + // Attributes. public: /// Widget attributes. diff --git a/parser.cpp b/parser.cpp index 34cf1bb..4f7ed98 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1,14 +1,14 @@ // vim: set expandtab tabstop=4 shiftwidth=4: -#include "widget.h" -#include "container.h" -#include "fallback.h" -#include "string.h" +#include "libbife/widget.h" +#include "libbife/container.h" +#include "libbife/fallback.h" +#include "libbife/string.h" #include "parser.h" -#include +//#include #include -using std::stringstream; +//using std::stringstream; using namespace bife; #ifdef DEBUG @@ -42,7 +42,11 @@ void Parser::on_start_element(const string& name, const AttributeMap& attrs) { } cerr << "]);" << endl; #endif - stack.push(fb_create(name, attrs)); + if (fbClass.empty()) { + throw string("Widget '") + name + "' not found and now using a fallback class."; + } else { + stack.push(fbNew(fbClass, name, attrs)); + } } void Parser::on_end_element(const string& name) { @@ -122,11 +126,12 @@ void Parser::on_validity_warning(const string& warn) { #endif } -Parser::Parser(void): fb_create(NULL), fb_destroy(NULL), root(NULL) { +/* +Parser::Parser(void): fallbackConstructor(NULL), fallbackDestructor(NULL), root(NULL) { #ifdef DEBUG cerr << "In Parser::Parser();" << endl; #endif - void* fb = dlopen("./translate.so", RTLD_NOW | RTLD_GLOBAL); // TODO - mas rapido: RTLD_LAZY); + void* fb = dlopen("./translate.so", RTLD_LAZY); // XXX - así anda: RTLD_NOW | RTLD_GLOBAL); if (!fb) { throw string("No se puede cargar el plug-in: ") + dlerror(); } @@ -136,12 +141,33 @@ Parser::Parser(void): fb_create(NULL), fb_destroy(NULL), root(NULL) { throw string("No se puede cargar el creador del plug-in: ") + dlerror(); } } +*/ -//Parser::Parser(const Hash& attrs): attrs(attrs) { -//#ifdef DEBUG -// cerr << "In Parser::Parser(attrs = {" /* TODO << attrs */ << "});" << endl; -//#endif -//} +Parser::Parser(const string& fallback): fbNew(NULL), fbDel(NULL), root(NULL) { +#ifdef DEBUG + cerr << "In Parser::Parser(fallback = '" << fallback << "');" << endl; +#endif + if (!fallback.empty()) { + string::size_type pos = fallback.find("."); + if (pos == string::npos) { + throw string("Fallback module not specified in fallback name: ") + fallback; + } + fbClass = fallback.substr(pos + 1, fallback.length() - 1); + // Opens the fallback module. + string modules_dir = "translate"; + string fb_module = modules_dir + "/" + fallback.substr(0, pos) + ".so"; + void* dlh = dlopen(fb_module.c_str(), RTLD_LAZY); + if (!dlh) { + throw string("No se puede cargar el plug-in: ") + dlerror(); + } + fbNew = (Fallback::Constructor*)dlsym(dlh, "fallback_constructor"); + fbDel = (Widget::Destructor*)dlsym(dlh, "widget_destructor"); + if (!fbNew || !fbDel) { + throw string("No se puede cargar el creador del plug-in: ") + dlerror(); + } + // TODO - CLOSE dl handler, destroy objects. + } +} Parser::~Parser(void) { #ifdef DEBUG diff --git a/parser.h b/parser.h index 3476d87..4efb92b 100644 --- a/parser.h +++ b/parser.h @@ -3,17 +3,14 @@ #ifndef BIFE_PARSER_H #define BIFE_PARSER_H -#include "hit.h" -#include "hash.h" -#include "widget.h" +#include "libbife/hit.h" +#include "libbife/hash.h" +#include "libbife/widget.h" +#include "libbife/fallback.h" #include #include #include -// FIXME - Poner esto en un lugar más bonito. -typedef bife::Widget* create_t(const std::string& name, const bife::Hash&); -typedef void destroy_t(bife::Widget*); - namespace bife { using std::string; @@ -21,24 +18,35 @@ namespace bife { /** * Base Widget Class. * - * @todo + * @todo Better plug-in support. Cleanning (a lot). + * @todo Try to free some memeory :) */ class Parser: public xmlpp::SaxParser { // Typedefs. protected: + /// Stack of widget pointers. typedef std::stack WidgetStack; // Attributes. protected: - /// Stack TODO. + /** + * Widget stack. + * This is the stack of widgets to know what widget is the parser + * proccesing. + */ WidgetStack stack; - // TODO Fallback. - create_t* fb_create; - destroy_t* fb_destroy; + /// Fallback constructor function pointer. + Fallback::Constructor* fbNew; + + /// Fallback destructor function pointer. + Widget::Destructor* fbDel; + + /// Fallback class name. + string fbClass; public: - /// Widget attributes. + /// Widget attributes (FIXME - this must be protected?). Widget* root; // Methods. @@ -121,14 +129,14 @@ namespace bife { /** * Constructor. */ - Parser(void); + //Parser(void); /** * Constructor. * - * @param fallback Fallback. + * @param fallback Fallback class name. */ - //Parser(const Fallback&); TODO + Parser(const string& = ""); /** * Destructor. diff --git a/parser_test.cpp b/parser_test.cpp index 992d6fe..5cf9ddb 100644 --- a/parser_test.cpp +++ b/parser_test.cpp @@ -1,6 +1,6 @@ // vim: set expandtab tabstop=4 shiftwidth=4: -#include "chit.h" +#include "libbife/chit.h" #include "parser.h" #include #include @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) { cout << "Parser example:" << endl; cout << "===============" << endl; try { - Parser parser; + Parser parser("translate.translate"); parser.parse_file(file); // Text is returned in utf-8 encoding. cout << parser.root->render(tpl) << endl; @@ -30,6 +30,12 @@ int main(int argc, char* argv[]) { } catch (string e) { cerr << "Error: " << e << endl; return 2; + } catch (const char* e) { + cerr << "Error: " << e << endl; + return 3; + } catch (...) { + cerr << "Unknown error!!!" << endl; + return -1; } return 0; } diff --git a/test.xml b/test.xml index a2fecfd..c962fe8 100644 --- a/test.xml +++ b/test.xml @@ -21,7 +21,7 @@ Los objetivos de BIFE son estos (en orden de importancia):
  • BIFE debe ser rápido.
  • -
  • BIFE debe ser simple.
  • +
  • BIFE debe ser simple.
  • BIFE debe ser modular.
  • BIFE debe ser fácil para el creador de contenidos (XML).
diff --git a/translate/Makefile b/translate/Makefile new file mode 100644 index 0000000..5d39448 --- /dev/null +++ b/translate/Makefile @@ -0,0 +1,19 @@ + +LIBBIFE_DIR=../libbife + +DEBUG=-Wall #-g3 -DDEBUG +CPPFLAGS=-fPIC $(DEBUG) +LDFLAGS=-L$(LIBBIFE_DIR) -lbife -shared + +all: translate.so + +translate.o: translate.h translate.cpp +translate_loader.o: translate_loader.cpp + +TARGETS=translate.o translate_loader.o +translate.so: $(TARGETS) + $(CXX) $(DEBUG) $(LDFLAGS) -o translate.so $(TAGETS) + +clean: + @rm -f *.o translate.so + diff --git a/translate.cpp b/translate/translate.cpp similarity index 100% rename from translate.cpp rename to translate/translate.cpp diff --git a/translate.h b/translate/translate.h similarity index 93% rename from translate.h rename to translate/translate.h index 7f4be70..cf0da38 100644 --- a/translate.h +++ b/translate/translate.h @@ -3,10 +3,10 @@ #ifndef BIFE_TRANSLATE_H #define BIFE_TRANSLATE_H -#include "fallback.h" -#include "widget.h" -#include "hit.h" -#include "hash.h" +#include "../libbife/fallback.h" +#include "../libbife/widget.h" +#include "../libbife/hit.h" +#include "../libbife/hash.h" #include #include diff --git a/translate/translate_loader.cpp b/translate/translate_loader.cpp new file mode 100644 index 0000000..d59f8d2 --- /dev/null +++ b/translate/translate_loader.cpp @@ -0,0 +1,43 @@ +// vim: set expandtab tabstop=4 shiftwidth=4: + +#include "translate.h" +#include "../libbife/fallback.h" +#include "../libbife/widget.h" +#include "../libbife/hash.h" +#include +#include + +using bife::Translate; +using bife::Fallback; +using bife::Widget; +using bife::Hash; +using std::string; + +/** + * Fallback constructor. + * + * @param classname Name of the class to construct. + * @param widgetname Name of the widget to construct. + * @param attrs Widget's attributes. + * @return A new Fallback widget. + */ +extern "C" +Fallback* fallback_constructor(const string& classname, const string& widgetname, const Hash& attrs) { + string cn = classname; + std::transform(cn.begin(), cn.end(), cn.begin(), std::tolower); + if (cn == "translate") { + return new Translate(widgetname, attrs); + } else { + return NULL; + } +} + +/** + * Widget destructor. + * + * @param w The widget to destroy. + */ +extern "C" +void widget_destructor(Widget* w) { + delete w; +} diff --git a/translate_loader.cpp b/translate_loader.cpp deleted file mode 100644 index e122fb7..0000000 --- a/translate_loader.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "translate.h" -#include "widget.h" -#include "hash.h" - -using bife::Translate; -using bife::Widget; -using bife::Hash; -using std::string; - -// the types of the class factories -typedef bife::Widget* create_t(const string& name, const Hash&); -typedef void destroy_t(Widget*); - -// the class factories -extern "C" Widget* create(const string& name, const Hash& attrs) { - return new Translate(name, attrs); -} -extern "C" void destroy(Widget* w) { - delete w; -} -- 2.43.0