]> git.llucax.com Git - software/bife/bife++.git/commitdiff
Added an initial experimental implementation of BIFE in C++.
authorLeandro Lucarella <llucax@gmail.com>
Mon, 11 Aug 2003 16:23:00 +0000 (16:23 +0000)
committerLeandro Lucarella <llucax@gmail.com>
Mon, 11 Aug 2003 16:23:00 +0000 (16:23 +0000)
16 files changed:
Doxyfile [new file with mode: 0644]
Makefile [new file with mode: 0644]
chit.cpp [new file with mode: 0644]
chit.h [new file with mode: 0644]
ghit.cpp [new file with mode: 0644]
ghit.h [new file with mode: 0644]
hash.h [new file with mode: 0644]
hit.cpp [new file with mode: 0644]
hit.h [new file with mode: 0644]
main.cpp [new file with mode: 0644]
test.tpl [new file with mode: 0644]
test.tpl.html [new file with mode: 0644]
tests/test.cpp [new file with mode: 0644]
tests/test2_stringize.cpp [new file with mode: 0644]
tpldir/test2.tpl [new file with mode: 0644]
tpldir/test2.tpl.html [new file with mode: 0644]

diff --git a/Doxyfile b/Doxyfile
new file mode 100644 (file)
index 0000000..4d73fde
--- /dev/null
+++ b/Doxyfile
@@ -0,0 +1,207 @@
+# Doxyfile 1.3.2
+
+#---------------------------------------------------------------------------
+# General configuration options
+#---------------------------------------------------------------------------
+PROJECT_NAME           = BIFE++
+PROJECT_NUMBER         = 0.1
+OUTPUT_DIRECTORY       = 
+OUTPUT_LANGUAGE        = Spanish
+USE_WINDOWS_ENCODING   = NO
+EXTRACT_ALL            = YES
+EXTRACT_PRIVATE        = YES
+EXTRACT_STATIC         = YES
+EXTRACT_LOCAL_CLASSES  = YES
+HIDE_UNDOC_MEMBERS     = NO
+HIDE_UNDOC_CLASSES     = NO
+HIDE_FRIEND_COMPOUNDS  = NO
+HIDE_IN_BODY_DOCS      = NO
+BRIEF_MEMBER_DESC      = YES
+REPEAT_BRIEF           = YES
+ALWAYS_DETAILED_SEC    = NO
+INLINE_INHERITED_MEMB  = NO
+FULL_PATH_NAMES        = NO
+STRIP_FROM_PATH        = 
+INTERNAL_DOCS          = YES
+CASE_SENSE_NAMES       = YES
+SHORT_NAMES            = NO
+HIDE_SCOPE_NAMES       = NO
+SHOW_INCLUDE_FILES     = YES
+JAVADOC_AUTOBRIEF      = YES
+MULTILINE_CPP_IS_BRIEF = NO
+DETAILS_AT_TOP         = NO
+INHERIT_DOCS           = YES
+INLINE_INFO            = YES
+SORT_MEMBER_DOCS       = YES
+DISTRIBUTE_GROUP_DOC   = NO
+TAB_SIZE               = 8
+GENERATE_TODOLIST      = YES
+GENERATE_TESTLIST      = YES
+GENERATE_BUGLIST       = YES
+GENERATE_DEPRECATEDLIST= YES
+ALIASES                = 
+ENABLED_SECTIONS       = 
+MAX_INITIALIZER_LINES  = 30
+OPTIMIZE_OUTPUT_FOR_C  = NO
+OPTIMIZE_OUTPUT_JAVA   = NO
+SHOW_USED_FILES        = YES
+#---------------------------------------------------------------------------
+# configuration options related to warning and progress messages
+#---------------------------------------------------------------------------
+QUIET                  = NO
+WARNINGS               = YES
+WARN_IF_UNDOCUMENTED   = YES
+WARN_IF_DOC_ERROR      = YES
+WARN_FORMAT            = "$file:$line: $text"
+WARN_LOGFILE           = 
+#---------------------------------------------------------------------------
+# configuration options related to the input files
+#---------------------------------------------------------------------------
+INPUT                  = 
+FILE_PATTERNS          = 
+RECURSIVE              = YES
+EXCLUDE                = 
+EXCLUDE_SYMLINKS       = NO
+EXCLUDE_PATTERNS       = 
+EXAMPLE_PATH           = 
+EXAMPLE_PATTERNS       = 
+EXAMPLE_RECURSIVE      = NO
+IMAGE_PATH             = 
+INPUT_FILTER           = 
+FILTER_SOURCE_FILES    = NO
+#---------------------------------------------------------------------------
+# configuration options related to source browsing
+#---------------------------------------------------------------------------
+SOURCE_BROWSER         = YES
+INLINE_SOURCES         = NO
+STRIP_CODE_COMMENTS    = YES
+REFERENCED_BY_RELATION = YES
+REFERENCES_RELATION    = YES
+VERBATIM_HEADERS       = YES
+#---------------------------------------------------------------------------
+# configuration options related to the alphabetical class index
+#---------------------------------------------------------------------------
+ALPHABETICAL_INDEX     = YES
+COLS_IN_ALPHA_INDEX    = 5
+IGNORE_PREFIX          = 
+#---------------------------------------------------------------------------
+# configuration options related to the HTML output
+#---------------------------------------------------------------------------
+GENERATE_HTML          = YES
+HTML_OUTPUT            = html
+HTML_FILE_EXTENSION    = .html
+HTML_HEADER            = 
+HTML_FOOTER            = 
+HTML_STYLESHEET        = 
+HTML_ALIGN_MEMBERS     = YES
+GENERATE_HTMLHELP      = NO
+CHM_FILE               = 
+HHC_LOCATION           = 
+GENERATE_CHI           = NO
+BINARY_TOC             = NO
+TOC_EXPAND             = NO
+DISABLE_INDEX          = NO
+ENUM_VALUES_PER_LINE   = 4
+GENERATE_TREEVIEW      = YES
+TREEVIEW_WIDTH         = 250
+#---------------------------------------------------------------------------
+# configuration options related to the LaTeX output
+#---------------------------------------------------------------------------
+GENERATE_LATEX         = NO
+LATEX_OUTPUT           = latex
+LATEX_CMD_NAME         = latex
+MAKEINDEX_CMD_NAME     = makeindex
+COMPACT_LATEX          = NO
+PAPER_TYPE             = a4wide
+EXTRA_PACKAGES         = 
+LATEX_HEADER           = 
+PDF_HYPERLINKS         = NO
+USE_PDFLATEX           = NO
+LATEX_BATCHMODE        = NO
+LATEX_HIDE_INDICES     = NO
+#---------------------------------------------------------------------------
+# configuration options related to the RTF output
+#---------------------------------------------------------------------------
+GENERATE_RTF           = NO
+RTF_OUTPUT             = rtf
+COMPACT_RTF            = NO
+RTF_HYPERLINKS         = NO
+RTF_STYLESHEET_FILE    = 
+RTF_EXTENSIONS_FILE    = 
+#---------------------------------------------------------------------------
+# configuration options related to the man page output
+#---------------------------------------------------------------------------
+GENERATE_MAN           = NO
+MAN_OUTPUT             = man
+MAN_EXTENSION          = .3
+MAN_LINKS              = NO
+#---------------------------------------------------------------------------
+# configuration options related to the XML output
+#---------------------------------------------------------------------------
+GENERATE_XML           = NO
+XML_OUTPUT             = xml
+XML_SCHEMA             = 
+XML_DTD                = 
+#---------------------------------------------------------------------------
+# configuration options for the AutoGen Definitions output
+#---------------------------------------------------------------------------
+GENERATE_AUTOGEN_DEF   = NO
+#---------------------------------------------------------------------------
+# configuration options related to the Perl module output
+#---------------------------------------------------------------------------
+GENERATE_PERLMOD       = NO
+PERLMOD_LATEX          = NO
+PERLMOD_PRETTY         = YES
+PERLMOD_MAKEVAR_PREFIX = 
+#---------------------------------------------------------------------------
+# Configuration options related to the preprocessor   
+#---------------------------------------------------------------------------
+ENABLE_PREPROCESSING   = YES
+MACRO_EXPANSION        = NO
+EXPAND_ONLY_PREDEF     = NO
+SEARCH_INCLUDES        = YES
+INCLUDE_PATH           = 
+INCLUDE_FILE_PATTERNS  = 
+PREDEFINED             = 
+EXPAND_AS_DEFINED      = 
+SKIP_FUNCTION_MACROS   = YES
+#---------------------------------------------------------------------------
+# Configuration::addtions related to external references   
+#---------------------------------------------------------------------------
+TAGFILES               = 
+GENERATE_TAGFILE       = 
+ALLEXTERNALS           = NO
+EXTERNAL_GROUPS        = YES
+PERL_PATH              = /usr/bin/perl
+#---------------------------------------------------------------------------
+# Configuration options related to the dot tool   
+#---------------------------------------------------------------------------
+CLASS_DIAGRAMS         = YES
+HIDE_UNDOC_RELATIONS   = YES
+HAVE_DOT               = YES
+CLASS_GRAPH            = YES
+COLLABORATION_GRAPH    = YES
+UML_LOOK               = YES
+TEMPLATE_RELATIONS     = YES
+INCLUDE_GRAPH          = YES
+INCLUDED_BY_GRAPH      = YES
+CALL_GRAPH             = YES
+GRAPHICAL_HIERARCHY    = YES
+DOT_IMAGE_FORMAT       = png
+DOT_PATH               = 
+DOTFILE_DIRS           = 
+MAX_DOT_GRAPH_WIDTH    = 1024
+MAX_DOT_GRAPH_HEIGHT   = 1024
+MAX_DOT_GRAPH_DEPTH    = 0
+GENERATE_LEGEND        = YES
+DOT_CLEANUP            = YES
+#---------------------------------------------------------------------------
+# Configuration::addtions related to the search engine   
+#---------------------------------------------------------------------------
+SEARCHENGINE           = NO
+CGI_NAME               = search.cgi
+CGI_URL                = 
+DOC_URL                = 
+DOC_ABSPATH            = 
+BIN_ABSPATH            = /usr/local/bin/
+EXT_DOC_PATHS          = 
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..70a59e6
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,5 @@
+CPPFLAGS=-g3
+
+all: main
+
+main: hit.o ghit.o chit.o
diff --git a/chit.cpp b/chit.cpp
new file mode 100644 (file)
index 0000000..f456cde
--- /dev/null
+++ b/chit.cpp
@@ -0,0 +1,18 @@
+// vim: set expandtab tabstop=4 shiftwidth=4:
+
+#include "chit.h"
+
+CHIT::CHIT(string root, string postfix, string group) {
+    GHIT(root, postfix, group);
+}
+
+string CHIT::getFileContent(string filename) {
+    Hash::iterator c = cache.find(filename);
+    if (c == cache.end()) {
+        cache[filename] = GHIT::getFileContent(filename);
+        return cache[filename];
+    } else {
+        return c->second;
+    }
+}
+
diff --git a/chit.h b/chit.h
new file mode 100644 (file)
index 0000000..a246288
--- /dev/null
+++ b/chit.h
@@ -0,0 +1,49 @@
+// vim: set expandtab tabstop=4 shiftwidth=4:
+
+#ifndef _BIFE_CHIT_H_
+#define _BIFE_CHIT_H_
+
+#include <string>
+#include "hash.h"
+#include "ghit.h"
+
+using namespace std;
+
+/**
+ * Cache enabled GHIT.
+ *
+ * GHIT with cache capabilities added. The templates are stored in memory once
+ * they are readed and reused in succesive parse() calls, avoiding disc reads
+ * overhead.
+ *
+ * @todo See if it's really usefull, since the OS is supposed to be in charge
+ *       of disc cache.
+ */
+class CHIT: public GHIT {
+    // Attributes.
+    protected:
+        /// Cache storage.
+        Hash cache;
+
+    // Methods.
+    protected:
+        /**
+         * Gets cached file content.
+         *
+         * @param  filename Name of the file to get the content.
+         * @return File content.
+         */
+        virtual string getFileContent(string);
+
+    public:
+        /**
+         * Constructor.
+         *
+         * @param root    Root directory from where to get the templates.
+         * @param postfix Postfix of the template files.
+         * @param group   Starting group.
+         */
+        CHIT(string = ".", string = ".tpl", string = "");
+};
+
+#endif
diff --git a/ghit.cpp b/ghit.cpp
new file mode 100644 (file)
index 0000000..05e17bf
--- /dev/null
+++ b/ghit.cpp
@@ -0,0 +1,27 @@
+// vim: set expandtab tabstop=4 shiftwidth=4:
+
+#include "ghit.h"
+
+GHIT::GHIT(string root, string postfix, string group) {
+    HIT(root, postfix);
+    this->group.push(group);
+}
+
+string GHIT::getFileName(string blockname) {
+    return string(root + '/' + group.top() + '/' + blockname + postfix);
+}
+
+void GHIT::pushGroup(string group = "") {
+    this->group.push(group);
+}
+
+string GHIT::popGroup() {
+    string g = group.top();
+    group.pop();
+    return g;
+}
+
+string GHIT::getGroup() {
+    return group.top();
+}
+
diff --git a/ghit.h b/ghit.h
new file mode 100644 (file)
index 0000000..42e25c3
--- /dev/null
+++ b/ghit.h
@@ -0,0 +1,68 @@
+// vim: set expandtab tabstop=4 shiftwidth=4:
+
+#ifndef _BIFE_GHIT_H_
+#define _BIFE_GHIT_H_
+
+#include <string>
+#include <stack>
+#include "hit.h"
+
+using namespace std;
+
+/**
+ * Group enabled HIT.
+ *
+ * HIT template with groups of templates added. You can specify a template
+ * group. The group is used as a subdirectory in root template directory, so you
+ * can group templates together. The default group ('') templates are stored in
+ * the root directory.
+ */
+class GHIT: public HIT {
+    // Attributes.
+    protected:
+        /// Group stack.
+        stack<string> group;
+
+    // Methods.
+    protected:
+        /**
+         * Gets file name based on the blockname and the group.
+         *
+         * @param  blockname Name of the block to get the filename.
+         * @return Block's filename.
+         */
+        virtual string getFileName(string);
+
+    public:
+        /**
+         * Constructor.
+         *
+         * @param root    Root directory from where to get the templates.
+         * @param postfix Postfix of the template files.
+         * @param group   Starting group.
+         */
+        GHIT(string = ".", string = ".tpl", string = "");
+
+        /**
+         * Starts working with a new group of templates.
+         *
+         * @param group Group of templates to work with.
+         */
+        virtual void pushGroup(string);
+
+        /**
+         * Stops working with a group of templates.
+         *
+         * @return Last template's group used.
+         */
+        virtual string popGroup(void);
+
+        /**
+         * Gets the current working group.
+         *
+         * @return Current template's group.
+         */
+        virtual string getGroup(void);
+};
+
+#endif
diff --git a/hash.h b/hash.h
new file mode 100644 (file)
index 0000000..bf69ab2
--- /dev/null
+++ b/hash.h
@@ -0,0 +1,14 @@
+// vim: set expandtab tabstop=4 shiftwidth=4:
+
+#ifndef _BIFE_HASH_H_
+#define _BIFE_HASH_H_
+
+#include <string>
+#include <map>
+
+using namespace std;
+
+/// String hash similar to high level languages (like perl, python or php).
+typedef map<string, string> Hash;
+
+#endif
diff --git a/hit.cpp b/hit.cpp
new file mode 100644 (file)
index 0000000..590fbcf
--- /dev/null
+++ b/hit.cpp
@@ -0,0 +1,51 @@
+// vim: set expandtab tabstop=4 shiftwidth=4:
+
+#include "hit.h"
+
+#ifdef DEBUG
+#include <iostream>
+#endif
+
+string HIT::getFileName(string blockname) {
+    return string(root + '/' + blockname + postfix);
+}
+
+string HIT::getFileContent(string filename) {
+    stringbuf buff;
+    ifstream in(filename.c_str());
+    // FIXME - Verificar apertura.
+    if (!in) {
+#ifdef DEBUG
+        cerr << "Can't read template file '" << filename << "'." << endl;
+#endif
+        return buff.str();
+    }
+    while (in.get(buff)) {
+        in.ignore();
+    }
+    in.close();
+    return buff.str();
+}
+
+HIT::HIT(string root, string postfix): root(root), postfix(postfix) {}
+
+string HIT::parse(string blockname, Hash& vars) {
+    string content = getFileContent(getFileName(blockname));
+    for (Hash::iterator i = vars.begin(); i != vars.end(); i++) {
+        string key = "{" + i->first +  "}";
+        int pos = -1;
+        while ((pos = content.find(key)) != -1) {
+#ifdef DEBUG
+            cout << "Founded at pos " << pos << ", key '" << key << "' (len: "
+                << key.length() << "). Will be replaced with '" << i->second
+                << "'" << endl;
+#endif
+            content.replace(pos, key.length(), i->second);
+#ifdef DEBUG
+            cout << "New content: " << content << endl << endl << endl;
+#endif
+        }
+    }
+    return content;
+}
+
diff --git a/hit.h b/hit.h
new file mode 100644 (file)
index 0000000..b24ee81
--- /dev/null
+++ b/hit.h
@@ -0,0 +1,69 @@
+// vim: set expandtab tabstop=4 shiftwidth=4:
+
+#ifndef _BIFE_HIT_H_
+#define _BIFE_HIT_H_
+
+#include <string>
+#include <fstream>
+#include <sstream>
+#include "hash.h"
+
+using namespace std;
+
+/**
+ * Hooks vs IT Template Engine.
+ *
+ * Hooks vs IT (HIT) is a simple template implementation, based on hooks
+ * and IT template systems.
+ *
+ * @todo Implementar buffers?
+ */
+class HIT {
+    // Attributes.
+    public:
+        /// Root directory where to search for templates.
+        string root;
+        /// Postfix added to the blockname to convert it to a filename.
+        string postfix;
+        // bool search_path = false
+        // TODO - Para subclases
+        //Hash buffer;
+
+    // Methods.
+    protected:
+        /**
+         * Gets file name based on the blockname.
+         *
+         * @param  blockname Name of the block to get the filename.
+         * @return Block's filename.
+         */
+        virtual string getFileName(string);
+
+        /**
+         * Gets file content.
+         *
+         * @param  filename Name of the file to get the content.
+         * @return File content.
+         */
+        virtual string getFileContent(string);
+
+    public:
+        /**
+         * Constructor.
+         *
+         * @param root    Root directory from where to get the templates.
+         * @param postfix Postfix of the template files.
+         */
+        HIT(string = ".", string = ".tpl");
+
+        /**
+         * Parses a block replacing keys with values in the hash.
+         *
+         * @param  blockname Name of the block to parse.
+         * @param  vars      Hash containing the variable names and their values.
+         * @return Parsed block with variables replaced.
+         */
+        string parse(string, Hash&);
+};
+
+#endif
diff --git a/main.cpp b/main.cpp
new file mode 100644 (file)
index 0000000..5a4351d
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,48 @@
+// vim: set expandtab tabstop=4 shiftwidth=4:
+
+#include <string>
+#include <iostream>
+#include <sstream>
+#include "hit.h"
+#include "ghit.h"
+#include "chit.h"
+
+int main(void) {
+    const string indent = "    ";
+    Hash vars;
+    vars["NOMBRE"] = "Luca man";
+    vars["EDAD"]   = "21";
+
+    cout << "HIT example:" << endl;
+    cout << "============" << endl;
+    HIT h;
+    cout << indent << h.parse("test", vars) << endl << endl << endl;
+
+    cout << "GHIT example:" << endl;
+    cout << "=============" << endl;
+    vars["NOMBRE"] = "Pedro";
+    vars["EDAD"]   = "26";
+    GHIT g;
+    cout << "We are using the default group: '" << g.getGroup() << "'." << endl;
+    cout << indent << g.parse("test", vars) << endl;
+    g.pushGroup("tpldir");
+    cout << "We start using the '" << g.getGroup() << "' group." << endl;
+    cout << indent << g.parse("test2", vars) << endl;
+    cout << "We stop using '";
+    cout << g.popGroup() << "' group, and go back to default one ('";
+    cout << g.getGroup() << "')." << endl;
+    cout << indent << g.parse("test", vars) << endl << endl << endl;
+
+    cout << "CHIT example:" << endl;
+    cout << "=============" << endl;
+    CHIT c;
+    const int n = 65;
+    for (int i = n; i < (n+10); i++) {
+        stringstream ssi, ssc;
+        ssi << i;
+        ssc << char(i);
+        vars["NOMBRE"] = "Chit gay " + ssc.str();
+        vars["EDAD"]   = ssi.str();
+        cout << indent << c.parse("test", vars) << endl;
+    }
+}
diff --git a/test.tpl b/test.tpl
new file mode 100644 (file)
index 0000000..b17428a
--- /dev/null
+++ b/test.tpl
@@ -0,0 +1 @@
+Hola {NOMBRE}, como estás? Vos sos {NOMBRE} el de {EDAD} años, no?
diff --git a/test.tpl.html b/test.tpl.html
new file mode 100644 (file)
index 0000000..d16de26
--- /dev/null
@@ -0,0 +1,8 @@
+<html>
+    <head>
+        <title>{NOMBRE}</title>
+    </head>
+    <body>
+        <p>{NOMBRE} tiene {EDAD} años.</p>
+    </body>
+</html>
diff --git a/tests/test.cpp b/tests/test.cpp
new file mode 100644 (file)
index 0000000..3891953
--- /dev/null
@@ -0,0 +1,34 @@
+//: C03:Specify.cpp
+// Demonstrates the use of specifiers
+#include <iostream>
+using namespace std;
+int main() {
+  char c;
+  unsigned char cu;
+  int i;
+  unsigned int iu;
+  short int is;
+  short iis; // Same as short int
+  unsigned short int isu;
+  unsigned short iisu;
+  long int il;
+  long iil;  // Same as long int
+  unsigned long int ilu;
+  unsigned long iilu;
+  float f;
+  double d;
+  long double ld;
+  cout 
+    << "\n char= " << sizeof(c)
+    << "\n unsigned char = " << sizeof(cu)
+    << "\n int = " << sizeof(i)
+    << "\n unsigned int = " << sizeof(iu)
+    << "\n short = " << sizeof(is)
+    << "\n unsigned short = " << sizeof(isu)
+    << "\n long = " << sizeof(il) 
+    << "\n unsigned long = " << sizeof(ilu)
+    << "\n float = " << sizeof(f)
+    << "\n double = " << sizeof(d)
+    << "\n long double = " << sizeof(ld) 
+    << endl;
+}
diff --git a/tests/test2_stringize.cpp b/tests/test2_stringize.cpp
new file mode 100644 (file)
index 0000000..a034661
--- /dev/null
@@ -0,0 +1,12 @@
+//: C03:StringizingExpressions.cpp
+#include <iostream>
+#include <cassert>
+using namespace std;
+#define P(A) cout << #A << ": " << (A) << endl;
+int main() {
+    int a = 1, b = 2, c = 3;
+    P(a); P(b); P(c);
+    P(a + b);
+    assert(a + b == 3); // corta el programa si a + b != 3
+    P((c - a)/b);
+}
diff --git a/tpldir/test2.tpl b/tpldir/test2.tpl
new file mode 100644 (file)
index 0000000..dd39337
--- /dev/null
@@ -0,0 +1 @@
+Hola, soy otra template y sé que tu nombre es {NOMBRE} y tenés {EDAD} años!
diff --git a/tpldir/test2.tpl.html b/tpldir/test2.tpl.html
new file mode 100644 (file)
index 0000000..7bb8a85
--- /dev/null
@@ -0,0 +1,8 @@
+<html>
+    <head>
+           <title>Página de {NOMBRE}</title>
+    </head>
+    <body>
+        <p>Mi nombre es {NOMBRE} y tengo {EDAD} años.</p>
+    </body>
+</html>