2 * Copyright (c) 2010, Christof Hanke,
3 * RZG, Max-Planck-Institut f. Plasmaphysik.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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
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.
28 #include <afsconfig.h>
29 #include <afs/param.h>
33 #include <afs/afsutil.h>
34 #include <afs/tabular_output.h>
36 /* private structures */
38 struct util_TableRow {
44 int numColumns,sortByColumn;
45 int numRows, numAllocatedRows;
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 *);
61 /* private functions */
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);
82 util_setTableBodyRow(struct util_Table *Table, int RowIndex, char **Contents) {
83 struct util_TableRow *aRow;
85 if (RowIndex >= Table->numRows) {
88 aRow=Table->Body[RowIndex];
89 return do_setTableRow(Table,aRow,Contents);
92 int util_setTableFooter(struct util_Table * Table, char ** Contents) {
93 return do_setTableRow(Table,Table->Footer,Contents);
97 int util_setTableHeader(struct util_Table *Table, char ** Contents) {
98 return do_setTableRow(Table,Table->Header,Contents);
102 util_addTableBodyRow(struct util_Table *Table, char **Contents) {
103 struct util_TableRow *aRow;
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);
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);
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);
136 if (thisRowLength > Table->RowLength)
137 Table->RowLength = thisRowLength;
138 return Table->numRows-1;
142 util_printTableBody(struct util_Table *Table) {
145 for (i=0;i<Table->numRows;i++)
146 Table->printRow(Table,Table->Body[i]);
151 util_printTable(struct util_Table *Table) {
152 Table->printHeader(Table);
153 Table->printBody(Table);
154 Table->printFooter(Table);
159 util_printTableHeader(struct util_Table *Table) {
160 Table->printHeader(Table);
165 util_printTableFooter(struct util_Table *Table) {
166 Table->printFooter(Table);
170 /* private functions */
173 do_setTableRow(struct util_Table *Table, struct util_TableRow *aRow, char **Contents) {
176 if ( Contents == NULL )
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);
182 if (thisRowLength > Table->RowLength)
183 Table->RowLength = thisRowLength;
188 /* ASCII output functions */
191 printTableRow_ASCII(struct util_Table *Table, struct util_TableRow *aRow) {
197 printf("%c",UTIL_T_CELLSEPARATOR);
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);
204 printf("%*s%c",Table->ColumnWidths[i],aRow->CellContents[i],\
205 UTIL_T_CELLSEPARATOR);
207 if ( Table->ColumnContentTypes[i] == UTIL_T_CONTENTTYPE_STRING)
208 printf("%-*s %c\n",Table->ColumnWidths[i],aRow->CellContents[i],\
209 UTIL_T_CELLSEPARATOR);
211 printf("%*s %c\n",Table->ColumnWidths[i],aRow->CellContents[i],UTIL_T_CELLSEPARATOR);
216 printTableHeader_ASCII(struct util_Table *Table) {
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);
224 printTableRow_ASCII(Table,Table->Header);
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);
236 printTableFooter_ASCII(struct util_Table *Table) {
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);
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);
255 /* HTML output functions */
258 printTableRow_HTML(struct util_Table *Table, struct util_TableRow *aRow) {
264 if (aRow == Table->Header)
265 printf("\t\t<th>\n");
267 printf("\t\t<tr>\n");
269 for (i=0;i< Table->numColumns;i++) {
271 printf("%s",aRow->CellContents[i]);
272 printf("\t\t</td>\n");
274 if (aRow == Table->Header)
275 printf("\t\t</th>\n");
277 printf("\t\t</tr>\n");
283 printTableFooter_HTML(struct util_Table *Table) {
285 printf("</tbody>\n");
287 printf("<tfooter>\n");
288 printTableRow_HTML(Table,Table->Footer);
289 printf("</tfooter>\n");
291 printf("</table>\n");
296 printTableHeader_HTML (struct util_Table *Table) {
300 printTableRow_HTML(Table,Table->Header);
301 printf("</thead>\n");
307 /* CSV output functions */
310 printTableRow_CSV(struct util_Table *Table, struct util_TableRow *aRow) {
315 for (i=0;i<Table->numColumns-1;i++) {
316 printf("%s,",aRow->CellContents[i]);
318 printf("%s\n",aRow->CellContents[i]);
323 printTableHeader_CSV (struct util_Table *Table) {
324 return printTableRow_CSV(Table,Table->Header);
328 printTableFooter_CSV (struct util_Table *Table) {
329 return printTableRow_CSV(Table,Table->Footer);
336 util_newCellContents(struct util_Table* Table) {
337 char **CellContents=NULL;
340 if ( (CellContents=malloc( sizeof(char *) * Table->numColumns))== NULL ) {
341 fprintf(stderr,"Internal Error. Cannot allocate memory for new CellContents-array.\n");
344 for (i=0;i<Table->numColumns;i++) {
345 if ( (CellContents[i]=malloc(UTIL_T_MAX_CELLCONTENT_LEN)) == NULL) {
347 "Internal Error. Cannot allocate memory for new CellContents-array.\n");
350 CellContents[i][0]='\0';
357 util_newTable(int Type, int numColumns, char **ColumnHeaders, int *ColumnContentTypes, int *ColumnWidths, int sortByColumn) {
358 struct util_Table *Table=NULL;
361 if ( (Table=malloc(sizeof(struct util_Table))) == NULL) {
363 "Internal Error. Cannot allocate memory for new Table.\n");
367 Table->numColumns=numColumns;
369 if (sortByColumn < 0 || sortByColumn > numColumns) {
370 fprintf(stderr,"Invalid Table Sortkey: %d.\n", sortByColumn);
374 if (sortByColumn > 0 )
375 Table->sortByColumn=sortByColumn-1; /* externally, we treat the first
376 column as 1, internally as 0 */
377 Table->ColumnHeaders=ColumnHeaders;
378 Table->ColumnContentTypes=ColumnContentTypes;
379 Table->ColumnWidths=ColumnWidths;
381 for (i=0; i< numColumns;i++)
382 Table->RowLength += ColumnWidths[i]+1;
383 switch (Table->Type) {
384 case UTIL_T_TYPE_ASCII :
385 Table->printHeader=printTableHeader_ASCII;
386 Table->printFooter=printTableFooter_ASCII;
387 Table->printRow=printTableRow_ASCII;
389 case UTIL_T_TYPE_CSV :
390 Table->printHeader=printTableHeader_CSV;
391 Table->printFooter=printTableFooter_CSV;
392 Table->printRow=printTableRow_CSV;
394 case UTIL_T_TYPE_HTML :
395 Table->printHeader=printTableHeader_HTML;
396 Table->printFooter=printTableFooter_HTML;
397 Table->printRow=printTableRow_HTML;
400 fprintf(stderr,"Error. Invalid TableType: %d.\n", Table->Type);
404 Table->printBody=util_printTableBody;
405 Table->Header=newTableRow(Table);
406 do_setTableRow(Table,Table->Header,ColumnHeaders);
413 /* private Constructors */
415 struct util_TableRow*
416 newTableRow(struct util_Table* Table) {
417 struct util_TableRow *aRow =NULL;
419 if ( (aRow = malloc(sizeof(struct util_TableRow))) == NULL) {
421 "Internal Error. Cannot allocate memory for new TableRow.\n");
424 aRow->CellContents=util_newCellContents(Table);
429 freeTableRow( struct util_Table* Table, struct util_TableRow *aRow) {
432 for (i=0;i<Table->numColumns;i++) {
433 free(aRow->CellContents[i]);
435 free(aRow->CellContents);
440 util_freeTable(struct util_Table *Table) {
443 freeTableRow(Table, Table->Header);
444 freeTableRow(Table, Table->Footer);
445 for(i=0;i<Table->numRows;i++) {
446 freeTableRow(Table, Table->Body[i]);
454 compareBodyRow(struct util_Table *Table, int RowIndx, struct util_TableRow *aRow) {
456 afs_int64 value1,value2;
457 if (Table->ColumnContentTypes[Table->sortByColumn] == UTIL_T_CONTENTTYPE_STRING) {
458 return strncmp(Table->Body[RowIndx]->CellContents[Table->sortByColumn],\
459 aRow->CellContents[Table->sortByColumn],UTIL_T_MAX_CELLCONTENT_LEN);
461 util_GetInt64(Table->Body[RowIndx]->CellContents[Table->sortByColumn],\
463 util_GetInt64(aRow->CellContents[Table->sortByColumn],&value2);
464 return ( value1 - value2 );
468 /* find correct index for new row by bi-secting the table */
470 findRowIndex(struct util_Table* Table, struct util_TableRow *aRow){
471 int cmp,lower,middle,upper;
474 if (Table->numRows == 0) {
477 /* Entry smaller than smallest so far */
478 if (compareBodyRow(Table,0,aRow) > 0) {
481 /* Entry larger than largest so far */
482 if (compareBodyRow(Table,Table->numRows-1,aRow) < 0) {
483 return Table->numRows;
487 upper= Table->numRows-1;
489 middle=(upper-lower)/2+lower;
490 cmp=compareBodyRow(Table,middle,aRow);
500 if (upper - lower < 2) {
501 if ( compareBodyRow(Table,lower,aRow) < 0 )