forked from rc0/jbofihe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textblk.c
422 lines (364 loc) · 8.21 KB
/
textblk.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/***************************************
$Header$
Driver for producing plain text output, using blocks rather than
free-flow.
***************************************/
/* COPYRIGHT */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
#include "output.h"
#define MAX_WIDTH 120
extern int opt_output_width; /* Defined in main.c */
static int max_width = MAX_WIDTH;
#define BUFFER_SIZE 512
typedef enum {
ST_OPEN,
ST_TEXT,
ST_CLOSE,
ST_START
} States;
static States state;
/* Largest number of tags supported on a single sumti */
#define MAX_TAGS 32
/* Variables to hold block being built */
static int tags_used;
static char tag_text[MAX_TAGS][BUFFER_SIZE];
static char loj_text[BUFFER_SIZE];
static char eng_text[BUFFER_SIZE];
/* Variables to hold line being constructed */
static int current_width; /* The width of the line buffer currently occupied */
static char tag_line[MAX_TAGS][BUFFER_SIZE];
static int line_tags_used;
static char loj_line[BUFFER_SIZE];
static char eng_line[BUFFER_SIZE];
static void clear_line_buffer(void)/*{{{*/
{
int i;
for (i=0; i<MAX_TAGS; i++) {
tag_line[i][0] = 0;
}
line_tags_used = 0;
loj_line[0] = 0;
eng_line[0] = 0;
current_width = 0;
}
/*}}}*/
static void initialise(void)/*{{{*/
{
int i;
state = ST_START;
for (i=0; i<MAX_TAGS; i++) {
tag_text[i][0] = 0;
}
loj_text[0] = 0;
eng_text[0] = 0;
tags_used = 0;
clear_line_buffer();
max_width = opt_output_width;
}
/*}}}*/
static void write_prologue(void)/*{{{*/
{
return;
}
/*}}}*/
static void flush_line(void)/*{{{*/
{
int i;
if (current_width == 0) return;
fputs(loj_line, stdout);
fputs("\n", stdout);
fputs(eng_line, stdout);
fputs("\n", stdout);
for (i=0; i<line_tags_used; i++) {
fputs(tag_line[i], stdout);
fputs("\n", stdout);
}
fputs("\n", stdout);
clear_line_buffer();
}/*}}}*/
/* Number of end of lines that are pending. (These are only inserted
when we have closed a sequence of close brackets, i.e. before the
next open bracket or ordinary text.) */
static int pending_eols = 0;
static void clear_eols(void)/*{{{*/
{
if (pending_eols > 0) {
flush_line();
if (pending_eols > 1) {
printf("\n------------------------------------------------------------\n");
} else {
printf("\n");
}
state = ST_OPEN;
pending_eols = 0;
}
}/*}}}*/
static void append_to_width(char *src, char *dbuf, int wid)/*{{{*/
/* Append a string onto a buffer, and right pad with spaces to bring up
to a specific width */
{
int len, toadd, i;
char buffer[BUFFER_SIZE];
len = strlen(src);
toadd = wid - len;
for (i=0; i<toadd; i++) {
buffer[i] = ' ';
}
buffer[toadd] = '\0';
strcat(dbuf, src);
strcat(dbuf, buffer);
}/*}}}*/
static void flush_block(void)/*{{{*/
/* Push current block onto line buffer */
{
int i;
int max_len, len;
if (!loj_text[0] && !eng_text[0] && (tags_used == 0)) return; /* Nothing to flush */
/* Calculate width of block */
max_len = 0;
len = strlen(loj_text);
if (len > max_len) max_len = len;
len = strlen(eng_text);
if (len > max_len) max_len = len;
for (i=0; i<tags_used; i++) {
len = strlen(tag_text[i]);
if (len > max_len) max_len = len;
}
if (max_len + current_width > max_width) {
flush_line();
}
append_to_width(loj_text, loj_line, max_len);
append_to_width(eng_text, eng_line, max_len);
for (i=0; i<tags_used; i++) {
if (i >= line_tags_used) {
append_to_width("", tag_line[i], current_width);
}
append_to_width(tag_text[i], tag_line[i], max_len);
}
/* Space-pad the tag lines on words that don't generate enough
tags. */
for (i=tags_used; i<line_tags_used; i++) {
append_to_width("", tag_line[i], max_len);
}
current_width += max_len;
if (tags_used > line_tags_used) line_tags_used = tags_used;
/* Clear the block buffers for the next lot */
for (i=0; i<MAX_TAGS; i++) {
tag_text[i][0] = 0;
}
loj_text[0] = 0;
eng_text[0] = 0;
tags_used = 0;
}
/*}}}*/
static void set_eols(int eols)/*{{{*/
{
pending_eols += eols;
}
/*}}}*/
static void write_epilog(void)/*{{{*/
/* Do final acts of writing to output file. */
{
flush_block();
flush_line();
return;
}/*}}}*/
static void write_open_bracket(BracketType type, int subscript)/*{{{*/
{
char *brac;
char *brac1, *brac2, *brac3;
if (type == BR_NONE) return;
if (loj_text[0] || eng_text[0] || tags_used > 0) {
flush_block();
}
clear_eols();
brac = brac1 = brac2 = brac3 = NULL;
switch (type) {
case BR_NONE:
brac = NULL;
break;
case BR_ROUND:
brac = "(";
break;
case BR_SQUARE:
brac = "[";
break;
case BR_BRACE:
brac = "{";
break;
case BR_ANGLE:
brac = "<";
break;
case BR_CEIL:
brac1 = " /";
brac2 = "|";
brac3 = "|";
break;
case BR_FLOOR:
brac1 = "|";
brac2 = "|";
brac3 = " \\";
break;
case BR_TRIANGLE:
brac = "<<";
break;
}
if (brac) {
sprintf(loj_text, "%s ", brac);
sprintf(eng_text, "%s ", brac);
sprintf(tag_text[0], "%s ", brac);
sprintf(tag_text[1], "%d ", subscript);
tags_used = 2;
} else if (brac1) {
sprintf(loj_text, "%s ", brac1);
sprintf(eng_text, "%s ", brac2);
sprintf(tag_text[0], "%s ", brac3);
sprintf(tag_text[1], "%d ", subscript);
tags_used = 2;
}
state = ST_OPEN;
}
/*}}}*/
static void write_close_bracket(BracketType type, int subscript)/*{{{*/
{
char *brac;
char *brac1, *brac2, *brac3;
if (type == BR_NONE) return;
if (loj_text[0] || eng_text[0]) {
flush_block();
}
brac = brac1 = brac2 = brac3 = NULL;
switch (type) {
case BR_NONE:
brac = NULL;
break;
case BR_ROUND:
brac = ")";
break;
case BR_SQUARE:
brac = "]";
break;
case BR_BRACE:
brac = "}";
break;
case BR_ANGLE:
brac = ">";
break;
case BR_CEIL:
brac1 = "\\";
brac2 = " |";
brac3 = " |";
break;
case BR_FLOOR:
brac1 = " |";
brac2 = " |";
brac3 = "/";
break;
case BR_TRIANGLE:
brac = ">>";
break;
}
if (brac) {
sprintf(loj_text, "%s ", brac);
sprintf(eng_text, "%s ", brac);
sprintf(tag_text[0], "%s ", brac);
sprintf(tag_text[1], "%d ", subscript);
tags_used = 2;
} else if (brac1) {
sprintf(loj_text, "%s ", brac1);
sprintf(eng_text, "%s ", brac2);
sprintf(tag_text[0], "%s ", brac3);
sprintf(tag_text[1], "%d ", subscript);
tags_used = 2;
}
state = ST_CLOSE;
}/*}}}*/
static void write_lojban_text(char *text)/*{{{*/
{
if (eng_text[0]) {
flush_block();
}
clear_eols();
strcat(loj_text, text);
strcat(loj_text, " ");
}/*}}}*/
static void write_special(char *text)/*{{{*/
{
if (!strcmp(text, "$LEFTARROW")) {
strcat(eng_text, "<-");
} else if (!strcmp(text, "$OPENQUOTE")) {
strcat(eng_text, "\"");
} else if (!strcmp(text, "$CLOSEQUOTE")) {
strcat(eng_text,"\"");
}
}/*}}}*/
static void write_translation(char *text)/*{{{*/
{
if (text[0] == '$') {
write_special(text);
} else {
strcat(eng_text, text);
strcat(eng_text, " ");
}
}
/*}}}*/
/*+ +*/
static int first_tag;
static void start_tags(void)/*{{{*/
{
if (loj_text[0] || eng_text[0]) {
flush_block();
}
clear_eols();
first_tag = 1;
}/*}}}*/
static void end_tags(void)/*{{{*/
{
tags_used++;
}/*}}}*/
static void start_tag(void)/*{{{*/
{
if (!first_tag) {
tags_used++;
}
first_tag = 0;
}
/*}}}*/
static void write_tag_text(char *brivla, char *place, char *trans, int brac)/*{{{*/
{
char buffer1[256], buffer2[256];
sprintf(buffer1, "%s%s ", brivla, place);
if (brac) {
sprintf(buffer2, "(%s) ", trans);
} else {
sprintf(buffer2, "%s ", trans);
}
strcat(tag_text[tags_used], buffer1);
strcat(tag_text[tags_used], buffer2);
}
/*}}}*/
static void write_partial_tag_text(char *t)/*{{{*/
{
strcat(tag_text[tags_used], t);
}
/*}}}*/
DriverVector text_block_driver = /*{{{*/
{
initialise,
write_prologue,
write_epilog,
write_open_bracket,
write_close_bracket,
set_eols,
write_lojban_text,
write_translation,
start_tags,
end_tags,
start_tag,
write_tag_text,
write_partial_tag_text
};/*}}}*/