grim/gplate

Parents 70d3469157ae
Children 7dc2dc131276
Added some more functions for tags in the library.
Made gplate-template compile with the new functions in the library
--- a/gplate/gplate-library.c Sun Feb 10 01:10:06 2008 -0600
+++ b/gplate/gplate-library.c Sun Feb 10 02:11:03 2008 -0600
@@ -488,6 +488,51 @@
return function;
}
+/**
+ * gplate_library_lookup_tag:
+ * @tag: The GType of the tag to lookup.
+ * @prefix: The return address for the prefix.
+ * @suffix: The return address for the suffix.
+ * @error: The return address for any errors.
+ *
+ * Looks for @tag in the library. If the tag is found, TRUE is returned and
+ * @prefix and @suffix will be set. NULL can be passed for @prefix and
+ * @suffix. If @prefix and @suffix are non-NULL they must be #g_free'd.
+ *
+ * If @tag is not found in the library, FALSE will be returned and @error will
+ * be set.
+ *
+ * Return Value: TRUE on success, FALSE otherwise.
+ */
+gboolean
+gplate_library_lookup_tag(GType tag, gchar **prefix, gchar **suffix,
+ GError **error)
+{
+ GPlateTagData *data = NULL;
+
+ gplate_library_lazy_init();
+
+ data = g_hash_table_lookup(tags, GSIZE_TO_POINTER(tag));
+ if(!data) {
+ if(error) {
+ *error = g_error_new(GPLATE_DOMAIN,
+ GPLATE_ERROR_LIBRARY_TAG_NOT_FOUND,
+ "Tag '%s' was not found in the library",
+ g_type_name(tag));
+ }
+
+ return FALSE;
+ }
+
+ if(prefix)
+ *prefix = (data->prefix) ? g_strdup(data->prefix) : NULL;
+
+ if(suffix)
+ *suffix = (data->suffix) ? g_strdup(data->suffix) : NULL;
+
+ return TRUE;
+}
+
/******************************************************************************
* For each stuff
*****************************************************************************/
--- a/gplate/gplate-library.h Sun Feb 10 01:10:06 2008 -0600
+++ b/gplate/gplate-library.h Sun Feb 10 02:11:03 2008 -0600
@@ -42,6 +42,7 @@
gboolean gplate_library_lookup_function(const gchar *name, GType *function, GType *tag, GError **error);
GType gplate_library_lookup_function_for_tag(const gchar *name, GType tag, GError **error);
+gboolean gplate_library_lookup_tag(GType tag, gchar **prefix, gchar **suffix, GError **error);
void gplate_library_tags_foreach(GPlateLibraryTagsForeachFunc func, gpointer data);
G_END_DECLS
--- a/gplate/gplate-template.c Sun Feb 10 01:10:06 2008 -0600
+++ b/gplate/gplate-template.c Sun Feb 10 02:11:03 2008 -0600
@@ -22,12 +22,9 @@
#include <gplate/gplate-template.h>
-#include <gplate/gplate-code-tag.h>
-#include <gplate/gplate-comment-tag.h>
#include <gplate/gplate-dictionary-variable.h>
#include <gplate/gplate-errors.h>
-#include <gplate/gplate-text-tag.h>
-#include <gplate/gplate-variable-tag.h>
+#include <gplate/gplate-library.h>
#include <stdio.h>
#include <string.h>
@@ -43,8 +40,6 @@
* Structs
*****************************************************************************/
typedef struct {
- GHashTable *tags;
-
GType fall_through;
gchar *pattern;
@@ -54,50 +49,12 @@
GList *tokens;
} GPlateTemplatePrivate;
-typedef struct {
- GType type;
- gchar *prefix;
- gchar *suffix;
-} GPlateTemplateTagInfo;
-
/******************************************************************************
* Globals
*****************************************************************************/
static GObjectClass *parent_class = NULL;
/******************************************************************************
- * TagInfo Stuff
- *****************************************************************************/
-static GPlateTemplateTagInfo *
-gplate_template_tag_info_new(GType type) {
- GPlateTemplateTagInfo *info = NULL;
- GPlateTagClass *tag_class = NULL;
- const gchar *prefix, *suffix;
-
- tag_class = g_type_class_ref(type);
-
- prefix = gplate_tag_class_get_prefix(tag_class);
- suffix = gplate_tag_class_get_suffix(tag_class);
-
- info = g_new(GPlateTemplateTagInfo, 1);
-
- info->type = type;
- info->prefix = (prefix) ? g_strdup(prefix) : NULL;
- info->suffix = (suffix) ? g_strdup(suffix) : NULL;
-
- g_type_class_unref(tag_class);
-
- return info;
-}
-
-static void
-gplate_template_tag_info_free(GPlateTemplateTagInfo *info) {
- g_free(info->prefix);
- g_free(info->suffix);
- g_free(info);
-}
-
-/******************************************************************************
* Collection Stuff
*****************************************************************************/
static GPlateVariable *
@@ -129,92 +86,21 @@
/******************************************************************************
* Template Stuff
*****************************************************************************/
-static gboolean
-gplate_template_real_add_tag(GPlateTemplate *tplate, GType tag, GError **e) {
- GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
- GPlateTemplateTagInfo *info = NULL;
-
- if(g_hash_table_lookup(priv->tags, GSIZE_TO_POINTER(tag))) {
- if(e) {
- *e =
- g_error_new(GPLATE_DOMAIN, GPLATE_ERROR_TAG_EXISTS,
- "Tag '%s' has already been added.\n",
- g_type_name(tag));
- }
-
- return FALSE;
- }
-
- info = gplate_template_tag_info_new(tag);
-
- if(info->prefix && info->suffix) {
- /* standard tag */
- g_hash_table_insert(priv->tags, GSIZE_TO_POINTER(tag), info);
- } else if(!info->prefix && !info->suffix) {
- /* this should be a fall through tag */
- if(priv->fall_through != G_TYPE_INVALID) {
- if(e) {
- *e = g_error_new(GPLATE_DOMAIN,
- GPLATE_ERROR_TAG_FALL_THROUGH_EXISTS,
- "A fall through tag named '%s' has alread "
- "been registered.\n",
- g_type_name(priv->fall_through));
- }
-
- gplate_template_tag_info_free(info);
-
- return FALSE;
- }
-
- priv->fall_through = tag;
- g_hash_table_insert(priv->tags, GSIZE_TO_POINTER(tag), info);
- } else if(!info->prefix && info->suffix) {
- /* misconfigured tag, no prefix, but has suffix */
- if(e) {
- *e = g_error_new(GPLATE_DOMAIN,
- GPLATE_ERROR_TAG_NO_PREFIX_HAVE_SUFFIX,
- "Tag '%s' has no prefix, but has a suffix.\n",
- g_type_name(tag));
- }
-
- gplate_template_tag_info_free(info);
-
- return FALSE;
- } else if(info->prefix && !info->suffix) {
- /* misconfigured tag, has prefix, but no suffix */
- if(e) {
- *e = g_error_new(GPLATE_DOMAIN,
- GPLATE_ERROR_TAG_HAVE_PREFIX_NO_SUFFIX,
- "Tag '%s' has a prefix, but no suffix.\n",
- g_type_name(tag));
- }
-
- gplate_template_tag_info_free(info);
-
- return FALSE;
- }
-
- /* clean up pattern, if it exists, since it needs to be rebuilt */
- g_free(priv->pattern);
- priv->pattern = NULL;
-
- return TRUE;
-}
-
static void
-gplate_template_update_pattern_helper(gpointer k, gpointer v, gpointer d) {
- GPlateTemplateTagInfo *info = (GPlateTemplateTagInfo *)v;
+gplate_template_update_pattern_helper(GType tag, const gchar *prefix,
+ const gchar *suffix, gpointer d)
+{
GString *str = (GString *)d;
const gchar *sep = "";
/* don't add fall through tags to the regex */
- if(!info->prefix || !info->suffix)
+ if(!prefix || !suffix)
return;
if(str->len > 1)
sep = "|";
- g_string_append_printf(str, "%s%s.*?%s", sep, info->prefix, info->suffix);
+ g_string_append_printf(str, "%s%s.*?%s", sep, prefix, suffix);
}
static void
@@ -222,8 +108,7 @@
GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
GString *str = g_string_new("(");
- g_hash_table_foreach(priv->tags, gplate_template_update_pattern_helper,
- str);
+ gplate_library_tags_foreach(gplate_template_update_pattern_helper, str);
g_string_append_printf(str, "%s", ")");
@@ -238,17 +123,18 @@
} GPlateTemplateFindTagData;
static void
-gplate_template_find_tag_helper(gpointer k, gpointer v, gpointer d) {
+gplate_template_find_tag_helper(GType tag, const gchar *prefix,
+ const gchar *suffix, gpointer d)
+{
GPlateTemplateFindTagData *data = (GPlateTemplateFindTagData *)d;
- GPlateTemplateTagInfo *info = (GPlateTemplateTagInfo *)v;
gchar *pattern = NULL;
/* if our tag info doesn't have a prefix or a suffix, we drop out. */
- if(!info->prefix || !info->suffix)
+ if(!prefix || !suffix)
return;
/* create our pattern string */
- pattern = g_strdup_printf("%s.*?%s", info->prefix, info->suffix);
+ pattern = g_strdup_printf("%s.*?%s", prefix, suffix);
/* look for a match:
*
@@ -257,7 +143,7 @@
* ie: {{\nfoo\n}} would be treated as a fall through if not for it.
*/
if(g_regex_match_simple(pattern, data->contents, G_REGEX_DOTALL, 0))
- data->type = GPOINTER_TO_SIZE(k);
+ data->type = tag;
/* cleanup */
g_free(pattern);
@@ -271,7 +157,7 @@
d.contents = contents;
d.type = G_TYPE_INVALID;
- g_hash_table_foreach(priv->tags, gplate_template_find_tag_helper, &d);
+ gplate_library_tags_foreach(gplate_template_find_tag_helper, &d);
return (d.type == G_TYPE_INVALID) ? priv->fall_through : d.type;
}
@@ -297,19 +183,18 @@
for(i = 0; matches[i]; i++) {
GPlateTag *tag = NULL;
- GPlateTemplateTagInfo *info = NULL;
GType type = G_TYPE_INVALID;
+ gchar *prefix = NULL, *suffix = NULL;
if(g_utf8_strlen(matches[i], -1) <= 0)
continue;
type = gplate_template_find_tag(tplate, matches[i]);
- info = g_hash_table_lookup(priv->tags, GSIZE_TO_POINTER(type));
- if(!info)
- continue;
+ if(!gplate_library_lookup_tag(type, &prefix, &suffix, error))
+ break;
- if(!info->prefix && !info->suffix) {
+ if(!prefix && !suffix) {
/* it's a fall through tag */
tag = g_object_new(type,
"contents", matches[i],
@@ -320,7 +205,7 @@
GMatchInfo *match = NULL;
gchar *pattern = NULL, *contents = NULL;
- pattern = g_strdup_printf("%s(.*?)%s", info->prefix, info->suffix);
+ pattern = g_strdup_printf("%s(.*?)%s", prefix, suffix);
regex2 = g_regex_new(pattern,
G_REGEX_OPTIMIZE | G_REGEX_DOTALL,
@@ -382,8 +267,6 @@
gplate_template_finalize(GObject *obj) {
GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(obj);
- g_hash_table_destroy(priv->tags);
-
g_free(priv->pattern);
g_object_unref(priv->vars);
@@ -401,7 +284,6 @@
obj_class->finalize = gplate_template_finalize;
- klass->add_tag = gplate_template_real_add_tag;
klass->tokenize = gplate_template_real_tokenize;
}
@@ -409,10 +291,6 @@
gplate_template_init(GTypeInstance *self, gpointer klass) {
GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(self);
- priv->tags =
- g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
- (GDestroyNotify)gplate_template_tag_info_free);
-
priv->fall_through = G_TYPE_INVALID;
priv->pattern = NULL;
@@ -468,62 +346,10 @@
gplate_template_new(void) {
GPlateTemplate *tplate = g_object_new(GPLATE_TYPE_TEMPLATE, NULL);
- gplate_template_add_tag(tplate, GPLATE_TYPE_CODE_TAG, NULL);
- gplate_template_add_tag(tplate, GPLATE_TYPE_COMMENT_TAG, NULL);
- gplate_template_add_tag(tplate, GPLATE_TYPE_TEXT_TAG, NULL);
- gplate_template_add_tag(tplate, GPLATE_TYPE_VARIABLE_TAG, NULL);
-
return tplate;
}
/**
- * gplate_template_add_tag:
- * @tplate: The #GPlateTemplate.
- * @tag: The GType of the tag.
- * @error: Return address for any errors.
- *
- * Adds #tag to the template.
- *
- * Return Value: TRUE on success, other FALSE.
- */
-gboolean
-gplate_template_add_tag(GPlateTemplate *tplate, GType tag, GError **error) {
- GPlateTemplateClass *klass = NULL;
-
- g_return_val_if_fail(GPLATE_IS_TEMPLATE(tplate), FALSE);
-
- if(!g_type_is_a(tag, GPLATE_TYPE_TAG)) {
- if(error) {
- *error =
- g_error_new(GPLATE_DOMAIN, GPLATE_ERROR_TYPE_IS_NOT_DESCENDANT,
- "type '%s' is not a child of '%s'.\n",
- g_type_name(tag),
- g_type_name(GPLATE_TYPE_TAG));
- }
-
- return FALSE;
- }
-
- if(G_TYPE_IS_ABSTRACT(tag)) {
- if(error) {
- *error =
- g_error_new(GPLATE_DOMAIN, GPLATE_ERROR_TYPE_IS_ABSTRACT,
- "type '%s' is abstract.\n",
- g_type_name(tag));
- }
-
- return FALSE;
- }
-
- klass = GPLATE_TEMPLATE_GET_CLASS(tplate);
-
- if(klass && klass->add_tag)
- return klass->add_tag(tplate, tag, error);
-
- return FALSE;
-}
-
-/**
* gplate_template_tokenize:
* @tplate: The #GPlateTemplate.
* @tplate_string: The string to be translated.
--- a/gplate/gplate-template.h Sun Feb 10 01:10:06 2008 -0600
+++ b/gplate/gplate-template.h Sun Feb 10 02:11:03 2008 -0600
@@ -46,8 +46,6 @@
struct _GPlateTemplateClass {
GObjectClass parent;
- gboolean (*add_tag)(GPlateTemplate *tplate, GType tag, GError **error);
-
GList *(*tokenize)(GPlateTemplate *tplate, const gchar *tplate_string, GError **error);
GPlateTag *(*first_tag)(GPlateTemplate *tplate);
@@ -69,8 +67,6 @@
GPlateTemplate *gplate_template_new(void);
-gboolean gplate_template_add_tag(GPlateTemplate *tplate, GType tag, GError **error);
-
GList *gplate_template_tokenize(GPlateTemplate *tplate, const gchar *tplate_string, GError **error);
gchar *gplate_template_render(GPlateTemplate *tplate, const gchar *tplate_string, GError **error);