grim/gplate

Parents 652090b4be42
Children b98475b93435
Include function implementation, not yet tested, but it compiles!
--- a/gplate/Makefile.am Sun Feb 17 18:49:40 2008 -0600
+++ b/gplate/Makefile.am Tue Feb 19 02:41:10 2008 -0600
@@ -13,6 +13,7 @@
gplate-dictionary-variable.h \
gplate-errors.h \
gplate-function.h \
+ gplate-include-function.h \
gplate-library.h \
gplate-noop-function.h \
gplate-object-variable.h \
@@ -31,6 +32,7 @@
gplate-dictionary-variable.c \
gplate-errors.c \
gplate-function.c \
+ gplate-include-function.c \
gplate-library.c \
gplate-noop-function.c \
gplate-print-function.c \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gplate/gplate-include-function.c Tue Feb 19 02:41:10 2008 -0600
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2007-2008 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This program 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.
+ *
+ * This program 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
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include <gplate/gplate-include-function.h>
+
+#include <gplate/gplate-collection.h>
+#include <gplate/gplate-variable.h>
+
+/******************************************************************************
+ * IncludeFunction Stuff
+ *****************************************************************************/
+static gchar *
+gplate_include_function_evaluate(GPlateFunction *function,
+ GPlateTemplate *tplate,
+ const gchar *contents)
+{
+ GRegex *regex = NULL;
+ GMatchInfo *info = NULL;
+ gchar *ret = NULL;
+
+ regex = g_regex_new("\"(.+)\"", 0, 0, NULL);
+
+ if(g_regex_match(regex, contents, 0, &info)) {
+ gchar *file = g_match_info_fetch(info, 1);
+
+ ret = gplate_template_render_file(tplate, file, NULL);
+
+ g_match_info_free(info);
+ g_free(file);
+ } else {
+ GPlateVariable *variable = NULL;
+
+ variable = gplate_collection_find_variable(GPLATE_COLLECTION(tplate),
+ contents);
+
+ if(GPLATE_IS_VARIABLE(variable)) {
+ const gchar *value = gplate_variable_get_value(variable);
+
+ ret = gplate_template_render(tplate, value, NULL);
+
+ g_object_unref(G_OBJECT(variable));
+ } else {
+ ret = g_strdup("");
+ }
+ }
+
+ return ret;
+}
+
+/******************************************************************************
+ * Object Stuff
+ *****************************************************************************/
+static void
+gplate_include_function_class_init(GPlateIncludeFunctionClass *klass) {
+ GPlateFunctionClass *func_class = GPLATE_FUNCTION_CLASS(klass);
+
+ func_class->evaluate = gplate_include_function_evaluate;
+}
+
+/******************************************************************************
+ * GPlateIncludeFunction API
+ *****************************************************************************/
+GType
+gplate_include_function_get_gtype(void) {
+ static GType type = 0;
+
+ if(type == 0) {
+ static const GTypeInfo info = {
+ sizeof(GPlateIncludeFunctionClass),
+ NULL,
+ NULL,
+ (GClassInitFunc)gplate_include_function_class_init,
+ NULL,
+ NULL,
+ sizeof(GPlateIncludeFunction),
+ 0,
+ NULL,
+ };
+
+ type = g_type_register_static(GPLATE_TYPE_FUNCTION,
+ "GPlateIncludeFunction",
+ &info, 0);
+ }
+
+ return type;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gplate/gplate-include-function.h Tue Feb 19 02:41:10 2008 -0600
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2007-2008 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This program 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.
+ *
+ * This program 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
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+#ifndef GPLATE_INCLUDE_FUNCTION_H
+#define GPLATE_INCLUDE_FUNCTION_H
+
+#include <gplate/gplate-function.h>
+
+#define GPLATE_TYPE_INCLUDE_FUNCTION (gplate_include_function_get_gtype())
+#define GPLATE_INCLUDE_FUNCTION(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GPLATE_TYPE_INCLUDE_FUNCTION, GPlateIncludeFunction))
+#define GPLATE_INCLUDE_FUNCTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GPLATE_TYPE_INCLUDE_FUNCTION, GPlateIncludeFunctionClass))
+#define GPLATE_IS_INCLUDE_FUNCTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GPLATE_TYPE_INCLUDE_FUNCTION))
+#define GPLATE_IS_INCLUDE_FUNCTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GPLATE_TYPE_INCLUDE_FUNCTION))
+#define GPLATE_INCLUDE_FUNCTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GPLATE_TYPE_INCLUDE_FUNCTION, GPlateIncludeFunctionClass))
+
+typedef struct _GPlateIncludeFunction GPlateIncludeFunction;
+typedef struct _GPlateIncludeFunctionClass GPlateIncludeFunctionClass;
+
+struct _GPlateIncludeFunction {
+ GPlateFunction gparent;
+
+ void (*_gplate_reserved1)(void);
+ void (*_gplate_reserved2)(void);
+ void (*_gplate_reserved3)(void);
+ void (*_gplate_reserved4)(void);
+};
+
+struct _GPlateIncludeFunctionClass {
+ GPlateFunctionClass parent;
+
+ void (*_gplate_reserved1)(void);
+ void (*_gplate_reserved2)(void);
+ void (*_gplate_reserved3)(void);
+ void (*_gplate_reserved4)(void);
+};
+
+G_BEGIN_DECLS
+
+GType gplate_include_function_get_gtype(void);
+
+G_END_DECLS
+
+#endif /* GPLATE_INCLUDE_FUNCTION_H */