示例#1
0
文件: internal.c 项目: Mpounta/augeas
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;
}
示例#2
0
int tree_sibling_index(struct tree *tree) {
    struct tree *siblings = tree->parent->children;

    int cnt = 0, ind = 0;

    list_for_each(t, siblings) {
        if (streqv(t->label, tree->label)) {
            cnt += 1;
            if (t == tree)
                ind = cnt;
        }
    }

    if (cnt > 1) {
        return ind;
    } else {
        return 0;
    }
}