From 0271add7a00a56a975c4e8da67fd3dbbeab2483b Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Wed, 13 Aug 2003 06:50:44 +0000 Subject: [PATCH] Cleaned up the code: - 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. --- chit.cpp | 19 +++++++++++++++++-- chit.h | 17 +++++++++++------ ghit.cpp | 21 ++++++++++++++++++--- ghit.h | 24 +++++++++++++++++------- hash.h | 8 +++----- hit.cpp | 36 +++++++++++++++++++++++++++--------- hit.h | 17 ++++++++++------- main.cpp | 6 ++++-- 8 files changed, 107 insertions(+), 41 deletions(-) diff --git a/chit.cpp b/chit.cpp index f456cde..4b09a9d 100644 --- a/chit.cpp +++ b/chit.cpp @@ -2,8 +2,23 @@ #include "chit.h" -CHIT::CHIT(string root, string postfix, string group) { - GHIT(root, postfix, group); +#ifdef DEBUG +#include +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) { diff --git a/chit.h b/chit.h index a246288..22776f5 100644 --- a/chit.h +++ b/chit.h @@ -1,13 +1,13 @@ // vim: set expandtab tabstop=4 shiftwidth=4: -#ifndef _BIFE_CHIT_H_ -#define _BIFE_CHIT_H_ +#ifndef BIFE_CHIT_H +#define BIFE_CHIT_H -#include -#include "hash.h" #include "ghit.h" +#include "hash.h" +#include -using namespace std; +using std::string; /** * Cache enabled GHIT. @@ -43,7 +43,12 @@ class CHIT: public 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 diff --git a/ghit.cpp b/ghit.cpp index 05e17bf..db05833 100644 --- a/ghit.cpp +++ b/ghit.cpp @@ -2,16 +2,31 @@ #include "ghit.h" -GHIT::GHIT(string root, string postfix, string group) { - HIT(root, postfix); +#ifdef DEBUG +#include +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); } diff --git a/ghit.h b/ghit.h index 42e25c3..32900a6 100644 --- a/ghit.h +++ b/ghit.h @@ -1,13 +1,13 @@ // vim: set expandtab tabstop=4 shiftwidth=4: -#ifndef _BIFE_GHIT_H_ -#define _BIFE_GHIT_H_ +#ifndef BIFE_GHIT_H +#define BIFE_GHIT_H -#include -#include #include "hit.h" +#include +#include -using namespace std; +using std::string; /** * Group enabled HIT. @@ -18,10 +18,15 @@ using namespace std; * the root directory. */ class GHIT: public HIT { + // Typedefs. + protected: + /// Group stack. + typedef std::stack GroupStack; + // Attributes. protected: /// Group stack. - stack group; + GroupStack group; // Methods. protected: @@ -41,7 +46,12 @@ class GHIT: public HIT { * @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. diff --git a/hash.h b/hash.h index bf69ab2..08f7adb 100644 --- a/hash.h +++ b/hash.h @@ -1,14 +1,12 @@ // vim: set expandtab tabstop=4 shiftwidth=4: -#ifndef _BIFE_HASH_H_ -#define _BIFE_HASH_H_ +#ifndef BIFE_HASH_H +#define BIFE_HASH_H #include #include -using namespace std; - /// String hash similar to high level languages (like perl, python or php). -typedef map Hash; +typedef std::map Hash; #endif diff --git a/hit.cpp b/hit.cpp index 590fbcf..a9bfd66 100644 --- a/hit.cpp +++ b/hit.cpp @@ -1,10 +1,29 @@ // vim: set expandtab tabstop=4 shiftwidth=4: #include "hit.h" +#include +#include + +using std::ifstream; +using std::stringbuf; #ifdef DEBUG #include +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); @@ -21,28 +40,27 @@ string HIT::getFileContent(string filename) { 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 } } diff --git a/hit.h b/hit.h index b24ee81..5086127 100644 --- a/hit.h +++ b/hit.h @@ -1,14 +1,12 @@ // vim: set expandtab tabstop=4 shiftwidth=4: -#ifndef _BIFE_HIT_H_ -#define _BIFE_HIT_H_ +#ifndef BIFE_HIT_H +#define BIFE_HIT_H -#include -#include -#include #include "hash.h" +#include -using namespace std; +using std::string; /** * Hooks vs IT Template Engine. @@ -54,7 +52,12 @@ class HIT { * @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. diff --git a/main.cpp b/main.cpp index 5a4351d..92fbb6a 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,8 @@ #include "ghit.h" #include "chit.h" +using namespace std; + int main(void) { const string indent = " "; Hash vars; @@ -22,7 +24,7 @@ int main(void) { 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"); @@ -35,7 +37,7 @@ int main(void) { 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; -- 2.43.0