Sort the pixel formats.
[metze/wireshark/wip.git] / ui / io_graph_item.c
1 /* io_graph_item.h
2  * Definitions and functions for IO graph items
3  *
4  * Copied from gtk/io_stat.c, (c) 2002 Ronnie Sahlberg
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27
28 #include <epan/epan_dissect.h>
29
30 #include "ui/io_graph_item.h"
31
32 int get_io_graph_index(packet_info *pinfo, int interval) {
33     nstime_t time_delta;
34
35     /*
36      * Find in which interval this is supposed to go and store the interval index as idx
37      */
38     time_delta = pinfo->rel_ts;
39     if (time_delta.nsecs<0) {
40         time_delta.secs--;
41         time_delta.nsecs += 1000000000;
42     }
43     if (time_delta.secs<0) {
44         return -1;
45     }
46     return (int) ((time_delta.secs*1000 + time_delta.nsecs/1000000) / interval);
47 }
48
49 GString *check_field_unit(const char *field_name, int *hf_index, io_graph_item_unit_t item_unit)
50 {
51     GString *err_str = NULL;
52     if (item_unit >= IOG_ITEM_UNIT_CALC_SUM) {
53         header_field_info *hfi;
54
55         const char *item_unit_names[NUM_IOG_ITEM_UNITS] = {
56             "Packets",
57             "Bytes",
58             "Bits",
59             "SUM",
60             "COUNT FRAMES",
61             "COUNT FIELDS",
62             "MAX",
63             "MIN",
64             "AVG",
65             "LOAD"
66         };
67
68         /* There was no field specified */
69         if ((field_name == NULL) || (field_name[0] == 0)) {
70             err_str = g_string_new("You didn't specify a field name.");
71             return err_str;
72         }
73
74         /* The field could not be found */
75         hfi = proto_registrar_get_byname(field_name);
76         if (hfi == NULL) {
77             err_str = g_string_new("");
78             g_string_printf(err_str, "There is no field named '%s'.", field_name);
79             return err_str;
80         }
81
82         if (hf_index) *hf_index = hfi->id;
83
84         /* Check that the type is compatible */
85         switch (hfi->type) {
86         case FT_UINT8:
87         case FT_UINT16:
88         case FT_UINT24:
89         case FT_UINT32:
90         case FT_UINT64:
91         case FT_INT8:
92         case FT_INT16:
93         case FT_INT24:
94         case FT_INT32:
95         case FT_INT64:
96         case FT_FLOAT:
97         case FT_DOUBLE:
98             /* These values support all calculations except LOAD */
99             switch (item_unit) {
100             case IOG_ITEM_UNIT_CALC_LOAD:
101                 err_str = g_string_new("LOAD is only supported for relative-time fields.");
102             default:
103                 break;
104             }
105             /* These types support all calculations */
106             break;
107         case FT_RELATIVE_TIME:
108             /* This type only supports COUNT, MAX, MIN, AVG */
109             switch (item_unit) {
110             case IOG_ITEM_UNIT_CALC_SUM:
111             case IOG_ITEM_UNIT_CALC_FRAMES:
112             case IOG_ITEM_UNIT_CALC_FIELDS:
113             case IOG_ITEM_UNIT_CALC_MAX:
114             case IOG_ITEM_UNIT_CALC_MIN:
115             case IOG_ITEM_UNIT_CALC_AVERAGE:
116             case IOG_ITEM_UNIT_CALC_LOAD:
117                 break;
118             default:
119                 err_str = g_string_new("");
120                 g_string_printf(err_str, "\"%s\" is a relative-time field. %s calculations are not supported on it.",
121                     field_name,
122                     item_unit_names[item_unit]);
123             }
124             break;
125         default:
126             if ((item_unit != IOG_ITEM_UNIT_CALC_FRAMES) &&
127                 (item_unit != IOG_ITEM_UNIT_CALC_FIELDS)) {
128                 err_str = g_string_new("");
129                 g_string_printf(err_str, "\"%s\" doesn't have integral or float values. %s calculations are not supported on it.",
130                     field_name,
131                     item_unit_names[item_unit]);
132             }
133             break;
134         }
135     }
136     return err_str;
137 }
138
139 /*
140  * Editor modelines
141  *
142  * Local Variables:
143  * c-basic-offset: 4
144  * tab-width: 8
145  * indent-tabs-mode: nil
146  * End:
147  *
148  * ex: set shiftwidth=4 tabstop=8 expandtab:
149  * :indentSize=4:tabSize=8:noTabs=true:
150  */