1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | #include "config.h" |
---|
28 | #include <inttypes.h> |
---|
29 | #include <lt_inttypes.h> |
---|
30 | #include <stdio.h> |
---|
31 | #include <assert.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include <string.h> |
---|
34 | |
---|
35 | #include "output.h" |
---|
36 | |
---|
37 | struct output_type_t *output_formats[] = { |
---|
38 | &output_txt, |
---|
39 | &output_csv, |
---|
40 | &output_html, |
---|
41 | #ifdef HAVE_LIBGDC |
---|
42 | &output_png, |
---|
43 | #endif |
---|
44 | NULL |
---|
45 | }; |
---|
46 | |
---|
47 | struct output_data_t *output_init(char *title,char *type) |
---|
48 | { |
---|
49 | output_data_t *data = malloc(sizeof(output_data_t)); |
---|
50 | int i=0; |
---|
51 | data->title=strdup(title); |
---|
52 | data->labels=NULL; |
---|
53 | data->columns=0; |
---|
54 | data->data=NULL; |
---|
55 | while(output_formats[i]) { |
---|
56 | if (strcmp(output_formats[i]->name,type)==0) { |
---|
57 | data->format = output_formats[i]; |
---|
58 | return data; |
---|
59 | } |
---|
60 | ++i; |
---|
61 | } |
---|
62 | /* Not found */ |
---|
63 | free(data->title); |
---|
64 | free(data); |
---|
65 | return NULL; |
---|
66 | } |
---|
67 | |
---|
68 | void output_add_column(struct output_data_t *out,char *col) |
---|
69 | { |
---|
70 | ++out->columns; |
---|
71 | out->labels=realloc(out->labels,out->columns*sizeof(char *)); |
---|
72 | out->labels[out->columns-1]=strdup(col); |
---|
73 | out->data=realloc(out->data,out->columns*sizeof(struct data_t)); |
---|
74 | } |
---|
75 | |
---|
76 | void output_flush_headings(struct output_data_t *out) |
---|
77 | { |
---|
78 | out->format->init(out); |
---|
79 | } |
---|
80 | |
---|
81 | #define output_set_data(type_) \ |
---|
82 | void output_set_data_ ## type_(struct output_data_t *out, \ |
---|
83 | int col,TYPE__ ## type_ data)\ |
---|
84 | { \ |
---|
85 | assert(col>=0 && col<out->columns); \ |
---|
86 | out->data[col].type=TYPE_ ## type_; \ |
---|
87 | out->data[col].d.d_ ## type_ = data; \ |
---|
88 | } |
---|
89 | |
---|
90 | output_set_data(str) |
---|
91 | output_set_data(int) |
---|
92 | output_set_data(float) |
---|
93 | output_set_data(time) |
---|
94 | #undef output_set_data |
---|
95 | |
---|
96 | void output_flush_row(struct output_data_t *out) |
---|
97 | { |
---|
98 | out->format->flush(out); |
---|
99 | } |
---|
100 | |
---|
101 | void output_destroy(struct output_data_t *out) |
---|
102 | { |
---|
103 | int i; |
---|
104 | out->format->destroy(out); |
---|
105 | for(i=0;i<out->columns;++i) { |
---|
106 | free(out->labels[i]); |
---|
107 | } |
---|
108 | free(out->data); |
---|
109 | free(out->labels); |
---|
110 | free(out->title); |
---|
111 | free(out); |
---|
112 | } |
---|
113 | |
---|