2 * Copyright (c) 2000, 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID ("$Id: rtbl.c,v 1.4 2002/09/04 21:25:09 joda Exp $");
51 struct column_entry
*rows
;
57 struct column_data
**columns
;
63 return calloc (1, sizeof (struct rtbl_data
));
66 static struct column_data
*
67 rtbl_get_column (rtbl_t table
, const char *column
)
70 for(i
= 0; i
< table
->num_columns
; i
++)
71 if(strcmp(table
->columns
[i
]->header
, column
) == 0)
72 return table
->columns
[i
];
77 rtbl_destroy (rtbl_t table
)
81 for (i
= 0; i
< table
->num_columns
; i
++) {
82 struct column_data
*c
= table
->columns
[i
];
84 for (j
= 0; j
< c
->num_rows
; j
++)
85 free (c
->rows
[j
].data
);
91 free (table
->column_prefix
);
92 free (table
->columns
);
97 rtbl_add_column (rtbl_t table
, const char *header
, unsigned int flags
)
99 struct column_data
*col
, **tmp
;
101 tmp
= realloc (table
->columns
, (table
->num_columns
+ 1) * sizeof (*tmp
));
104 table
->columns
= tmp
;
105 col
= malloc (sizeof (*col
));
108 col
->header
= strdup (header
);
109 if (col
->header
== NULL
) {
118 table
->columns
[table
->num_columns
++] = col
;
123 column_compute_width (struct column_data
*column
)
127 column
->width
= strlen (column
->header
);
128 for (i
= 0; i
< column
->num_rows
; i
++)
129 column
->width
= max (column
->width
, strlen (column
->rows
[i
].data
));
133 rtbl_set_prefix (rtbl_t table
, const char *prefix
)
135 if (table
->column_prefix
)
136 free (table
->column_prefix
);
137 table
->column_prefix
= strdup (prefix
);
138 if (table
->column_prefix
== NULL
)
144 rtbl_set_column_prefix (rtbl_t table
, const char *column
,
147 struct column_data
*c
= rtbl_get_column (table
, column
);
153 c
->prefix
= strdup (prefix
);
154 if (c
->prefix
== NULL
)
161 get_column_prefix (rtbl_t table
, struct column_data
*c
)
167 if (table
->column_prefix
)
168 return table
->column_prefix
;
173 rtbl_add_column_entry (rtbl_t table
, const char *column
, const char *data
)
175 struct column_entry row
, *tmp
;
177 struct column_data
*c
= rtbl_get_column (table
, column
);
182 row
.data
= strdup (data
);
183 if (row
.data
== NULL
)
185 tmp
= realloc (c
->rows
, (c
->num_rows
+ 1) * sizeof (*tmp
));
191 c
->rows
[c
->num_rows
++] = row
;
196 rtbl_format (rtbl_t table
, FILE * f
)
200 for (i
= 0; i
< table
->num_columns
; i
++)
201 column_compute_width (table
->columns
[i
]);
202 for (i
= 0; i
< table
->num_columns
; i
++) {
203 struct column_data
*c
= table
->columns
[i
];
205 fprintf (f
, "%s", get_column_prefix (table
, c
));
206 fprintf (f
, "%-*s", (int)c
->width
, c
->header
);
213 for (i
= 0; flag
== 0 && i
< table
->num_columns
; ++i
) {
214 struct column_data
*c
= table
->columns
[i
];
216 if (c
->num_rows
> j
) {
224 for (i
= 0; i
< table
->num_columns
; i
++) {
226 struct column_data
*c
= table
->columns
[i
];
230 if ((c
->flags
& RTBL_ALIGN_RIGHT
) == 0)
232 fprintf (f
, "%s", get_column_prefix (table
, c
));
233 if (c
->num_rows
<= j
)
234 fprintf (f
, "%*s", w
, "");
236 fprintf (f
, "%*s", w
, c
->rows
[j
].data
);
245 main (int argc
, char **argv
)
248 unsigned int a
, b
, c
, d
;
250 table
= rtbl_create ();
251 rtbl_add_column (table
, "Issued", 0, &a
);
252 rtbl_add_column (table
, "Expires", 0, &b
);
253 rtbl_add_column (table
, "Foo", RTBL_ALIGN_RIGHT
, &d
);
254 rtbl_add_column (table
, "Principal", 0, &c
);
256 rtbl_add_column_entry (table
, a
, "Jul 7 21:19:29");
257 rtbl_add_column_entry (table
, b
, "Jul 8 07:19:29");
258 rtbl_add_column_entry (table
, d
, "73");
259 rtbl_add_column_entry (table
, d
, "0");
260 rtbl_add_column_entry (table
, d
, "-2000");
261 rtbl_add_column_entry (table
, c
, "krbtgt/NADA.KTH.SE@NADA.KTH.SE");
263 rtbl_add_column_entry (table
, a
, "Jul 7 21:19:29");
264 rtbl_add_column_entry (table
, b
, "Jul 8 07:19:29");
265 rtbl_add_column_entry (table
, c
, "afs/pdc.kth.se@NADA.KTH.SE");
267 rtbl_add_column_entry (table
, a
, "Jul 7 21:19:29");
268 rtbl_add_column_entry (table
, b
, "Jul 8 07:19:29");
269 rtbl_add_column_entry (table
, c
, "afs@NADA.KTH.SE");
271 rtbl_set_prefix (table
, " ");
272 rtbl_set_column_prefix (table
, a
, "");
274 rtbl_format (table
, stdout
);
276 rtbl_destroy (table
);