From: Leandro Lucarella Date: Sun, 14 Mar 2004 19:30:32 +0000 (+0000) Subject: Se version inicial del nona (solo tiene fast, sistema de templates implementado con... X-Git-Tag: svn_import X-Git-Url: https://git.llucax.com/software/bife/fast.git/commitdiff_plain?ds=sidebyside Se version inicial del nona (solo tiene fast, sistema de templates implementado con templates). --- 64239820c49ad491dc1d2c6235c336ffe8c75032 diff --git a/.tm_project.cache b/.tm_project.cache new file mode 100644 index 0000000..2ad9148 Binary files /dev/null and b/.tm_project.cache differ diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..0fb7f0a --- /dev/null +++ b/Makefile.am @@ -0,0 +1,29 @@ +## vim: set noexpandtab tabstop=4 shiftwidth=4: +##---------------------------------------------------------------------------- +## fast +##---------------------------------------------------------------------------- +## This file is part of fast. +## +## fast is free software; you can redistribute it and/or modify it under the +## terms of the GNU General Public License as published by the Free Software +## Foundation; either version 2 of the License, or (at your option) any later +## version. +## +## fast is distributed in the hope that it will be useful, but WITHOUT ANY +## WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along +## with fast; if not, write to the Free Software Foundation, Inc., 59 Temple +## Place, Suite 330, Boston, MA 02111-1307 USA +##---------------------------------------------------------------------------- +## Creado: sáb feb 7 17:22:25 ART 2004 +## Autores: Leandro Lucarella +##---------------------------------------------------------------------------- +## +## $Id: Makefile.am 247 2003-11-20 20:26:01Z luca $ +## + +SUBDIRS = src tests + diff --git a/configure.in b/configure.in new file mode 100644 index 0000000..214b1c7 --- /dev/null +++ b/configure.in @@ -0,0 +1,68 @@ +## vim: set noexpandtab tabstop=4 shiftwidth=4: +##---------------------------------------------------------------------------- +## fast +##---------------------------------------------------------------------------- +## This file is part of fast. +## +## fast is free software; you can redistribute it and/or modify it under the +## terms of the GNU General Public License as published by the Free Software +## Foundation; either version 2 of the License, or (at your option) any later +## version. +## +## fast is distributed in the hope that it will be useful, but WITHOUT ANY +## WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along +## with fast; if not, write to the Free Software Foundation, Inc., 59 Temple +## Place, Suite 330, Boston, MA 02111-1307 USA +##---------------------------------------------------------------------------- +## Creado: sáb feb 7 17:22:50 ART 2004 +## Autores: Leandro Lucarella +##---------------------------------------------------------------------------- +## +## $Id: configure.in 450 2003-12-05 14:57:11Z luca $ +## + +AC_INIT(configure.in) +AM_INIT_AUTOMAKE(fast, 0.1) +AM_MAINTAINER_MODE + +dnl Prevents setting flags. +CXXFLAGS="" + +AC_ISC_POSIX +AC_PROG_CXX +AM_PROG_CC_STDC +AC_PROG_RANLIB +AC_HEADER_STDC + +dnl Debugging turned on +AC_MSG_CHECKING(for debugging) +AC_ARG_ENABLE(debug,[ --enable-debug compile for debugging]) +AC_MSG_RESULT([$enable_debug]) +if test "x$enable_debug" = "xyes" ; then +CXXFLAGS="${CXXFLAGS} -g -DDEBUG" +fi + +## pkg_modules="libgnomeui-2.0" +##PKG_CHECK_MODULES(PACKAGE, +## libxml-2.0 >= 0.15.0 \ +## libxml++-1.0 >= 0.15.0 \ +## gthread-2.0 \ +## sigc++-1.2) + +AC_SUBST(PACKAGE_CFLAGS) +AC_SUBST(PACKAGE_LIBS) + +AC_OUTPUT([ +Makefile +src/Makefile +tests/Makefile +]) + +echo +echo "fast is now ready for compilation. Just run make." +echo + diff --git a/include/fast/basic_template.h b/include/fast/basic_template.h new file mode 100644 index 0000000..56fe706 --- /dev/null +++ b/include/fast/basic_template.h @@ -0,0 +1,174 @@ +// vim: set expandtab tabstop=4 shiftwidth=4: +//---------------------------------------------------------------------------- +// fast +//---------------------------------------------------------------------------- +// This file is part of fast. +// +// fast is free software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// fast is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with fast; if not, write to the Free Software Foundation, Inc., 59 Temple +// Place, Suite 330, Boston, MA 02111-1307 USA +//---------------------------------------------------------------------------- +// Creado: sáb feb 7 20:04:43 ART 2004 +// Autores: Leandro Lucarella +//---------------------------------------------------------------------------- +// +// $Id$ +// + +#ifndef FAST_BASIC_TEMPLATE_H +#define FAST_BASIC_TEMPLATE_H + +//#include "fast/string.h" +//#include "fast/string_map.h" +//#include +#include + +namespace fast { + +/** + * bate - BAse Template Engine. + * + * bate is the base template engine implemented in faste. + */ +template < class F, class S, class M > +class basic_template { + + // Types. + public: + /// Cache traits type. + typedef F file_content_type; + /// String type. + typedef S string_type; + /// Map type. + typedef M< string_type, string_type > map_type; + + // Attributes. + private: + /// Root directory where to search for templates. + string_type _root; + /// Postfix added to the blockname to convert it to a filename. + string_type _postfix; + /// Function object to get the contents of a "file". + file_content_type _file_content; + /// Group stack. + std::stack< string_type > _group; + + // Methods. + protected: + /** + * Gets a filename based on the block. + * + * @param block Name of the block to get the filename. + * + * @return Block's filename. + */ + string_type build_filename(const string_type& block) const + { + string_type group = _group.top(); + if (group.length()) group += '/'; + return _root + '/' + group + block + _postfix; + } + + public: + /** + * Constructor. + * + * @param root Root directory from where to get the templates. + * @param postfix Postfix of the template files. + * @param group Starting group. + * @param file_content Function object to get the contents of a "file". + */ + basic_template(const string_type& root = ".", + const string_type& postfix = ".tpl", const string_type& group = "", + const file_content_type& file_content = file_content_type()) + : _root(root), _postfix(postfix), _file_content(file_content) + { _group.push(group); } + + /// Gets templates root directory. + string_type root(void) const + { return _root; } + + /// Sets templates root directory. + void root(const string_type& root) + { _root = root; } + + /// Gets templates postfix. + string_type postfix(void) const + { return _postfix; } + + /// Sets templates postfix. + void postfix(const string_type& postfix) + { _postfix = postfix; } + + /** + * Gets the current working group. + * + * @return Current template's group. + */ + string_type group(void) const + { return _group.top(); } + + /** + * Starts working with a new group of templates. + * + * @param group Group of templates to work with. + */ + void push_group(const string_type& group) + { _group.push(group); } + + /** + * Stops working with a group of templates. + * + * @return Last template's group used. + */ + string_type pop_group(void) + { + string_type group = _group.top(); + _group.pop(); + return group; + } + + /** + * Parses a block replacing keys with values in the hash. + * + * @param block Name of the block to parse. + * @param vars Hash containing the variable names and their values. + * + * @return Parsed block with variables replaced. + */ + string_type parse(const string_type& block, const map_type& vars) const + { + // TODO - el build_filename() debería ser parte de la política + // de reemplazo? o del file_contents? + // TODO: capturar excepción (pensar política para reportar errores) + string_type content = _file_content(build_filename(block)); + for (map_type::const_iterator i = vars.begin(); + i != vars.end(); ++i) { + // TODO - Parametrizar los 'separadores' y toda la política + // de search & replace en un template para hacerlo más + // flexible e implementar cosas como funciones de + // transformación, por ej. {upper(KEYWORD)} + string_type key = string("{") + i->first + "}"; + string_type::size_type pos; + while ((pos = content.find(key)) != string_type::npos) { + content.replace(pos, key.length(), i->second); + } + } + return content; + } + +}; // class basic_template + +} // namespace fast + +#endif // FAST_BASIC_TEMPLATE_H diff --git a/include/fast/get_file_content.h b/include/fast/get_file_content.h new file mode 100644 index 0000000..912d028 --- /dev/null +++ b/include/fast/get_file_content.h @@ -0,0 +1,48 @@ +// vim: set expandtab tabstop=4 shiftwidth=4: +//---------------------------------------------------------------------------- +// fast +//---------------------------------------------------------------------------- +// This file is part of fast. +// +// fast is free software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// fast is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with fast; if not, write to the Free Software Foundation, Inc., 59 Temple +// Place, Suite 330, Boston, MA 02111-1307 USA +//---------------------------------------------------------------------------- +// Creado: sáb feb 7 20:13:34 ART 2004 +// Autores: Leandro Lucarella +//---------------------------------------------------------------------------- +// +// $Id$ +// + +#ifndef FAST_GET_FILE_CONTENT_H +#define FAST_GET_FILE_CONTENT_H + +#include "fast/string.h" + +namespace fast { + +/** + * Gets file content. + * + * @param filename Name of the file to get the content. + * @return File content. + */ +class get_file_content { + public: + string operator()(const string&) const; +}; + +} // namespace fast + +#endif // FAST_GET_FILE_CONTENT_H diff --git a/include/fast/get_file_content_cached.h b/include/fast/get_file_content_cached.h new file mode 100644 index 0000000..4de30da --- /dev/null +++ b/include/fast/get_file_content_cached.h @@ -0,0 +1,52 @@ +// vim: set expandtab tabstop=4 shiftwidth=4: +//---------------------------------------------------------------------------- +// fast +//---------------------------------------------------------------------------- +// This file is part of fast. +// +// fast is free software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// fast is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with fast; if not, write to the Free Software Foundation, Inc., 59 Temple +// Place, Suite 330, Boston, MA 02111-1307 USA +//---------------------------------------------------------------------------- +// Creado: sáb feb 7 20:04:43 ART 2004 +// Autores: Leandro Lucarella +//---------------------------------------------------------------------------- +// +// $Id$ +// + +#ifndef FAST_GET_FILE_CONTENT_CACHED_H +#define FAST_GET_FILE_CONTENT_CACHED_H + +#include "fast/string.h" +#include "fast/string_map.h" +#include "fast/get_file_content.h" + +namespace fast { + +/** + * Gets file content from a cache. + * + * @param filename Name of the file to get the content. + * @return File content. + */ +class get_file_content_cached { + private: + mutable string_map _cache; + public: + string operator()(const string&) const; +}; + +} // namespace fast + +#endif // FAST_GET_FILE_CONTENT_CACHED_H diff --git a/include/fast/string.h b/include/fast/string.h new file mode 100644 index 0000000..d65f7c0 --- /dev/null +++ b/include/fast/string.h @@ -0,0 +1,41 @@ +// vim: set expandtab tabstop=4 shiftwidth=4: +//---------------------------------------------------------------------------- +// fast +//---------------------------------------------------------------------------- +// This file is part of fast. +// +// fast is free software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// fast is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with fast; if not, write to the Free Software Foundation, Inc., 59 Temple +// Place, Suite 330, Boston, MA 02111-1307 USA +//---------------------------------------------------------------------------- +// Creado: sáb feb 7 22:21:00 ART 2004 +// Autores: Leandro Lucarella +//---------------------------------------------------------------------------- +// +// $Id$ +// + + +#ifndef FAST_STRING_H +#define FAST_STRING_H + +#include + +namespace fast { + +/// String. +typedef std::string string; + +} // namespace fast + +#endif // FAST_STRING_H diff --git a/include/fast/string_map.h b/include/fast/string_map.h new file mode 100644 index 0000000..6418dae --- /dev/null +++ b/include/fast/string_map.h @@ -0,0 +1,41 @@ +// vim: set expandtab tabstop=4 shiftwidth=4: +//---------------------------------------------------------------------------- +// fast +//---------------------------------------------------------------------------- +// This file is part of fast. +// +// fast is free software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// fast is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with fast; if not, write to the Free Software Foundation, Inc., 59 Temple +// Place, Suite 330, Boston, MA 02111-1307 USA +//---------------------------------------------------------------------------- +// Creado: sáb feb 7 22:23:59 ART 2004 +// Autores: Leandro Lucarella +//---------------------------------------------------------------------------- +// +// $Id$ +// + +#ifndef FAST_STRING_MAP_H +#define FAST_STRING_MAP_H + +#include "fast/string.h" +#include + +namespace fast { + +/// String map used to describe a set of variables. +typedef std::map< string, string > string_map; + +} // namespace fast + +#endif // FAST_STRING_MAP_H diff --git a/src/get_file_content.cpp b/src/get_file_content.cpp new file mode 100644 index 0000000..a30844a --- /dev/null +++ b/src/get_file_content.cpp @@ -0,0 +1,49 @@ +// vim: set expandtab tabstop=4 shiftwidth=4 fdm=marker: +//---------------------------------------------------------------------------- +// fast +//---------------------------------------------------------------------------- +// This file is part of fast. +// +// fast is free software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// fast is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with fast; if not, write to the Free Software Foundation, Inc., 59 Temple +// Place, Suite 330, Boston, MA 02111-1307 USA +//---------------------------------------------------------------------------- +// Creado: sáb feb 7 23:19:07 ART 2004 +// Autores: Leandro Lucarella +//---------------------------------------------------------------------------- +// +// $Id$ +// + +//{{{ include +#include "fast/get_file_content.h" +#include +#include +#include +//}}} + +//{{{ get_file_content +fast::string fast::get_file_content::operator()(const string& filename) const +{ + std::ifstream in(filename.c_str()); + // TODO - Verificar apertura. + // - Tirar excepción si pasa algo (21.3.6 Stroustrup). + if (!in) { + throw std::runtime_error(filename); + } + std::stringstream out; + out << in.rdbuf(); + return out.str(); +} +//}}} get_file_content + diff --git a/src/get_file_content_cached.cpp b/src/get_file_content_cached.cpp new file mode 100644 index 0000000..b758e50 --- /dev/null +++ b/src/get_file_content_cached.cpp @@ -0,0 +1,48 @@ +// vim: set expandtab tabstop=4 shiftwidth=4 fdm=marker: +//---------------------------------------------------------------------------- +// fast +//---------------------------------------------------------------------------- +// This file is part of fast. +// +// fast is free software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// fast is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with fast; if not, write to the Free Software Foundation, Inc., 59 Temple +// Place, Suite 330, Boston, MA 02111-1307 USA +//---------------------------------------------------------------------------- +// Creado: sáb feb 7 20:04:43 ART 2004 +// Autores: Leandro Lucarella +//---------------------------------------------------------------------------- +// +// $Id$ +// + +//{{{ include +#include "fast/get_file_content_cached.h" +#include "fast/get_file_content.h" +//}}} + +//{{{ get_file_content_cached +fast::string fast::get_file_content_cached::operator()(const string& filename) + const +{ + string_map::const_iterator c = _cache.find(filename); + if (c == _cache.end()) { // Not found. + // Get file contents using regular function object. + get_file_content file; + _cache[filename] = file(filename); + return _cache[filename]; + } else { + return c->second; + } +} +//}}} get_file_content_cached + diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..c61d780 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,4 @@ +CXXFLAGS=-Wall -g -I../include #-DDEBUG + +bate: fast.cpp ../src/get_file_content.cpp ../include/fast/basic_template.h + $(CXX) $(CXXFLAGS) -o fast fast.cpp ../src/get_file_content.cpp diff --git a/tests/fast b/tests/fast new file mode 100755 index 0000000..8c17c2f Binary files /dev/null and b/tests/fast differ diff --git a/tests/fast.cpp b/tests/fast.cpp new file mode 100644 index 0000000..b484aec --- /dev/null +++ b/tests/fast.cpp @@ -0,0 +1,28 @@ +// vim: set expandtab tabstop=4 shiftwidth=4: + +#include "fast/basic_template.h" +#include "fast/get_file_content.h" +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + std::set_terminate(__gnu_cxx::__verbose_terminate_handler); + if (argc < 3) { + std::cerr << "Argument missing." << std::endl; + return 1; + } else if (!(argc % 2)) { // is even + std::cerr << "Malformed arguments." << std::endl; + return 2; + } + fast::basic_template< fast::get_file_content, std::string, std::map > tpl(argv[1]); + std::string block = argv[2]; + fast::map_type vars; + for (int i = 3; i < argc; ++i) { + vars[argv[i]] = argv[++i]; + } + std::cout << tpl.parse(block, vars); + return 0; +} + diff --git a/tests/test.tpl b/tests/test.tpl new file mode 100644 index 0000000..0ab3bcf --- /dev/null +++ b/tests/test.tpl @@ -0,0 +1,3 @@ +Hola, me llamo {NOMBRE} y soy un {CLASE}. + +El que lee es {ALGO}.