]> git.llucax.com Git - software/bife/bife++.git/commitdiff
- Moved libbife to it's own directory.
authorLeandro Lucarella <llucax@gmail.com>
Sun, 17 Aug 2003 23:18:44 +0000 (23:18 +0000)
committerLeandro Lucarella <llucax@gmail.com>
Sun, 17 Aug 2003 23:18:44 +0000 (23:18 +0000)
- 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.

26 files changed:
Makefile
libbife/Makefile [new file with mode: 0644]
libbife/chit.cpp [moved from chit.cpp with 100% similarity]
libbife/chit.h [moved from chit.h with 100% similarity]
libbife/container.cpp [moved from container.cpp with 100% similarity]
libbife/container.h [moved from container.h with 100% similarity]
libbife/fallback.cpp [moved from fallback.cpp with 100% similarity]
libbife/fallback.h [moved from fallback.h with 90% similarity]
libbife/ghit.cpp [moved from ghit.cpp with 100% similarity]
libbife/ghit.h [moved from ghit.h with 100% similarity]
libbife/hash.h [moved from hash.h with 100% similarity]
libbife/hit.cpp [moved from hit.cpp with 100% similarity]
libbife/hit.h [moved from hit.h with 100% similarity]
libbife/string.cpp [moved from string.cpp with 100% similarity]
libbife/string.h [moved from string.h with 100% similarity]
libbife/widget.cpp [moved from widget.cpp with 100% similarity]
libbife/widget.h [moved from widget.h with 83% similarity]
parser.cpp
parser.h
parser_test.cpp
test.xml
translate/Makefile [new file with mode: 0644]
translate/translate.cpp [moved from translate.cpp with 100% similarity]
translate/translate.h [moved from translate.h with 93% similarity]
translate/translate_loader.cpp [new file with mode: 0644]
translate_loader.cpp [deleted file]

index 3809eb765e34a8e9c1731732f4b014d187d4232b..fe31c00ca47cca3ded0f1d47087b6f62f40577e1 100644 (file)
--- 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 (file)
index 0000000..30429cd
--- /dev/null
@@ -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
+
similarity index 100%
rename from chit.cpp
rename to libbife/chit.cpp
similarity index 100%
rename from chit.h
rename to libbife/chit.h
similarity index 100%
rename from container.cpp
rename to libbife/container.cpp
similarity index 100%
rename from container.h
rename to libbife/container.h
similarity index 100%
rename from fallback.cpp
rename to libbife/fallback.cpp
similarity index 90%
rename from fallback.h
rename to libbife/fallback.h
index c54dae1ec570626b04b3292484c98d473904bd16..f772893b075007f10806fda19052b46329b2a46d 100644 (file)
@@ -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.
similarity index 100%
rename from ghit.cpp
rename to libbife/ghit.cpp
similarity index 100%
rename from ghit.h
rename to libbife/ghit.h
similarity index 100%
rename from hash.h
rename to libbife/hash.h
similarity index 100%
rename from hit.cpp
rename to libbife/hit.cpp
similarity index 100%
rename from hit.h
rename to libbife/hit.h
similarity index 100%
rename from string.cpp
rename to libbife/string.cpp
similarity index 100%
rename from string.h
rename to libbife/string.h
similarity index 100%
rename from widget.cpp
rename to libbife/widget.cpp
similarity index 83%
rename from widget.h
rename to libbife/widget.h
index de9f47bb31104e9f3d2a1e27fdfbb30dbbc581af..202387062201491de277b040b4530cdb5e6a3261 100644 (file)
--- a/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.
index 34cf1bbb498fb353437edf683a9f420f4005c5cf..4f7ed98315af780f3b00f1fd180d525d5147d097 100644 (file)
@@ -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 <sstream>
+//#include <sstream>
 #include <dlfcn.h>
 
-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
index 3476d87a34753eb14c05b1f937353b226fd06e4c..4efb92beae2fff3a97323abb9d981bcd4400b3a6 100644 (file)
--- 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 <libxml++/libxml++.h>
 #include <string>
 #include <stack>
 
-// 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<Widget*> 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.
index 992d6fe150a5437612fb6d6980d9e02f67b9f5e5..5cf9ddb2e82033ba50cac23f1acfab7257d2a1f2 100644 (file)
@@ -1,6 +1,6 @@
 // vim: set expandtab tabstop=4 shiftwidth=4:
 
-#include "chit.h"
+#include "libbife/chit.h"
 #include "parser.h"
 #include <sstream>
 #include <iostream>
@@ -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;
 }
index a2fecfd3838dbdf88093b767244bed9d4a5712a3..c962fe885fad3c530379c11988f2b6915bf92f33 100644 (file)
--- a/test.xml
+++ b/test.xml
@@ -21,7 +21,7 @@
         Los objetivos de BIFE son estos (en orden de importancia):
         <ul>
             <li>BIFE debe ser rápido.</li>
-            <li>BIFE debe ser simple.</li>
+           <li>BIFE debe ser simple.</li>
             <li>BIFE debe ser modular.</li>
             <li>BIFE debe ser fácil para el creador de contenidos (XML).</li>
         </ul>
diff --git a/translate/Makefile b/translate/Makefile
new file mode 100644 (file)
index 0000000..5d39448
--- /dev/null
@@ -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
+
similarity index 100%
rename from translate.cpp
rename to translate/translate.cpp
similarity index 93%
rename from translate.h
rename to translate/translate.h
index 7f4be7079926933e591a318a50c575ca360313c2..cf0da382d822a6df2b4c30eb288e9271b2a6c0bb 100644 (file)
@@ -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 <vector>
 #include <string>
 
diff --git a/translate/translate_loader.cpp b/translate/translate_loader.cpp
new file mode 100644 (file)
index 0000000..d59f8d2
--- /dev/null
@@ -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 <algorithm>
+#include <string>
+
+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 (file)
index e122fb7..0000000
+++ /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;
-}