forked from n-t-roff/sc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.c
432 lines (398 loc) · 15.7 KB
/
print.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
423
424
425
426
427
428
429
430
431
432
/* SC A Spreadsheet Calculator
* Printing routines
*
* original by James Gosling, September 1982
* modifications by Mark Weiser and Bruce Israel,
* University of Maryland
* More mods Robert Bond, 12/86
* updated by Charlie Gordon: June, 2021
*
* $Revision: 9.1 $
*/
#include "sc.h"
#define DEFCOLDELIM ':'
void printfile(sheet_t *sp, SCXMEM string_t *str, rangeref_t rr) {
char field[FBUFLEN];
buf_t(buf, FBUFLEN);
FILE *f;
int pid = -1;
int fieldlen, nextcol;
int row, col;
char path[PATHLEN];
char *ext;
const char *fname = str ? s2c(str) : NULL;
if (fname) {
/* printfile will be the [path/]file ---> [path/]file.out */
if (*fname == '\0') {
pstrcpy(path, sizeof path, sp->curfile);
ext = get_extension(path);
/* keep the extension unless .sc or scext */
if (strcmp(ext, ".sc") && !(scext && !strcmp(ext, s2c(scext))))
ext += strlen(ext);
snprintf(ext, path + sizeof(path) - ext, ".%s",
ascext ? s2c(ascext) : "asc");
} else {
/* strarg in gram.y, always size of \0 terminated string. */
pstrcpy(path, sizeof path, fname);
}
if (!strcmp(path, sp->curfile)
&& !yn_ask("Confirm that you want to destroy the data base: (y,n)")) {
string_free(str);
return;
}
if ((f = openfile(path, sizeof path, &pid, NULL)) == NULL) {
error("Cannot create file \"%s\"", path);
string_free(str);
return;
}
} else {
f = stdout;
}
for (row = rr.left.row; row <= rr.right.row; row++) {
int w = 0;
if (row_hidden(sp, row))
continue;
buf_reset(buf);
for (col = rr.left.col; col <= rr.right.col; col = nextcol, w += fieldlen) {
struct ent *p = getcell(sp, row, col);
const char *s;
int align, len;
fieldlen = 0;
nextcol = col + 1;
if (col_hidden(sp, col)) {
continue;
}
// XXX: should handle cell fusion
fieldlen = col_fwidth(sp, col);
if (!p)
continue;
if (buf_extend(buf, w + fieldlen + 2, FBUFLEN))
goto malloc_error;
// XXX: alignment should be determined from cell format
// ALIGN_AUTO, ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT
align = p->flags & ALIGN_MASK;
if (p->type == SC_NUMBER || p->type == SC_ERROR || p->type == SC_BOOLEAN) {
if (p->type == SC_ERROR) {
// XXX: append a space for cell alignment
len = pstrcpy(field, sizeof field, error_name[p->cellerror]);
align |= ALIGN_CLIP;
} else
if (p->type == SC_BOOLEAN) {
len = pstrcpy(field, sizeof field, boolean_name[!!p->v]);
if (!align)
align = ALIGN_CENTER;
} else {
if (p->format) {
len = format(field, sizeof field, s2c(p->format), sp->colfmt[col].precision, p->v, &align);
} else {
len = engformat(field, sizeof field, sp->colfmt[col].realfmt, sp->colfmt[col].precision, p->v, &align);
}
}
if ((int)buf->len < w) {
buf_repc(buf, ' ', w - buf->len);
} else {
buf->buf[buf->len = w] = '\0';
}
if (align & ALIGN_CLIP) {
if (len < 0)
len = 0;
if (len > fieldlen)
len = fieldlen;
field[len] = '\0';
align &= ~ALIGN_CLIP;
}
if (len < 0 || len > fieldlen) {
buf_repc(buf, '*', fieldlen);
} else
if (align == ALIGN_LEFT) {
buf_printf(buf, "%-*s", fieldlen, field);
} else
if (align == ALIGN_CENTER) {
int half = (fieldlen - len) / 2;
buf_printf(buf, "%*s%*s", half, field, len - half, "");
} else {
buf_printf(buf, "%*s", fieldlen, field);
}
} else
if (p->type == SC_STRING) {
int slen = strlen(s = s2str(p->label));
int pad = 0;
int soff = 0;
if (*s == '\\' && slen > 1) {
/* A string starting with a backslash is repeated across
the column width. */
int remain = fieldlen;
slen -= 1; /* skip the '\' */
s += 1;
while (remain > 0) {
int chunk = slen <= remain ? slen : remain;
buf_put(buf, s, chunk);
remain -= chunk;
}
continue;
}
/* Figure out if the label slops over to a blank field. */
while (slen > fieldlen && nextcol <= rr.right.col) {
if (!col_hidden(sp, nextcol)) {
struct ent *nc = getcell(sp, row, nextcol);
if (nc && (nc->type || nc->expr))
break;
fieldlen += col_fwidth(sp, nextcol);
}
nextcol++;
}
switch (p->flags & ALIGN_MASK) {
default:
case ALIGN_LEFT:
pad = w - buf->len;
if (slen > fieldlen)
slen = fieldlen;
break;
case ALIGN_CENTER:
pad = w - buf->len + (col_fwidth(sp, nextcol) - slen) / 2;
if (pad < 0) {
soff = -pad;
slen -= soff;
}
if ((int)buf->len + pad + slen > w + fieldlen)
slen = w + fieldlen - buf->len - pad;
break;
case ALIGN_RIGHT:
pad = w - buf->len + fieldlen - slen;
if (pad < 0) {
soff = -pad;
slen -= soff;
pad = 0;
}
break;
}
if (buf_extend(buf, w + fieldlen + 2, FBUFLEN))
goto malloc_error;
buf_repc(buf, ' ', pad);
buf_put(buf, s + soff, slen);
if (nextcol <= rr.right.col)
buf_repc(buf, ' ', w + fieldlen - buf->len);
}
}
buf_putc(buf, '\n');
fputs(buf->buf, f);
}
if (0) {
malloc_error:
error("Realloc failed in printfile()");
}
buf_free(buf);
if (fname) closefile(f, pid, 0);
string_free(str);
}
/* unspecial (backquote) things that are special chars in a table */
static void unspecial(sheet_t *sp, FILE *f, const char *str, int delim) {
if (*str == '\\') str++; /* delete wheeling string operator, OK? */
while (*str) {
if (((sp->tbl_style == LATEX) || (sp->tbl_style == SLATEX) ||
(sp->tbl_style == TEX)) &&
((*str == delim) || (*str == '$') || (*str == '#') ||
(*str == '%') || (*str == '{') || (*str == '}') ||
(*str == '&')))
putc('\\', f);
putc(*str, f);
str++;
}
}
void tblprintfile(sheet_t *sp, SCXMEM string_t *str, rangeref_t rr) {
FILE *f;
int pid;
int row, col, ncols = rr.right.col - rr.left.col + 1;
char coldelim = DEFCOLDELIM;
char path[PATHLEN];
char *ext;
const char *fname = s2str(str);
/* tblprintfile will be the [path/]file ---> [path/]file.out */
if (*fname == '\0') {
pstrcpy(path, sizeof path, sp->curfile);
ext = get_extension(path);
/* keep the extension unless .sc or scext */
if (strcmp(ext, ".sc") && !(scext && !strcmp(ext, s2c(scext))))
ext += strlen(ext);
if (sp->tbl_style == 0) {
snprintf(ext, path + sizeof(path) - ext, ".%s",
tbl0ext ? s2c(tbl0ext) : "cln");
} else
if (sp->tbl_style == TBL) {
snprintf(ext, path + sizeof(path) - ext, ".%s",
tblext ? s2c(tblext) : "tbl");
} else
if (sp->tbl_style == LATEX) {
snprintf(ext, path + sizeof(path) - ext, ".%s",
latexext ? s2c(latexext) : "lat");
} else
if (sp->tbl_style == SLATEX) {
snprintf(ext, path + sizeof(path) - ext, ".%s",
slatexext ? s2c(slatexext) : "stx");
} else
if (sp->tbl_style == TEX) {
snprintf(ext, path + sizeof(path) - ext, ".%s",
texext ? s2c(texext) : "tex");
}
} else {
pstrcpy(path, sizeof path, fname);
}
if (!strcmp(path, sp->curfile)
&& !yn_ask("Confirm that you want to destroy the data base: (y,n)")) {
string_free(str);
return;
}
if ((f = openfile(path, sizeof path, &pid, NULL)) == NULL) {
error("Cannot create file \"%s\"", path);
string_free(str);
return;
}
if (sp->tbl_style == TBL) {
fprintf(f, ".\\\" ** %s spreadsheet output \n.TS\n", progname);
fprintf(f, "tab(%c);\n", coldelim);
for (col = rr.left.col; col <= rr.right.col; col++)
fprintf(f, " n");
fprintf(f, ".\n");
} else
if (sp->tbl_style == LATEX) {
fprintf(f, "%% ** %s spreadsheet output\n\\begin{tabular}{", progname);
for (col = rr.left.col; col <= rr.right.col; col++)
fprintf(f, "c");
fprintf(f, "}\n");
coldelim = '&';
} else
if (sp->tbl_style == SLATEX) {
fprintf(f, "%% ** %s spreadsheet output\n!begin<tabular><", progname);
for (col = rr.left.col; col <= rr.right.col; col++)
fprintf(f, "c");
fprintf(f, ">\n");
coldelim = '&';
} else
if (sp->tbl_style == TEX) {
fprintf(f, "{\t%% ** %s spreadsheet output\n\\settabs %d \\columns\n",
progname, ncols);
coldelim = '&';
} else
if (sp->tbl_style == FRAME) {
fprintf(f, "<MIFFile 3.00> # generated by the sc spreadsheet calculator\n");
fprintf(f, "<Tbls\n");
fprintf(f, " <Tbl \n");
fprintf(f, " <TblID 1> # This table's ID is 1\n");
fprintf(f, " <TblFormat \n");
fprintf(f, " <TblTag `Format A'> # Table Format Catalog\n");
fprintf(f, " > # end of TblFormat\n");
fprintf(f, " <TblNumColumns %d> # Has %d columns\n", ncols, ncols);
fprintf(f, " <TblTitleContent\n");
fprintf(f, " <Para\n");
fprintf(f, " <PgfTag `TableTitle'> # Forces lookup in Paragraph Format Catalog\n");
fprintf(f, " <ParaLine\n");
fprintf(f, " <String `%s'>\n", fname);
fprintf(f, " > # end of ParaLine\n");
fprintf(f, " > # end of Para\n");
fprintf(f, " > # end of TblTitleContent\n");
fprintf(f, " <TblH # The heading\n");
fprintf(f, " <Row # The heading row\n");
for (col = rr.left.col; col <= rr.right.col; col++) {
fprintf(f, " <Cell <CellContent <Para # Cell in column \n");
fprintf(f, " <PgfTag `CellHeading'> # in Paragraph Format Catalog\n");
// XXX: incorrect for columns beyond 25
fprintf(f, " <ParaLine <String `%c'>>\n", 'A'+col);
fprintf(f, " >>> # end of Cell\n");
}
fprintf(f, " > # end of Row\n");
fprintf(f, " > # end of TblH\n");
fprintf(f, " <TblBody # The body\n");
}
for (row = rr.left.row; row <= rr.right.row; row++) {
// XXX: print hidden rows?
if (sp->tbl_style == TEX)
fprintf(f, "\\+");
else
if (sp->tbl_style == FRAME) {
fprintf(f, " <Row # The next body row\n");
}
for (col = rr.left.col; col <= rr.right.col; col++) {
struct ent *p = getcell(sp, row, col);
// XXX: print hidden columns?
// XXX: should handle cell fusion
if (sp->tbl_style == FRAME) {
fprintf(f, " <Cell <CellContent <Para\n");
fprintf(f, " <PgfTag `CellBody'> # in Paragraph Format Catalog\n");
fprintf(f, " <ParaLine <String `");
}
if (p) {
char field[FBUFLEN];
int align = p->flags & ALIGN_MASK;
if (p->type == SC_NUMBER || p->type == SC_ERROR || p->type == SC_BOOLEAN) {
/* convert cell contents, do not test width, do not align with spaces */
// XXX: should implement alignment in output format
if (p->type == SC_ERROR) {
pstrcpy(field, sizeof field, error_name[p->cellerror]);
align |= ALIGN_CLIP;
} else
if (p->type == SC_BOOLEAN) {
pstrcpy(field, sizeof field, boolean_name[!!p->v]);
if (!align)
align = ALIGN_CENTER;
} else {
if (p->format) {
format(field, sizeof field, s2c(p->format), sp->colfmt[col].precision, p->v, &align);
} else {
engformat(field, sizeof field, sp->colfmt[col].realfmt, sp->colfmt[col].precision, p->v, &align);
}
}
// XXX: should fill fieldlen with * if too long
unspecial(sp, f, field, coldelim);
} else
if (p->type == SC_STRING) {
// XXX: should handle repeated pattern starting with '\'
unspecial(sp, f, s2str(p->label), coldelim);
}
}
if (sp->tbl_style == FRAME) {
fprintf(f, "'>>\n");
fprintf(f, " >>> # end of Cell\n");
}
if (col < rr.right.col) {
if (sp->tbl_style != FRAME)
fprintf(f, "%c", coldelim);
}
}
if (sp->tbl_style == LATEX) {
if (row < rr.right.row) fprintf (f, "\\\\");
} else
if (sp->tbl_style == SLATEX) {
if (row < rr.right.row) fprintf(f, "!!");
} else
if (sp->tbl_style == TEX) {
fprintf (f, "\\cr");
} else
if (sp->tbl_style == FRAME) {
fprintf(f, " > # end of Row\n");
}
fprintf(f, "\n");
}
if (sp->tbl_style == TBL)
fprintf (f,".TE\n.\\\" ** end of %s spreadsheet output\n", progname);
else
if (sp->tbl_style == LATEX)
fprintf(f, "\\end{tabular}\n%% ** end of %s spreadsheet output\n", progname);
else
if (sp->tbl_style == SLATEX)
fprintf (f,"!end<tabular>\n%% ** end of %s spreadsheet output\n", progname);
else
if (sp->tbl_style == TEX)
fprintf (f,"}\n%% ** end of %s spreadsheet output\n", progname);
else
if (sp->tbl_style == FRAME) {
fprintf(f, " > # end of TblBody\n");
fprintf(f, " ># end of Tbl\n");
fprintf(f, "> # end of Tbls\n");
fprintf(f, "<TextFlow <Para \n");
fprintf(f, " <PgfTag Body> \n");
fprintf(f, " <ParaLine <ATbl 1>> # Reference to table ID 1\n");
fprintf(f, ">>\n");
}
closefile(f, pid, 0);
string_free(str);
}