[augeas-devel] [PATCH 6/8] Move path formatting functions around

David Lutterkort lutter at redhat.com
Wed Feb 25 21:58:36 UTC 2009


Move path_expand and format_path to internal.c
Rename format_path to path_of_tree
---
 src/augeas.c   |   62 +------------------------------------------------------
 src/internal.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/internal.h |    2 +
 3 files changed, 58 insertions(+), 60 deletions(-)

diff --git a/src/augeas.c b/src/augeas.c
index 1ac3df9..af2117b 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -44,64 +44,6 @@ static const char *const static_nodes[][2] = {
     { AUGEAS_META_TREE "/version/save/mode[4]", AUG_SAVE_OVERWRITE_TEXT }
 };
 
-static const char *pretty_label(const struct tree *tree) {
-    if (tree == NULL)
-        return "(no_tree)";
-    else if (tree->label == NULL)
-        return "(none)";
-    else
-        return tree->label;
-}
-
-static char *path_expand(struct tree *tree, const char *ppath) {
-    struct tree *siblings = tree->parent->children;
-
-    char *path;
-    const char *label;
-    int cnt = 0, ind = 0, r;
-
-    list_for_each(t, siblings) {
-        if (streqv(t->label, tree->label)) {
-            cnt += 1;
-            if (t == tree)
-                ind = cnt;
-        }
-    }
-
-    if (ppath == NULL)
-        ppath = "";
-
-    label = pretty_label(tree);
-    if (cnt > 1) {
-        r = asprintf(&path, "%s/%s[%d]", ppath, label, ind);
-    } else {
-        r = asprintf(&path, "%s/%s", ppath, label);
-    }
-    if (r == -1)
-        return NULL;
-    return path;
-}
-
-static char *format_path(struct tree *tree) {
-    int depth, i;
-    struct tree *t, **anc;
-    char *path = NULL;
-
-    for (t = tree, depth = 1; ! ROOT_P(t); depth++, t = t->parent);
-    if (ALLOC_N(anc, depth) < 0)
-        return NULL;
-
-    for (t = tree, i = depth - 1; i >= 0; i--, t = t->parent)
-        anc[i] = t;
-
-    for (i = 0; i < depth; i++) {
-        char *p = path_expand(anc[i], path);
-        free(path);
-        path = p;
-    }
-    return path;
-}
-
 /* Propagate dirty flags towards the root */
 static int tree_propagate_dirty(struct tree *tree) {
     if (tree->dirty)
@@ -578,7 +520,7 @@ int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
     for (tree = pathx_first(p); tree != NULL; tree = pathx_next(p)) {
         if (TREE_HIDDEN(tree))
             continue;
-        (*matches)[i] = format_path(tree);
+        (*matches)[i] = path_of_tree(tree);
         if ((*matches)[i] == NULL) {
             goto error;
         }
@@ -761,7 +703,7 @@ static int print_tree(FILE *out, struct pathx *p, int pr_hidden) {
         if (TREE_HIDDEN(tree) && ! pr_hidden)
             continue;
 
-        path = format_path(tree);
+        path = path_of_tree(tree);
         if (path == NULL)
             goto error;
         r = print_one(out, path, tree->value);
diff --git a/src/internal.c b/src/internal.c
index 2e88f9c..ca1df4e 100644
--- a/src/internal.c
+++ b/src/internal.c
@@ -305,7 +305,61 @@ int close_memstream(struct memstream *ms) {
     return 0;
 }
 
+char *path_expand(struct tree *tree, const char *ppath) {
+    struct tree *siblings = tree->parent->children;
+
+    char *path;
+    const char *label;
+    int cnt = 0, ind = 0, r;
+
+    list_for_each(t, siblings) {
+        if (streqv(t->label, tree->label)) {
+            cnt += 1;
+            if (t == tree)
+                ind = cnt;
+        }
+    }
+
+    if (ppath == NULL)
+        ppath = "";
+
+    if (tree == NULL)
+        label = "(no_tree)";
+    else if (tree->label == NULL)
+        label = "(none)";
+    else
+        label = tree->label;
 
+    if (cnt > 1) {
+        r = asprintf(&path, "%s/%s[%d]", ppath, label, ind);
+    } else {
+        r = asprintf(&path, "%s/%s", ppath, label);
+    }
+    if (r == -1)
+        return NULL;
+    return path;
+}
+
+char *path_of_tree(struct tree *tree) {
+    int depth, i;
+    struct tree *t, **anc;
+    char *path = NULL;
+
+    for (t = tree, depth = 1; ! ROOT_P(t); depth++, t = t->parent);
+    if (ALLOC_N(anc, depth) < 0)
+        return NULL;
+
+    for (t = tree, i = depth - 1; i >= 0; i--, t = t->parent)
+        anc[i] = t;
+
+    for (i = 0; i < depth; i++) {
+        char *p = path_expand(anc[i], path);
+        free(path);
+        path = p;
+    }
+    FREE(anc);
+    return path;
+}
 
 /*
  * Local variables:
diff --git a/src/internal.h b/src/internal.h
index 7c69211..faa407f 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -319,6 +319,8 @@ int tree_insert(struct pathx *p, const char *label, int before);
 int free_tree(struct tree *tree);
 int dump_tree(FILE *out, struct tree *tree);
 int tree_equal(const struct tree *t1, const struct tree *t2);
+char *path_expand(struct tree *tree, const char *ppath);
+char *path_of_tree(struct tree *tree);
 
 /* Struct: memstream
  * Wrappers to simulate OPEN_MEMSTREAM where that's not available. The
-- 
1.6.0.6




More information about the augeas-devel mailing list