- Better usage of namespaces.
- Added some new, class specific, types.
- Added (virtual) destructors.
- Added new constructor.
- Better constructor implementation (calling parent's constructor properly).
- Fixed a bug in HIT::getFileContent().
- Added (optional) debug info to constructors and destructors.
#include "chit.h"
-CHIT::CHIT(string root, string postfix, string group) {
- GHIT(root, postfix, group);
+#ifdef DEBUG
+#include <iostream>
+using std::cerr;
+using std::endl;
+#endif
+
+CHIT::CHIT(string root, string postfix, string group): GHIT(root, postfix, group) {
+#ifdef DEBUG
+ cerr << "In CHIT::CHIT(root = '" << root << "', postfix = '" << postfix
+ << "', group = '" << group << "');" << endl;
+#endif
+}
+
+CHIT::~CHIT(void) {
+#ifdef DEBUG
+ cerr << "In CHIT destructor." << endl;
+#endif
}
string CHIT::getFileContent(string filename) {
// vim: set expandtab tabstop=4 shiftwidth=4:
-#ifndef _BIFE_CHIT_H_
-#define _BIFE_CHIT_H_
+#ifndef BIFE_CHIT_H
+#define BIFE_CHIT_H
-#include <string>
-#include "hash.h"
#include "ghit.h"
+#include "hash.h"
+#include <string>
-using namespace std;
+using std::string;
/**
* Cache enabled GHIT.
* @param postfix Postfix of the template files.
* @param group Starting group.
*/
- CHIT(string = ".", string = ".tpl", string = "");
+ CHIT(string = string("."), string = string(".tpl"), string = string(""));
+
+ /**
+ * Destructor.
+ */
+ virtual ~CHIT(void);
};
#endif
#include "ghit.h"
-GHIT::GHIT(string root, string postfix, string group) {
- HIT(root, postfix);
+#ifdef DEBUG
+#include <iostream>
+using std::cerr;
+using std::endl;
+#endif
+
+GHIT::GHIT(string root, string postfix, string group): HIT(root, postfix) {
+#ifdef DEBUG
+ cerr << "In GHIT::GHIT(root = '" << root << "', postfix = '" << postfix
+ << "', group = '" << group << "');" << endl;
+#endif
this->group.push(group);
}
+GHIT::~GHIT(void) {
+#ifdef DEBUG
+ cerr << "In GHIT destructor." << endl;
+#endif
+}
+
string GHIT::getFileName(string blockname) {
return string(root + '/' + group.top() + '/' + blockname + postfix);
}
-void GHIT::pushGroup(string group = "") {
+void GHIT::pushGroup(string group) {
this->group.push(group);
}
// vim: set expandtab tabstop=4 shiftwidth=4:
-#ifndef _BIFE_GHIT_H_
-#define _BIFE_GHIT_H_
+#ifndef BIFE_GHIT_H
+#define BIFE_GHIT_H
-#include <string>
-#include <stack>
#include "hit.h"
+#include <stack>
+#include <string>
-using namespace std;
+using std::string;
/**
* Group enabled HIT.
* the root directory.
*/
class GHIT: public HIT {
+ // Typedefs.
+ protected:
+ /// Group stack.
+ typedef std::stack<string> GroupStack;
+
// Attributes.
protected:
/// Group stack.
- stack<string> group;
+ GroupStack group;
// Methods.
protected:
* @param postfix Postfix of the template files.
* @param group Starting group.
*/
- GHIT(string = ".", string = ".tpl", string = "");
+ GHIT(string = string("."), string = string(".tpl"), string = string(""));
+
+ /**
+ * Destructor.
+ */
+ virtual ~GHIT(void);
/**
* Starts working with a new group of templates.
// vim: set expandtab tabstop=4 shiftwidth=4:
-#ifndef _BIFE_HASH_H_
-#define _BIFE_HASH_H_
+#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;
+typedef std::map<std::string, std::string> Hash;
#endif
// vim: set expandtab tabstop=4 shiftwidth=4:
#include "hit.h"
+#include <fstream>
+#include <sstream>
+
+using std::ifstream;
+using std::stringbuf;
#ifdef DEBUG
#include <iostream>
+using std::cerr;
+using std::endl;
+#endif
+
+HIT::HIT(string root, string postfix): root(root), postfix(postfix) {
+#ifdef DEBUG
+ cerr << "In HIT::HIT(root = '" << root << "', postfix = '" << postfix << "')" << endl;
+#endif
+}
+
+HIT::~HIT(void) {
+#ifdef DEBUG
+ cerr << "In HIT destructor." << endl;
#endif
+}
string HIT::getFileName(string blockname) {
return string(root + '/' + blockname + postfix);
return buff.str();
}
while (in.get(buff)) {
- in.ignore();
+ buff.sputc(in.get());
}
in.close();
return buff.str();
}
-HIT::HIT(string root, string postfix): root(root), postfix(postfix) {}
-
string HIT::parse(string blockname, Hash& vars) {
+ int pos;
+ string key;
string content = getFileContent(getFileName(blockname));
for (Hash::iterator i = vars.begin(); i != vars.end(); i++) {
- string key = "{" + i->first + "}";
- int pos = -1;
+ key = "{" + i->first + "}";
while ((pos = content.find(key)) != -1) {
-#ifdef DEBUG
- cout << "Founded at pos " << pos << ", key '" << key << "' (len: "
+#ifdef DEBUG2
+ cerr << "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;
+#ifdef DEBUG2
+ cerr << "New content: " << content << endl << endl << endl;
#endif
}
}
// vim: set expandtab tabstop=4 shiftwidth=4:
-#ifndef _BIFE_HIT_H_
-#define _BIFE_HIT_H_
+#ifndef BIFE_HIT_H
+#define BIFE_HIT_H
-#include <string>
-#include <fstream>
-#include <sstream>
#include "hash.h"
+#include <string>
-using namespace std;
+using std::string;
/**
* Hooks vs IT Template Engine.
* @param root Root directory from where to get the templates.
* @param postfix Postfix of the template files.
*/
- HIT(string = ".", string = ".tpl");
+ HIT(string = string("."), string = string(".tpl"));
+
+ /**
+ * Destructor.
+ */
+ virtual ~HIT(void);
/**
* Parses a block replacing keys with values in the hash.
#include "ghit.h"
#include "chit.h"
+using namespace std;
+
int main(void) {
const string indent = " ";
Hash vars;
cout << "=============" << endl;
vars["NOMBRE"] = "Pedro";
vars["EDAD"] = "26";
- GHIT g;
+ GHIT g("./././");
cout << "We are using the default group: '" << g.getGroup() << "'." << endl;
cout << indent << g.parse("test", vars) << endl;
g.pushGroup("tpldir");
cout << "CHIT example:" << endl;
cout << "=============" << endl;
- CHIT c;
+ CHIT c("././././././", ".tpl.html");
const int n = 65;
for (int i = n; i < (n+10); i++) {
stringstream ssi, ssc;