[augeas-devel] [PATCH 5/8] * src/pathx.c: add count() function

David Lutterkort lutter at redhat.com
Wed Feb 18 21:30:52 UTC 2009


---
 src/pathx.c       |   18 +++++++++++++++++-
 tests/xpath.tests |    3 +++
 2 files changed, 20 insertions(+), 1 deletions(-)

diff --git a/src/pathx.c b/src/pathx.c
index 09374e1..feca067 100644
--- a/src/pathx.c
+++ b/src/pathx.c
@@ -222,12 +222,18 @@ struct func {
 
 static void func_last(struct state *state);
 static void func_position(struct state *state);
+static void func_count(struct state *state);
+
+static const enum type const count_arg_types[] = { T_NODESET };
 
 static const struct func builtin_funcs[] = {
     { .name = "last", .arity = 0, .type = T_NUMBER, .arg_types = NULL,
       .impl = func_last },
     { .name = "position", .arity = 0, .type = T_NUMBER, .arg_types = NULL,
-      .impl = func_position }
+      .impl = func_position },
+    { .name = "count", .arity = 1, .type = T_NUMBER,
+      .arg_types = count_arg_types,
+      .impl = func_count }
 };
 
 #define CHECK_ERROR                                                     \
@@ -446,6 +452,16 @@ static void func_position(struct state *state) {
     push_value(vind, state);
 }
 
+static void func_count(struct state *state) {
+    value_ind_t vind;
+    struct value *ns = pop_value(state);
+
+    vind = make_value(T_NUMBER, state);
+    CHECK_ERROR;
+    state->value_pool[vind].number = ns->nodeset->used;
+    push_value(vind, state);
+}
+
 static int calc_eq_nodeset_nodeset(struct nodeset *ns1, struct nodeset *ns2,
                                    int neq) {
     for (int i1=0; i1 < ns1->used; i1++) {
diff --git a/tests/xpath.tests b/tests/xpath.tests
index 8031f23..33685d6 100644
--- a/tests/xpath.tests
+++ b/tests/xpath.tests
@@ -148,3 +148,6 @@ test nodeset-nodeset-eq /files/etc/sysconfig/network-scripts/*[BRIDGE = /files/e
 
 test last-ssh-service /files/etc/services/service-name[port = '22'][last()]
      /files/etc/services/service-name[24] = ssh
+
+test count-one-alias /files/etc/hosts/*[count(alias) = 1]
+     /files/etc/hosts/2
-- 
1.6.0.6




More information about the augeas-devel mailing list