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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include "leven.h"
#include "parse.h"
#include "log.h"
int main(int argc, char **argv) {
int rc;
char *a;
{
int fd = open("../xmldiff/file_a.xml", O_RDONLY);
int len = lseek(fd, 0, SEEK_END);
a = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
}
char* b;
{
int fd = open("../xmldiff/file_b.xml", O_RDONLY);
int len = lseek(fd, 0, SEEK_END);
b = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
}
/* char* a = "<root><a/><b></b></root>"; */
/* char* b = "<root><a><b/></a></root>"; */
log("Parse a");
struct Tree a_tree;
size_t *a_chunks;
rc = parse_string(a, &a_tree, &a_chunks);
if(rc != 0) return 1;
log("Tree a size %ld", a_tree.len);
log("Parse b");
struct Tree b_tree;
size_t *b_chunks;
rc = parse_string(b, &b_tree, &b_chunks);
if(rc != 0) return 1;
log("Tree b size %ld", b_tree.len);
DECL_MAT_DATA(cost, uint32_t, 5, 5,
0, 2, 2, 2, 2,
2, 1, 1, 1, 1,
2, 1, 1, 1, 1,
2, 1, 1, 0, 1,
2, 1, 1, 1, 1
);
for(size_t i = 0; i < 4; i++) {
size_t a_tag_end = a_chunks[i];
while(a[a_tag_end] != '>') a_tag_end++;
size_t a_len = a_tag_end - a_chunks[i];
*imat_uint32_t(cost, i+1, 0) = a_len;
}
for(size_t j = 0; j < 4; j++) {
size_t b_tag_end = b_chunks[j];
while(b[b_tag_end] != '>') b_tag_end++;
size_t b_len = b_tag_end - b_chunks[j];
*imat_uint32_t(cost, 0, j+1) = b_len;
}
for(size_t j = 0; j < 4; j++) {
size_t b_tag_end = b_chunks[j];
while(b[b_tag_end] != '>') b_tag_end++;
for(size_t i = 0; i < 4; i++) {
size_t a_tag_end = a_chunks[i];
while(a[a_tag_end] != '>') a_tag_end++;
log("%ld, %ld", i, j);
size_t a_len = a_tag_end - a_chunks[i];
size_t b_len = b_tag_end - b_chunks[j];
log("B: %ld, A: %ld", b_len, a_len);
log("A: %.*s, B: %.*s", (int)a_len, a + a_chunks[i], (int)b_len, b + b_chunks[j]);
*imat_uint32_t(cost, i+1, j+1) = !(a_len == b_len && memcmp(a + a_chunks[i], b + b_chunks[j], a_len) == 0) * 1;
}
}
printf("\n");
for(size_t y = 0; y < b_tree.len; y++) {
for(size_t x = 0; x < b_tree.adj.stride; x++) {
printf("%d ", *imat_nid(b_tree.adj, x, y));
}
printf("\n");
}
printf("\n");
printf("\n");
for(size_t y = 0; y < a_tree.len; y++) {
for(size_t x = 0; x < a_tree.adj.stride; x++) {
printf("%d ", *imat_nid(a_tree.adj, x, y));
}
printf("\n");
}
printf("\n");
logb("2D Array %s [%ldx%d]", "cost", cost.stride, 5);
for(uint32_t y = 0; y < 5; y++) {
lognl();
for(uint32_t x = 0; x < cost.stride; x++) {
logc("%03d ", *imat_uint32_t(cost, x, y));
}
}
loge();
mat_uint32_t cost_n = {
.data = malloc((a_tree.len+1) * (b_tree.len+1) * sizeof(uint32_t)),
.stride = a_tree.len+1,
};
assert(cost_n.data != NULL);
mat_uint32_t cost_f = {
.data = malloc((a_tree.len+1) * (b_tree.len+1) * sizeof(uint32_t)),
.stride = a_tree.len+1,
};
assert(cost_f.data != NULL);
mat_uint32_t cost_s = {
.data = malloc((a_tree.adj.stride+1) * (b_tree.adj.stride+1) * sizeof(uint32_t)),
.stride = a_tree.adj.stride+1,
};
assert(cost_s.data != NULL);
CTedData cted = {cost, cost_n, cost_f, cost_s};
constrained_tree_distance(a_tree, b_tree, cted);
log("Final Cost %d", *imat_uint32_t(cost_n, 1, 1));
uint32_t *alignment = malloc(b_tree.len * sizeof(*alignment));
uint32_t *adj_alignment = malloc(b_tree.adj.stride * sizeof(*adj_alignment));
constrained_tree_alignment(
a_tree,
b_tree,
cted,
adj_alignment,
alignment
);
logb("Alignment: ");
for(size_t i = 0; i < b_tree.len; i++) {
logc("%d ", alignment[i]);
}
loge();
size_t a_chunk = 0;
size_t b_chunk = 0;
for(size_t i = 0; i < b_tree.len; i++) {
size_t bc_len = b_chunks[b_chunk+1] - b_chunks[b_chunk];
/* log("Dumping a from %ld until %d", a_chunk, alignment[i]); */
while(a_chunk + 1 < alignment[i]) {
size_t ac_len = a_chunks[a_chunk+1] - a_chunks[a_chunk];
fwrite(a + a_chunks[a_chunk], ac_len, 1, stdout);
a_chunk++;
}
if(alignment[i] == 0) {
/* log("Alignment says %ld was created", b_chunk); */
fwrite(b + b_chunks[b_chunk], bc_len, 1, stdout);
} else {
/* log("Alignment says %ld matches with %ld", b_chunk, a_chunk); */
fwrite(b + b_chunks[b_chunk], bc_len, 1, stdout);
a_chunk++;
}
b_chunk++;
}
return 0;
}
|