Hello All,
As you all probably know there is no HDF node attribute manipulating
facilities for templates. It may be so because nobody needs them, but, well,
it seems we need them. So I've just implemented a prototype function for
retrieving HDF node attributes.
I'm not sure if it is the most proper way of doing this, and I'm not
proposing it for inclusion in the project, but it works. So in case somebody
else needs it, here is the patch.
---:<---
Index: cs/csparse.c
===================================================================
--- cs/csparse.c (revision 48632)
+++ cs/csparse.c (working copy)
@@ -3636,6 +3636,57 @@
return STATUS_OK;
}
+
+/* Retrieve variable attribute */
+static NEOERR * _builtin_attr(CSPARSE *parse, CS_FUNCTION *csf, CSARG *arg,
CSARG *result)
+{
+ NEOERR *err;
+ CSARG node;
+ char *attr_name;
+ HDF *obj;
+ HDF_ATTR *attr;
+
+ /* Evaluate first (node) argument */
+ memset(&node, 0, sizeof(node));
+ err = eval_expr(parse, arg, &node);
+ if (err) return nerr_pass(err);
+
+ /* Evaluate second (attribute name) argument and convert to string */
+ memset(&attr, 0, sizeof(attr));
+ err = cs_arg_parse(parse, arg->next, "s", &attr_name);
+ if (err)
+ {
+ if (node.alloc) free(node.s);
+ return nerr_pass(err);
+ }
+
+ /* NOTE: assuming "result" is zeroed for us */
+ result->op_type = CS_TYPE_STRING;
+ result->s = "";
+
+ /* If the node argument is a node name and there is an attribute name */
+ if ((node.op_type & CS_TYPE_VAR) && attr_name != NULL)
+ {
+ /* Lookup node */
+ obj = var_lookup_obj(parse, node.s);
+
+ /* Lookup node attribute */
+ for (attr = hdf_obj_attr(obj); attr != NULL; attr = attr->next)
+ if (strcmp(attr->key, attr_name) == 0)
+ {
+ if (attr->value != NULL)
+ result->s = attr->value;
+ break;
+ }
+ }
+
+ free(attr_name);
+ if (node.alloc) free(node.s);
+
+ return STATUS_OK;
+}
+
+
/* Check to see if a local variable is the first in an each/loop sequence */
static NEOERR * _builtin_first(CSPARSE *parse, CS_FUNCTION *csf, CSARG *args,
CSARG *result)
@@ -3987,6 +4038,7 @@
{ "len", 1, _builtin_subcount },
{ "subcount", 1, _builtin_subcount },
{ "name", 1, _builtin_name },
+ { "attr", 2, _builtin_attr },
{ "first", 1, _builtin_first },
{ "last", 1, _builtin_last },
{ "abs", 1, _builtin_abs },
--->:---
Sincerely,
Nick