tabular output: fix segmentation fault
[openafs.git] / src / util / tabular_output.c
1 /*
2  * Copyright (c) 2010, Christof Hanke,
3  * RZG, Max-Planck-Institut f. Plasmaphysik.
4  * All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in
13  *      the documentation and/or other materials provided with the
14  *      distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <afsconfig.h>
29 #include <afs/param.h>
30
31 #include <roken.h>
32
33 #include <afs/afsutil.h>
34 #include <afs/tabular_output.h>
35
36 /* private structures */
37
38 struct util_TableRow {
39     char **CellContents;
40 };
41
42 struct util_Table {
43     int Type;
44     int numColumns,sortByColumn;
45     int numRows, numAllocatedRows;
46     int *ColumnWidths;
47     char **ColumnHeaders;
48     int  *ColumnContentTypes;
49     int RowLength; /* number of character per Row */
50     /* Basic subentities */
51     struct util_TableRow *Header;
52     struct util_TableRow **Body;
53     struct util_TableRow *Footer;
54     /* output methods provided for this table */
55     int (*printHeader) (struct util_Table *Table);
56     int (*printFooter) (struct util_Table *Table);
57     int (*printBody) (struct util_Table *Table);
58     int (*printRow) (struct util_Table *, struct util_TableRow *);
59 };
60
61 /* private functions */
62
63 struct util_TableRow* newTableRow(struct util_Table *);
64 int printTableFooter_CSV(struct util_Table* Table);
65 int printTableHeader_CSV(struct util_Table* Table);
66 int printTableRow_CSV(struct util_Table* Table, struct util_TableRow *aTableRow);
67 int printTableFooter_ASCII(struct util_Table* Table);
68 int printTableHeader_ASCII(struct util_Table* Table);
69 int printTableRow_ASCII(struct util_Table* Table, struct util_TableRow *aTableRow);
70 int printTableFooter_HTML(struct util_Table* Table);
71 int printTableHeader_HTML(struct util_Table* Table);
72 int printTableRow_HTML(struct util_Table* Table, struct util_TableRow *aTableRow);
73 int findRowIndex(struct util_Table* Table, struct util_TableRow *aRow);
74 int do_setTableRow(struct util_Table *Table, struct util_TableRow *aRow, char **Contents);
75
76
77 /*
78 Public Interface
79 */
80
81 int
82 util_setTableBodyRow(struct util_Table *Table, int RowIndex, char **Contents) {
83     struct util_TableRow *aRow;
84
85     if (RowIndex >= Table->numRows) {
86         return -1;
87     }
88     aRow=Table->Body[RowIndex];
89     return do_setTableRow(Table,aRow,Contents);
90 }
91
92 int util_setTableFooter(struct util_Table * Table, char ** Contents) {
93     return do_setTableRow(Table,Table->Footer,Contents);
94 }
95
96
97 int util_setTableHeader(struct util_Table *Table, char ** Contents) {
98     return do_setTableRow(Table,Table->Header,Contents);
99 }
100
101 int
102 util_addTableBodyRow(struct util_Table *Table, char **Contents) {
103     struct util_TableRow *aRow;
104     int indx,i,row,col;
105     int thisRowLength=0;
106
107     /* Allocate more Rows if required. */
108     if (Table->numRows >= Table->numAllocatedRows) {
109         Table->numAllocatedRows += UTIL_T_NUMALLOC_ROW;
110         Table->Body=realloc(Table->Body,\
111                     Table->numAllocatedRows*sizeof(struct util_TableRow*));
112         for (i=0;i<UTIL_T_NUMALLOC_ROW;i++) {
113             Table->Body[Table->numRows+i]=newTableRow(Table);
114         }
115     }
116     aRow=newTableRow(Table);
117     do_setTableRow(Table,aRow,Contents);
118     if (Table->sortByColumn >= 0)  {
119         indx=findRowIndex(Table,aRow);
120         for (row=Table->numRows;row>indx;row--) {
121             for (col=0;col<Table->numColumns;col++) {
122                  strncpy(Table->Body[row]->CellContents[col],
123                          Table->Body[row-1]->CellContents[col],
124                          UTIL_T_MAX_CELLCONTENT_LEN);
125             }
126         }
127     } else {
128       indx=Table->numRows;
129     }
130     Table->numRows += 1;
131     for (i=0;i<Table->numColumns;i++) {
132         strncpy(Table->Body[indx]->CellContents[i],Contents[i],\
133                 UTIL_T_MAX_CELLCONTENT_LEN);
134         thisRowLength += min(strlen(Contents[i]),UTIL_T_MAX_CELLCONTENT_LEN);
135     }
136     if (thisRowLength > Table->RowLength)
137         Table->RowLength = thisRowLength;
138     return Table->numRows-1;
139 }
140
141 int
142 util_printTableBody(struct util_Table *Table) {
143     int i;
144
145     for (i=0;i<Table->numRows;i++)
146         Table->printRow(Table,Table->Body[i]);
147     return 0;
148 }
149
150 int
151 util_printTable(struct util_Table *Table) {
152     Table->printHeader(Table);
153     Table->printBody(Table);
154     Table->printFooter(Table);
155     return 0;
156 }
157
158 int
159 util_printTableHeader(struct util_Table *Table) {
160     Table->printHeader(Table);
161     return 0;
162 }
163
164 int
165 util_printTableFooter(struct util_Table *Table) {
166     Table->printFooter(Table);
167     return 0;
168 }
169
170 /* private functions */
171
172 int
173 do_setTableRow(struct util_Table *Table, struct util_TableRow *aRow, char **Contents) {
174     int i;
175     int thisRowLength=0;
176     if ( Contents == NULL )
177         return -1;
178     for (i=0;i<Table->numColumns;i++) {
179         strcpy(aRow->CellContents[i],Contents[i]);
180         thisRowLength += min(strlen(Contents[i]),UTIL_T_MAX_CELLCONTENT_LEN);
181     }
182     if (thisRowLength > Table->RowLength)
183         Table->RowLength = thisRowLength;
184     return 0;
185 }
186
187
188 /* ASCII output functions */
189
190 int
191 printTableRow_ASCII(struct util_Table *Table, struct util_TableRow *aRow) {
192     int i;
193
194     if (!aRow)
195         return 1;
196
197     printf("%c",UTIL_T_CELLSEPARATOR);
198
199     for (i=0;i< Table->numColumns-1;i++) {
200         if ( Table->ColumnContentTypes[i] == UTIL_T_CONTENTTYPE_STRING)
201             printf("%-*s%c",Table->ColumnWidths[i],aRow->CellContents[i],\
202                    UTIL_T_CELLSEPARATOR);
203         else
204             printf("%*s%c",Table->ColumnWidths[i],aRow->CellContents[i],\
205                    UTIL_T_CELLSEPARATOR);
206     }
207     if ( Table->ColumnContentTypes[i] == UTIL_T_CONTENTTYPE_STRING)
208         printf("%-*s %c\n",Table->ColumnWidths[i],aRow->CellContents[i],\
209                UTIL_T_CELLSEPARATOR);
210     else
211         printf("%*s %c\n",Table->ColumnWidths[i],aRow->CellContents[i],UTIL_T_CELLSEPARATOR);
212     return 0;
213 }
214
215 int
216 printTableHeader_ASCII(struct util_Table *Table) {
217     int i;
218
219     printf("%c",UTIL_T_CELLSEPARATOR);
220     for(i=0;i<Table->RowLength;i++)
221         printf("%c",UTIL_T_ROWSEPARATOR);
222     printf("%c\n",UTIL_T_CELLSEPARATOR);
223
224     printTableRow_ASCII(Table,Table->Header);
225
226     printf("%c",UTIL_T_CELLSEPARATOR);
227     for(i=0;i<Table->RowLength;i++)
228         printf("%c",UTIL_T_ROWSEPARATOR);
229     printf("%c",UTIL_T_CELLSEPARATOR);
230     printf("\n");
231     return 0;
232 }
233
234
235 int
236 printTableFooter_ASCII(struct util_Table *Table) {
237     int i;
238
239     printf("%c",UTIL_T_CELLSEPARATOR);
240     for(i=0;i<Table->RowLength;i++)
241         printf("%c",UTIL_T_ROWSEPARATOR);
242     printf("%c",UTIL_T_CELLSEPARATOR);
243     printf( "\n");
244     if (Table->Footer) {
245         printTableRow_ASCII(Table,Table->Footer);
246         printf("%c",UTIL_T_CELLSEPARATOR);
247         for(i=0;i<Table->RowLength;i++)
248             printf("%c",UTIL_T_ROWSEPARATOR);
249         printf("%c",UTIL_T_CELLSEPARATOR);
250         printf( "\n");
251     }
252     return 0;
253 }
254
255 /* HTML output functions */
256
257 int
258 printTableRow_HTML(struct util_Table *Table, struct util_TableRow *aRow) {
259     int i;
260
261     if (!aRow)
262         return 1;
263
264     if (aRow == Table->Header)
265         printf("\t\t<th>\n");
266     else
267         printf("\t\t<tr>\n");
268
269     for (i=0;i< Table->numColumns;i++) {
270         printf("\t\t<td>");
271         printf("%s",aRow->CellContents[i]);
272         printf("\t\t</td>\n");
273     }
274     if (aRow == Table->Header)
275         printf("\t\t</th>\n");
276     else
277         printf("\t\t</tr>\n");
278     printf("\n");
279     return 0;
280 }
281
282 int
283 printTableFooter_HTML(struct util_Table *Table) {
284
285     printf("</tbody>\n");
286     if (Table->Footer) {
287         printf("<tfooter>\n");
288         printTableRow_HTML(Table,Table->Footer);
289         printf("</tfooter>\n");
290     }
291     printf("</table>\n");
292     return 0;
293 }
294
295 int
296 printTableHeader_HTML (struct util_Table *Table) {
297
298     printf("<table>\n");
299     printf("<thead>\n");
300     printTableRow_HTML(Table,Table->Header);
301     printf("</thead>\n");
302     printf("<tbody>\n");
303     return 0;
304 }
305
306
307 /* CSV output functions */
308
309 int
310 printTableRow_CSV(struct util_Table *Table, struct util_TableRow *aRow) {
311     int i;
312
313     if (!aRow)
314         return 1;
315     for (i=0;i<Table->numColumns-1;i++) {
316         printf("%s,",aRow->CellContents[i]);
317     }
318     printf("%s\n",aRow->CellContents[i]);
319     return 0;
320 }
321
322 int
323 printTableHeader_CSV (struct util_Table *Table) {
324     return printTableRow_CSV(Table,Table->Header);
325 }
326
327 int
328 printTableFooter_CSV (struct util_Table *Table) {
329     return printTableRow_CSV(Table,Table->Footer);
330 }
331
332
333 /* Constructors */
334
335 char **
336 util_newCellContents(struct util_Table* Table) {
337     char **CellContents=NULL;
338     int i;
339
340     if ( (CellContents=malloc( sizeof(char *) * Table->numColumns))== NULL ) {
341         fprintf(stderr,"Internal Error. Cannot allocate memory for new CellContents-array.\n");
342         exit(EXIT_FAILURE);
343     }
344     for (i=0;i<Table->numColumns;i++) {
345         if ( (CellContents[i]=malloc(UTIL_T_MAX_CELLCONTENT_LEN)) == NULL)  {
346             fprintf(stderr,\
347                     "Internal Error. Cannot allocate memory for new CellContents-array.\n");
348             exit(EXIT_FAILURE);
349         }
350         CellContents[i][0]='\0';
351     }
352     return CellContents;
353 }
354
355
356 struct util_Table*
357 util_newTable(int Type, int numColumns, char **ColumnHeaders, int *ColumnContentTypes, int *ColumnWidths, int sortByColumn) {
358     struct util_Table *Table=NULL;
359     int i;
360
361     if ( (Table=malloc(sizeof(struct util_Table))) == NULL) {
362           fprintf(stderr,\
363                   "Internal Error. Cannot allocate memory for new Table.\n");
364           exit(EXIT_FAILURE);
365     }
366     Table->Type=Type;
367     Table->numColumns=numColumns;
368     Table->numRows=0;
369     Table->numAllocatedRows=0;
370     if (sortByColumn < 0 || sortByColumn > numColumns) {
371         fprintf(stderr,"Invalid Table Sortkey: %d.\n", sortByColumn);
372         errno=EINVAL;
373         free(Table);
374         return NULL;
375     }
376     if (sortByColumn > 0 )
377         Table->sortByColumn=sortByColumn-1; /* externally, we treat the first
378                                              column as 1, internally as 0 */
379     Table->ColumnHeaders=ColumnHeaders;
380     Table->ColumnContentTypes=ColumnContentTypes;
381     Table->ColumnWidths=ColumnWidths;
382     Table->RowLength=0;
383     for (i=0; i< numColumns;i++)
384         Table->RowLength += ColumnWidths[i]+1;
385     switch (Table->Type) {
386         case UTIL_T_TYPE_ASCII :
387                 Table->printHeader=printTableHeader_ASCII;
388                 Table->printFooter=printTableFooter_ASCII;
389                 Table->printRow=printTableRow_ASCII;
390                 break;
391         case UTIL_T_TYPE_CSV :
392                 Table->printHeader=printTableHeader_CSV;
393                 Table->printFooter=printTableFooter_CSV;
394                 Table->printRow=printTableRow_CSV;
395                 break;
396         case UTIL_T_TYPE_HTML :
397                 Table->printHeader=printTableHeader_HTML;
398                 Table->printFooter=printTableFooter_HTML;
399                 Table->printRow=printTableRow_HTML;
400                 break;
401         default :
402                 fprintf(stderr,"Error. Invalid TableType: %d.\n", Table->Type);
403                 free(Table);
404                 errno=EINVAL;
405                 return NULL;
406     }
407     Table->printBody=util_printTableBody;
408     Table->Header=newTableRow(Table);
409     do_setTableRow(Table,Table->Header,ColumnHeaders);
410     Table->Body=NULL;
411     Table->Footer=NULL;
412     return Table;
413 }
414
415
416 /* private Constructors */
417
418 struct util_TableRow*
419 newTableRow(struct util_Table* Table) {
420     struct util_TableRow *aRow =NULL;
421
422     if ( (aRow = malloc(sizeof(struct util_TableRow))) == NULL) {
423         fprintf(stderr,\
424                 "Internal Error. Cannot allocate memory for new TableRow.\n");
425         exit(EXIT_FAILURE);
426     }
427     aRow->CellContents=util_newCellContents(Table);
428     return aRow;
429 }
430
431 int
432 freeTableRow( struct util_Table* Table, struct util_TableRow *aRow) {
433     int i;
434
435     for (i=0;i<Table->numColumns;i++) {
436         free(aRow->CellContents[i]);
437     }
438     free(aRow->CellContents);
439     return 0;
440 }
441
442 int
443 util_freeTable(struct util_Table *Table) {
444     int i;
445
446     freeTableRow(Table, Table->Header);
447     freeTableRow(Table, Table->Footer);
448     for(i=0;i<Table->numRows;i++) {
449         freeTableRow(Table, Table->Body[i]);
450     }
451     free(Table);
452     return 0;
453 }
454
455
456 afs_int64
457 compareBodyRow(struct util_Table *Table, int RowIndx, struct util_TableRow *aRow) {
458
459     afs_int64 value1,value2;
460     if (Table->ColumnContentTypes[Table->sortByColumn] == UTIL_T_CONTENTTYPE_STRING) {
461         return strncmp(Table->Body[RowIndx]->CellContents[Table->sortByColumn],\
462                aRow->CellContents[Table->sortByColumn],UTIL_T_MAX_CELLCONTENT_LEN);
463     } else {
464         util_GetInt64(Table->Body[RowIndx]->CellContents[Table->sortByColumn],\
465                &value1);
466         util_GetInt64(aRow->CellContents[Table->sortByColumn],&value2);
467         return ( value1 - value2 );
468     }
469 }
470
471 /* find correct index for new row by bi-secting the table */
472 int
473 findRowIndex(struct util_Table* Table, struct util_TableRow *aRow){
474     int cmp,lower,middle,upper;
475
476     /* empty Table */
477     if (Table->numRows == 0)  {
478         return 0;
479     }
480     /* Entry smaller than smallest so far */
481     if (compareBodyRow(Table,0,aRow) > 0)  {
482         return 0;
483     }
484     /* Entry larger than largest so far */
485     if (compareBodyRow(Table,Table->numRows-1,aRow) < 0)  {
486         return Table->numRows;
487     }
488
489     lower =  0;
490     upper= Table->numRows-1;
491     do {
492         middle=(upper-lower)/2+lower;
493         cmp=compareBodyRow(Table,middle,aRow);
494         if (cmp > 0)  {
495             upper=middle;
496         }
497         if (cmp < 0)  {
498             lower=middle;
499         }
500         if (cmp == 0) {
501             return middle;
502         }
503         if (upper - lower < 2) {
504             if ( compareBodyRow(Table,lower,aRow) < 0 )
505                 return upper;
506             else
507                 return lower;
508         }
509     }  while(1);
510     return 0;
511 }