grim/gplate

4464c1a04980
Parents 197c6e92dcdd
Children d841559018a2
Implemented the tag stack functions.
Added gplate_template_current_tag
Removed the pattern member variable from GPlateTemplate
--- a/gplate/gplate-template.c Sun Feb 10 22:14:32 2008 -0600
+++ b/gplate/gplate-template.c Mon Feb 11 03:44:52 2008 -0600
@@ -40,11 +40,10 @@
* Structs
*****************************************************************************/
typedef struct {
- gchar *pattern;
-
GPlateVariable *vars;
GList *tokens;
+ GList *current_token;
} GPlateTemplatePrivate;
/******************************************************************************
@@ -101,18 +100,20 @@
g_string_append_printf(str, "%s%s.*?%s", sep, prefix, suffix);
}
-static void
+static gchar *
gplate_template_update_pattern(GPlateTemplate *tplate) {
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
GString *str = g_string_new("(");
+ gchar *ret = NULL;
gplate_library_tags_foreach(gplate_template_update_pattern_helper, str);
g_string_append_printf(str, "%s", ")");
- priv->pattern = str->str;
+ ret = str->str;
g_string_free(str, FALSE);
+
+ return ret;
}
typedef struct {
@@ -166,18 +167,22 @@
gplate_template_real_tokenize(GPlateTemplate *tplate, const gchar *tplate_string,
GError **error)
{
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
GList *ret = NULL;
GRegex *regex1 = NULL;
gchar **matches = NULL;
+ gchar *pattern = NULL;
gint i = 0;
- if(!priv->pattern)
- gplate_template_update_pattern(tplate);
+ pattern = gplate_template_update_pattern(tplate);
- regex1 = g_regex_new(priv->pattern, G_REGEX_OPTIMIZE, 0, error);
- if(!regex1)
+ regex1 = g_regex_new(pattern, G_REGEX_OPTIMIZE, 0, error);
+ if(!regex1) {
+ g_free(pattern);
+
return NULL;
+ }
+
+ g_free(pattern);
matches = g_regex_split(regex1, tplate_string, 0);
@@ -260,17 +265,109 @@
return ret;
}
+static GPlateTag *
+gplate_template_real_first_tag(GPlateTemplate *tplate) {
+ GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
+ GPlateTag *tag = NULL;
+
+ priv->current_token = priv->tokens;
+
+ if(priv->current_token && GPLATE_IS_TAG(priv->current_token->data))
+ tag = GPLATE_TAG(priv->current_token->data);
+
+ return tag;
+}
+
+static GPlateTag *
+gplate_template_real_last_tag(GPlateTemplate *tplate) {
+ GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
+ GPlateTag *tag = NULL;
+
+ priv->current_token = g_list_last(priv->tokens);
+
+ if(priv->current_token && GPLATE_IS_TAG(priv->current_token->data))
+ tag = GPLATE_TAG(priv->current_token->data);
+
+ return tag;
+}
+
+static GPlateTag *
+gplate_template_real_current_tag(GPlateTemplate *tplate) {
+ GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
+ GPlateTag *tag = NULL;
+
+ if(priv->current_token && GPLATE_IS_TAG(priv->current_token->data))
+ tag = GPLATE_TAG(priv->current_token->data);
+
+ return tag;
+}
+
+static GPlateTag *
+gplate_template_real_next_tag(GPlateTemplate *tplate) {
+ GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
+ GPlateTag *tag = NULL;
+
+ priv->current_token = g_list_next(priv->current_token);
+
+ if(priv->current_token && GPLATE_IS_TAG(priv->current_token->data))
+ tag = GPLATE_TAG(priv->current_token->data);
+
+ return tag;
+}
+
+static GPlateTag *
+gplate_template_real_previous_tag(GPlateTemplate *tplate) {
+ GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
+ GPlateTag *tag = NULL;
+
+ priv->current_token = g_list_previous(priv->current_token);
+
+ if(priv->current_token && GPLATE_IS_TAG(priv->current_token->data))
+ tag = GPLATE_TAG(priv->current_token->data);
+
+ return tag;
+}
+
+static GPlateTag *
+gplate_template_real_nth_tag(GPlateTemplate *tplate, guint nth) {
+ GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
+ GPlateTag *tag = NULL;
+
+ priv->current_token = g_list_nth(priv->current_token, nth);
+
+ if(priv->current_token && GPLATE_IS_TAG(priv->current_token->data))
+ tag = GPLATE_TAG(priv->current_token->data);
+
+ return tag;
+}
+
+static GPlateTag *
+gplate_template_real_nth_previous_tag(GPlateTemplate *tplate, guint nth) {
+ GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
+ GPlateTag *tag = NULL;
+
+ priv->current_token = g_list_nth_prev(priv->current_token, nth);
+
+ if(priv->current_token && GPLATE_IS_TAG(priv->current_token->data))
+ tag = GPLATE_TAG(priv->current_token->data);
+
+ return tag;
+}
+
/******************************************************************************
* Object Stuff
*****************************************************************************/
static void
gplate_template_finalize(GObject *obj) {
GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(obj);
-
- g_free(priv->pattern);
+ GList *l = NULL;
g_object_unref(priv->vars);
+ for(l = priv->tokens; l; l = l->next)
+ g_object_unref(G_OBJECT(l->data));
+ g_list_free(priv->tokens);
+
G_OBJECT_CLASS(parent_class)->finalize(obj);
}
@@ -285,14 +382,20 @@
obj_class->finalize = gplate_template_finalize;
klass->tokenize = gplate_template_real_tokenize;
+
+ klass->first_tag = gplate_template_real_first_tag;
+ klass->last_tag = gplate_template_real_last_tag;
+ klass->current_tag = gplate_template_real_current_tag;
+ klass->next_tag = gplate_template_real_next_tag;
+ klass->previous_tag = gplate_template_real_previous_tag;
+ klass->nth_tag = gplate_template_real_nth_tag;
+ klass->nth_previous_tag = gplate_template_real_nth_previous_tag;
}
static void
gplate_template_init(GTypeInstance *self, gpointer klass) {
GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(self);
- priv->pattern = NULL;
-
priv->vars = gplate_dictionary_variable_new("dict");
}
@@ -491,6 +594,20 @@
}
GPlateTag *
+gplate_template_current_tag(GPlateTemplate *tplate) {
+ GPlateTemplateClass *klass = NULL;
+
+ g_return_val_if_fail(GPLATE_IS_TEMPLATE(tplate), NULL);
+
+ klass = GPLATE_TEMPLATE_GET_CLASS(tplate);
+
+ if(klass && klass->current_tag)
+ return klass->current_tag(tplate);
+
+ return NULL;
+}
+
+GPlateTag *
gplate_template_next_tag(GPlateTemplate *tplate) {
GPlateTemplateClass *klass = NULL;
@@ -519,7 +636,7 @@
}
GPlateTag *
-gplate_template_nth_tag(GPlateTemplate *tplate) {
+gplate_template_nth_tag(GPlateTemplate *tplate, guint nth) {
GPlateTemplateClass *klass = NULL;
g_return_val_if_fail(GPLATE_IS_TEMPLATE(tplate), NULL);
@@ -527,13 +644,13 @@
klass = GPLATE_TEMPLATE_GET_CLASS(tplate);
if(klass && klass->nth_tag)
- return klass->nth_tag(tplate);
+ return klass->nth_tag(tplate, nth);
return NULL;
}
GPlateTag *
-gplate_template_nth_previous_tag(GPlateTemplate *tplate) {
+gplate_template_nth_previous_tag(GPlateTemplate *tplate, guint nth) {
GPlateTemplateClass *klass = NULL;
g_return_val_if_fail(GPLATE_IS_TEMPLATE(tplate), NULL);
@@ -541,7 +658,7 @@
klass = GPLATE_TEMPLATE_GET_CLASS(tplate);
if(klass && klass->nth_previous_tag)
- return klass->nth_previous_tag(tplate);
+ return klass->nth_previous_tag(tplate, nth);
return NULL;
}
--- a/gplate/gplate-template.h Sun Feb 10 22:14:32 2008 -0600
+++ b/gplate/gplate-template.h Mon Feb 11 03:44:52 2008 -0600
@@ -50,10 +50,11 @@
GPlateTag *(*first_tag)(GPlateTemplate *tplate);
GPlateTag *(*last_tag)(GPlateTemplate *tplate);
+ GPlateTag *(*current_tag)(GPlateTemplate *tplate);
GPlateTag *(*next_tag)(GPlateTemplate *tplate);
GPlateTag *(*previous_tag)(GPlateTemplate *tplate);
- GPlateTag *(*nth_tag)(GPlateTemplate *tplate);
- GPlateTag *(*nth_previous_tag)(GPlateTemplate *tplate);
+ GPlateTag *(*nth_tag)(GPlateTemplate *tplate, guint nth);
+ GPlateTag *(*nth_previous_tag)(GPlateTemplate *tplate, guint nth);
void (*_gplate_reserved1)(void);
void (*_gplate_reserved2)(void);
@@ -74,10 +75,11 @@
GPlateTag *gplate_template_first_tag(GPlateTemplate *tplate);
GPlateTag *gplate_template_last_tag(GPlateTemplate *tplate);
+GPlateTag *gplate_template_current_tag(GPlateTemplate *tplate);
GPlateTag *gplate_template_next_tag(GPlateTemplate *tplate);
GPlateTag *gplate_template_previous_tag(GPlateTemplate *tplate);
-GPlateTag *gplate_template_nth_tag(GPlateTemplate *tplate);
-GPlateTag *gplate_template_nth_previous_tag(GPlateTemplate *tplate);
+GPlateTag *gplate_template_nth_tag(GPlateTemplate *tplate, guint nth);
+GPlateTag *gplate_template_nth_previous_tag(GPlateTemplate *tplate, guint nth);
G_END_DECLS