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
|
/* $Id: latex-table.cc,v 1.8 1997/04/13 13:27:29 dps Exp $ */
/* Latex table layout */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#ifdef __GNUC__
#define alloca __builtin_alloca
#else
#if HAVE_ALLOCA_H
#include <alloca.h>
#else /* Do not have alloca.h */
#ifdef _AIX
#pragma alloca
#else /* not _AIX */
extern "C" char *alloca(int);
#endif /* _AIX */
#endif /* HAVE_ALLOCA_H */
#endif /* __GNUC__ */
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#include "tblock.h"
#include "latex-table.h"
#define __EXCLUDE_READER_CLASSES
#include "lib.h"
struct rdata
{
struct wd_info w;
const char *data;
};
/* Print row after folding of columns has been done */
static void basic_print_row(int ncols, const struct rdata *cols,
int maxw, FILE *out)
{
const char *s;
int i, nsp, nz;
align_t al;
num_info n;
tblock output, *outp;
/* Print the content */
for (i=0; i<ncols; i++)
{
s=cols[i].data;
al=cols[i].w.align;
if (cols[i].w.dp_col>=0)
{
n=scan_num(s);
if (n.dot_pos!=-1)
{
nz=cols[i].w.dp_col-n.dot_pos;
if ((!n.has_sign) && (cols[i].w.has_sign))
{
output.add("\\phantom{$-$");
if (nz>0)
{
while (nz--)
output.add('0');
}
output.add("}");
}
else
{
if (n.has_sign)
nz++; // - does not count as a digit.
if (nz>0)
{
output.add("\\phantom{");
while (nz--)
output.add('0');
output.add("}");
}
}
if (n.has_sign)
{
/* Typeset the sign in math mode */
output.add('$');
output.add(*s);
output.add('$');
s++;
}
output.add(s);
if (nsp-cols[i].w.dp_col+n.dot_pos>0
&& cols[i].w.align!=ALIGN_LEFT)
output.add("\\hfill");
al=ALIGN_DP;
}
}
switch(al)
{
case ALIGN_DP:
break;
case ALIGN_LEFT:
case ALIGN_CENTER:
case ALIGN_RIGHT:
output.add(s);
break;
default:
fprintf(stderr,"basic_print_row: Invalid alignment\n");
break;
}
if (i<ncols-1)
output.add(" & ", 3); // Seperate columns
}
output.add(" \n"); // converted to \\ \n by word_wrap.
outp=word_wrap((const char *) output, "\n", "\n", maxw, 0);
fputs((const char *) (*outp), out);
delete(outp);
}
extern tblock *__latex_do_map(const char *);
/* Split the elements of a row */
static void print_row(int ncols, const struct rdata *dp, int maxw, FILE *out)
{
static const char null_string[]={'\0'};
struct rdata *rd;
tblock *d;
int i;
/* Allocate structures */
if ((rd=(struct rdata *) alloca(ncols*sizeof(rdata)))==NULL)
{
fprintf(stderr, "print_row: fatal alloca failure\n");
exit(1);
}
/* Convert data to *TeX */
for (i=0; i<ncols; i++)
{
rd[i].w=dp[i].w;
if (dp[i].data==NULL)
{
rd[i].data=null_string; // Avoid complication of null in
// basic_print_row
continue; // Move on to next item
}
d=__latex_do_map(dp[i].data);
if ((rd[i].data=strdup(*d))==NULL)
{
fprintf(stderr, "latex_table::print_row: fatal alloca failure\n");
exit(1);
}
delete(d);
}
basic_print_row(ncols, rd, maxw, out); // Printing the rows is actually
// quite complex.
for (i=0; i<ncols; i++)
{
if (rd[i].data!=null_string)
free((void *) rd[i].data); // Free data
}
}
/* Returns NULL or text message */
const char *latex_table::print_table(int wd, FILE *out, const int ruled)
{
int i,j;
struct rdata *d;
const struct col_info *col;
if ((d=(struct rdata *) alloca(cols*sizeof(struct rdata)))==NULL)
{
cerr<<"latex_table::print_table alloca failute (fatal)\n";
return "[Table omitted due to lack of memory]";
}
if (cols==0 || rows==0)
{
fputs("[empty tabel ignored]\n", out);
return "[Ignored empty table]";
}
for (i=0, col=cdata; col!=NULL; i++, col=col->next)
d[i].w=find_width(rows, col->data);
fputs("\\begin{tabular}{", out);
for (i=0; i<cols; i++)
{
if (ruled)
fputc('|', out);
switch(d[i].w.align)
{
default:
cerr<<"Alignment "<<d[i].w.align<<" unsupported\n";
/* Fall through */
case ALIGN_LEFT:
fputc('l', out);
break;
case ALIGN_CENTER:
fputc('c', out);
break;
case ALIGN_RIGHT:
fputc('r', out);
break;
}
}
fprintf(out, "%s}%s\n", (ruled) ? "|" : "", (ruled) ? "\\hline " : "");
for (i=0; i<rows; i++)
{
for (j=0, col=cdata; col!=NULL; j++, col=col->next)
d[j].data=(col->data)[i];
print_row(cols, d, wd, out);
if (i==0)
fputs("\\hline\n", out);
}
fprintf(out, "%s\\end{tabular}\\\\\n", (ruled) ? "\\hline " : "");
return NULL;
}
/* Set */
int latex_table::set(int c, int r, const char *s)
{
struct col_info *col;
int i;
if (c<0 || c>=cols || r<0 || r>=rows)
{
cerr<<"Invalid request to set "<<c<<','<<r<<" in "
<<cols<<'x'<<rows<<" table (value "<<s<<")\n";
return 0;
}
for (col=cdata, i=0; i<c && col!=NULL; i++, col=col->next) ;
if (col!=NULL)
{
if (col->data[r]!=NULL)
free((void *) col->data[r]);
col->data[r]=strdup(s);
}
return 1;
}
/* Constructor */
latex_table::latex_table(int c, int r)
{
int i, j;
struct col_info *col, **nptr;
cols=c;
rows=r;
cdata=NULL; // Hardenning against cols=0
for (nptr=&cdata, i=0; i<cols; i++)
{
col=new(struct col_info);
*nptr=col;
col->next=NULL;
nptr=&(col->next);
if ((col->data=
(const char **) malloc(rows*(sizeof(const char *))))==NULL)
{
cerr<<"latex_table::constructor: malloc failure (fatal)\n";
exit(1);
}
for (j=0; j<rows; j++)
(col->data)[j]=NULL;
}
}
/* Destructor */
latex_table::~latex_table()
{
int i;
struct col_info *col, *nxt;
for (col=cdata; col!=NULL;)
{
for (i=0; i<rows; i++)
if (col->data[i]==NULL)
free((void *) col->data[i]);
nxt=col->next;
free(col);
col=nxt;
}
}
|