-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_join.c
More file actions
90 lines (81 loc) · 2.17 KB
/
Copy pathnode_join.c
File metadata and controls
90 lines (81 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef _AMALGAMATE_
#include "script.h"
#include "xstr.h"
#include "utils.h"
#include <stdlib.h>
#endif
static const char* _join_value(struct node *n) {
struct node_foreach *fe = node_find_parent_foreach(n);
if (fe) {
free(n->impl);
n->impl = 0;
}
if (n->impl) {
return n->impl;
}
struct xstr *xstr = xstr_create_empty();
int c = 0;
struct node *pair[] = { 0, 0 };
for (struct node *nn = n->child; nn; nn = nn->next, ++c) {
if (c < 2) {
pair[c] = nn;
}
}
if (c == 2) {
const char *vpair[] = { node_value(pair[0]), node_value(pair[1]) };
if ((vpair[0] && !is_vlist(vpair[0]) && is_vlist(vpair[1]))) {
const char *prefix = vpair[0];
size_t prefix_len = strlen(prefix);
struct vlist_iter iter;
vlist_iter_init(vpair[1], &iter);
while (vlist_iter_next(&iter)) {
xstr_cat2(xstr, "\1", 1);
xstr_cat2(xstr, prefix, prefix_len);
xstr_cat2(xstr, iter.item, iter.len);
}
n->impl = xstr_destroy_keep_ptr(xstr);
return n->impl;
} else if (vpair[1] && !is_vlist(vpair[1]) && is_vlist(vpair[0])) {
const char *suffix = vpair[1];
size_t suffix_len = strlen(suffix);
struct vlist_iter iter;
vlist_iter_init(vpair[0], &iter);
while (vlist_iter_next(&iter)) {
xstr_cat2(xstr, "\1", 1);
xstr_cat2(xstr, iter.item, iter.len);
xstr_cat2(xstr, suffix, suffix_len);
}
n->impl = xstr_destroy_keep_ptr(xstr);
return n->impl;
}
}
bool list = n->value[0] == '.';
for (struct node *nn = n->child; nn; nn = nn->next) {
if (list) {
xstr_cat(xstr, "\1");
}
const char *val = node_value(nn);
if (is_vlist(val)) {
struct vlist_iter iter;
vlist_iter_init(val, &iter);
while (vlist_iter_next(&iter)) {
xstr_cat2(xstr, iter.item, iter.len);
}
} else {
xstr_cat(xstr, val);
}
}
n->impl = xstr_destroy_keep_ptr(xstr);
return n->impl;
}
static void _join_dispose(struct node *n) {
if (n->impl) {
free(n->impl);
}
}
int node_join_setup(struct node *n) {
n->flags |= NODE_FLG_NO_CWD;
n->value_get = _join_value;
n->dispose = _join_dispose;
return 0;
}