-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.c
196 lines (167 loc) · 4.98 KB
/
results.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* $Id$ */
/*
* Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <expat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "extern.h"
static int
xcmp(const void *p1, const void *p2)
{
const struct xliff *x1 = p1, *x2 = p2;
return strcmp(x1->source, x2->source);
}
static int
cmp(const void *p1, const void *p2)
{
const struct word *x1 = p1, *x2 = p2;
return strcmp(x1->source, x2->source);
}
void
results_update(struct hparse *hp, int copy, int keep, int quiet)
{
char *cp;
size_t i, j, ssz, smax;
struct xliff *sorted;
/* Allows us to de-dupe in place. */
qsort(hp->words, hp->wordsz, sizeof(struct word), cmp);
sorted = NULL;
ssz = smax = 0;
/* Merge found words in input into the merge buffer. */
for (i = 0; i < hp->wordsz; i++) {
cp = hp->words[i].source;
if (i && 0 == strcmp(cp, hp->words[i - 1].source))
continue;
if (ssz + 1 > smax) {
smax = ssz + 512;
sorted = reallocarray(sorted,
smax, sizeof(struct xliff));
if (NULL == sorted) {
perror(NULL);
exit(EXIT_FAILURE);
}
}
/*
* Are we finding this in the xliff?
* If so, use the xliff's target information.
* Otherwise, copy only the source.
*/
for (j = 0; j < hp->xp->xliffsz; j++)
if (0 == strcmp(cp, hp->xp->xliffs[j].source))
break;
if (j == hp->xp->xliffsz) {
if ( ! quiet)
fprintf(stderr, "%s:%zu:%zu: "
"new translation\n",
hp->fname, hp->words[i].line,
hp->words[i].col);
memset(&sorted[ssz], 0, sizeof(struct xliff));
sorted[ssz].source = cp;
} else
sorted[ssz] = hp->xp->xliffs[j];
ssz++;
}
/* Merge from the existing XLIFF back into the words. */
for (i = 0; i < hp->xp->xliffsz; i++) {
cp = hp->xp->xliffs[i].source;
for (j = 0; j < hp->wordsz; j++)
if (0 == strcmp(cp, hp->words[j].source))
break;
if (j < hp->wordsz)
continue;
if ( ! keep && ! quiet) {
fprintf(stderr, "%s:%zu:%zu: discarding "
"unused translation\n",
hp->xp->fname,
hp->xp->xliffs[i].line,
hp->xp->xliffs[i].col);
continue;
} else if ( ! keep)
continue;
if (ssz + 1 > smax) {
smax = ssz + 512;
sorted = reallocarray(sorted,
smax, sizeof(struct xliff));
if (NULL == sorted) {
perror(NULL);
exit(EXIT_FAILURE);
}
}
sorted[ssz++] = hp->xp->xliffs[i];
}
/* Output the sorted dictionary file. */
if (ssz)
qsort(sorted, ssz, sizeof(struct xliff), xcmp);
printf("<xliff version=\"1.2\">\n"
"\t<file source-language=\"%s\" "
"target-language=\"%s\" tool=\"sintl\">\n"
"\t\t<body>\n",
NULL == hp->xp->srclang ? "TODO" : hp->xp->srclang,
NULL == hp->xp->trglang ? "TODO" : hp->xp->trglang);
for (i = 0; i < ssz; i++)
if (0 == sorted[i].target.copysz && copy)
printf("\t\t\t<trans-unit id=\"%zu\">\n"
"\t\t\t\t<source>%s</source>\n"
"\t\t\t\t<target>%s</target>\n"
"\t\t\t</trans-unit>\n",
i + 1, sorted[i].source,
sorted[i].source);
else if (0 == sorted[i].target.copysz)
printf("\t\t\t<trans-unit id=\"%zu\">\n"
"\t\t\t\t<source>%s</source>\n"
"\t\t\t</trans-unit>\n",
i + 1, sorted[i].source);
else
printf("\t\t\t<trans-unit id=\"%zu\">\n"
"\t\t\t\t<source>%s</source>\n"
"\t\t\t\t<target>%.*s</target>\n"
"\t\t\t</trans-unit>\n",
i + 1, sorted[i].source,
(int)sorted[i].target.copysz,
sorted[i].target.copy);
puts("\t\t</body>");
puts("\t</file>");
puts("</xliff>");
free(sorted);
}
void
results_extract(struct hparse *p, int copy)
{
size_t i, j;
qsort(p->words, p->wordsz, sizeof(struct word), cmp);
printf("<xliff version=\"1.2\">\n"
"\t<file source-language=\"%s\" "
"target-language=\"TODO\" tool=\"sintl\">\n"
"\t\t<body>\n",
NULL == p->lang ? "TODO" : p->lang);
for (i = j = 0; i < p->wordsz; i++) {
if (i && 0 == strcmp(p->words[i].source, p->words[i - 1].source))
continue;
printf("\t\t\t<trans-unit id=\"%zu\">\n"
"\t\t\t\t<source>%s</source>\n",
++j, p->words[i].source);
if (copy)
printf("\t\t\t\t<target>%s</target>\n",
p->words[i].source);
puts("\t\t\t</trans-unit>");
}
puts("\t\t</body>\n"
"\t</file>\n"
"</xliff>");
}